coupons.vue 14 KB

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