coupons.vue 15 KB

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