coupons.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 empty-hint-link" v-if="tabIndex === 0" @click="goToCouponCenter">去领券中心看看有哪些优惠吧 ›</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">{{ getCardValue(coupon) }}</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. <view v-if="tabIndex === 0 && (!coupon.status || coupon.status === 'AVAILABLE')" class="coupon-actions">
  80. <button class="use-btn" @click="useCoupon(coupon)">使用</button>
  81. <button class="gift-btn" @click="giftCoupon(coupon)">转赠</button>
  82. </view>
  83. </view>
  84. </view>
  85. </view>
  86. </scroll-view>
  87. </view>
  88. </template>
  89. <script>
  90. import { getMyCoupons, getExchangeableCoupons, exchangeCouponByCf, getMyMembership, productDetail } from '../../utils/api.js'
  91. import { parseDate } from '../../utils/format.js'
  92. import fontScaleMixin from '@/mixins/font-scale.js'
  93. export default {
  94. mixins: [fontScaleMixin],
  95. data() {
  96. return {
  97. tabs: [
  98. { name: '可使用', status: 'AVAILABLE' },
  99. { name: '已使用', status: 'USED' },
  100. { name: '已过期', status: 'EXPIRED' },
  101. { name: 'CF值兑换' }
  102. ],
  103. tabIndex: 0,
  104. coupons: [],
  105. exchangeableList: [],
  106. loading: false,
  107. refreshing: false,
  108. isMember: false
  109. }
  110. },
  111. computed: {
  112. filteredList: function() {
  113. var self = this
  114. var currentStatus = this.tabs[this.tabIndex].status
  115. if (currentStatus === 'AVAILABLE') {
  116. return this.coupons.filter(function(c) {
  117. return c.status === 'AVAILABLE' || !c.status
  118. })
  119. }
  120. return this.coupons.filter(function(c) {
  121. return c.status === currentStatus
  122. })
  123. },
  124. statusClassMap: function() {
  125. var map = {}
  126. var list = this.filteredList
  127. for (var i = 0; i < list.length; i++) {
  128. var c = list[i]
  129. if (c.status === 'USED') {
  130. map[c.id] = 'status-used'
  131. } else if (c.status === 'EXPIRED') {
  132. map[c.id] = 'status-expired'
  133. } else {
  134. map[c.id] = ''
  135. }
  136. }
  137. return map
  138. }
  139. },
  140. onShow() {
  141. this.loadCoupons()
  142. this.loadExchangeable()
  143. this.checkMemberStatus()
  144. },
  145. onPullDownRefresh() {
  146. this.refreshing = true
  147. this.loadCoupons()
  148. this.loadExchangeable()
  149. },
  150. methods: {
  151. async loadCoupons() {
  152. this.loading = true
  153. try {
  154. var res = await getMyCoupons()
  155. this.coupons = res.data || []
  156. } catch (e) {
  157. console.error('加载优惠券失败', e)
  158. } finally {
  159. this.loading = false
  160. this.refreshing = false
  161. uni.stopPullDownRefresh()
  162. }
  163. },
  164. onRefresh() {
  165. this.refreshing = true
  166. this.loadCoupons()
  167. },
  168. onScrollToLower() {},
  169. switchTab(idx) {
  170. this.tabIndex = idx
  171. if (idx === 3) {
  172. this.loadExchangeable()
  173. }
  174. },
  175. loadExchangeable() {
  176. getExchangeableCoupons().then(res => {
  177. if (res.code === 200) {
  178. this.exchangeableList = res.data || []
  179. }
  180. }).catch(e => {
  181. console.error('加载可兑换优惠券失败', e)
  182. })
  183. },
  184. checkMemberStatus() {
  185. var self = this
  186. getMyMembership().then(function(res) {
  187. if (res && res.code === 200 && res.data) {
  188. var membership = res.data.membership
  189. self.isMember = membership && membership.isActive
  190. }
  191. }).catch(function() {})
  192. },
  193. getCouponDesc(item) {
  194. if (item.type === 'DISCOUNT' || item.type === 'PERCENT') {
  195. return '折扣券 ' + (item.discountRate / 100).toFixed(1) + '折'
  196. }
  197. return '立减 ' + (item.value / 100).toFixed(2) + '元'
  198. },
  199. getCardValue(coupon) {
  200. if (coupon.type === 'DISCOUNT' || coupon.type === 'PERCENT') {
  201. return (coupon.discountRate / 100).toFixed(1) + '折'
  202. }
  203. return this.formatPriceWithSymbol(coupon.value)
  204. },
  205. doExchange(item) {
  206. var self = this
  207. uni.showModal({
  208. title: '确认兑换',
  209. content: '消耗 ' + item.pointsPrice + ' CF值兑换「' + item.name + '」?',
  210. success: (r) => {
  211. if (r.confirm) {
  212. exchangeCouponByCf({ couponId: item.id }).then(res => {
  213. if (res.code === 200) {
  214. uni.showToast({ title: '兑换成功', icon: 'success' })
  215. self.loadExchangeable()
  216. self.loadCoupons()
  217. } else {
  218. uni.showToast({ title: res.message || '兑换失败', icon: 'none' })
  219. }
  220. }).catch(e => {
  221. uni.showToast({ title: e.message || '兑换失败', icon: 'none' })
  222. })
  223. }
  224. }
  225. })
  226. },
  227. goGiftCoupon() {
  228. uni.navigateTo({ url: '/pages/membership/gift-coupon' })
  229. },
  230. goToCouponCenter() {
  231. uni.navigateTo({ url: '/pages/profile-extra/coupon-center' })
  232. },
  233. async useCoupon(coupon) {
  234. var userId = uni.getStorageSync('userId')
  235. if (!userId) {
  236. this.openLoginSheet()
  237. return
  238. }
  239. if (coupon.applicableTo === 'PRODUCT') {
  240. if (!coupon.productId) {
  241. uni.navigateTo({ url: '/pages/shop/index/index' })
  242. return
  243. }
  244. try {
  245. var res = await productDetail({ id: coupon.productId })
  246. var d = res && res.data
  247. if (!d) {
  248. uni.navigateTo({ url: '/pages/shop/index/index' })
  249. return
  250. }
  251. uni.setStorageSync('checkoutItems', [{
  252. productId: d.id,
  253. productName: d.name || '商品',
  254. coverImage: d.coverImage || '',
  255. unitPrice: d.salePrice != null ? d.salePrice : d.price,
  256. quantity: 1
  257. }])
  258. uni.navigateTo({
  259. url: '/pages/shop/checkout/checkout?from=coupon&id=' + d.id + '&couponId=' + coupon.id
  260. })
  261. } catch (e) {
  262. uni.navigateTo({ url: '/pages/shop/index/index' })
  263. }
  264. } else if (coupon.applicableTo === 'ALL') {
  265. uni.navigateTo({ url: '/pages/shop/index/index' })
  266. } else if (coupon.applicableTo === 'MEMBERSHIP') {
  267. uni.navigateTo({ url: '/pages/membership/upgrade' })
  268. } else {
  269. uni.navigateTo({ url: '/pages/shop/index/index' })
  270. }
  271. },
  272. giftCoupon(coupon) {
  273. uni.navigateTo({ url: '/pages/membership/gift-coupon' })
  274. },
  275. getConditionText(minSpend) {
  276. if (!minSpend || minSpend <= 0) return '无门槛'
  277. return '满' + (minSpend / 100).toFixed(2) + '元可用'
  278. },
  279. getScopeText(applicableTo, isMember) {
  280. if (applicableTo === 'ALL') return '全品类适用'
  281. if (applicableTo === 'MEMBERSHIP') {
  282. return isMember ? '会员专享' : '开通会员后可用'
  283. }
  284. if (applicableTo === 'PRODUCT') return '特定商品'
  285. if (applicableTo === 'ASSESSMENT') return '测评服务'
  286. return applicableTo || '全品类适用'
  287. },
  288. getTypeLabel(type) {
  289. if (type === 'FIXED') return '固定金额'
  290. if (type === 'PERCENT') return '折扣券'
  291. if (type === 'DISCOUNT') return '折扣券'
  292. if (type === 'CASH') return '无门槛券'
  293. return type || ''
  294. },
  295. getStatusLabel(coupon) {
  296. if (coupon.status === 'USED') return '已使用'
  297. if (coupon.status === 'EXPIRED') return '已过期'
  298. return ''
  299. },
  300. getStatusClass(coupon) {
  301. if (coupon.status === 'USED') return 'status-used'
  302. if (coupon.status === 'EXPIRED') return 'status-expired'
  303. return ''
  304. },
  305. formatDate(date) {
  306. if (!date) return ''
  307. var d = parseDate(date)
  308. if (!d) return date
  309. var y = d.getFullYear()
  310. var m = String(d.getMonth() + 1).padStart(2, '0')
  311. var day = String(d.getDate()).padStart(2, '0')
  312. return y + '.' + m + '.' + day
  313. },
  314. goToMembership() {
  315. uni.navigateTo({ url: '/pages/membership/index' })
  316. }
  317. }
  318. }
  319. </script>
  320. <style scoped lang="scss">
  321. .page {
  322. min-height: 100vh;
  323. background: $facade-fill-2;
  324. }
  325. .tab-bar {
  326. display: flex;
  327. background: $app-surface;
  328. border-bottom: 1rpx solid $facade-hairline-2;
  329. position: sticky;
  330. top: 0;
  331. z-index: 10;
  332. }
  333. .tab-item {
  334. flex: 1;
  335. text-align: center;
  336. padding: 24rpx 0;
  337. font-size: calc(28rpx * var(--font-scale, 1));
  338. color: $facade-text-body;
  339. position: relative;
  340. }
  341. .tab-item.active {
  342. color: $brand-primary;
  343. font-weight: 600;
  344. }
  345. .tab-item.active::after {
  346. content: '';
  347. position: absolute;
  348. bottom: 0;
  349. left: 50%;
  350. transform: translateX(-50%);
  351. width: 40rpx;
  352. height: 4rpx;
  353. background: $brand-primary;
  354. border-radius: 8rpx;
  355. }
  356. .coupon-scroll {
  357. height: calc(100vh - 88rpx);
  358. }
  359. .loading-wrap {
  360. display: flex;
  361. justify-content: center;
  362. padding-top: 200rpx;
  363. }
  364. .loading-text {
  365. font-size: calc(28rpx * var(--font-scale, 1));
  366. color: $facade-text-muted;
  367. }
  368. .empty-state {
  369. display: flex;
  370. flex-direction: column;
  371. align-items: center;
  372. padding-top: 200rpx;
  373. }
  374. .empty-icon {
  375. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  376. margin-bottom: 20rpx;
  377. }
  378. .empty-text {
  379. font-size: calc(28rpx * var(--font-scale, 1));
  380. color: $facade-text-muted;
  381. }
  382. .empty-hint {
  383. font-size: calc(24rpx * var(--font-scale, 1));
  384. color: $facade-text-ghost;
  385. margin-top: 12rpx;
  386. }
  387. .empty-hint-link {
  388. color: $brand-primary;
  389. }
  390. .empty-hint-link:active {
  391. opacity: 0.8;
  392. }
  393. .coupon-list {
  394. padding: 20rpx;
  395. }
  396. .coupon-card {
  397. display: flex;
  398. background: $app-surface;
  399. border-radius: 16rpx;
  400. margin-bottom: 20rpx;
  401. overflow: hidden;
  402. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  403. }
  404. .coupon-card.status-used {
  405. opacity: 0.65;
  406. }
  407. .coupon-card.status-expired {
  408. opacity: 0.5;
  409. }
  410. .card-left {
  411. width: 200rpx;
  412. display: flex;
  413. flex-direction: column;
  414. align-items: center;
  415. justify-content: center;
  416. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  417. padding: $uni-space-lg 12rpx;
  418. flex-shrink: 0;
  419. }
  420. .status-used .card-left {
  421. background: #b0b0b0;
  422. }
  423. .status-expired .card-left {
  424. background: #d0d0d0;
  425. }
  426. .card-value {
  427. font-size: calc(40rpx * var(--font-scale, 1));
  428. font-weight: bold;
  429. color: $app-surface;
  430. }
  431. .card-type-label {
  432. font-size: calc(20rpx * var(--font-scale, 1));
  433. color: rgba(255,255,255,0.8);
  434. margin-top: 8rpx;
  435. }
  436. .card-right {
  437. flex: 1;
  438. padding: 24rpx 24rpx 20rpx;
  439. position: relative;
  440. }
  441. .card-header {
  442. display: flex;
  443. align-items: center;
  444. justify-content: space-between;
  445. margin-bottom: 10rpx;
  446. }
  447. .card-name {
  448. font-size: calc(28rpx * var(--font-scale, 1));
  449. font-weight: 600;
  450. color: $facade-text-strong;
  451. flex: 1;
  452. overflow: hidden;
  453. text-overflow: ellipsis;
  454. white-space: nowrap;
  455. }
  456. .card-status-badge {
  457. font-size: calc(20rpx * var(--font-scale, 1));
  458. padding: 4rpx 14rpx;
  459. border-radius: 24rpx;
  460. flex-shrink: 0;
  461. margin-left: 12rpx;
  462. }
  463. .card-status-badge.status-used {
  464. background: $facade-fill;
  465. color: $facade-text-muted;
  466. }
  467. .card-status-badge.status-expired {
  468. background: $status-danger-tint-3;
  469. color: $status-danger-2;
  470. }
  471. .card-condition {
  472. font-size: calc(24rpx * var(--font-scale, 1));
  473. color: $brand-primary;
  474. display: block;
  475. margin-bottom: 6rpx;
  476. }
  477. .card-scope {
  478. font-size: calc(22rpx * var(--font-scale, 1));
  479. color: $uni-info;
  480. background: $tint-sky-ghost;
  481. display: inline-block;
  482. padding: 2rpx 12rpx;
  483. border-radius: 8rpx;
  484. margin-bottom: 6rpx;
  485. }
  486. .card-expiry {
  487. font-size: calc(22rpx * var(--font-scale, 1));
  488. color: $facade-text-ghost-light;
  489. display: block;
  490. }
  491. .claim-btn {
  492. position: absolute;
  493. right: 24rpx;
  494. bottom: 20rpx;
  495. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  496. color: $app-surface;
  497. font-size: calc(24rpx * var(--font-scale, 1));
  498. padding: 10rpx 28rpx;
  499. border-radius: 24rpx;
  500. border: none;
  501. line-height: 1.4;
  502. height: auto;
  503. }
  504. .claim-btn::after {
  505. border: none;
  506. }
  507. .claim-btn:active {
  508. opacity: 0.85;
  509. }
  510. .member-guide-btn {
  511. position: absolute;
  512. right: 24rpx;
  513. bottom: 20rpx;
  514. background: $brand-primary;
  515. color: $app-surface;
  516. font-size: calc(24rpx * var(--font-scale, 1));
  517. padding: 10rpx 28rpx;
  518. border-radius: 24rpx;
  519. border: none;
  520. line-height: 1.4;
  521. height: auto;
  522. }
  523. .member-guide-btn::after {
  524. border: none;
  525. }
  526. .member-guide-btn:active {
  527. opacity: 0.85;
  528. }
  529. /* 已领取券操作区:使用 + 转赠 */
  530. .coupon-actions {
  531. position: absolute;
  532. right: 24rpx;
  533. bottom: 20rpx;
  534. display: flex;
  535. align-items: center;
  536. gap: 12rpx;
  537. }
  538. .use-btn {
  539. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  540. color: $app-surface;
  541. font-size: calc(24rpx * var(--font-scale, 1));
  542. padding: 10rpx 24rpx;
  543. border-radius: 24rpx;
  544. border: none;
  545. line-height: 1.4;
  546. height: auto;
  547. }
  548. .use-btn::after {
  549. border: none;
  550. }
  551. .use-btn:active {
  552. opacity: 0.85;
  553. }
  554. .gift-btn {
  555. background: $facade-fill;
  556. color: $facade-text-strong;
  557. font-size: calc(24rpx * var(--font-scale, 1));
  558. padding: 10rpx 24rpx;
  559. border-radius: 24rpx;
  560. border: none;
  561. line-height: 1.4;
  562. height: auto;
  563. }
  564. .gift-btn::after {
  565. border: none;
  566. }
  567. .gift-btn:active {
  568. opacity: 0.85;
  569. }
  570. .gift-entry {
  571. display: flex;
  572. align-items: center;
  573. justify-content: space-between;
  574. margin: 20rpx 0;
  575. padding: 24rpx;
  576. background: linear-gradient(135deg, #ff8a4c, #ff6b35);
  577. border-radius: 16rpx;
  578. box-shadow: 0 6rpx 18rpx rgba(255, 107, 53, 0.25);
  579. }
  580. .gift-entry-text {
  581. color: #fff;
  582. font-size: 28rpx;
  583. font-weight: bold;
  584. }
  585. .gift-entry-arrow {
  586. color: #fff;
  587. font-size: 36rpx;
  588. }
  589. </style>