rewards.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. // 只跳转一次:onShow 重复触发时多次 redirectTo 会让框架对已销毁的
  81. // webview 调用 routeDone,报 "routeDone with a webviewId N is not found"
  82. if (this._redirected) return
  83. this._redirected = true
  84. uni.redirectTo({ url: '/pages/teacher/index' })
  85. return
  86. }
  87. this.loadRewards()
  88. },
  89. methods: {
  90. async loadRewards() {
  91. // 获取当前角色用于判断加载哪个API
  92. const currentRole = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  93. try {
  94. if (currentRole === 'parent') {
  95. // 家长模式:使用家长心愿单API
  96. const res = await getParentWishlist()
  97. this.rewards = res.data || []
  98. // 家长积分从用户信息获取
  99. const userInfo = await this.getUserInfo()
  100. this.currentPoints = userInfo.totalPoints || 0
  101. } else {
  102. // 孩子模式:使用孩子心愿单API
  103. const childrenRes = await getChildren()
  104. const children = childrenRes.data
  105. if (children && children.length > 0) {
  106. this.currentChildId = children[0].id
  107. const balanceRes = await getPointsBalance(this.currentChildId)
  108. this.currentPoints = balanceRes.data.balance
  109. const res = await getWishlist(this.currentChildId)
  110. this.rewards = res.data || []
  111. }
  112. }
  113. } catch (e) {
  114. console.error('加载心愿单失败', e)
  115. }
  116. },
  117. async getUserInfo() {
  118. return new Promise((resolve, reject) => {
  119. uni.request({
  120. url: config.api('/api/user/info'),
  121. method: 'POST',
  122. header: {
  123. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  124. },
  125. success: (res) => {
  126. if (res.data.code === 200) {
  127. resolve(res.data.data)
  128. } else {
  129. reject(res.data)
  130. }
  131. },
  132. fail: reject
  133. })
  134. })
  135. },
  136. async exchangeReward(rewardId) {
  137. uni.showModal({
  138. title: '确认兑换',
  139. content: '确定要兑换这个奖励吗?',
  140. success: async (res) => {
  141. if (res.confirm) {
  142. try {
  143. if (this.currentRole === 'parent') {
  144. await exchangeParentWishlist(rewardId)
  145. } else {
  146. await exchangeRewardApi(rewardId, this.currentChildId)
  147. }
  148. uni.showToast({ title: '兑换成功', icon: 'success' })
  149. this.loadRewards()
  150. } catch (e) {
  151. console.error('兑换失败', e)
  152. }
  153. }
  154. }
  155. })
  156. },
  157. goToBadge() {
  158. uni.navigateTo({ url: '/pages/rewards/badge' })
  159. },
  160. async createReward() {
  161. if (!this.newReward.title) {
  162. uni.showToast({ title: '请填写心愿名称', icon: 'none' })
  163. return
  164. }
  165. try {
  166. if (this.currentRole === 'parent') {
  167. // 家长模式:创建家长心愿,需要填写积分
  168. if (!this.newReward.pointsRequired) {
  169. uni.showToast({ title: '请填写所需积分', icon: 'none' })
  170. return
  171. }
  172. await createParentWishlist({
  173. title: this.newReward.title,
  174. pointsRequired: parseInt(this.newReward.pointsRequired)
  175. })
  176. uni.showToast({ title: '添加成功', icon: 'success' })
  177. } else {
  178. // 孩子模式:创建孩子心愿,积分为0,等待家长填写
  179. await createRewardApi({
  180. title: this.newReward.title,
  181. memberId: this.currentChildId
  182. // pointsRequired不传,默认为0
  183. })
  184. uni.showToast({ title: '已提交,等待家长填写积分', icon: 'success' })
  185. }
  186. this.showAddModal = false
  187. this.newReward = { title: '', pointsRequired: '' }
  188. this.loadRewards()
  189. } catch (e) {
  190. console.error('添加心愿失败', e)
  191. }
  192. }
  193. }
  194. }
  195. </script>
  196. <style scoped>
  197. .container {
  198. padding: 30rpx;
  199. padding-bottom: 120rpx;
  200. }
  201. .nav-item {
  202. display: flex;
  203. align-items: center;
  204. padding: 16rpx 24rpx;
  205. background: #fff;
  206. border-radius: 12rpx;
  207. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  208. }
  209. .nav-icon {
  210. font-size: 32rpx;
  211. margin-right: 8rpx;
  212. }
  213. .nav-text {
  214. font-size: 28rpx;
  215. color: #333;
  216. font-weight: 500;
  217. }
  218. .reward-item {
  219. background: #fff;
  220. border-radius: 16rpx;
  221. padding: 30rpx;
  222. margin-bottom: 20rpx;
  223. display: flex;
  224. justify-content: space-between;
  225. align-items: center;
  226. }
  227. .reward-title {
  228. font-size: 30rpx;
  229. color: #333;
  230. margin-bottom: 15rpx;
  231. }
  232. .reward-progress {
  233. width: 400rpx;
  234. }
  235. .progress-text {
  236. font-size: 22rpx;
  237. color: #999;
  238. display: block;
  239. margin-top: 8rpx;
  240. }
  241. .btn-exchange {
  242. background: #F97316;
  243. color: #fff;
  244. font-size: 26rpx;
  245. padding: 10rpx 30rpx;
  246. border-radius: 30rpx;
  247. }
  248. .btn-exchange[disabled] {
  249. background: #ccc;
  250. }
  251. .add-btn {
  252. position: fixed;
  253. bottom: 30rpx;
  254. left: 30rpx;
  255. right: 30rpx;
  256. }
  257. .empty {
  258. text-align: center;
  259. color: #999;
  260. padding: 100rpx;
  261. }
  262. .modal-mask {
  263. position: fixed;
  264. top: 0;
  265. left: 0;
  266. right: 0;
  267. bottom: 0;
  268. background: rgba(0, 0, 0, 0.5);
  269. display: flex;
  270. align-items: center;
  271. justify-content: center;
  272. }
  273. .modal {
  274. background: #fff;
  275. border-radius: 20rpx;
  276. padding: 40rpx;
  277. width: 80%;
  278. }
  279. .modal-title {
  280. font-size: 32rpx;
  281. font-weight: bold;
  282. margin-bottom: 30rpx;
  283. text-align: center;
  284. }
  285. .form-item {
  286. margin-bottom: 20rpx;
  287. }
  288. .form-item input {
  289. border: 1rpx solid #ddd;
  290. border-radius: 10rpx;
  291. padding: 20rpx;
  292. font-size: 28rpx;
  293. }
  294. .form-label {
  295. font-size: 28rpx;
  296. color: #333;
  297. margin-bottom: 10rpx;
  298. }
  299. .form-label .required {
  300. color: #F97316;
  301. }
  302. .form-tip {
  303. font-size: 24rpx;
  304. color: #999;
  305. padding: 20rpx;
  306. background: #f5f5f5;
  307. border-radius: 10rpx;
  308. margin-bottom: 20rpx;
  309. }
  310. .modal-btns {
  311. display: flex;
  312. gap: 20rpx;
  313. margin-top: 30rpx;
  314. }
  315. .modal-btns button {
  316. flex: 1;
  317. }
  318. </style>