coupons.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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="loading" class="loading-wrap">
  23. <text class="loading-text">加载中...</text>
  24. </view>
  25. <view v-else-if="filteredList.length === 0" class="empty-state">
  26. <text class="empty-icon">🎫</text>
  27. <text class="empty-text">{{ tabIndex === 0 ? '暂无可用优惠券' : tabIndex === 1 ? '暂无已使用优惠券' : '暂无已过期优惠券' }}</text>
  28. <text class="empty-hint" v-if="tabIndex === 0">去领券中心看看有哪些优惠吧</text>
  29. </view>
  30. <view v-else class="coupon-list">
  31. <view
  32. v-for="coupon in filteredList"
  33. :key="coupon.id"
  34. class="coupon-card"
  35. :class="statusClassMap[coupon.id]"
  36. >
  37. <view class="card-left">
  38. <text class="card-value">{{ formatPriceWithSymbol(coupon.value) }}</text>
  39. <text class="card-type-label">{{ getTypeLabel(coupon.type) }}</text>
  40. </view>
  41. <view class="card-right">
  42. <view class="card-header">
  43. <text class="card-name">{{ coupon.name }}</text>
  44. <text v-if="getStatusLabel(coupon)" class="card-status-badge" :class="statusClassMap[coupon.id]">{{ getStatusLabel(coupon) }}</text>
  45. </view>
  46. <text class="card-condition">{{ getConditionText(coupon.minSpend) }}</text>
  47. <text class="card-scope">{{ getScopeText(coupon.applicableTo) }}</text>
  48. <text class="card-expiry">有效期至 {{ formatDate(coupon.validUntil) }}</text>
  49. <button
  50. v-if="canClaim(coupon)"
  51. class="claim-btn"
  52. @click="handleClaim(coupon)"
  53. >立即领取</button>
  54. </view>
  55. </view>
  56. </view>
  57. </scroll-view>
  58. </view>
  59. </template>
  60. <script>
  61. import { getCouponList, claimCoupon } from '../../utils/api.js'
  62. export default {
  63. data() {
  64. return {
  65. tabs: [
  66. { name: '可使用', status: 'AVAILABLE' },
  67. { name: '已使用', status: 'USED' },
  68. { name: '已过期', status: 'EXPIRED' }
  69. ],
  70. tabIndex: 0,
  71. coupons: [],
  72. loading: false,
  73. refreshing: false
  74. }
  75. },
  76. computed: {
  77. filteredList: function() {
  78. var self = this
  79. var currentStatus = this.tabs[this.tabIndex].status
  80. if (currentStatus === 'AVAILABLE') {
  81. return this.coupons.filter(function(c) {
  82. return c.status === 'AVAILABLE' || !c.status
  83. })
  84. }
  85. return this.coupons.filter(function(c) {
  86. return c.status === currentStatus
  87. })
  88. },
  89. statusClassMap: function() {
  90. var map = {}
  91. var list = this.filteredList
  92. for (var i = 0; i < list.length; i++) {
  93. var c = list[i]
  94. if (c.status === 'USED') {
  95. map[c.id] = 'status-used'
  96. } else if (c.status === 'EXPIRED') {
  97. map[c.id] = 'status-expired'
  98. } else {
  99. map[c.id] = ''
  100. }
  101. }
  102. return map
  103. }
  104. },
  105. onShow() {
  106. this.loadCoupons()
  107. },
  108. onPullDownRefresh() {
  109. this.refreshing = true
  110. this.loadCoupons()
  111. },
  112. methods: {
  113. async loadCoupons() {
  114. this.loading = true
  115. try {
  116. var res = await getCouponList()
  117. this.coupons = res.data || []
  118. } catch (e) {
  119. console.error('加载优惠券失败', e)
  120. } finally {
  121. this.loading = false
  122. this.refreshing = false
  123. uni.stopPullDownRefresh()
  124. }
  125. },
  126. onRefresh() {
  127. this.refreshing = true
  128. this.loadCoupons()
  129. },
  130. onScrollToLower() {},
  131. switchTab(idx) {
  132. this.tabIndex = idx
  133. },
  134. canClaim(coupon) {
  135. return this.tabIndex === 0 && (!coupon.status || coupon.status === 'AVAILABLE')
  136. },
  137. async handleClaim(coupon) {
  138. try {
  139. var res = await claimCoupon(coupon.id)
  140. uni.showToast({ title: res.message || '领取成功', icon: 'success' })
  141. this.loadCoupons()
  142. } catch (e) {
  143. uni.showToast({ title: e.message || '领取失败', icon: 'none' })
  144. }
  145. },
  146. getConditionText(minSpend) {
  147. if (!minSpend || minSpend <= 0) return '无门槛'
  148. return '满' + (minSpend / 100).toFixed(2) + '元可用'
  149. },
  150. getScopeText(applicableTo) {
  151. if (applicableTo === 'ALL') return '全品类适用'
  152. if (applicableTo === 'MEMBERSHIP') return '仅限会员'
  153. return applicableTo || '全品类适用'
  154. },
  155. getTypeLabel(type) {
  156. if (type === 'FIXED') return '固定金额'
  157. if (type === 'PERCENT') return '折扣券'
  158. return type || ''
  159. },
  160. getStatusLabel(coupon) {
  161. if (coupon.status === 'USED') return '已使用'
  162. if (coupon.status === 'EXPIRED') return '已过期'
  163. return ''
  164. },
  165. getStatusClass(coupon) {
  166. if (coupon.status === 'USED') return 'status-used'
  167. if (coupon.status === 'EXPIRED') return 'status-expired'
  168. return ''
  169. },
  170. formatDate(date) {
  171. if (!date) return ''
  172. var d = new Date(date)
  173. var y = d.getFullYear()
  174. var m = String(d.getMonth() + 1).padStart(2, '0')
  175. var day = String(d.getDate()).padStart(2, '0')
  176. return y + '.' + m + '.' + day
  177. }
  178. }
  179. }
  180. </script>
  181. <style scoped>
  182. .page {
  183. min-height: 100vh;
  184. background: #f5f5f5;
  185. }
  186. .tab-bar {
  187. display: flex;
  188. background: #fff;
  189. border-bottom: 1rpx solid #eee;
  190. position: sticky;
  191. top: 0;
  192. z-index: 10;
  193. }
  194. .tab-item {
  195. flex: 1;
  196. text-align: center;
  197. padding: 24rpx 0;
  198. font-size: 28rpx;
  199. color: #666;
  200. position: relative;
  201. }
  202. .tab-item.active {
  203. color: #F97316;
  204. font-weight: 600;
  205. }
  206. .tab-item.active::after {
  207. content: '';
  208. position: absolute;
  209. bottom: 0;
  210. left: 50%;
  211. transform: translateX(-50%);
  212. width: 40rpx;
  213. height: 4rpx;
  214. background: #F97316;
  215. border-radius: 2rpx;
  216. }
  217. .coupon-scroll {
  218. height: calc(100vh - 88rpx);
  219. }
  220. .loading-wrap {
  221. display: flex;
  222. justify-content: center;
  223. padding-top: 200rpx;
  224. }
  225. .loading-text {
  226. font-size: 28rpx;
  227. color: #999;
  228. }
  229. .empty-state {
  230. display: flex;
  231. flex-direction: column;
  232. align-items: center;
  233. padding-top: 200rpx;
  234. }
  235. .empty-icon {
  236. font-size: 80rpx;
  237. margin-bottom: 20rpx;
  238. }
  239. .empty-text {
  240. font-size: 28rpx;
  241. color: #999;
  242. }
  243. .empty-hint {
  244. font-size: 24rpx;
  245. color: #ccc;
  246. margin-top: 12rpx;
  247. }
  248. .coupon-list {
  249. padding: 20rpx;
  250. }
  251. .coupon-card {
  252. display: flex;
  253. background: #fff;
  254. border-radius: 16rpx;
  255. margin-bottom: 20rpx;
  256. overflow: hidden;
  257. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  258. }
  259. .coupon-card.status-used {
  260. opacity: 0.65;
  261. }
  262. .coupon-card.status-expired {
  263. opacity: 0.5;
  264. }
  265. .card-left {
  266. width: 200rpx;
  267. display: flex;
  268. flex-direction: column;
  269. align-items: center;
  270. justify-content: center;
  271. background: linear-gradient(135deg, #F97316, #FB923C);
  272. padding: 30rpx 12rpx;
  273. flex-shrink: 0;
  274. }
  275. .status-used .card-left {
  276. background: #b0b0b0;
  277. }
  278. .status-expired .card-left {
  279. background: #d0d0d0;
  280. }
  281. .card-value {
  282. font-size: 40rpx;
  283. font-weight: bold;
  284. color: #fff;
  285. }
  286. .card-type-label {
  287. font-size: 20rpx;
  288. color: rgba(255,255,255,0.8);
  289. margin-top: 8rpx;
  290. }
  291. .card-right {
  292. flex: 1;
  293. padding: 24rpx 24rpx 20rpx;
  294. position: relative;
  295. }
  296. .card-header {
  297. display: flex;
  298. align-items: center;
  299. justify-content: space-between;
  300. margin-bottom: 10rpx;
  301. }
  302. .card-name {
  303. font-size: 28rpx;
  304. font-weight: 600;
  305. color: #333;
  306. flex: 1;
  307. overflow: hidden;
  308. text-overflow: ellipsis;
  309. white-space: nowrap;
  310. }
  311. .card-status-badge {
  312. font-size: 20rpx;
  313. padding: 4rpx 14rpx;
  314. border-radius: 20rpx;
  315. flex-shrink: 0;
  316. margin-left: 12rpx;
  317. }
  318. .card-status-badge.status-used {
  319. background: #f0f0f0;
  320. color: #999;
  321. }
  322. .card-status-badge.status-expired {
  323. background: #fff1f0;
  324. color: #ff4d4f;
  325. }
  326. .card-condition {
  327. font-size: 24rpx;
  328. color: #F97316;
  329. display: block;
  330. margin-bottom: 6rpx;
  331. }
  332. .card-scope {
  333. font-size: 22rpx;
  334. color: #5B9BD5;
  335. background: #f0f7ff;
  336. display: inline-block;
  337. padding: 2rpx 12rpx;
  338. border-radius: 8rpx;
  339. margin-bottom: 6rpx;
  340. }
  341. .card-expiry {
  342. font-size: 22rpx;
  343. color: #bbb;
  344. display: block;
  345. }
  346. .claim-btn {
  347. position: absolute;
  348. right: 24rpx;
  349. bottom: 20rpx;
  350. background: linear-gradient(135deg, #F97316, #FB923C);
  351. color: #fff;
  352. font-size: 24rpx;
  353. padding: 10rpx 28rpx;
  354. border-radius: 28rpx;
  355. border: none;
  356. line-height: 1.4;
  357. height: auto;
  358. }
  359. .claim-btn::after {
  360. border: none;
  361. }
  362. .claim-btn:active {
  363. opacity: 0.85;
  364. }
  365. </style>