coupons.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <template>
  2. <view class="page">
  3. <!-- 顶部Tab -->
  4. <view class="tab-bar">
  5. <view
  6. v-for="tab in tabs"
  7. :key="tab.value"
  8. :class="['tab-pill', currentTab === tab.value ? 'tab-pill--active' : '']"
  9. @click="switchTab(tab.value)"
  10. >
  11. <text class="tab-pill-text">{{ tab.label }}</text>
  12. </view>
  13. </view>
  14. <!-- 优惠券列表 -->
  15. <view v-if="!loading && coupons.length > 0" class="coupon-list">
  16. <view
  17. v-for="(item, index) in coupons"
  18. :key="item.id"
  19. :class="['ticket', item.status === 0 ? '' : 'ticket--dim']"
  20. >
  21. <!-- 左侧:金额 -->
  22. <view :class="['ticket-face', item.couponType ? 'ticket-face--type' + item.couponType : 'ticket-face--type1']">
  23. <view class="ticket-amount">
  24. <text class="ticket-currency">{{ item.couponType === 2 ? '' : '¥' }}</text>
  25. <text class="ticket-figure">{{ formatValue(item) }}</text>
  26. </view>
  27. <text class="ticket-threshold">
  28. {{ item.minAmount && item.minAmount > 0 ? '满' + item.minAmount + '可用' : '无门槛' }}
  29. </text>
  30. </view>
  31. <!-- 锯齿分割 -->
  32. <view class="ticket-serration">
  33. <view class="serration-gap serration-gap--top"></view>
  34. <view class="serration-rail"></view>
  35. <view class="serration-gap serration-gap--bottom"></view>
  36. </view>
  37. <!-- 右侧:信息 -->
  38. <view class="ticket-body">
  39. <text class="ticket-title">{{ item.couponName || '优惠券' }}</text>
  40. <view class="ticket-meta">
  41. <view class="ticket-badge">{{ getCouponTypeLabel(item.couponType) }}</view>
  42. <text class="ticket-scope">{{ getScopeLabel(item.applyScope) }}</text>
  43. </view>
  44. <text v-if="item.couponDesc" class="ticket-desc">{{ item.couponDesc }}</text>
  45. <text class="ticket-period">{{ formatTime(item.validStartTime) }} ~ {{ formatTime(item.validEndTime) }}</text>
  46. <!-- 状态水印 -->
  47. <view v-if="item.status !== 0" class="ticket-watermark">
  48. <text class="watermark-text">{{ item.status === 1 ? '已使用' : '已过期' }}</text>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 加载更多 -->
  54. <view v-if="coupons.length > 0 && hasMore" class="load-more" @click="loadMore">
  55. <text class="load-more-text">{{ loadingMore ? '加载中...' : '点击加载更多' }}</text>
  56. </view>
  57. <!-- 空状态 -->
  58. <view v-if="!loading && coupons.length === 0" class="empty">
  59. <view class="empty-visual">
  60. <view class="empty-ticket-shadow"></view>
  61. <view class="empty-ticket-body">
  62. <view class="empty-ticket-left"></view>
  63. <view class="empty-ticket-dots">
  64. <view class="empty-dot"></view>
  65. <view class="empty-dot"></view>
  66. </view>
  67. <view class="empty-ticket-right">
  68. <view class="empty-line empty-line--long"></view>
  69. <view class="empty-line empty-line--short"></view>
  70. </view>
  71. </view>
  72. </view>
  73. <text class="empty-title">{{ emptyText }}</text>
  74. <view v-if="currentTab === 0" class="empty-cta" @click="goToCouponCenter">
  75. <text class="empty-cta-text">去领券中心</text>
  76. </view>
  77. </view>
  78. <!-- 加载中 -->
  79. <view v-if="loading" class="loading">
  80. <view class="loading-spinner"></view>
  81. <text class="loading-label">加载中</text>
  82. </view>
  83. <!-- 底部领券入口 -->
  84. <view v-if="!loading && currentTab === 0 && coupons.length > 0" class="bottom-bar">
  85. <view class="bottom-btn" @click="goToCouponCenter">
  86. <text class="bottom-btn-label">领券中心</text>
  87. </view>
  88. </view>
  89. </view>
  90. </template>
  91. <script setup lang="ts">
  92. import { ref, computed, onMounted } from 'vue';
  93. import { getMyCoupons } from '../../api/coupon';
  94. import type { UserCouponInfo, PageResult } from '../../api/coupon';
  95. import { checkAuth } from '../../utils/auth';
  96. import { getCouponTypeLabel, getScopeLabel, formatTime } from '../../utils/coupon';
  97. import { logger } from '../../utils/logger';
  98. const tabs = [
  99. { label: '未使用', value: 0 },
  100. { label: '已使用', value: 1 },
  101. { label: '已过期', value: 2 }
  102. ];
  103. const currentTab = ref(0);
  104. const coupons = ref<UserCouponInfo[]>([]);
  105. const loading = ref(false);
  106. const loadingMore = ref(false);
  107. const currentPage = ref(1);
  108. const pageSize = 10;
  109. const total = ref(0);
  110. const hasMore = computed(() => coupons.value.length < total.value);
  111. const emptyText = computed(() => {
  112. const map: Record<number, string> = {
  113. 0: '暂无可用优惠券',
  114. 1: '暂无已使用的优惠券',
  115. 2: '暂无已过期的优惠券'
  116. };
  117. return map[currentTab.value] || '暂无优惠券';
  118. });
  119. const switchTab = (tab: number) => {
  120. if (currentTab.value === tab) return;
  121. currentTab.value = tab;
  122. coupons.value = [];
  123. currentPage.value = 1;
  124. loadCoupons();
  125. };
  126. const loadCoupons = async (isLoadMore = false) => {
  127. if (isLoadMore) {
  128. if (loadingMore.value) return;
  129. loadingMore.value = true;
  130. } else {
  131. if (loading.value) return;
  132. loading.value = true;
  133. }
  134. try {
  135. const result: PageResult<UserCouponInfo> = await getMyCoupons(currentTab.value, currentPage.value, pageSize);
  136. if (isLoadMore) {
  137. coupons.value = [...coupons.value, ...(result.list || [])];
  138. } else {
  139. coupons.value = result.list || [];
  140. }
  141. total.value = result.total;
  142. } catch (error) {
  143. logger.error('加载优惠券列表失败:', error);
  144. } finally {
  145. loading.value = false;
  146. loadingMore.value = false;
  147. }
  148. };
  149. const loadMore = () => {
  150. currentPage.value++;
  151. loadCoupons(true);
  152. };
  153. const formatValue = (item: UserCouponInfo): string => {
  154. if (item.couponType === 2) {
  155. const val = item.discountValue;
  156. return val ? `${val}折` : '折扣';
  157. }
  158. const val = item.discountValue;
  159. return val ? `${val}` : '--';
  160. };
  161. const goToCouponCenter = () => {
  162. uni.navigateTo({
  163. url: '/pages/couponCenter/couponCenter'
  164. });
  165. };
  166. onMounted(() => {
  167. if (!checkAuth('/pages/coupons/coupons')) {
  168. return;
  169. }
  170. loadCoupons();
  171. });
  172. </script>
  173. <style scoped lang="scss">
  174. .page {
  175. min-height: 100vh;
  176. background: linear-gradient(180deg, $color-primary-bg 0%, $color-bg-secondary 100%);
  177. }
  178. /* ========== Tab Bar ========== */
  179. .tab-bar {
  180. display: flex;
  181. gap: 16rpx;
  182. padding: 20rpx 32rpx 12rpx;
  183. background: transparent;
  184. position: sticky;
  185. top: 0;
  186. z-index: 10;
  187. }
  188. .tab-pill {
  189. padding: 12rpx 40rpx;
  190. border-radius: 32rpx;
  191. background: rgba(0, 0, 0, 0.04);
  192. &:active {
  193. transform: scale(0.95);
  194. }
  195. }
  196. .tab-pill--active {
  197. background: $color-primary;
  198. box-shadow: $shadow-sm;
  199. }
  200. .tab-pill-text {
  201. font-size: 26rpx;
  202. color: $color-text-secondary;
  203. font-weight: 500;
  204. }
  205. .tab-pill--active .tab-pill-text {
  206. color: #1A1A1A;
  207. font-weight: 600;
  208. }
  209. /* ========== Coupon List ========== */
  210. .coupon-list {
  211. padding: 12rpx 28rpx 0;
  212. }
  213. /* ========== Ticket Card ========== */
  214. .ticket {
  215. display: flex;
  216. margin-bottom: 24rpx;
  217. border-radius: 20rpx;
  218. overflow: hidden;
  219. background: $color-bg-primary;
  220. box-shadow: $shadow-md;
  221. position: relative;
  222. &:active {
  223. transform: scale(0.98);
  224. box-shadow: $shadow-sm;
  225. }
  226. }
  227. .ticket--dim {
  228. opacity: 0.5;
  229. .ticket-face {
  230. background: linear-gradient(150deg, #B0B0B0, #C8C8C8);
  231. }
  232. .ticket-badge {
  233. color: $color-text-secondary;
  234. background: $color-bg-tertiary;
  235. }
  236. }
  237. /* --- Left: Amount Face --- */
  238. .ticket-face {
  239. width: 210rpx;
  240. display: flex;
  241. flex-direction: column;
  242. align-items: center;
  243. justify-content: center;
  244. padding: 36rpx 12rpx;
  245. flex-shrink: 0;
  246. position: relative;
  247. &::after {
  248. content: '';
  249. position: absolute;
  250. right: 0;
  251. top: 0;
  252. bottom: 0;
  253. width: 1rpx;
  254. background: rgba(255, 255, 255, 0.2);
  255. }
  256. }
  257. .ticket-face--type1 {
  258. background: linear-gradient(150deg, #FF8A00, #FFA940);
  259. }
  260. .ticket-face--type2 {
  261. background: linear-gradient(150deg, $color-error, #FF7B6B);
  262. }
  263. .ticket-face--type3 {
  264. background: linear-gradient(150deg, #7B61FF, #A78BFA);
  265. }
  266. .ticket-face--type4 {
  267. background: linear-gradient(150deg, $color-info, #38BDF8);
  268. }
  269. .ticket-amount {
  270. display: flex;
  271. align-items: baseline;
  272. }
  273. .ticket-currency {
  274. font-size: 26rpx;
  275. font-weight: 600;
  276. color: rgba(255, 255, 255, 0.9);
  277. margin-right: 2rpx;
  278. }
  279. .ticket-figure {
  280. font-size: 56rpx;
  281. font-weight: 800;
  282. color: #ffffff;
  283. line-height: 1;
  284. letter-spacing: -2rpx;
  285. }
  286. .ticket-threshold {
  287. font-size: 20rpx;
  288. color: rgba(255, 255, 255, 0.8);
  289. margin-top: 12rpx;
  290. padding: 4rpx 16rpx;
  291. background: rgba(255, 255, 255, 0.2);
  292. border-radius: 20rpx;
  293. }
  294. /* --- Serration Divider --- */
  295. .ticket-serration {
  296. width: 36rpx;
  297. display: flex;
  298. flex-direction: column;
  299. align-items: center;
  300. position: relative;
  301. flex-shrink: 0;
  302. }
  303. .serration-gap {
  304. width: 36rpx;
  305. height: 18rpx;
  306. background: $color-bg-primary;
  307. position: absolute;
  308. z-index: 2;
  309. }
  310. .serration-gap--top {
  311. top: 0;
  312. border-radius: 0 0 50% 50%;
  313. }
  314. .serration-gap--bottom {
  315. bottom: 0;
  316. border-radius: 50% 50% 0 0;
  317. }
  318. .serration-rail {
  319. flex: 1;
  320. border-left: 2rpx dashed $color-border;
  321. margin: 18rpx 0;
  322. }
  323. /* --- Right: Body Info --- */
  324. .ticket-body {
  325. flex: 1;
  326. padding: 28rpx 28rpx 28rpx 8rpx;
  327. display: flex;
  328. flex-direction: column;
  329. justify-content: center;
  330. min-width: 0;
  331. position: relative;
  332. }
  333. .ticket-title {
  334. font-size: 28rpx;
  335. color: $color-text-primary;
  336. font-weight: 700;
  337. margin-bottom: 12rpx;
  338. line-height: 1.4;
  339. overflow: hidden;
  340. text-overflow: ellipsis;
  341. white-space: nowrap;
  342. padding-right: 80rpx;
  343. }
  344. .ticket-meta {
  345. display: flex;
  346. align-items: center;
  347. gap: 12rpx;
  348. margin-bottom: 8rpx;
  349. }
  350. .ticket-badge {
  351. font-size: 18rpx;
  352. color: #FF8A00;
  353. background: #FFF4E0;
  354. padding: 4rpx 14rpx;
  355. border-radius: 6rpx;
  356. font-weight: 600;
  357. flex-shrink: 0;
  358. }
  359. .ticket-scope {
  360. font-size: 20rpx;
  361. color: $color-text-secondary;
  362. }
  363. .ticket-desc {
  364. font-size: 22rpx;
  365. color: $color-text-secondary;
  366. margin-bottom: 10rpx;
  367. overflow: hidden;
  368. text-overflow: ellipsis;
  369. white-space: nowrap;
  370. }
  371. .ticket-period {
  372. font-size: 22rpx;
  373. color: $color-text-tertiary;
  374. }
  375. /* --- Watermark --- */
  376. .ticket-watermark {
  377. position: absolute;
  378. top: 20rpx;
  379. right: 20rpx;
  380. border: 3rpx solid $color-text-tertiary;
  381. border-radius: 8rpx;
  382. padding: 2rpx 14rpx;
  383. transform: rotate(-12deg);
  384. opacity: 0.6;
  385. }
  386. .watermark-text {
  387. font-size: 22rpx;
  388. color: $color-text-tertiary;
  389. font-weight: 700;
  390. }
  391. /* ========== Load More ========== */
  392. .load-more {
  393. text-align: center;
  394. padding: 16rpx 0 40rpx;
  395. }
  396. .load-more-text {
  397. font-size: 24rpx;
  398. color: $color-text-tertiary;
  399. }
  400. /* ========== Empty State ========== */
  401. .empty {
  402. display: flex;
  403. flex-direction: column;
  404. align-items: center;
  405. justify-content: center;
  406. min-height: 55vh;
  407. padding: 40rpx;
  408. }
  409. .empty-visual {
  410. margin-bottom: 40rpx;
  411. position: relative;
  412. }
  413. .empty-ticket-shadow {
  414. position: absolute;
  415. top: 8rpx;
  416. left: 4rpx;
  417. right: 4rpx;
  418. bottom: -8rpx;
  419. background: $color-border;
  420. border-radius: 16rpx;
  421. }
  422. .empty-ticket-body {
  423. display: flex;
  424. width: 320rpx;
  425. height: 140rpx;
  426. background: $color-bg-tertiary;
  427. border-radius: 16rpx;
  428. overflow: hidden;
  429. position: relative;
  430. z-index: 1;
  431. }
  432. .empty-ticket-left {
  433. width: 110rpx;
  434. display: flex;
  435. align-items: center;
  436. justify-content: center;
  437. &::before {
  438. content: '¥';
  439. font-size: 44rpx;
  440. font-weight: 800;
  441. color: $color-text-tertiary;
  442. opacity: 0.4;
  443. }
  444. }
  445. .empty-ticket-dots {
  446. width: 30rpx;
  447. display: flex;
  448. flex-direction: column;
  449. align-items: center;
  450. justify-content: center;
  451. gap: 16rpx;
  452. }
  453. .empty-dot {
  454. width: 14rpx;
  455. height: 14rpx;
  456. border-radius: 50%;
  457. background: $color-bg-tertiary;
  458. border: 2rpx solid $color-border;
  459. }
  460. .empty-ticket-right {
  461. flex: 1;
  462. display: flex;
  463. flex-direction: column;
  464. justify-content: center;
  465. padding: 24rpx 20rpx;
  466. gap: 14rpx;
  467. }
  468. .empty-line {
  469. height: 10rpx;
  470. border-radius: 5rpx;
  471. background: #E8E8E8;
  472. }
  473. .empty-line--long {
  474. width: 100%;
  475. }
  476. .empty-line--short {
  477. width: 60%;
  478. }
  479. .empty-title {
  480. font-size: 28rpx;
  481. color: $color-text-secondary;
  482. font-weight: 600;
  483. margin-bottom: 36rpx;
  484. }
  485. .empty-cta {
  486. padding: 18rpx 64rpx;
  487. background: $color-primary;
  488. border-radius: 44rpx;
  489. &:active {
  490. opacity: 0.85;
  491. }
  492. }
  493. .empty-cta-text {
  494. font-size: 28rpx;
  495. color: #1A1A1A;
  496. font-weight: 600;
  497. }
  498. /* ========== Loading ========== */
  499. .loading {
  500. display: flex;
  501. flex-direction: column;
  502. align-items: center;
  503. justify-content: center;
  504. min-height: 55vh;
  505. gap: 20rpx;
  506. }
  507. .loading-spinner {
  508. width: 48rpx;
  509. height: 48rpx;
  510. border: 4rpx solid $color-border;
  511. border-top-color: $color-primary;
  512. border-radius: 50%;
  513. animation: spin 0.8s linear infinite;
  514. }
  515. @keyframes spin {
  516. to { transform: rotate(360deg); }
  517. }
  518. .loading-label {
  519. font-size: 24rpx;
  520. color: $color-text-tertiary;
  521. }
  522. /* ========== Bottom CTA ========== */
  523. .bottom-bar {
  524. padding: 20rpx 64rpx 56rpx;
  525. display: flex;
  526. justify-content: center;
  527. }
  528. .bottom-btn {
  529. padding: 22rpx 0;
  530. width: 100%;
  531. background: $color-primary;
  532. border-radius: 44rpx;
  533. text-align: center;
  534. &:active {
  535. opacity: 0.85;
  536. }
  537. }
  538. .bottom-btn-label {
  539. font-size: 28rpx;
  540. color: #1A1A1A;
  541. font-weight: 600;
  542. }
  543. </style>