mall.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <text class="title">积分商城</text>
  5. <view class="points-info">
  6. <text class="points-label">我的积分</text>
  7. <text class="points-value">{{ systemPoints }}</text>
  8. </view>
  9. </view>
  10. <view class="category-tabs">
  11. <view
  12. v-for="tab in categories"
  13. :key="tab.key"
  14. class="tab-item"
  15. :class="{ active: currentCategory === tab.key }"
  16. @click="onCategoryChange(tab.key)"
  17. >
  18. <text class="tab-text">{{ tab.label }}</text>
  19. </view>
  20. </view>
  21. <scroll-view
  22. scroll-y
  23. class="product-list"
  24. @scrolltolower="onLoadMore"
  25. refresher-enabled
  26. :refresher-triggered="refreshing"
  27. @refresherrefresh="onRefresh"
  28. >
  29. <view v-if="products.length > 0" class="product-grid">
  30. <view
  31. v-for="product in products"
  32. :key="product.id"
  33. class="product-card"
  34. @click="onProductClick(product)"
  35. >
  36. <image
  37. class="product-cover"
  38. :src="product.coverImage || '/static/default-product.png'"
  39. mode="aspectFill"
  40. />
  41. <view class="product-info">
  42. <text class="product-name">{{ product.name }}</text>
  43. <text class="product-points">{{ product.pointsPrice }} 积分</text>
  44. <view class="exchange-btn" @click.stop="onExchangeClick(product)">
  45. <text class="exchange-btn-text">兑换</text>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. <view v-else-if="!loading" class="empty-state">
  51. <text class="empty-text">暂无商品</text>
  52. </view>
  53. <view v-if="loadingMore" class="loading-more">
  54. <text class="loading-text">加载中...</text>
  55. </view>
  56. </scroll-view>
  57. <view class="records-btn" @click="onRecordsClick">
  58. <text class="records-btn-text">兑换记录</text>
  59. </view>
  60. <view v-if="showExchangeModal" class="modal-mask" @click="onCloseModal">
  61. <view class="modal-content" @click.stop>
  62. <text class="modal-title">确认兑换</text>
  63. <view v-if="selectedProduct" class="modal-product">
  64. <image
  65. class="modal-cover"
  66. :src="selectedProduct.coverImage || '/static/default-product.png'"
  67. mode="aspectFill"
  68. />
  69. <view class="modal-info">
  70. <text class="modal-name">{{ selectedProduct.name }}</text>
  71. <text class="modal-points">{{ selectedProduct.pointsPrice }} 积分</text>
  72. </view>
  73. </view>
  74. <view class="modal-actions">
  75. <view class="modal-btn cancel" @click="onCloseModal">
  76. <text class="modal-btn-text">取消</text>
  77. </view>
  78. <view class="modal-btn confirm" @click="onConfirmExchange">
  79. <text class="modal-btn-text">确认兑换</text>
  80. </view>
  81. </view>
  82. </view>
  83. </view>
  84. <view v-if="showRecordsModal" class="modal-mask" @click="onCloseRecordsModal">
  85. <view class="modal-content records-modal" @click.stop>
  86. <text class="modal-title">兑换记录</text>
  87. <scroll-view scroll-y class="records-list">
  88. <view v-if="records.length > 0">
  89. <view
  90. v-for="record in records"
  91. :key="record.id"
  92. class="record-item"
  93. >
  94. <view class="record-header">
  95. <text class="record-name">{{ record.productName }}</text>
  96. <text class="record-points">-{{ record.pointsCost }}积分</-afficher>
  97. </view>
  98. <view class="record-meta">
  99. <text class="record-code">兑换码: {{ record.redeemCode }}</text>
  100. <text class="record-date">{{ formatDate(record.createdAt) }}</text>
  101. </view>
  102. </view>
  103. </view>
  104. <view v-else class="empty-state">
  105. <text class="empty-text">暂无兑换记录</text>
  106. </view>
  107. </scroll-view>
  108. <view class="modal-close" @click="onCloseRecordsModal">
  109. <text class="modal-close-text">关闭</text>
  110. </view>
  111. </view>
  112. </view>
  113. </view>
  114. </template>
  115. <script>
  116. import {
  117. getPointsExchangeProducts,
  118. submitPointsExchange,
  119. getPointsExchangeRecords,
  120. getSystemBalance
  121. } from '@/utils/api.js'
  122. export default {
  123. data() {
  124. return {
  125. categories: [
  126. { key: 'all', label: '全部' },
  127. { key: 'vip', label: 'VIP' },
  128. { key: 'redeem', label: '兑换' },
  129. { key: 'flow', label: '流量' },
  130. { key: 'gift', label: '礼品' }
  131. ],
  132. currentCategory: 'all',
  133. products: [],
  134. records: [],
  135. systemPoints: 0,
  136. pageNum: 1,
  137. pageSize: 10,
  138. loading: false,
  139. loadingMore: false,
  140. refreshing: false,
  141. hasMore: true,
  142. showExchangeModal: false,
  143. showRecordsModal: false,
  144. selectedProduct: null
  145. }
  146. },
  147. onLoad() {
  148. this.loadSystemPoints()
  149. this.loadProducts()
  150. },
  151. onShow() {
  152. this.loadSystemPoints()
  153. },
  154. methods: {
  155. loadSystemPoints() {
  156. var childId = uni.getStorageSync('currentChildId')
  157. getSystemBalance(childId).then(function(res) {
  158. if (res.code === 200 && res.data) {
  159. this.systemPoints = res.data.systemPoints || 0
  160. }
  161. }.bind(this)).catch(function() {})
  162. },
  163. loadProducts() {
  164. this.loading = true
  165. getPointsExchangeProducts({
  166. category: this.currentCategory,
  167. pageNum: this.pageNum,
  168. pageSize: this.pageSize
  169. }).then(function(res) {
  170. this.loading = false
  171. if (res.code === 200 && res.data) {
  172. var list = res.data.records || []
  173. if (this.pageNum === 1) {
  174. this.products = list
  175. } else {
  176. this.products = this.products.concat(list)
  177. }
  178. this.hasMore = list.length >= this.pageSize
  179. }
  180. }.bind(this)).catch(function() {
  181. this.loading = false
  182. }.bind(this))
  183. },
  184. onCategoryChange(category) {
  185. this.currentCategory = category
  186. this.pageNum = 1
  187. this.products = []
  188. this.loadProducts()
  189. },
  190. onRefresh() {
  191. this.refreshing = true
  192. this.pageNum = 1
  193. getPointsExchangeProducts({
  194. category: this.currentCategory,
  195. pageNum: 1,
  196. pageSize: this.pageSize
  197. }).then(function(res) {
  198. this.refreshing = false
  199. if (res.code === 200 && res.data) {
  200. this.products = res.data.records || []
  201. this.hasMore = (res.data.records || []).length >= this.pageSize
  202. }
  203. }.bind(this)).catch(function() {
  204. this.refreshing = false
  205. }.bind(this))
  206. },
  207. onLoadMore() {
  208. if (!this.hasMore && !this.loadingMore) {
  209. this.loadingMore = true
  210. this.pageNum = this.pageNum + 1
  211. getPointsExchangeProducts({
  212. category: this.currentCategory,
  213. pageNum: this.pageNum,
  214. pageSize: this.pageSize
  215. }).then(function(res) {
  216. this.loadingMore = false
  217. if (res.code === 200 && res.data) {
  218. var list = res.data.records || []
  219. this.products = this.products.concat(list)
  220. this.hasMore = list.length >= this.pageSize
  221. }
  222. }.bind(this)).catch(function() {
  223. this.loadingMore = false
  224. }.bind(this))
  225. }
  226. },
  227. onProductClick(product) {
  228. this.selectedProduct = product
  229. this.showExchangeModal = true
  230. },
  231. onExchangeClick(product) {
  232. this.selectedProduct = product
  233. this.showExchangeModal = true
  234. },
  235. onCloseModal() {
  236. this.showExchangeModal = false
  237. this.selectedProduct = null
  238. },
  239. onConfirmExchange() {
  240. if (!this.selectedProduct) {
  241. return
  242. }
  243. var productId = this.selectedProduct.id
  244. submitPointsExchange({ productId: productId, quantity: 1 }).then(function(res) {
  245. if (res.code === 200) {
  246. uni.showToast({ title: '兑换成功', icon: 'success' })
  247. this.loadSystemPoints()
  248. this.loadProducts()
  249. }
  250. }.bind(this)).catch(function(err) {
  251. uni.showToast({ title: err && err.message ? err.message : '兑换失败', icon: 'none' })
  252. }.bind(this))
  253. this.onCloseModal()
  254. },
  255. onRecordsClick() {
  256. this.loadRecords()
  257. this.showRecordsModal = true
  258. },
  259. loadRecords() {
  260. getPointsExchangeRecords().then(function(res) {
  261. if (res.code === 200 && res.data) {
  262. this.records = res.data || []
  263. }
  264. }.bind(this)).catch(function() {})
  265. },
  266. onCloseRecordsModal() {
  267. this.showRecordsModal = false
  268. },
  269. formatDate(dateStr) {
  270. if (!dateStr) {
  271. return ''
  272. }
  273. var date = new Date(dateStr)
  274. var year = date.getFullYear()
  275. var month = date.getMonth() + 1
  276. var day = date.getDate()
  277. return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)
  278. }
  279. }
  280. }
  281. </script>
  282. <style scoped>
  283. .container {
  284. min-height: 100vh;
  285. background-color: #f5f5f5;
  286. padding-bottom: 120rpx;
  287. }
  288. .header {
  289. background: linear-gradient(135deg, #ff8c42 0%, #ff6b35 100%);
  290. padding: 40rpx 30rpx;
  291. display: flex;
  292. justify-content: space-between;
  293. align-items: center;
  294. }
  295. .title {
  296. font-size: 40rpx;
  297. font-weight: bold;
  298. color: #ffffff;
  299. }
  300. .points-info {
  301. display: flex;
  302. flex-direction: column;
  303. align-items: flex-end;
  304. }
  305. .points-label {
  306. font-size: 24rpx;
  307. color: rgba(255, 255, 255, 0.8);
  308. }
  309. .points-value {
  310. font-size: 48rpx;
  311. font-weight: bold;
  312. color: #ffffff;
  313. }
  314. .category-tabs {
  315. display: flex;
  316. background-color: #ffffff;
  317. padding: 20rpx 0;
  318. border-bottom: 1rpx solid #eeeeee;
  319. overflow-x: auto;
  320. }
  321. .tab-item {
  322. flex: 1;
  323. text-align: center;
  324. padding: 16rpx 0;
  325. margin: 0 10rpx;
  326. border-radius: 30rpx;
  327. background-color: #f5f5f5;
  328. }
  329. .tab-item.active {
  330. background-color: #ff8c42;
  331. }
  332. .tab-text {
  333. font-size: 28rpx;
  334. color: #666666;
  335. }
  336. .tab-item.active .tab-text {
  337. color: #ffffff;
  338. font-weight: bold;
  339. }
  340. .product-list {
  341. padding: 20rpx;
  342. }
  343. .product-grid {
  344. display: flex;
  345. flex-wrap: wrap;
  346. justify-content: space-between;
  347. }
  348. .product-card {
  349. width: 48%;
  350. background-color: #ffffff;
  351. border-radius: 16rpx;
  352. overflow: hidden;
  353. margin-bottom: 20rpx;
  354. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
  355. }
  356. .product-cover {
  357. width: 100%;
  358. height: 240rpx;
  359. }
  360. .product-info {
  361. padding: 20rpx;
  362. }
  363. .product-name {
  364. font-size: 28rpx;
  365. color: #333333;
  366. font-weight: bold;
  367. display: block;
  368. margin-bottom: 10rpx;
  369. overflow: hidden;
  370. text-overflow: ellipsis;
  371. white-space: nowrap;
  372. }
  373. .product-points {
  374. font-size: 32rpx;
  375. color: #ff6b35;
  376. font-weight: bold;
  377. display: block;
  378. margin-bottom: 16rpx;
  379. }
  380. .exchange-btn {
  381. background: linear-gradient(135deg, #ff8c42 0%, #ff6b35 100%);
  382. border-radius: 30rpx;
  383. padding: 16rpx 0;
  384. text-align: center;
  385. }
  386. .exchange-btn-text {
  387. font-size: 28rpx;
  388. color: #ffffff;
  389. font-weight: bold;
  390. }
  391. .empty-state {
  392. text-align: center;
  393. padding: 100rpx 0;
  394. }
  395. .empty-text {
  396. font-size: 28rpx;
  397. color: #999999;
  398. }
  399. .loading-more {
  400. text-align: center;
  401. padding: 30rpx 0;
  402. }
  403. .loading-text {
  404. font-size: 24rpx;
  405. color: #999999;
  406. }
  407. .records-btn {
  408. position: fixed;
  409. bottom: 40rpx;
  410. left: 50%;
  411. transform: translateX(-50%);
  412. background: linear-gradient(135deg, #ff8c42 0%, #ff6b35 100%);
  413. border-radius: 40rpx;
  414. padding: 24rpx 60rpx;
  415. box-shadow: 0 4rpx 12rpx rgba(255, 107, 53, 0.3);
  416. }
  417. .records-btn-text {
  418. font-size: 30rpx;
  419. color: #ffffff;
  420. font-weight: bold;
  421. }
  422. .modal-mask {
  423. position: fixed;
  424. top: 0;
  425. left: 0;
  426. right: 0;
  427. bottom: 0;
  428. background-color: rgba(0, 0, 0, 0.5);
  429. display: flex;
  430. justify-content: center;
  431. align-items: center;
  432. z-index: 1000;
  433. }
  434. .modal-content {
  435. background-color: #ffffff;
  436. border-radius: 20rpx;
  437. padding: 40rpx;
  438. width: 80%;
  439. max-width: 600rpx;
  440. }
  441. .records-modal {
  442. max-height: 70vh;
  443. display: flex;
  444. flex-direction: column;
  445. }
  446. .modal-title {
  447. font-size: 36rpx;
  448. font-weight: bold;
  449. color: #333333;
  450. text-align: center;
  451. display: block;
  452. margin-bottom: 30rpx;
  453. }
  454. .modal-product {
  455. display: flex;
  456. align-items: center;
  457. margin-bottom: 30rpx;
  458. padding: 20rpx;
  459. background-color: #f9f9f9;
  460. border-radius: 12rpx;
  461. }
  462. .modal-cover {
  463. width: 120rpx;
  464. height: 120rpx;
  465. border-radius: 8rpx;
  466. margin-right: 20rpx;
  467. }
  468. .modal-info {
  469. flex: 1;
  470. }
  471. .modal-name {
  472. font-size: 30rpx;
  473. color: #333333;
  474. font-weight: bold;
  475. display: block;
  476. margin-bottom: 10rpx;
  477. }
  478. .modal-points {
  479. font-size: 32rpx;
  480. color: #ff6b35;
  481. font-weight: bold;
  482. }
  483. .modal-actions {
  484. display: flex;
  485. justify-content: space-between;
  486. }
  487. .modal-btn {
  488. flex: 1;
  489. padding: 20rpx 0;
  490. border-radius: 12rpx;
  491. text-align: center;
  492. margin: 0 10rpx;
  493. }
  494. .modal-btn.cancel {
  495. background-color: #f5f5f5;
  496. }
  497. .modal-btn.confirm {
  498. background: linear-gradient(135deg, #ff8c42 0%, #ff6b35 100%);
  499. }
  500. .modal-btn-text {
  501. font-size: 30rpx;
  502. color: #666666;
  503. font-weight: bold;
  504. }
  505. .modal-btn.confirm .modal-btn-text {
  506. color: #ffffff;
  507. }
  508. .records-list {
  509. flex: 1;
  510. overflow-y: auto;
  511. max-height: 50vh;
  512. }
  513. .record-item {
  514. padding: 20rpx 0;
  515. border-bottom: 1rpx solid #eeeeee;
  516. }
  517. .record-header {
  518. display: flex;
  519. justify-content: space-between;
  520. align-items: center;
  521. margin-bottom: 10rpx;
  522. }
  523. .record-name {
  524. font-size: 28rpx;
  525. color: #333333;
  526. font-weight: bold;
  527. }
  528. .record-points {
  529. font-size: 28rpx;
  530. color: #ff6b35;
  531. font-weight: bold;
  532. }
  533. .record-meta {
  534. display: flex;
  535. justify-content: space-between;
  536. }
  537. .record-code {
  538. font-size: 24rpx;
  539. color: #666666;
  540. }
  541. .record-date {
  542. font-size: 24rpx;
  543. color: #999999;
  544. }
  545. .modal-close {
  546. margin-top: 20rpx;
  547. padding: 20rpx 0;
  548. background: linear-gradient(135deg, #ff8c42 0%, #ff6b35 100%);
  549. border-radius: 12rpx;
  550. text-align: center;
  551. }
  552. .modal-close-text {
  553. font-size: 30rpx;
  554. color: #ffffff;
  555. font-weight: bold;
  556. }
  557. </style>