coupons.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. memberPrice: d.memberPrice != null ? d.memberPrice : 0,
  257. quantity: 1
  258. }])
  259. uni.navigateTo({
  260. url: '/pages/shop/checkout/checkout?from=coupon&id=' + d.id + '&couponId=' + coupon.id
  261. })
  262. } catch (e) {
  263. uni.navigateTo({ url: '/pages/shop/index/index' })
  264. }
  265. } else if (coupon.applicableTo === 'ALL') {
  266. uni.navigateTo({ url: '/pages/shop/index/index' })
  267. } else if (coupon.applicableTo === 'MEMBERSHIP') {
  268. uni.navigateTo({ url: '/pages/membership/upgrade' })
  269. } else {
  270. uni.navigateTo({ url: '/pages/shop/index/index' })
  271. }
  272. },
  273. giftCoupon(coupon) {
  274. uni.navigateTo({ url: '/pages/membership/gift-coupon' })
  275. },
  276. getConditionText(minSpend) {
  277. if (!minSpend || minSpend <= 0) return '无门槛'
  278. return '满' + (minSpend / 100).toFixed(2) + '元可用'
  279. },
  280. getScopeText(applicableTo, isMember) {
  281. if (applicableTo === 'ALL') return '全品类适用'
  282. if (applicableTo === 'MEMBERSHIP') {
  283. return isMember ? '会员专享' : '开通会员后可用'
  284. }
  285. if (applicableTo === 'PRODUCT') return '特定商品'
  286. if (applicableTo === 'ASSESSMENT') return '测评服务'
  287. return applicableTo || '全品类适用'
  288. },
  289. getTypeLabel(type) {
  290. if (type === 'FIXED') return '固定金额'
  291. if (type === 'PERCENT') return '折扣券'
  292. if (type === 'DISCOUNT') return '折扣券'
  293. if (type === 'CASH') return '无门槛券'
  294. return type || ''
  295. },
  296. getStatusLabel(coupon) {
  297. if (coupon.status === 'USED') return '已使用'
  298. if (coupon.status === 'EXPIRED') return '已过期'
  299. return ''
  300. },
  301. getStatusClass(coupon) {
  302. if (coupon.status === 'USED') return 'status-used'
  303. if (coupon.status === 'EXPIRED') return 'status-expired'
  304. return ''
  305. },
  306. formatDate(date) {
  307. if (!date) return ''
  308. var d = parseDate(date)
  309. if (!d) return date
  310. var y = d.getFullYear()
  311. var m = String(d.getMonth() + 1).padStart(2, '0')
  312. var day = String(d.getDate()).padStart(2, '0')
  313. return y + '.' + m + '.' + day
  314. },
  315. goToMembership() {
  316. uni.navigateTo({ url: '/pages/membership/index' })
  317. }
  318. }
  319. }
  320. </script>
  321. <style scoped lang="scss">
  322. .page {
  323. min-height: 100vh;
  324. background: $facade-fill-2;
  325. }
  326. .tab-bar {
  327. display: flex;
  328. background: $app-surface;
  329. border-bottom: 1rpx solid $facade-hairline-2;
  330. position: sticky;
  331. top: 0;
  332. z-index: 10;
  333. }
  334. .tab-item {
  335. flex: 1;
  336. text-align: center;
  337. padding: 24rpx 0;
  338. font-size: calc(28rpx * var(--font-scale, 1));
  339. color: $facade-text-body;
  340. position: relative;
  341. }
  342. .tab-item.active {
  343. color: $brand-primary;
  344. font-weight: 600;
  345. }
  346. .tab-item.active::after {
  347. content: '';
  348. position: absolute;
  349. bottom: 0;
  350. left: 50%;
  351. transform: translateX(-50%);
  352. width: 40rpx;
  353. height: 4rpx;
  354. background: $brand-primary;
  355. border-radius: 8rpx;
  356. }
  357. .coupon-scroll {
  358. height: calc(100vh - 88rpx);
  359. }
  360. .loading-wrap {
  361. display: flex;
  362. justify-content: center;
  363. padding-top: 200rpx;
  364. }
  365. .loading-text {
  366. font-size: calc(28rpx * var(--font-scale, 1));
  367. color: $facade-text-muted;
  368. }
  369. .empty-state {
  370. display: flex;
  371. flex-direction: column;
  372. align-items: center;
  373. padding-top: 200rpx;
  374. }
  375. .empty-icon {
  376. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  377. margin-bottom: 20rpx;
  378. }
  379. .empty-text {
  380. font-size: calc(28rpx * var(--font-scale, 1));
  381. color: $facade-text-muted;
  382. }
  383. .empty-hint {
  384. font-size: calc(24rpx * var(--font-scale, 1));
  385. color: $facade-text-ghost;
  386. margin-top: 12rpx;
  387. }
  388. .empty-hint-link {
  389. color: $brand-primary;
  390. }
  391. .empty-hint-link:active {
  392. opacity: 0.8;
  393. }
  394. .coupon-list {
  395. padding: 20rpx;
  396. }
  397. .coupon-card {
  398. display: flex;
  399. background: $app-surface;
  400. border-radius: 16rpx;
  401. margin-bottom: 20rpx;
  402. overflow: hidden;
  403. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  404. }
  405. .coupon-card.status-used {
  406. opacity: 0.65;
  407. }
  408. .coupon-card.status-expired {
  409. opacity: 0.5;
  410. }
  411. .card-left {
  412. width: 200rpx;
  413. display: flex;
  414. flex-direction: column;
  415. align-items: center;
  416. justify-content: center;
  417. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  418. padding: $uni-space-lg 12rpx;
  419. flex-shrink: 0;
  420. }
  421. .status-used .card-left {
  422. background: #b0b0b0;
  423. }
  424. .status-expired .card-left {
  425. background: #d0d0d0;
  426. }
  427. .card-value {
  428. font-size: calc(40rpx * var(--font-scale, 1));
  429. font-weight: bold;
  430. color: $app-surface;
  431. }
  432. .card-type-label {
  433. font-size: calc(20rpx * var(--font-scale, 1));
  434. color: rgba(255,255,255,0.8);
  435. margin-top: 8rpx;
  436. }
  437. .card-right {
  438. flex: 1;
  439. padding: 24rpx 24rpx 20rpx;
  440. position: relative;
  441. }
  442. .card-header {
  443. display: flex;
  444. align-items: center;
  445. justify-content: space-between;
  446. margin-bottom: 10rpx;
  447. }
  448. .card-name {
  449. font-size: calc(28rpx * var(--font-scale, 1));
  450. font-weight: 600;
  451. color: $facade-text-strong;
  452. flex: 1;
  453. overflow: hidden;
  454. text-overflow: ellipsis;
  455. white-space: nowrap;
  456. }
  457. .card-status-badge {
  458. font-size: calc(20rpx * var(--font-scale, 1));
  459. padding: 4rpx 14rpx;
  460. border-radius: 24rpx;
  461. flex-shrink: 0;
  462. margin-left: 12rpx;
  463. }
  464. .card-status-badge.status-used {
  465. background: $facade-fill;
  466. color: $facade-text-muted;
  467. }
  468. .card-status-badge.status-expired {
  469. background: $status-danger-tint-3;
  470. color: $status-danger-2;
  471. }
  472. .card-condition {
  473. font-size: calc(24rpx * var(--font-scale, 1));
  474. color: $brand-primary;
  475. display: block;
  476. margin-bottom: 6rpx;
  477. }
  478. .card-scope {
  479. font-size: calc(22rpx * var(--font-scale, 1));
  480. color: $uni-info;
  481. background: $tint-sky-ghost;
  482. display: inline-block;
  483. padding: 2rpx 12rpx;
  484. border-radius: 8rpx;
  485. margin-bottom: 6rpx;
  486. }
  487. .card-expiry {
  488. font-size: calc(22rpx * var(--font-scale, 1));
  489. color: $facade-text-ghost-light;
  490. display: block;
  491. }
  492. .claim-btn {
  493. position: absolute;
  494. right: 24rpx;
  495. bottom: 20rpx;
  496. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  497. color: $app-surface;
  498. font-size: calc(24rpx * var(--font-scale, 1));
  499. padding: 10rpx 28rpx;
  500. border-radius: 24rpx;
  501. border: none;
  502. line-height: 1.4;
  503. height: auto;
  504. }
  505. .claim-btn::after {
  506. border: none;
  507. }
  508. .claim-btn:active {
  509. opacity: 0.85;
  510. }
  511. .member-guide-btn {
  512. position: absolute;
  513. right: 24rpx;
  514. bottom: 20rpx;
  515. background: $brand-primary;
  516. color: $app-surface;
  517. font-size: calc(24rpx * var(--font-scale, 1));
  518. padding: 10rpx 28rpx;
  519. border-radius: 24rpx;
  520. border: none;
  521. line-height: 1.4;
  522. height: auto;
  523. }
  524. .member-guide-btn::after {
  525. border: none;
  526. }
  527. .member-guide-btn:active {
  528. opacity: 0.85;
  529. }
  530. /* 已领取券操作区:使用 + 转赠 */
  531. .coupon-actions {
  532. position: absolute;
  533. right: 24rpx;
  534. bottom: 20rpx;
  535. display: flex;
  536. align-items: center;
  537. gap: 12rpx;
  538. }
  539. .use-btn {
  540. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  541. color: $app-surface;
  542. font-size: calc(24rpx * var(--font-scale, 1));
  543. padding: 10rpx 24rpx;
  544. border-radius: 24rpx;
  545. border: none;
  546. line-height: 1.4;
  547. height: auto;
  548. }
  549. .use-btn::after {
  550. border: none;
  551. }
  552. .use-btn:active {
  553. opacity: 0.85;
  554. }
  555. .gift-btn {
  556. background: $facade-fill;
  557. color: $facade-text-strong;
  558. font-size: calc(24rpx * var(--font-scale, 1));
  559. padding: 10rpx 24rpx;
  560. border-radius: 24rpx;
  561. border: none;
  562. line-height: 1.4;
  563. height: auto;
  564. }
  565. .gift-btn::after {
  566. border: none;
  567. }
  568. .gift-btn:active {
  569. opacity: 0.85;
  570. }
  571. .gift-entry {
  572. display: flex;
  573. align-items: center;
  574. justify-content: space-between;
  575. margin: 20rpx 0;
  576. padding: 24rpx;
  577. background: linear-gradient(135deg, #ff8a4c, #ff6b35);
  578. border-radius: 16rpx;
  579. box-shadow: 0 6rpx 18rpx rgba(255, 107, 53, 0.25);
  580. }
  581. .gift-entry-text {
  582. color: #fff;
  583. font-size: 28rpx;
  584. font-weight: bold;
  585. }
  586. .gift-entry-arrow {
  587. color: #fff;
  588. font-size: 36rpx;
  589. }
  590. </style>