coupons.vue 13 KB

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