coupons.vue 11 KB

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