rewards.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <view class="container">
  3. <view class="nav-bar">
  4. <view class="nav-item" @click="goToBadge">
  5. <text class="nav-icon">🏅</text>
  6. <text class="nav-text">勋章墙</text>
  7. </view>
  8. </view>
  9. <view class="reward-list" v-if="rewards.length > 0">
  10. <view v-for="reward in rewards" :key="reward.id" class="reward-item">
  11. <view class="reward-info">
  12. <view class="reward-title">{{ reward.title }}</view>
  13. <view class="reward-progress">
  14. <progress
  15. :percent="(currentPoints / reward.pointsRequired) * 100"
  16. stroke-width="6"
  17. activeColor="#F97316"
  18. backgroundColor="#eee"
  19. />
  20. <text class="progress-text">{{ currentPoints }}/{{ reward.pointsRequired }}积分</text>
  21. </view>
  22. </view>
  23. <button
  24. class="btn-exchange"
  25. :disabled="currentPoints < reward.pointsRequired"
  26. @click="exchangeReward(reward.id)"
  27. >
  28. 兑换
  29. </button>
  30. </view>
  31. </view>
  32. <view class="empty" v-else>
  33. <text>暂无心愿单,快去添加吧</text>
  34. </view>
  35. <button class="btn-primary add-btn" @click="showAddModal = true">
  36. + 添加心愿
  37. </button>
  38. <!-- 添加心愿弹窗 -->
  39. <view class="modal-mask" v-if="showAddModal" @click="showAddModal = false">
  40. <view class="modal" @click.stop>
  41. <view class="modal-title">{{ currentRole === 'child' ? '添加心愿' : '添加心愿' }}</view>
  42. <view class="form-item">
  43. <view class="form-label">心愿名称 <text class="required">*</text></view>
  44. <input v-model="newReward.title" placeholder="请输入心愿名称" />
  45. </view>
  46. <!-- 家长模式可以填写积分,孩子模式不需要填写积分 -->
  47. <view class="form-item" v-if="currentRole === 'parent'">
  48. <view class="form-label">所需积分 <text class="required">*</text></view>
  49. <input v-model="newReward.pointsRequired" type="number" placeholder="请输入所需积分" />
  50. </view>
  51. <!-- 孩子模式提示 -->
  52. <view class="form-tip" v-if="currentRole === 'child'">
  53. <text>孩子创建心愿后,需要家长填写所需积分才能生效</text>
  54. </view>
  55. <view class="modal-btns">
  56. <button @click="showAddModal = false">取消</button>
  57. <button class="btn-primary" @click="createReward">确定</button>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. import config from '@/config.js'
  65. import { getWishlist, exchangeReward as exchangeRewardApi, createReward as createRewardApi, getPointsBalance, getChildren, getParentWishlist, createParentWishlist, exchangeParentWishlist } from '../../utils/api.js'
  66. export default {
  67. data() {
  68. return {
  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. // 规划师角色跳转到消息页
  82. const role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  83. if (role === 'teacher') {
  84. uni.redirectTo({ url: '/pages/teacher/messages' })
  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-bar {
  202. display: flex;
  203. margin-bottom: 20rpx;
  204. gap: 16rpx;
  205. }
  206. .nav-item {
  207. display: flex;
  208. align-items: center;
  209. padding: 16rpx 24rpx;
  210. background: #fff;
  211. border-radius: 12rpx;
  212. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  213. }
  214. .nav-icon {
  215. font-size: 32rpx;
  216. margin-right: 8rpx;
  217. }
  218. .nav-text {
  219. font-size: 28rpx;
  220. color: #333;
  221. font-weight: 500;
  222. }
  223. .reward-item {
  224. background: #fff;
  225. border-radius: 16rpx;
  226. padding: 30rpx;
  227. margin-bottom: 20rpx;
  228. display: flex;
  229. justify-content: space-between;
  230. align-items: center;
  231. }
  232. .reward-title {
  233. font-size: 30rpx;
  234. color: #333;
  235. margin-bottom: 15rpx;
  236. }
  237. .reward-progress {
  238. width: 400rpx;
  239. }
  240. .progress-text {
  241. font-size: 22rpx;
  242. color: #999;
  243. display: block;
  244. margin-top: 8rpx;
  245. }
  246. .btn-exchange {
  247. background: #F97316;
  248. color: #fff;
  249. font-size: 26rpx;
  250. padding: 10rpx 30rpx;
  251. border-radius: 30rpx;
  252. }
  253. .btn-exchange[disabled] {
  254. background: #ccc;
  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: #999;
  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: #fff;
  280. border-radius: 20rpx;
  281. padding: 40rpx;
  282. width: 80%;
  283. }
  284. .modal-title {
  285. font-size: 32rpx;
  286. font-weight: bold;
  287. margin-bottom: 30rpx;
  288. text-align: center;
  289. }
  290. .form-item {
  291. margin-bottom: 20rpx;
  292. }
  293. .form-item input {
  294. border: 1rpx solid #ddd;
  295. border-radius: 10rpx;
  296. padding: 20rpx;
  297. font-size: 28rpx;
  298. }
  299. .form-label {
  300. font-size: 28rpx;
  301. color: #333;
  302. margin-bottom: 10rpx;
  303. }
  304. .form-label .required {
  305. color: #F97316;
  306. }
  307. .form-tip {
  308. font-size: 24rpx;
  309. color: #999;
  310. padding: 20rpx;
  311. background: #f5f5f5;
  312. border-radius: 10rpx;
  313. margin-bottom: 20rpx;
  314. }
  315. .modal-btns {
  316. display: flex;
  317. gap: 20rpx;
  318. margin-top: 30rpx;
  319. }
  320. .modal-btns button {
  321. flex: 1;
  322. }
  323. </style>