rewards.vue 8.7 KB

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