coupons.vue 13 KB

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