rewards.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <view class="container" :style="{ '--font-scale': fontScale }">
  3. <!-- 退出切换按钮 -->
  4. <view class="exit-switch" v-if="isSwitchedChild" @click="exitSwitch">
  5. <text>🔄 退出切换</text>
  6. </view>
  7. <view class="reward-list" v-if="rewards.length > 0">
  8. <view v-for="reward in rewards" :key="reward.id" class="reward-item">
  9. <view class="reward-info">
  10. <view class="reward-title">{{ reward.title }}</view>
  11. <view class="reward-progress">
  12. <progress :percent="reward.pointsRequired > 0 ? (currentPoints / reward.pointsRequired) * 100 : 0" stroke-width="6" :activeColor="primaryColor" :backgroundColor="trackColor" />
  13. <text class="progress-text">{{ currentPoints }}/{{ reward.pointsRequired }}积分</text>
  14. </view>
  15. </view>
  16. </view>
  17. </view>
  18. <view class="empty" v-else>
  19. <text>暂无心愿单,快去添加吧</text>
  20. </view>
  21. <button class="btn-primary add-btn" @click="showAddModal = true">+ 添加心愿</button>
  22. <view class="modal-mask" v-if="showAddModal" @click="showAddModal = false">
  23. <view class="modal" @click.stop>
  24. <view class="modal-title">添加心愿</view>
  25. <view class="form-item">
  26. <view class="form-label">心愿名称 <text class="required">*</text></view>
  27. <input v-model="newReward.title" placeholder="请输入心愿名称" />
  28. </view>
  29. <view class="form-tip">
  30. <text>成员创建心愿后,需要家庭主账号填写所需积分才能生效</text>
  31. </view>
  32. <view class="modal-btns">
  33. <button @click="showAddModal = false">取消</button>
  34. <button class="btn-primary" @click="createChildReward">确定</button>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { BrandColors } from '@/utils/brand-colors.js'
  42. import { getWishlist, createReward as createRewardApi, getPointsBalance, getChildren, switchBackToParent, verifyPassword } from '../../utils/api.js'
  43. import fontScaleMixin from '@/mixins/font-scale.js'
  44. export default {
  45. mixins: [fontScaleMixin],
  46. data() {
  47. return {
  48. trackColor: BrandColors.hairline2,
  49. primaryColor: BrandColors.primary,
  50. rewards: [],
  51. currentPoints: 0,
  52. currentChildId: '',
  53. showAddModal: false,
  54. newReward: { title: '' },
  55. isSwitchedChild: false // 家庭主账号切换到成员状态的标记
  56. }
  57. },
  58. onShow() {
  59. this.isSwitchedChild = uni.getStorageSync('isSwitchedChild') || false
  60. this.loadRewards()
  61. },
  62. methods: {
  63. async loadRewards() {
  64. try {
  65. const childrenRes = await getChildren()
  66. const children = childrenRes.data || []
  67. const storedChildId = uni.getStorageSync('currentChildId')
  68. const currentChild = children.find(c => c.id == storedChildId) || children[0]
  69. if (!currentChild) {
  70. this.rewards = []
  71. this.currentPoints = 0
  72. return
  73. }
  74. this.currentChildId = currentChild.id
  75. const balanceRes = await getPointsBalance(currentChild.id)
  76. this.currentPoints = balanceRes.data.balance
  77. const res = await getWishlist(currentChild.id)
  78. this.rewards = res.data || []
  79. } catch (e) {
  80. console.error('加载心愿单失败', e)
  81. }
  82. },
  83. async createChildReward() {
  84. if (!this.newReward.title) {
  85. uni.showToast({ title: '请填写心愿名称', icon: 'none' })
  86. return
  87. }
  88. try {
  89. await createRewardApi({ title: this.newReward.title, memberId: this.currentChildId })
  90. uni.showToast({ title: '已提交,等待家庭主账号填写积分', icon: 'success' })
  91. this.showAddModal = false
  92. this.newReward.title = ''
  93. this.loadRewards()
  94. } catch (e) {
  95. console.error('添加心愿失败', e)
  96. }
  97. },
  98. exitSwitch() {
  99. if (!this.isSwitchedChild) return
  100. uni.showModal({
  101. title: '退出切换',
  102. content: '请输入家庭主账号密码以确认退出',
  103. editable: true,
  104. success: async (res) => {
  105. if (res.confirm && res.content) {
  106. try {
  107. const verifyResult = await verifyPassword(res.content)
  108. if (verifyResult.code === 200) {
  109. await this.switchBackToParentMode()
  110. } else {
  111. uni.showToast({ title: '密码错误', icon: 'none' })
  112. }
  113. } catch (e) {
  114. uni.showToast({ title: '密码验证失败', icon: 'none' })
  115. }
  116. }
  117. }
  118. })
  119. },
  120. async switchBackToParentMode() {
  121. try {
  122. const result = await switchBackToParent()
  123. if (result.data) {
  124. this.$store.commit('switchBackToParent')
  125. uni.showToast({ title: '已切换回本人模式', icon: 'success' })
  126. uni.reLaunch({ url: '/pages/home-pages/parent-index' })
  127. }
  128. } catch (e) {
  129. uni.showToast({ title: '切换失败', icon: 'none' })
  130. }
  131. }
  132. }
  133. }
  134. </script>
  135. <style scoped lang="scss">
  136. .container { padding: $uni-space-lg; padding-bottom: 120rpx; }
  137. .exit-switch { background: $facade-fill-2; padding: 20rpx; border-radius: 8rpx; margin-bottom: 20rpx; text-align: center; color: $facade-text-body; font-size: calc(28rpx * var(--font-scale, 1)); }
  138. .reward-item { background: $app-surface; border-radius: 16rpx; padding: $uni-space-lg; margin-bottom: 20rpx; }
  139. .reward-title { font-size: calc(30rpx * var(--font-scale, 1)); color: $facade-text-strong; margin-bottom: 15rpx; }
  140. .reward-progress { width: 100%; }
  141. .progress-text { font-size: calc(22rpx * var(--font-scale, 1)); color: $facade-text-muted; display: block; margin-top: 8rpx; }
  142. .add-btn { position: fixed; bottom: 30rpx; left: 30rpx; right: 30rpx; }
  143. .empty { text-align: center; color: $facade-text-muted; padding: 100rpx; }
  144. .modal-mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.5); display: flex; align-items: center; justify-content: center; }
  145. .modal { background: $app-surface; border-radius: 24rpx; padding: 40rpx; width: 80%; }
  146. .modal-title { font-size: calc(32rpx * var(--font-scale, 1)); font-weight: bold; margin-bottom: $uni-space-lg; text-align: center; }
  147. .form-item { margin-bottom: 20rpx; }
  148. .form-label { font-size: calc(28rpx * var(--font-scale, 1)); color: $facade-text-strong; margin-bottom: 10rpx; }
  149. .required { color: $brand-primary; }
  150. .form-tip { font-size: calc(24rpx * var(--font-scale, 1)); color: $facade-text-muted; padding: 20rpx; background: $facade-fill-2; border-radius: 8rpx; margin-bottom: 20rpx; }
  151. .modal-btns { display: flex; gap: 20rpx; margin-top: $uni-space-lg; }
  152. .modal-btns button { flex: 1; }
  153. </style>