coupons.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <template>
  2. <view class="page">
  3. <view class="tab-bar">
  4. <view
  5. v-for="(tab, idx) in tabs"
  6. :key="idx"
  7. class="tab-item"
  8. :class="tabIndex === idx ? 'active' : ''"
  9. @click="switchTab(idx)"
  10. >
  11. <text>{{ tab.name }}</text>
  12. </view>
  13. </view>
  14. <scroll-view
  15. scroll-y
  16. class="coupon-scroll"
  17. @scrolltolower="onScrollToLower"
  18. refresher-enabled
  19. :refresher-triggered="refreshing"
  20. @refresherrefresh="onRefresh"
  21. >
  22. <view v-if="tabIndex === 3" class="coupon-list">
  23. <view
  24. v-for="(item, index) in exchangeableList"
  25. :key="getCouponKey(item, index)"
  26. class="coupon-card"
  27. >
  28. <view class="card-left">
  29. <text class="card-value">{{ item.pointsPrice }}</text>
  30. <text class="card-type-label">积分</text>
  31. </view>
  32. <view class="card-right">
  33. <view class="card-header">
  34. <text class="card-name">{{ item.name }}</text>
  35. </view>
  36. <text class="card-condition">{{ getCouponDesc(item) }}</text>
  37. <text class="card-scope">{{ getScopeText(item.applicableTo) }}</text>
  38. <text class="card-expiry">有效期至 {{ formatDate(item.validUntil) }}</text>
  39. <button class="claim-btn" @click="doExchange(item)">兑换</button>
  40. </view>
  41. </view>
  42. <view v-if="!loading && exchangeableList.length === 0" class="empty-state">
  43. <text class="empty-icon">🎫</text>
  44. <text class="empty-text">暂无可用兑换的优惠券</text>
  45. <text class="empty-hint">积分可以在商城中兑换优惠券</text>
  46. </view>
  47. </view>
  48. <view v-else-if="loading" class="loading-wrap">
  49. <text class="loading-text">加载中...</text>
  50. </view>
  51. <view v-else-if="filteredList.length === 0" class="empty-state">
  52. <text class="empty-icon">🎫</text>
  53. <text class="empty-text">{{ tabIndex === 0 ? '暂无可用优惠券' : tabIndex === 1 ? '暂无已使用优惠券' : '暂无已过期优惠券' }}</text>
  54. <text class="empty-hint" v-if="tabIndex === 0">去领券中心看看有哪些优惠吧</text>
  55. </view>
  56. <view v-else class="coupon-list">
  57. <view
  58. v-for="coupon in filteredList"
  59. :key="coupon.id"
  60. class="coupon-card"
  61. :class="statusClassMap[coupon.id]"
  62. >
  63. <view class="card-left">
  64. <text class="card-value">{{ formatPriceWithSymbol(coupon.value) }}</text>
  65. <text class="card-type-label">{{ getTypeLabel(coupon.type) }}</text>
  66. </view>
  67. <view class="card-right">
  68. <view class="card-header">
  69. <text class="card-name">{{ coupon.name }}</text>
  70. <text v-if="getStatusLabel(coupon)" class="card-status-badge" :class="statusClassMap[coupon.id]">{{ getStatusLabel(coupon) }}</text>
  71. </view>
  72. <text class="card-condition">{{ getConditionText(coupon.minSpend) }}</text>
  73. <text class="card-scope">{{ getScopeText(coupon.applicableTo) }}</text>
  74. <text class="card-expiry">有效期至 {{ formatDate(coupon.validUntil) }}</text>
  75. <button
  76. v-if="canClaim(coupon)"
  77. class="claim-btn"
  78. @click="handleClaim(coupon)"
  79. >立即领取</button>
  80. </view>
  81. </view>
  82. </view>
  83. </scroll-view>
  84. </view>
  85. </template>
  86. <script>
  87. import { getCouponList, claimCoupon, getExchangeableCoupons, exchangeCoupon } from '../../utils/api.js'
  88. export default {
  89. data() {
  90. return {
  91. tabs: [
  92. { name: '可使用', status: 'AVAILABLE' },
  93. { name: '已使用', status: 'USED' },
  94. { name: '已过期', status: 'EXPIRED' },
  95. { name: '可兑换' }
  96. ],
  97. tabIndex: 0,
  98. coupons: [],
  99. exchangeableList: [],
  100. loading: false,
  101. refreshing: false
  102. }
  103. },
  104. computed: {
  105. filteredList: function() {
  106. var self = this
  107. var currentStatus = this.tabs[this.tabIndex].status
  108. if (currentStatus === 'AVAILABLE') {
  109. return this.coupons.filter(function(c) {
  110. return c.status === 'AVAILABLE' || !c.status
  111. })
  112. }
  113. return this.coupons.filter(function(c) {
  114. return c.status === currentStatus
  115. })
  116. },
  117. statusClassMap: function() {
  118. var map = {}
  119. var list = this.filteredList
  120. for (var i = 0; i < list.length; i++) {
  121. var c = list[i]
  122. if (c.status === 'USED') {
  123. map[c.id] = 'status-used'
  124. } else if (c.status === 'EXPIRED') {
  125. map[c.id] = 'status-expired'
  126. } else {
  127. map[c.id] = ''
  128. }
  129. }
  130. return map
  131. }
  132. },
  133. onShow() {
  134. this.loadCoupons()
  135. this.loadExchangeable()
  136. },
  137. onPullDownRefresh() {
  138. this.refreshing = true
  139. this.loadCoupons()
  140. this.loadExchangeable()
  141. },
  142. methods: {
  143. async loadCoupons() {
  144. this.loading = true
  145. try {
  146. var res = await getCouponList()
  147. this.coupons = res.data || []
  148. } catch (e) {
  149. console.error('加载优惠券失败', e)
  150. } finally {
  151. this.loading = false
  152. this.refreshing = false
  153. uni.stopPullDownRefresh()
  154. }
  155. },
  156. onRefresh() {
  157. this.refreshing = true
  158. this.loadCoupons()
  159. },
  160. onScrollToLower() {},
  161. switchTab(idx) {
  162. this.tabIndex = idx
  163. if (idx === 3) {
  164. this.loadExchangeable()
  165. }
  166. },
  167. loadExchangeable() {
  168. getExchangeableCoupons().then(res => {
  169. if (res.code === 200) {
  170. this.exchangeableList = res.data || []
  171. }
  172. }).catch(e => {
  173. console.error('加载可兑换优惠券失败', e)
  174. })
  175. },
  176. getCouponKey(item, index) {
  177. return item.id + '-' + index
  178. },
  179. getCouponDesc(item) {
  180. if (item.type === 'DISCOUNT') {
  181. return '折扣券 ' + (item.discountRate / 100).toFixed(1) + '折'
  182. }
  183. return '立减 ' + (item.value / 100).toFixed(2) + '元'
  184. },
  185. doExchange(item) {
  186. var self = this
  187. uni.showModal({
  188. title: '确认兑换',
  189. content: '消耗 ' + item.pointsPrice + ' 积分兑换「' + item.name + '」?',
  190. success: (r) => {
  191. if (r.confirm) {
  192. exchangeCoupon({ couponId: item.id }).then(res => {
  193. if (res.code === 200) {
  194. uni.showToast({ title: '兑换成功', icon: 'success' })
  195. self.loadExchangeable()
  196. self.loadCoupons()
  197. } else {
  198. uni.showToast({ title: res.message || '兑换失败', icon: 'none' })
  199. }
  200. }).catch(e => {
  201. uni.showToast({ title: e.message || '兑换失败', icon: 'none' })
  202. })
  203. }
  204. }
  205. })
  206. },
  207. canClaim(coupon) {
  208. return this.tabIndex === 0 && (!coupon.status || coupon.status === 'AVAILABLE')
  209. },
  210. async handleClaim(coupon) {
  211. try {
  212. var res = await claimCoupon(coupon.id)
  213. uni.showToast({ title: res.message || '领取成功', icon: 'success' })
  214. this.loadCoupons()
  215. } catch (e) {
  216. uni.showToast({ title: e.message || '领取失败', icon: 'none' })
  217. }
  218. },
  219. getConditionText(minSpend) {
  220. if (!minSpend || minSpend <= 0) return '无门槛'
  221. return '满' + (minSpend / 100).toFixed(2) + '元可用'
  222. },
  223. getScopeText(applicableTo) {
  224. if (applicableTo === 'ALL') return '全品类适用'
  225. if (applicableTo === 'MEMBERSHIP') return '仅限会员'
  226. return applicableTo || '全品类适用'
  227. },
  228. getTypeLabel(type) {
  229. if (type === 'FIXED') return '固定金额'
  230. if (type === 'PERCENT') return '折扣券'
  231. return type || ''
  232. },
  233. getStatusLabel(coupon) {
  234. if (coupon.status === 'USED') return '已使用'
  235. if (coupon.status === 'EXPIRED') return '已过期'
  236. return ''
  237. },
  238. getStatusClass(coupon) {
  239. if (coupon.status === 'USED') return 'status-used'
  240. if (coupon.status === 'EXPIRED') return 'status-expired'
  241. return ''
  242. },
  243. formatDate(date) {
  244. if (!date) return ''
  245. var d = new Date(date)
  246. var y = d.getFullYear()
  247. var m = String(d.getMonth() + 1).padStart(2, '0')
  248. var day = String(d.getDate()).padStart(2, '0')
  249. return y + '.' + m + '.' + day
  250. }
  251. }
  252. }
  253. </script>
  254. <style scoped>
  255. .page {
  256. min-height: 100vh;
  257. background: #f5f5f5;
  258. }
  259. .tab-bar {
  260. display: flex;
  261. background: #fff;
  262. border-bottom: 1rpx solid #eee;
  263. position: sticky;
  264. top: 0;
  265. z-index: 10;
  266. }
  267. .tab-item {
  268. flex: 1;
  269. text-align: center;
  270. padding: 24rpx 0;
  271. font-size: 28rpx;
  272. color: #666;
  273. position: relative;
  274. }
  275. .tab-item.active {
  276. color: #F97316;
  277. font-weight: 600;
  278. }
  279. .tab-item.active::after {
  280. content: '';
  281. position: absolute;
  282. bottom: 0;
  283. left: 50%;
  284. transform: translateX(-50%);
  285. width: 40rpx;
  286. height: 4rpx;
  287. background: #F97316;
  288. border-radius: 2rpx;
  289. }
  290. .coupon-scroll {
  291. height: calc(100vh - 88rpx);
  292. }
  293. .loading-wrap {
  294. display: flex;
  295. justify-content: center;
  296. padding-top: 200rpx;
  297. }
  298. .loading-text {
  299. font-size: 28rpx;
  300. color: #999;
  301. }
  302. .empty-state {
  303. display: flex;
  304. flex-direction: column;
  305. align-items: center;
  306. padding-top: 200rpx;
  307. }
  308. .empty-icon {
  309. font-size: 80rpx;
  310. margin-bottom: 20rpx;
  311. }
  312. .empty-text {
  313. font-size: 28rpx;
  314. color: #999;
  315. }
  316. .empty-hint {
  317. font-size: 24rpx;
  318. color: #ccc;
  319. margin-top: 12rpx;
  320. }
  321. .coupon-list {
  322. padding: 20rpx;
  323. }
  324. .coupon-card {
  325. display: flex;
  326. background: #fff;
  327. border-radius: 16rpx;
  328. margin-bottom: 20rpx;
  329. overflow: hidden;
  330. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  331. }
  332. .coupon-card.status-used {
  333. opacity: 0.65;
  334. }
  335. .coupon-card.status-expired {
  336. opacity: 0.5;
  337. }
  338. .card-left {
  339. width: 200rpx;
  340. display: flex;
  341. flex-direction: column;
  342. align-items: center;
  343. justify-content: center;
  344. background: linear-gradient(135deg, #F97316, #FB923C);
  345. padding: 30rpx 12rpx;
  346. flex-shrink: 0;
  347. }
  348. .status-used .card-left {
  349. background: #b0b0b0;
  350. }
  351. .status-expired .card-left {
  352. background: #d0d0d0;
  353. }
  354. .card-value {
  355. font-size: 40rpx;
  356. font-weight: bold;
  357. color: #fff;
  358. }
  359. .card-type-label {
  360. font-size: 20rpx;
  361. color: rgba(255,255,255,0.8);
  362. margin-top: 8rpx;
  363. }
  364. .card-right {
  365. flex: 1;
  366. padding: 24rpx 24rpx 20rpx;
  367. position: relative;
  368. }
  369. .card-header {
  370. display: flex;
  371. align-items: center;
  372. justify-content: space-between;
  373. margin-bottom: 10rpx;
  374. }
  375. .card-name {
  376. font-size: 28rpx;
  377. font-weight: 600;
  378. color: #333;
  379. flex: 1;
  380. overflow: hidden;
  381. text-overflow: ellipsis;
  382. white-space: nowrap;
  383. }
  384. .card-status-badge {
  385. font-size: 20rpx;
  386. padding: 4rpx 14rpx;
  387. border-radius: 20rpx;
  388. flex-shrink: 0;
  389. margin-left: 12rpx;
  390. }
  391. .card-status-badge.status-used {
  392. background: #f0f0f0;
  393. color: #999;
  394. }
  395. .card-status-badge.status-expired {
  396. background: #fff1f0;
  397. color: #ff4d4f;
  398. }
  399. .card-condition {
  400. font-size: 24rpx;
  401. color: #F97316;
  402. display: block;
  403. margin-bottom: 6rpx;
  404. }
  405. .card-scope {
  406. font-size: 22rpx;
  407. color: #5B9BD5;
  408. background: #f0f7ff;
  409. display: inline-block;
  410. padding: 2rpx 12rpx;
  411. border-radius: 8rpx;
  412. margin-bottom: 6rpx;
  413. }
  414. .card-expiry {
  415. font-size: 22rpx;
  416. color: #bbb;
  417. display: block;
  418. }
  419. .claim-btn {
  420. position: absolute;
  421. right: 24rpx;
  422. bottom: 20rpx;
  423. background: linear-gradient(135deg, #F97316, #FB923C);
  424. color: #fff;
  425. font-size: 24rpx;
  426. padding: 10rpx 28rpx;
  427. border-radius: 28rpx;
  428. border: none;
  429. line-height: 1.4;
  430. height: auto;
  431. }
  432. .claim-btn::after {
  433. border: none;
  434. }
  435. .claim-btn:active {
  436. opacity: 0.85;
  437. }
  438. </style>