coupon-center.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <view class="page" :style="{ '--font-scale': fontScale }">
  3. <view v-if="loading" class="loading-wrap">
  4. <text class="loading-text">加载中...</text>
  5. </view>
  6. <view v-else-if="couponList.length === 0" class="empty-state">
  7. <text class="empty-icon">🎫</text>
  8. <text class="empty-text">暂无可领取的优惠券</text>
  9. <text class="empty-hint">平台发放的优惠券将在这里展示,敬请期待</text>
  10. </view>
  11. <scroll-view v-else scroll-y class="coupon-scroll">
  12. <view class="coupon-list">
  13. <view v-for="item in couponList" :key="item.id" class="coupon-card">
  14. <view class="card-left">
  15. <text class="card-value">{{ formatPriceWithSymbol(item.value) }}</text>
  16. <text class="card-type-label">{{ getTypeLabel(item.type) }}</text>
  17. </view>
  18. <view class="card-right">
  19. <view class="card-header">
  20. <text class="card-name">{{ item.name }}</text>
  21. </view>
  22. <text class="card-condition">{{ getConditionText(item) }}</text>
  23. <text class="card-scope">{{ getScopeText(item.applicableTo) }}</text>
  24. <text class="card-expiry">有效期至 {{ formatDate(item.validUntil) }}</text>
  25. <button class="claim-btn" @click="handleClaim(item)">立即领取</button>
  26. </view>
  27. </view>
  28. </view>
  29. </scroll-view>
  30. </view>
  31. </template>
  32. <script>
  33. import { getAvailableCouponTemplates, claimCoupon } from '../../utils/api.js'
  34. import { parseDate } from '../../utils/format.js'
  35. import fontScaleMixin from '@/mixins/font-scale.js'
  36. export default {
  37. mixins: [fontScaleMixin],
  38. data() {
  39. return {
  40. couponList: [],
  41. loading: false,
  42. claiming: false
  43. }
  44. },
  45. onLoad() {
  46. this.loadCoupons()
  47. this._onLoadFired = true
  48. },
  49. onShow() {
  50. // 首次进入由 onLoad 加载,避免 onLoad/onShow 双发;返回本页时刷新
  51. if (this._onLoadFired) {
  52. this._onLoadFired = false
  53. } else {
  54. this.loadCoupons()
  55. }
  56. },
  57. methods: {
  58. loadCoupons() {
  59. var self = this
  60. this.loading = true
  61. getAvailableCouponTemplates().then(function(res) {
  62. if (res && res.code === 200) {
  63. self.couponList = res.data || []
  64. } else {
  65. self.couponList = []
  66. }
  67. }).catch(function(e) {
  68. console.error('加载领券中心失败', e)
  69. self.couponList = []
  70. }).then(function() {
  71. self.loading = false
  72. })
  73. },
  74. handleClaim(item) {
  75. if (this.claiming) return
  76. var self = this
  77. uni.showModal({
  78. title: '确认领取',
  79. content: '确认领取「' + (item.name || '') + '」优惠券?',
  80. success: function(r) {
  81. if (!r.confirm) return
  82. self.claiming = true
  83. // /api/coupon/claim 契约:couponId 为券模板 id(available-templates 返回 List<Coupon>,字段为 id)
  84. claimCoupon(item.id).then(function(res) {
  85. self.claiming = false
  86. if (res.code === 200) {
  87. uni.showToast({ title: res.message || '领取成功', icon: 'success' })
  88. self.loadCoupons()
  89. } else {
  90. uni.showToast({ title: res.message || '领取失败', icon: 'none' })
  91. }
  92. }).catch(function(e) {
  93. self.claiming = false
  94. uni.showToast({ title: (e && e.message) || '领取失败', icon: 'none' })
  95. })
  96. }
  97. })
  98. },
  99. getTypeLabel(type) {
  100. if (type === 'FIXED') return '满减券'
  101. if (type === 'CASH') return '无门槛券'
  102. if (type === 'DISCOUNT') return '折扣券'
  103. return type || ''
  104. },
  105. getConditionText(item) {
  106. if (item.type === 'DISCOUNT') {
  107. return '折扣券 ' + ((item.discountRate || 0) / 100).toFixed(1) + '折'
  108. }
  109. if (item.minSpend && item.minSpend > 0) {
  110. return '满' + (item.minSpend / 100).toFixed(2) + '元可用'
  111. }
  112. return '无门槛'
  113. },
  114. getScopeText(applicableTo) {
  115. if (applicableTo === 'ALL') return '全品类适用'
  116. if (applicableTo === 'MEMBERSHIP') return '会员专享'
  117. return applicableTo || '全品类适用'
  118. },
  119. formatDate(date) {
  120. if (!date) return ''
  121. var d = parseDate(date)
  122. if (!d) return date
  123. var y = d.getFullYear()
  124. var m = String(d.getMonth() + 1).padStart(2, '0')
  125. var day = String(d.getDate()).padStart(2, '0')
  126. return y + '.' + m + '.' + day
  127. }
  128. }
  129. }
  130. </script>
  131. <style scoped lang="scss">
  132. .page {
  133. min-height: 100vh;
  134. background: $facade-fill-2;
  135. }
  136. .coupon-scroll {
  137. height: 100vh;
  138. }
  139. .loading-wrap {
  140. display: flex;
  141. justify-content: center;
  142. padding-top: 200rpx;
  143. }
  144. .loading-text {
  145. font-size: calc(28rpx * var(--font-scale, 1));
  146. color: $facade-text-muted;
  147. }
  148. .empty-state {
  149. display: flex;
  150. flex-direction: column;
  151. align-items: center;
  152. padding-top: 200rpx;
  153. }
  154. .empty-icon {
  155. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  156. margin-bottom: 20rpx;
  157. }
  158. .empty-text {
  159. font-size: calc(28rpx * var(--font-scale, 1));
  160. color: $facade-text-muted;
  161. }
  162. .empty-hint {
  163. font-size: calc(24rpx * var(--font-scale, 1));
  164. color: $facade-text-ghost;
  165. margin-top: 12rpx;
  166. }
  167. .coupon-list {
  168. padding: 20rpx;
  169. }
  170. .coupon-card {
  171. display: flex;
  172. background: $app-surface;
  173. border-radius: 16rpx;
  174. margin-bottom: 20rpx;
  175. overflow: hidden;
  176. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  177. }
  178. .card-left {
  179. width: 200rpx;
  180. display: flex;
  181. flex-direction: column;
  182. align-items: center;
  183. justify-content: center;
  184. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  185. padding: 30rpx 12rpx;
  186. flex-shrink: 0;
  187. }
  188. .card-value {
  189. font-size: calc(40rpx * var(--font-scale, 1));
  190. font-weight: bold;
  191. color: $app-surface;
  192. }
  193. .card-type-label {
  194. font-size: calc(20rpx * var(--font-scale, 1));
  195. color: rgba(255, 255, 255, 0.8);
  196. margin-top: 8rpx;
  197. }
  198. .card-right {
  199. flex: 1;
  200. padding: 24rpx 24rpx 20rpx;
  201. position: relative;
  202. }
  203. .card-header {
  204. display: flex;
  205. align-items: center;
  206. justify-content: space-between;
  207. margin-bottom: 10rpx;
  208. }
  209. .card-name {
  210. font-size: calc(28rpx * var(--font-scale, 1));
  211. font-weight: 600;
  212. color: $facade-text-strong;
  213. flex: 1;
  214. overflow: hidden;
  215. text-overflow: ellipsis;
  216. white-space: nowrap;
  217. }
  218. .card-condition {
  219. font-size: calc(24rpx * var(--font-scale, 1));
  220. color: $brand-primary;
  221. display: block;
  222. margin-bottom: 6rpx;
  223. }
  224. .card-scope {
  225. font-size: calc(22rpx * var(--font-scale, 1));
  226. color: $uni-info;
  227. background: $tint-sky-ghost;
  228. display: inline-block;
  229. padding: 2rpx 12rpx;
  230. border-radius: 8rpx;
  231. margin-bottom: 6rpx;
  232. }
  233. .card-expiry {
  234. font-size: calc(22rpx * var(--font-scale, 1));
  235. color: $facade-text-ghost-light;
  236. display: block;
  237. }
  238. .claim-btn {
  239. position: absolute;
  240. right: 24rpx;
  241. bottom: 20rpx;
  242. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  243. color: $app-surface;
  244. font-size: calc(24rpx * var(--font-scale, 1));
  245. padding: 10rpx 28rpx;
  246. border-radius: 28rpx;
  247. border: none;
  248. line-height: 1.4;
  249. height: auto;
  250. }
  251. .claim-btn::after {
  252. border: none;
  253. }
  254. .claim-btn:active {
  255. opacity: 0.85;
  256. }
  257. </style>