mall.vue 14 KB

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