rewards.vue 9.8 KB

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