coupons.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. <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, getMyMembership } 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. getMyMembership().then(function(res) {
  191. if (res && res.code === 200 && res.data) {
  192. var membership = res.data.membership
  193. self.isMember = membership && membership.isActive
  194. }
  195. }).catch(function() {})
  196. },
  197. getCouponDesc(item) {
  198. if (item.type === 'DISCOUNT' || item.type === 'PERCENT') {
  199. return '折扣券 ' + (item.discountRate / 100).toFixed(1) + '折'
  200. }
  201. return '立减 ' + (item.value / 100).toFixed(2) + '元'
  202. },
  203. getCardValue(coupon) {
  204. if (coupon.type === 'DISCOUNT' || coupon.type === 'PERCENT') {
  205. return (coupon.discountRate / 100).toFixed(1) + '折'
  206. }
  207. return this.formatPriceWithSymbol(coupon.value)
  208. },
  209. doExchange(item) {
  210. var self = this
  211. uni.showModal({
  212. title: '确认兑换',
  213. content: '消耗 ' + item.pointsPrice + ' CF值兑换「' + item.name + '」?',
  214. success: (r) => {
  215. if (r.confirm) {
  216. exchangeCouponByCf({ couponId: item.id }).then(res => {
  217. if (res.code === 200) {
  218. uni.showToast({ title: '兑换成功', icon: 'success' })
  219. self.loadExchangeable()
  220. self.loadCoupons()
  221. } else {
  222. uni.showToast({ title: res.message || '兑换失败', icon: 'none' })
  223. }
  224. }).catch(e => {
  225. uni.showToast({ title: e.message || '兑换失败', icon: 'none' })
  226. })
  227. }
  228. }
  229. })
  230. },
  231. canClaim(coupon) {
  232. // 非会员也可领取会员券,但使用时需开通会员
  233. return this.tabIndex === 0 && (!coupon.status || coupon.status === 'AVAILABLE')
  234. },
  235. goGiftCoupon() {
  236. uni.navigateTo({ url: '/pages/membership/gift-coupon' })
  237. },
  238. goToCouponCenter() {
  239. uni.navigateTo({ url: '/pages/profile-extra/coupon-center' })
  240. },
  241. async handleClaim(coupon) {
  242. try {
  243. var res = await claimCoupon(coupon.id)
  244. uni.showToast({ title: res.message || '领取成功', icon: 'success' })
  245. this.loadCoupons()
  246. } catch (e) {
  247. uni.showToast({ title: e.message || '领取失败', icon: 'none' })
  248. }
  249. },
  250. getConditionText(minSpend) {
  251. if (!minSpend || minSpend <= 0) return '无门槛'
  252. return '满' + (minSpend / 100).toFixed(2) + '元可用'
  253. },
  254. getScopeText(applicableTo, isMember) {
  255. if (applicableTo === 'ALL') return '全品类适用'
  256. if (applicableTo === 'MEMBERSHIP') {
  257. return isMember ? '会员专享' : '开通会员后可用'
  258. }
  259. if (applicableTo === 'PRODUCT') return '特定商品'
  260. if (applicableTo === 'ASSESSMENT') return '测评服务'
  261. return applicableTo || '全品类适用'
  262. },
  263. getTypeLabel(type) {
  264. if (type === 'FIXED') return '固定金额'
  265. if (type === 'PERCENT') return '折扣券'
  266. if (type === 'DISCOUNT') return '折扣券'
  267. if (type === 'CASH') return '无门槛券'
  268. return type || ''
  269. },
  270. getStatusLabel(coupon) {
  271. if (coupon.status === 'USED') return '已使用'
  272. if (coupon.status === 'EXPIRED') return '已过期'
  273. return ''
  274. },
  275. getStatusClass(coupon) {
  276. if (coupon.status === 'USED') return 'status-used'
  277. if (coupon.status === 'EXPIRED') return 'status-expired'
  278. return ''
  279. },
  280. formatDate(date) {
  281. if (!date) return ''
  282. var d = parseDate(date)
  283. if (!d) return date
  284. var y = d.getFullYear()
  285. var m = String(d.getMonth() + 1).padStart(2, '0')
  286. var day = String(d.getDate()).padStart(2, '0')
  287. return y + '.' + m + '.' + day
  288. },
  289. goToMembership() {
  290. uni.navigateTo({ url: '/pages/membership/index' })
  291. }
  292. }
  293. }
  294. </script>
  295. <style scoped lang="scss">
  296. .page {
  297. min-height: 100vh;
  298. background: $facade-fill-2;
  299. }
  300. .tab-bar {
  301. display: flex;
  302. background: $app-surface;
  303. border-bottom: 1rpx solid $facade-hairline-2;
  304. position: sticky;
  305. top: 0;
  306. z-index: 10;
  307. }
  308. .tab-item {
  309. flex: 1;
  310. text-align: center;
  311. padding: 24rpx 0;
  312. font-size: calc(28rpx * var(--font-scale, 1));
  313. color: $facade-text-body;
  314. position: relative;
  315. }
  316. .tab-item.active {
  317. color: $brand-primary;
  318. font-weight: 600;
  319. }
  320. .tab-item.active::after {
  321. content: '';
  322. position: absolute;
  323. bottom: 0;
  324. left: 50%;
  325. transform: translateX(-50%);
  326. width: 40rpx;
  327. height: 4rpx;
  328. background: $brand-primary;
  329. border-radius: 8rpx;
  330. }
  331. .coupon-scroll {
  332. height: calc(100vh - 88rpx);
  333. }
  334. .loading-wrap {
  335. display: flex;
  336. justify-content: center;
  337. padding-top: 200rpx;
  338. }
  339. .loading-text {
  340. font-size: calc(28rpx * var(--font-scale, 1));
  341. color: $facade-text-muted;
  342. }
  343. .empty-state {
  344. display: flex;
  345. flex-direction: column;
  346. align-items: center;
  347. padding-top: 200rpx;
  348. }
  349. .empty-icon {
  350. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  351. margin-bottom: 20rpx;
  352. }
  353. .empty-text {
  354. font-size: calc(28rpx * var(--font-scale, 1));
  355. color: $facade-text-muted;
  356. }
  357. .empty-hint {
  358. font-size: calc(24rpx * var(--font-scale, 1));
  359. color: $facade-text-ghost;
  360. margin-top: 12rpx;
  361. }
  362. .empty-hint-link {
  363. color: $brand-primary;
  364. }
  365. .empty-hint-link:active {
  366. opacity: 0.8;
  367. }
  368. .coupon-list {
  369. padding: 20rpx;
  370. }
  371. .coupon-card {
  372. display: flex;
  373. background: $app-surface;
  374. border-radius: 16rpx;
  375. margin-bottom: 20rpx;
  376. overflow: hidden;
  377. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  378. }
  379. .coupon-card.status-used {
  380. opacity: 0.65;
  381. }
  382. .coupon-card.status-expired {
  383. opacity: 0.5;
  384. }
  385. .card-left {
  386. width: 200rpx;
  387. display: flex;
  388. flex-direction: column;
  389. align-items: center;
  390. justify-content: center;
  391. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  392. padding: $uni-space-lg 12rpx;
  393. flex-shrink: 0;
  394. }
  395. .status-used .card-left {
  396. background: #b0b0b0;
  397. }
  398. .status-expired .card-left {
  399. background: #d0d0d0;
  400. }
  401. .card-value {
  402. font-size: calc(40rpx * var(--font-scale, 1));
  403. font-weight: bold;
  404. color: $app-surface;
  405. }
  406. .card-type-label {
  407. font-size: calc(20rpx * var(--font-scale, 1));
  408. color: rgba(255,255,255,0.8);
  409. margin-top: 8rpx;
  410. }
  411. .card-right {
  412. flex: 1;
  413. padding: 24rpx 24rpx 20rpx;
  414. position: relative;
  415. }
  416. .card-header {
  417. display: flex;
  418. align-items: center;
  419. justify-content: space-between;
  420. margin-bottom: 10rpx;
  421. }
  422. .card-name {
  423. font-size: calc(28rpx * var(--font-scale, 1));
  424. font-weight: 600;
  425. color: $facade-text-strong;
  426. flex: 1;
  427. overflow: hidden;
  428. text-overflow: ellipsis;
  429. white-space: nowrap;
  430. }
  431. .card-status-badge {
  432. font-size: calc(20rpx * var(--font-scale, 1));
  433. padding: 4rpx 14rpx;
  434. border-radius: 24rpx;
  435. flex-shrink: 0;
  436. margin-left: 12rpx;
  437. }
  438. .card-status-badge.status-used {
  439. background: $facade-fill;
  440. color: $facade-text-muted;
  441. }
  442. .card-status-badge.status-expired {
  443. background: $status-danger-tint-3;
  444. color: $status-danger-2;
  445. }
  446. .card-condition {
  447. font-size: calc(24rpx * var(--font-scale, 1));
  448. color: $brand-primary;
  449. display: block;
  450. margin-bottom: 6rpx;
  451. }
  452. .card-scope {
  453. font-size: calc(22rpx * var(--font-scale, 1));
  454. color: $uni-info;
  455. background: $tint-sky-ghost;
  456. display: inline-block;
  457. padding: 2rpx 12rpx;
  458. border-radius: 8rpx;
  459. margin-bottom: 6rpx;
  460. }
  461. .card-expiry {
  462. font-size: calc(22rpx * var(--font-scale, 1));
  463. color: $facade-text-ghost-light;
  464. display: block;
  465. }
  466. .claim-btn {
  467. position: absolute;
  468. right: 24rpx;
  469. bottom: 20rpx;
  470. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  471. color: $app-surface;
  472. font-size: calc(24rpx * var(--font-scale, 1));
  473. padding: 10rpx 28rpx;
  474. border-radius: 24rpx;
  475. border: none;
  476. line-height: 1.4;
  477. height: auto;
  478. }
  479. .claim-btn::after {
  480. border: none;
  481. }
  482. .claim-btn:active {
  483. opacity: 0.85;
  484. }
  485. .member-guide-btn {
  486. position: absolute;
  487. right: 24rpx;
  488. bottom: 20rpx;
  489. background: $brand-primary;
  490. color: $app-surface;
  491. font-size: calc(24rpx * var(--font-scale, 1));
  492. padding: 10rpx 28rpx;
  493. border-radius: 24rpx;
  494. border: none;
  495. line-height: 1.4;
  496. height: auto;
  497. }
  498. .member-guide-btn::after {
  499. border: none;
  500. }
  501. .member-guide-btn:active {
  502. opacity: 0.85;
  503. }
  504. .gift-entry {
  505. display: flex;
  506. align-items: center;
  507. justify-content: space-between;
  508. margin: 20rpx 0;
  509. padding: 24rpx;
  510. background: linear-gradient(135deg, #ff8a4c, #ff6b35);
  511. border-radius: 16rpx;
  512. box-shadow: 0 6rpx 18rpx rgba(255, 107, 53, 0.25);
  513. }
  514. .gift-entry-text {
  515. color: #fff;
  516. font-size: 28rpx;
  517. font-weight: bold;
  518. }
  519. .gift-entry-arrow {
  520. color: #fff;
  521. font-size: 36rpx;
  522. }
  523. </style>