coupons.vue 8.2 KB

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