rewards.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <view class="container">
  3. <view class="reward-list" v-if="rewards.length > 0">
  4. <view v-for="reward in rewards" :key="reward.id" class="reward-item">
  5. <view class="reward-info">
  6. <view class="reward-title">{{ reward.title }}</view>
  7. <view class="reward-progress">
  8. <progress
  9. :percent="(currentPoints / reward.pointsRequired) * 100"
  10. stroke-width="6"
  11. activeColor="#F97316"
  12. backgroundColor="#eee"
  13. />
  14. <text class="progress-text">{{ currentPoints }}/{{ reward.pointsRequired }}积分</text>
  15. </view>
  16. </view>
  17. <button
  18. class="btn-exchange"
  19. :disabled="currentPoints < reward.pointsRequired"
  20. @click="exchangeReward(reward.id)"
  21. >
  22. 兑换
  23. </button>
  24. </view>
  25. </view>
  26. <view class="empty" v-else>
  27. <text>暂无心愿单,快去添加吧</text>
  28. </view>
  29. <button class="btn-primary add-btn" @click="showAddModal = true">
  30. + 添加心愿
  31. </button>
  32. <!-- 添加心愿弹窗 -->
  33. <view class="modal-mask" v-if="showAddModal" @click="showAddModal = false">
  34. <view class="modal" @click.stop>
  35. <view class="modal-title">{{ currentRole === 'child' ? '添加心愿' : '添加心愿' }}</view>
  36. <view class="form-item">
  37. <view class="form-label">心愿名称 <text class="required">*</text></view>
  38. <input v-model="newReward.title" placeholder="请输入心愿名称" />
  39. </view>
  40. <!-- 家长模式可以填写积分,孩子模式不需要填写积分 -->
  41. <view class="form-item" v-if="currentRole === 'parent'">
  42. <view class="form-label">所需积分 <text class="required">*</text></view>
  43. <input v-model="newReward.pointsRequired" type="number" placeholder="请输入所需积分" />
  44. </view>
  45. <!-- 孩子模式提示 -->
  46. <view class="form-tip" v-if="currentRole === 'child'">
  47. <text>孩子创建心愿后,需要家长填写所需积分才能生效</text>
  48. </view>
  49. <view class="modal-btns">
  50. <button @click="showAddModal = false">取消</button>
  51. <button class="btn-primary" @click="createReward">确定</button>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. import config from '@/config.js'
  59. import { getWishlist, exchangeReward as exchangeRewardApi, createReward as createRewardApi, getPointsBalance, getChildren, getParentWishlist, createParentWishlist, exchangeParentWishlist, reportAppEnter
  60. } from '../../utils/api.js'
  61. export default {
  62. data() {
  63. return {
  64. rewards: [],
  65. currentPoints: 0,
  66. currentChildId: '',
  67. currentRole: 'parent', // 当前角色: parent/child
  68. showAddModal: false,
  69. newReward: {
  70. title: '',
  71. pointsRequired: ''
  72. }
  73. }
  74. },
  75. onShow() {
  76. reportAppEnter('medal');
  77. // 规划师不适用心愿单,中转至成长规划师中心
  78. const role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  79. if (role === 'teacher') {
  80. uni.redirectTo({ url: '/pages/teacher/index' })
  81. return
  82. }
  83. this.loadRewards()
  84. },
  85. methods: {
  86. async loadRewards() {
  87. // 获取当前角色用于判断加载哪个API
  88. const currentRole = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  89. try {
  90. if (currentRole === 'parent') {
  91. // 家长模式:使用家长心愿单API
  92. const res = await getParentWishlist()
  93. this.rewards = res.data || []
  94. // 家长积分从用户信息获取
  95. const userInfo = await this.getUserInfo()
  96. this.currentPoints = userInfo.totalPoints || 0
  97. } else {
  98. // 孩子模式:使用孩子心愿单API
  99. const childrenRes = await getChildren()
  100. const children = childrenRes.data
  101. if (children && children.length > 0) {
  102. this.currentChildId = children[0].id
  103. const balanceRes = await getPointsBalance(this.currentChildId)
  104. this.currentPoints = balanceRes.data.balance
  105. const res = await getWishlist(this.currentChildId)
  106. this.rewards = res.data || []
  107. }
  108. }
  109. } catch (e) {
  110. console.error('加载心愿单失败', e)
  111. }
  112. },
  113. async getUserInfo() {
  114. return new Promise((resolve, reject) => {
  115. uni.request({
  116. url: config.api('/api/user/info'),
  117. method: 'POST',
  118. header: {
  119. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  120. },
  121. success: (res) => {
  122. if (res.data.code === 200) {
  123. resolve(res.data.data)
  124. } else {
  125. reject(res.data)
  126. }
  127. },
  128. fail: reject
  129. })
  130. })
  131. },
  132. async exchangeReward(rewardId) {
  133. uni.showModal({
  134. title: '确认兑换',
  135. content: '确定要兑换这个奖励吗?',
  136. success: async (res) => {
  137. if (res.confirm) {
  138. try {
  139. if (this.currentRole === 'parent') {
  140. await exchangeParentWishlist(rewardId)
  141. } else {
  142. await exchangeRewardApi(rewardId, this.currentChildId)
  143. }
  144. uni.showToast({ title: '兑换成功', icon: 'success' })
  145. this.loadRewards()
  146. } catch (e) {
  147. console.error('兑换失败', e)
  148. }
  149. }
  150. }
  151. })
  152. },
  153. goToBadge() {
  154. uni.navigateTo({ url: '/pages/rewards/badge' })
  155. },
  156. async createReward() {
  157. if (!this.newReward.title) {
  158. uni.showToast({ title: '请填写心愿名称', icon: 'none' })
  159. return
  160. }
  161. try {
  162. if (this.currentRole === 'parent') {
  163. // 家长模式:创建家长心愿,需要填写积分
  164. if (!this.newReward.pointsRequired) {
  165. uni.showToast({ title: '请填写所需积分', icon: 'none' })
  166. return
  167. }
  168. await createParentWishlist({
  169. title: this.newReward.title,
  170. pointsRequired: parseInt(this.newReward.pointsRequired)
  171. })
  172. uni.showToast({ title: '添加成功', icon: 'success' })
  173. } else {
  174. // 孩子模式:创建孩子心愿,积分为0,等待家长填写
  175. await createRewardApi({
  176. title: this.newReward.title,
  177. memberId: this.currentChildId
  178. // pointsRequired不传,默认为0
  179. })
  180. uni.showToast({ title: '已提交,等待家长填写积分', icon: 'success' })
  181. }
  182. this.showAddModal = false
  183. this.newReward = { title: '', pointsRequired: '' }
  184. this.loadRewards()
  185. } catch (e) {
  186. console.error('添加心愿失败', e)
  187. }
  188. }
  189. }
  190. }
  191. </script>
  192. <style scoped>
  193. .container {
  194. padding: 30rpx;
  195. padding-bottom: 120rpx;
  196. }
  197. .nav-item {
  198. display: flex;
  199. align-items: center;
  200. padding: 16rpx 24rpx;
  201. background: #fff;
  202. border-radius: 12rpx;
  203. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  204. }
  205. .nav-icon {
  206. font-size: 32rpx;
  207. margin-right: 8rpx;
  208. }
  209. .nav-text {
  210. font-size: 28rpx;
  211. color: #333;
  212. font-weight: 500;
  213. }
  214. .reward-item {
  215. background: #fff;
  216. border-radius: 16rpx;
  217. padding: 30rpx;
  218. margin-bottom: 20rpx;
  219. display: flex;
  220. justify-content: space-between;
  221. align-items: center;
  222. }
  223. .reward-title {
  224. font-size: 30rpx;
  225. color: #333;
  226. margin-bottom: 15rpx;
  227. }
  228. .reward-progress {
  229. width: 400rpx;
  230. }
  231. .progress-text {
  232. font-size: 22rpx;
  233. color: #999;
  234. display: block;
  235. margin-top: 8rpx;
  236. }
  237. .btn-exchange {
  238. background: #F97316;
  239. color: #fff;
  240. font-size: 26rpx;
  241. padding: 10rpx 30rpx;
  242. border-radius: 30rpx;
  243. }
  244. .btn-exchange[disabled] {
  245. background: #ccc;
  246. }
  247. .add-btn {
  248. position: fixed;
  249. bottom: 30rpx;
  250. left: 30rpx;
  251. right: 30rpx;
  252. }
  253. .empty {
  254. text-align: center;
  255. color: #999;
  256. padding: 100rpx;
  257. }
  258. .modal-mask {
  259. position: fixed;
  260. top: 0;
  261. left: 0;
  262. right: 0;
  263. bottom: 0;
  264. background: rgba(0, 0, 0, 0.5);
  265. display: flex;
  266. align-items: center;
  267. justify-content: center;
  268. }
  269. .modal {
  270. background: #fff;
  271. border-radius: 20rpx;
  272. padding: 40rpx;
  273. width: 80%;
  274. }
  275. .modal-title {
  276. font-size: 32rpx;
  277. font-weight: bold;
  278. margin-bottom: 30rpx;
  279. text-align: center;
  280. }
  281. .form-item {
  282. margin-bottom: 20rpx;
  283. }
  284. .form-item input {
  285. border: 1rpx solid #ddd;
  286. border-radius: 10rpx;
  287. padding: 20rpx;
  288. font-size: 28rpx;
  289. }
  290. .form-label {
  291. font-size: 28rpx;
  292. color: #333;
  293. margin-bottom: 10rpx;
  294. }
  295. .form-label .required {
  296. color: #F97316;
  297. }
  298. .form-tip {
  299. font-size: 24rpx;
  300. color: #999;
  301. padding: 20rpx;
  302. background: #f5f5f5;
  303. border-radius: 10rpx;
  304. margin-bottom: 20rpx;
  305. }
  306. .modal-btns {
  307. display: flex;
  308. gap: 20rpx;
  309. margin-top: 30rpx;
  310. }
  311. .modal-btns button {
  312. flex: 1;
  313. }
  314. </style>