rewards.vue 9.3 KB

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