couponCenter.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <template>
  2. <view class="page">
  3. <!-- 头部提示 -->
  4. <view class="header">
  5. <text class="header-title">领取优惠券</text>
  6. <text class="header-desc">每日更新 · 限量发放</text>
  7. </view>
  8. <!-- 优惠券列表 -->
  9. <scroll-view
  10. v-if="!loading && templates.length > 0"
  11. class="coupon-scroll"
  12. scroll-y
  13. refresher-enabled
  14. :refresher-triggered="refreshing"
  15. @refresherrefresh="onRefresh"
  16. >
  17. <view class="coupon-list">
  18. <view
  19. v-for="item in templates"
  20. :key="item.id"
  21. class="ticket"
  22. >
  23. <!-- 左侧:金额 -->
  24. <view :class="['ticket-face', item.couponType ? 'ticket-face--type' + item.couponType : 'ticket-face--type1']">
  25. <view class="ticket-amount">
  26. <text class="ticket-currency">{{ item.couponType === 2 ? '' : '¥' }}</text>
  27. <text class="ticket-figure">{{ formatDiscountValue(item) }}</text>
  28. </view>
  29. <text class="ticket-threshold">
  30. {{ item.minAmount && item.minAmount > 0 ? '满' + item.minAmount + '可用' : '无门槛' }}
  31. </text>
  32. </view>
  33. <!-- 锯齿分割 -->
  34. <view class="ticket-serration">
  35. <view class="serration-gap serration-gap--top"></view>
  36. <view class="serration-rail"></view>
  37. <view class="serration-gap serration-gap--bottom"></view>
  38. </view>
  39. <!-- 右侧:信息 -->
  40. <view class="ticket-body">
  41. <text class="ticket-title">{{ item.couponName || '优惠券' }}</text>
  42. <view class="ticket-meta">
  43. <view class="ticket-badge">{{ getCouponTypeLabel(item.couponType) }}</view>
  44. <text class="ticket-scope">{{ getScopeLabel(item.applyScope) }}</text>
  45. </view>
  46. <text v-if="item.couponDesc" class="ticket-desc">{{ item.couponDesc }}</text>
  47. <!-- 库存和领取信息 -->
  48. <view class="ticket-stock-row">
  49. <text class="ticket-stock">
  50. 剩余 <text class="stock-num">{{ item.remainCount }}</text> / {{ item.totalCount }}
  51. </text>
  52. <text v-if="item.receiveLimit > 0" class="ticket-limit">
  53. 每人限领{{ item.receiveLimit }}张
  54. </text>
  55. </view>
  56. <!-- 有效期 -->
  57. <text class="ticket-period">
  58. <text v-if="item.validType === 1">{{ formatTime(item.validStartTime) }} ~ {{ formatTime(item.validEndTime) }}</text>
  59. <text v-else>领取后{{ item.validDays }}天内有效</text>
  60. </text>
  61. <!-- 领取按钮 -->
  62. <view
  63. :class="['ticket-grab', canReceive(item) ? '' : (item.alreadyReceived ? 'ticket-grab--received' : 'ticket-grab--out')]"
  64. @click="handleReceive(item)"
  65. >
  66. <text class="ticket-grab-text">
  67. {{ receiving[item.id] ? '领取中...' : (item.alreadyReceived ? '已领取' : (canReceive(item) ? '立即领取' : '已抢光')) }}
  68. </text>
  69. </view>
  70. </view>
  71. </view>
  72. </view>
  73. </scroll-view>
  74. <!-- 空状态 -->
  75. <view v-if="!loading && templates.length === 0" class="empty">
  76. <view class="empty-visual">
  77. <view class="empty-ticket-shadow"></view>
  78. <view class="empty-ticket-body">
  79. <view class="empty-ticket-left"></view>
  80. <view class="empty-ticket-dots">
  81. <view class="empty-dot"></view>
  82. <view class="empty-dot"></view>
  83. </view>
  84. <view class="empty-ticket-right">
  85. <view class="empty-line empty-line--long"></view>
  86. <view class="empty-line empty-line--short"></view>
  87. </view>
  88. </view>
  89. </view>
  90. <text class="empty-title">暂无可领取的优惠券</text>
  91. <text class="empty-desc">过段时间再来看看吧</text>
  92. <view class="empty-cta" @click="goToMyCoupons">
  93. <text class="empty-cta-text">查看我的优惠券</text>
  94. </view>
  95. </view>
  96. <!-- 加载中 -->
  97. <view v-if="loading" class="loading">
  98. <view class="loading-spinner"></view>
  99. <text class="loading-label">加载中</text>
  100. </view>
  101. </view>
  102. </template>
  103. <script setup lang="ts">
  104. import { ref, reactive, onMounted } from 'vue';
  105. import { getAvailableCoupons, receiveCoupon } from '../../api/coupon';
  106. import type { CouponTemplateInfo } from '../../api/coupon';
  107. import { checkAuth } from '../../utils/auth';
  108. // 扩展接口,添加前端需要的字段
  109. interface CouponTemplateInfoExtended extends CouponTemplateInfo {
  110. alreadyReceived?: boolean;
  111. }
  112. const templates = ref<CouponTemplateInfoExtended[]>([]);
  113. const loading = ref(false);
  114. const refreshing = ref(false);
  115. const receiving = reactive<Record<string, boolean>>({});
  116. /**
  117. * 格式化优惠值
  118. */
  119. const formatDiscountValue = (item: CouponTemplateInfoExtended): string => {
  120. if (item.couponType === 2) {
  121. return item.discountValue ? `${item.discountValue}折` : '0';
  122. }
  123. return item.discountValue ? `${item.discountValue}` : '0';
  124. };
  125. /**
  126. * 获取优惠券类型标签
  127. */
  128. const getCouponTypeLabel = (type: number): string => {
  129. const map: Record<number, string> = {
  130. 1: '满减',
  131. 2: '折扣',
  132. 3: '抵扣',
  133. 4: '兑换'
  134. };
  135. return map[type] || '优惠';
  136. };
  137. /**
  138. * 获取适用范围标签
  139. */
  140. const getScopeLabel = (scope: number): string => {
  141. const map: Record<number, string> = {
  142. 1: '全场通用',
  143. 2: '指定品类',
  144. 3: '指定商品'
  145. };
  146. return map[scope] || '';
  147. };
  148. /**
  149. * 格式化时间
  150. */
  151. const formatTime = (timeStr: string): string => {
  152. if (!timeStr) return '';
  153. return timeStr.substring(0, 10);
  154. };
  155. /**
  156. * 判断是否可领取
  157. */
  158. const canReceive = (item: CouponTemplateInfoExtended): boolean => {
  159. return item.remainCount > 0 && item.status === 1 && !item.alreadyReceived;
  160. };
  161. /**
  162. * 领取优惠券
  163. */
  164. const handleReceive = async (item: CouponTemplateInfoExtended) => {
  165. if (!canReceive(item) || receiving[item.id]) return;
  166. receiving[item.id] = true;
  167. try {
  168. await receiveCoupon(item.id);
  169. uni.showToast({
  170. title: '领取成功',
  171. icon: 'success'
  172. });
  173. // 更新剩余数量
  174. item.remainCount = Math.max(0, item.remainCount - 1);
  175. item.alreadyReceived = true;
  176. } catch (error: any) {
  177. console.error('领取优惠券失败:', error);
  178. // 错误信息已在 request 工具中展示
  179. } finally {
  180. receiving[item.id] = false;
  181. }
  182. };
  183. /**
  184. * 下拉刷新
  185. */
  186. const onRefresh = async () => {
  187. refreshing.value = true;
  188. try {
  189. await loadTemplates();
  190. } finally {
  191. refreshing.value = false;
  192. }
  193. };
  194. /**
  195. * 跳转到我的优惠券
  196. */
  197. const goToMyCoupons = () => {
  198. uni.navigateTo({
  199. url: '/pages/coupons/coupons'
  200. });
  201. };
  202. /**
  203. * 加载可领取优惠券列表
  204. */
  205. const loadTemplates = async () => {
  206. try {
  207. const result = await getAvailableCoupons();
  208. templates.value = result.map(item => ({
  209. ...item,
  210. alreadyReceived: false // 初始化为未领取
  211. }));
  212. } catch (error) {
  213. console.error('加载可领取优惠券失败:', error);
  214. }
  215. };
  216. onMounted(() => {
  217. if (!checkAuth('/pages/couponCenter/couponCenter')) {
  218. return;
  219. }
  220. loadTemplates();
  221. });
  222. </script>
  223. <style>
  224. .page {
  225. min-height: 100vh;
  226. background: linear-gradient(180deg, #FFF1F0 0%, #F5F5F5 30%);
  227. }
  228. .coupon-scroll {
  229. height: calc(100vh - 140rpx);
  230. }
  231. /* ========== Header ========== */
  232. .header {
  233. padding: 28rpx 36rpx 16rpx;
  234. }
  235. .header-title {
  236. font-size: 36rpx;
  237. font-weight: 800;
  238. color: #1A1A1A;
  239. display: block;
  240. }
  241. .header-desc {
  242. font-size: 24rpx;
  243. color: #BBBBBB;
  244. margin-top: 4rpx;
  245. display: block;
  246. }
  247. /* ========== Coupon List ========== */
  248. .coupon-list {
  249. padding: 8rpx 28rpx 40rpx;
  250. }
  251. /* ========== Ticket Card ========== */
  252. .ticket {
  253. display: flex;
  254. margin-bottom: 24rpx;
  255. border-radius: 20rpx;
  256. overflow: hidden;
  257. background: #ffffff;
  258. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05), 0 1rpx 4rpx rgba(0, 0, 0, 0.03);
  259. }
  260. /* --- Left: Amount Face --- */
  261. .ticket-face {
  262. width: 210rpx;
  263. display: flex;
  264. flex-direction: column;
  265. align-items: center;
  266. justify-content: center;
  267. padding: 36rpx 12rpx;
  268. flex-shrink: 0;
  269. position: relative;
  270. }
  271. .ticket-face::after {
  272. content: '';
  273. position: absolute;
  274. right: 0;
  275. top: 0;
  276. bottom: 0;
  277. width: 1rpx;
  278. background: rgba(255, 255, 255, 0.25);
  279. }
  280. /* 类型色系 */
  281. .ticket-face--type1 {
  282. background: linear-gradient(150deg, #FF8A00 0%, #FFA940 100%);
  283. }
  284. .ticket-face--type2 {
  285. background: linear-gradient(150deg, #E8533E 0%, #FF7B6B 100%);
  286. }
  287. .ticket-face--type3 {
  288. background: linear-gradient(150deg, #7B61FF 0%, #A78BFA 100%);
  289. }
  290. .ticket-face--type4 {
  291. background: linear-gradient(150deg, #0EA5E9 0%, #38BDF8 100%);
  292. }
  293. .ticket-amount {
  294. display: flex;
  295. align-items: baseline;
  296. }
  297. .ticket-currency {
  298. font-size: 26rpx;
  299. font-weight: 600;
  300. color: rgba(255, 255, 255, 0.9);
  301. margin-right: 2rpx;
  302. }
  303. .ticket-figure {
  304. font-size: 56rpx;
  305. font-weight: 800;
  306. color: #ffffff;
  307. line-height: 1;
  308. letter-spacing: -2rpx;
  309. }
  310. .ticket-threshold {
  311. font-size: 20rpx;
  312. color: rgba(255, 255, 255, 0.8);
  313. margin-top: 12rpx;
  314. padding: 4rpx 16rpx;
  315. background: rgba(255, 255, 255, 0.2);
  316. border-radius: 20rpx;
  317. }
  318. /* --- Serration Divider --- */
  319. .ticket-serration {
  320. width: 36rpx;
  321. display: flex;
  322. flex-direction: column;
  323. align-items: center;
  324. position: relative;
  325. flex-shrink: 0;
  326. }
  327. .serration-gap {
  328. width: 36rpx;
  329. height: 18rpx;
  330. background: #ffffff;
  331. position: absolute;
  332. z-index: 2;
  333. }
  334. .serration-gap--top {
  335. top: 0;
  336. border-radius: 0 0 50% 50%;
  337. }
  338. .serration-gap--bottom {
  339. bottom: 0;
  340. border-radius: 50% 50% 0 0;
  341. }
  342. .serration-rail {
  343. flex: 1;
  344. border-left: 2rpx dashed #E0E0E0;
  345. margin: 18rpx 0;
  346. }
  347. /* --- Right: Body Info --- */
  348. .ticket-body {
  349. flex: 1;
  350. padding: 28rpx 28rpx 28rpx 8rpx;
  351. display: flex;
  352. flex-direction: column;
  353. justify-content: center;
  354. min-width: 0;
  355. position: relative;
  356. }
  357. .ticket-title {
  358. font-size: 28rpx;
  359. color: #1A1A1A;
  360. font-weight: 700;
  361. margin-bottom: 12rpx;
  362. line-height: 1.4;
  363. overflow: hidden;
  364. text-overflow: ellipsis;
  365. white-space: nowrap;
  366. padding-right: 120rpx;
  367. }
  368. .ticket-meta {
  369. display: flex;
  370. align-items: center;
  371. gap: 12rpx;
  372. margin-bottom: 12rpx;
  373. }
  374. .ticket-badge {
  375. font-size: 18rpx;
  376. color: #FF8A00;
  377. background: #FFF4E0;
  378. padding: 4rpx 14rpx;
  379. border-radius: 6rpx;
  380. font-weight: 600;
  381. flex-shrink: 0;
  382. }
  383. .ticket-scope {
  384. font-size: 20rpx;
  385. color: #999999;
  386. }
  387. .ticket-desc {
  388. font-size: 22rpx;
  389. color: #999999;
  390. margin-bottom: 10rpx;
  391. overflow: hidden;
  392. text-overflow: ellipsis;
  393. white-space: nowrap;
  394. }
  395. /* 库存行 */
  396. .ticket-stock-row {
  397. display: flex;
  398. align-items: center;
  399. justify-content: space-between;
  400. margin-bottom: 10rpx;
  401. }
  402. .ticket-stock {
  403. font-size: 20rpx;
  404. color: #999999;
  405. }
  406. .stock-num {
  407. color: #E8533E;
  408. font-weight: 600;
  409. }
  410. .ticket-limit {
  411. font-size: 20rpx;
  412. color: #BBBBBB;
  413. }
  414. .ticket-period {
  415. font-size: 22rpx;
  416. color: #BBBBBB;
  417. }
  418. /* --- Grab Button --- */
  419. .ticket-grab {
  420. position: absolute;
  421. top: 28rpx;
  422. right: 28rpx;
  423. padding: 10rpx 28rpx;
  424. background: #1A1A1A;
  425. border-radius: 28rpx;
  426. }
  427. .ticket-grab-text {
  428. font-size: 24rpx;
  429. color: #FFD700;
  430. font-weight: 700;
  431. }
  432. .ticket-grab--out {
  433. background: #F0F0F0;
  434. }
  435. .ticket-grab--out .ticket-grab-text {
  436. color: #CCCCCC;
  437. }
  438. .ticket-grab--received {
  439. background: #F0F0F0;
  440. }
  441. .ticket-grab--received .ticket-grab-text {
  442. color: #999999;
  443. }
  444. /* ========== Empty State ========== */
  445. .empty {
  446. display: flex;
  447. flex-direction: column;
  448. align-items: center;
  449. justify-content: center;
  450. min-height: 55vh;
  451. padding: 40rpx;
  452. }
  453. .empty-visual {
  454. margin-bottom: 40rpx;
  455. position: relative;
  456. }
  457. .empty-ticket-shadow {
  458. position: absolute;
  459. top: 8rpx;
  460. left: 4rpx;
  461. right: 4rpx;
  462. bottom: -8rpx;
  463. background: #E0E0E0;
  464. border-radius: 16rpx;
  465. }
  466. .empty-ticket-body {
  467. display: flex;
  468. width: 320rpx;
  469. height: 140rpx;
  470. background: #F5F5F5;
  471. border-radius: 16rpx;
  472. overflow: hidden;
  473. position: relative;
  474. z-index: 1;
  475. }
  476. .empty-ticket-left {
  477. width: 110rpx;
  478. display: flex;
  479. align-items: center;
  480. justify-content: center;
  481. }
  482. .empty-ticket-left::before {
  483. content: '¥';
  484. font-size: 44rpx;
  485. font-weight: 800;
  486. color: #DCDCDC;
  487. }
  488. .empty-ticket-dots {
  489. width: 30rpx;
  490. display: flex;
  491. flex-direction: column;
  492. align-items: center;
  493. justify-content: center;
  494. gap: 16rpx;
  495. }
  496. .empty-dot {
  497. width: 14rpx;
  498. height: 14rpx;
  499. border-radius: 50%;
  500. background: #F5F5F5;
  501. border: 2rpx solid #E0E0E0;
  502. }
  503. .empty-ticket-right {
  504. flex: 1;
  505. display: flex;
  506. flex-direction: column;
  507. justify-content: center;
  508. padding: 24rpx 20rpx;
  509. gap: 14rpx;
  510. }
  511. .empty-line {
  512. height: 10rpx;
  513. border-radius: 5rpx;
  514. background: #E8E8E8;
  515. }
  516. .empty-line--long {
  517. width: 100%;
  518. }
  519. .empty-line--short {
  520. width: 60%;
  521. }
  522. .empty-title {
  523. font-size: 30rpx;
  524. color: #999999;
  525. font-weight: 600;
  526. margin-bottom: 12rpx;
  527. }
  528. .empty-desc {
  529. font-size: 24rpx;
  530. color: #BBBBBB;
  531. margin-bottom: 36rpx;
  532. }
  533. .empty-cta {
  534. padding: 18rpx 64rpx;
  535. background: #1A1A1A;
  536. border-radius: 44rpx;
  537. }
  538. .empty-cta-text {
  539. font-size: 28rpx;
  540. color: #FFD700;
  541. font-weight: 700;
  542. }
  543. /* ========== Loading ========== */
  544. .loading {
  545. display: flex;
  546. flex-direction: column;
  547. align-items: center;
  548. justify-content: center;
  549. min-height: 55vh;
  550. gap: 20rpx;
  551. }
  552. .loading-spinner {
  553. width: 48rpx;
  554. height: 48rpx;
  555. border: 4rpx solid #E0E0E0;
  556. border-top-color: #FFD700;
  557. border-radius: 50%;
  558. animation: spin 0.8s linear infinite;
  559. }
  560. @keyframes spin {
  561. to { transform: rotate(360deg); }
  562. }
  563. .loading-label {
  564. font-size: 24rpx;
  565. color: #CCCCCC;
  566. }
  567. </style>