index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <template>
  2. <!-- 优惠券页面 -->
  3. <view class="page">
  4. <!-- 券状态横向滚动 + 右侧固定「全部」(覆盖滚动内容) -->
  5. <view class="tabs-wrap">
  6. <view class="tabs-row">
  7. <scroll-view
  8. class="tabs-scroll"
  9. scroll-x
  10. :show-scrollbar="false"
  11. enable-flex
  12. >
  13. <view class="tabs-scroll-inner">
  14. <view
  15. v-for="(tab, index) in mainTabs"
  16. :key="'main-' + index"
  17. :class="['tab-item', { 'tab-item--active': currentTab === index }]"
  18. @click="handleTabChange(index)"
  19. >
  20. {{ tab }}
  21. </view>
  22. </view>
  23. </scroll-view>
  24. <view
  25. class="tabs-type-trigger"
  26. :class="{
  27. 'tabs-type-trigger--open': typePanelOpen,
  28. 'tabs-type-trigger--filtered': !typePanelOpen && typeFilter !== 'all'
  29. }"
  30. @click.stop="toggleTypePanel"
  31. >
  32. <view class="tabs-type-trigger__inner">
  33. <text class="tabs-type-trigger__text">{{ typeTriggerLabel }}</text>
  34. <view
  35. :class="['tabs-type-trigger__chevron', { 'tabs-type-trigger__chevron--open': typePanelOpen }]"
  36. />
  37. </view>
  38. </view>
  39. </view>
  40. <view v-if="typePanelOpen" class="type-filter-row">
  41. <view
  42. v-for="opt in typeFilterOptions"
  43. :key="opt.value"
  44. :class="['type-pill', { 'type-pill--active': typeFilter === opt.value }]"
  45. @click="selectTypeFilter(opt.value)"
  46. >
  47. {{ opt.label }}
  48. </view>
  49. </view>
  50. </view>
  51. <!-- 列表区域:展开类型筛选时盖半透明遮罩,点击关闭 -->
  52. <view class="page-body">
  53. <view v-if="typePanelOpen" class="type-mask" @tap="closeTypePanel" />
  54. <scroll-view
  55. class="content"
  56. scroll-y
  57. :scroll-top="listScrollTop"
  58. :scroll-with-animation="false"
  59. :class="{ 'content--dimmed': typePanelOpen }"
  60. >
  61. <view class="coupon-list" v-if="filteredCoupons.length > 0">
  62. <view v-for="(coupon, index) in filteredCoupons" :key="coupon.id || index"
  63. :class="['coupon-card', getCouponCardClass(coupon)]">
  64. <image :src="getFileUrl('img/personal/coupon.png')" mode="widthFix" class="coupon-card-bg"></image>
  65. <image :src="getFileUrl('img/personal/couponLeft.png')" mode="heightFix" class="coupon-card-bgLeft"></image>
  66. <view class="coupon-card-content">
  67. <!-- 左侧金额区域 -->
  68. <view class="coupon-card__left">
  69. <view class="amount-wrapper">
  70. <text class="amount-number">{{ coupon.amount }}</text>
  71. <text class="amount-unit">{{ coupon.amountUnit || '元' }}</text>
  72. </view>
  73. <text class="condition-text">{{ coupon.conditionText || (coupon.minAmount > 0 ? '满' + coupon.minAmount + '可用' : '无门槛') }}</text>
  74. </view>
  75. <!-- 右侧信息区域 -->
  76. <view class="coupon-card__right">
  77. <view class="coupon-info">
  78. <text class="coupon-name">{{ coupon.name }}</text>
  79. <view class="coupon-rules" @click="handleShowRules(coupon)">
  80. 使用规则
  81. <text class="arrow">›</text>
  82. </view>
  83. <text class="coupon-expire">{{ formatCouponExpireLine(coupon) }}</text>
  84. </view>
  85. </view>
  86. </view>
  87. </view>
  88. </view>
  89. <!-- 空状态 -->
  90. <view class="empty-state" v-if="filteredCoupons.length === 0">
  91. <image :src="getFileUrl('img/icon/noCoupon.png')" mode="widthFix" class="empty-icon"></image>
  92. <text class="empty-text">暂无优惠券</text>
  93. </view>
  94. </scroll-view>
  95. </view>
  96. <!-- 使用规则弹窗 -->
  97. <RulesModal v-model:open="showRulesModal" :couponData="selectedCoupon" />
  98. </view>
  99. </template>
  100. <script setup>
  101. import { onShow } from "@dcloudio/uni-app";
  102. import { ref, computed, nextTick } from "vue";
  103. import { getFileUrl } from "@/utils/file.js";
  104. import { normalizeUserCouponListItem, parseCouponListPage } from "@/utils/couponNormalize.js";
  105. import RulesModal from "./components/RulesModal.vue";
  106. import * as diningApi from "@/api/dining.js";
  107. // 状态 Tab:0未使用 1即将过期 2已使用 3已过期;右侧「全部」为券类型筛选入口
  108. const mainTabs = ['未使用', '即将过期', '已使用', '已过期'];
  109. const currentTab = ref(0);
  110. /** 右侧:展开折扣券 / 满减券筛选(couponType 1 满减 2 折扣) */
  111. const typePanelOpen = ref(false);
  112. const typeFilter = ref('all');
  113. const typeFilterOptions = [
  114. { value: 'all', label: '全部' },
  115. { value: 'discount', label: '折扣券' },
  116. { value: 'reduction', label: '满减券' }
  117. ];
  118. /** 右上角文案与当前选中的券类型一致 */
  119. const typeTriggerLabel = computed(() => {
  120. const hit = typeFilterOptions.find((o) => o.value === typeFilter.value);
  121. return hit?.label ?? '全部';
  122. });
  123. function toggleTypePanel() {
  124. typePanelOpen.value = !typePanelOpen.value;
  125. }
  126. function closeTypePanel() {
  127. typePanelOpen.value = false;
  128. }
  129. function selectTypeFilter(value) {
  130. typeFilter.value = value;
  131. typePanelOpen.value = false;
  132. // 类型为前端筛选;与列表请求共用队列,保证「切换类型」排在进行中的请求之后(后续 Tab/加载更多按序执行)
  133. enqueueListFetch(() => Promise.resolve());
  134. void nextTick(() => {
  135. void scrollCouponListToTop();
  136. });
  137. }
  138. // 弹窗控制
  139. const showRulesModal = ref(false);
  140. const selectedCoupon = ref({
  141. amount: 0,
  142. amountUnit: '元',
  143. minAmount: 0,
  144. name: '',
  145. expireDate: '',
  146. expirationTime: '',
  147. longTermValid: 0,
  148. specifiedDay: '',
  149. supplementaryInstruction: '',
  150. conditionText: '',
  151. qrCodeUrl: '',
  152. verificationCode: ''
  153. });
  154. // 优惠券数据(单次拉全量,不做分页)
  155. const couponList = ref([]);
  156. const loading = ref(false);
  157. /** 列表纵向滚动位置:刷新后归零,避免 scroll-view 仍停在旧滚动位置 */
  158. const listScrollTop = ref(0);
  159. /** 与后端约定的一次拉取上限(不分页) */
  160. const COUPON_LIST_SIZE = 999;
  161. /** 小程序 scroll-view 对 scroll-top 需「变化」才生效,先置非 0 再回 0 */
  162. async function scrollCouponListToTop() {
  163. listScrollTop.value = 1;
  164. await nextTick();
  165. listScrollTop.value = 0;
  166. await nextTick();
  167. }
  168. /** 列表相关请求串行:上一请求结束(成功/失败)后再执行下一请求,避免切换状态/类型时竞态 */
  169. let listFetchQueue = Promise.resolve();
  170. function enqueueListFetch(task) {
  171. const next = listFetchQueue.then(() => task());
  172. listFetchQueue = next.catch(() => {});
  173. return next;
  174. }
  175. // 列表:主 Tab 数据 + 类型筛选(全部 / 折扣券 / 满减券)
  176. const filteredCoupons = computed(() => {
  177. const list = couponList.value;
  178. const t = typeFilter.value;
  179. if (t === 'discount') return list.filter((c) => Number(c.couponType) === 2);
  180. if (t === 'reduction') return list.filter((c) => Number(c.couponType) === 1);
  181. return list;
  182. });
  183. // 切换标签页
  184. const handleTabChange = (index) => {
  185. currentTab.value = index;
  186. typePanelOpen.value = false;
  187. enqueueListFetch(() => fetchCouponList({ tabType: index }));
  188. };
  189. // 拉取优惠券列表(不分页:page=1、size=COUPON_LIST_SIZE)
  190. const fetchCouponList = async (options = {}) => {
  191. const { tabType: tabTypeOpt } = options;
  192. const tabForRequest = tabTypeOpt != null ? tabTypeOpt : currentTab.value;
  193. const storeId = uni.getStorageSync('currentStoreId') || '';
  194. loading.value = true;
  195. try {
  196. const res = await diningApi.GetUserCouponList({
  197. storeId,
  198. tabType: tabForRequest,
  199. page: 1,
  200. size: COUPON_LIST_SIZE
  201. });
  202. const { list: rawList } = parseCouponListPage(res);
  203. const arr = Array.isArray(rawList) ? rawList : [];
  204. couponList.value = arr.map((raw) => normalizeUserCouponListItem(raw, tabForRequest)).filter(Boolean);
  205. await nextTick();
  206. await scrollCouponListToTop();
  207. } catch (err) {
  208. console.error('获取优惠券列表失败:', err);
  209. uni.showToast({ title: '加载失败', icon: 'none' });
  210. couponList.value = [];
  211. await nextTick();
  212. await scrollCouponListToTop();
  213. } finally {
  214. loading.value = false;
  215. }
  216. };
  217. /** longTermValid=1 显示长期有效;=0 显示 expirationTime + 到期 */
  218. function formatCouponExpireLine(coupon) {
  219. if (Number(coupon?.longTermValid) === 1) return '长期有效';
  220. const t = String(coupon?.expirationTime ?? coupon?.expireDate ?? '').trim();
  221. return t ? `${t}到期` : '';
  222. }
  223. // 获取优惠券卡片样式类
  224. const getCouponCardClass = (coupon) => {
  225. if (coupon?.status === 2) return 'coupon-card--used';
  226. if (coupon?.status === 3) return 'coupon-card--expired';
  227. return '';
  228. };
  229. // 查看使用规则
  230. const handleShowRules = (coupon) => {
  231. selectedCoupon.value = { ...coupon };
  232. showRulesModal.value = true;
  233. };
  234. // 使用 onShow 拉取数据(onLoad 在 uni-app Vue3 组合式 API 下可能不触发,onShow 更可靠)
  235. onShow(() => {
  236. enqueueListFetch(() => fetchCouponList());
  237. });
  238. </script>
  239. <style lang="scss" scoped>
  240. .page {
  241. height: 100vh;
  242. min-height: 100vh;
  243. background: #F5F5F5;
  244. display: flex;
  245. flex-direction: column;
  246. overflow: hidden;
  247. }
  248. .tabs-wrap {
  249. flex-shrink: 0;
  250. background: #ffffff;
  251. position: relative;
  252. z-index: 20;
  253. }
  254. .page-body {
  255. flex: 1;
  256. min-height: 0;
  257. position: relative;
  258. display: flex;
  259. flex-direction: column;
  260. overflow: hidden;
  261. }
  262. .type-mask {
  263. position: absolute;
  264. left: 0;
  265. right: 0;
  266. top: 0;
  267. bottom: 0;
  268. z-index: 10;
  269. background: rgba(0, 0, 0, 0.45);
  270. }
  271. .tabs-row {
  272. position: relative;
  273. display: flex;
  274. flex-direction: row;
  275. align-items: flex-end;
  276. min-height: 96rpx;
  277. box-sizing: border-box;
  278. }
  279. /* 券状态:横向滚动,内容可滑到「全部」下方被遮挡 */
  280. .tabs-scroll {
  281. width: 100%;
  282. flex: 1;
  283. min-width: 0;
  284. height: 96rpx;
  285. white-space: nowrap;
  286. }
  287. .tabs-scroll-inner {
  288. display: inline-flex;
  289. flex-direction: row;
  290. align-items: flex-end;
  291. flex-wrap: nowrap;
  292. column-gap: 28rpx;
  293. min-height: 96rpx;
  294. padding: 0 0 0 28rpx;
  295. /* 右侧留白:最后一项可向左滑出「全部」遮挡区,便于看清 */
  296. padding-right: 120rpx;
  297. box-sizing: border-box;
  298. }
  299. /* 右侧固定:白底盖在滚动层之上;与 .tab-item 相同上下内边距与行高,保证与券状态垂直对齐 */
  300. .tabs-type-trigger {
  301. position: absolute;
  302. right: 0;
  303. bottom: 0;
  304. z-index: 3;
  305. display: flex;
  306. flex-direction: row;
  307. align-items: flex-end;
  308. justify-content: flex-end;
  309. height: 96rpx;
  310. min-height: 96rpx;
  311. /* 与 .tab-item 一致:上 28rpx / 下 24rpx,文字行高 40rpx */
  312. padding: 28rpx 24rpx 24rpx 28rpx;
  313. box-sizing: border-box;
  314. background: #ffffff;
  315. box-shadow: -16rpx 0 20rpx -8rpx #ffffff;
  316. }
  317. .tabs-type-trigger__inner {
  318. display: flex;
  319. flex-direction: row;
  320. align-items: center;
  321. flex-wrap: nowrap;
  322. height: 40rpx;
  323. }
  324. .tabs-type-trigger__text {
  325. font-size: 26rpx;
  326. line-height: 40rpx;
  327. height: 40rpx;
  328. color: #151515;
  329. white-space: nowrap;
  330. }
  331. .tabs-type-trigger--open .tabs-type-trigger__text,
  332. .tabs-type-trigger--filtered .tabs-type-trigger__text {
  333. color: #f47d1f;
  334. }
  335. .tabs-type-trigger__chevron {
  336. flex-shrink: 0;
  337. width: 0;
  338. height: 0;
  339. margin-left: 8rpx;
  340. border-left: 7rpx solid transparent;
  341. border-right: 7rpx solid transparent;
  342. border-top: 9rpx solid #999999;
  343. }
  344. .tabs-type-trigger__chevron--open {
  345. border-top: none;
  346. border-bottom: 9rpx solid #f47d1f;
  347. border-left: 7rpx solid transparent;
  348. border-right: 7rpx solid transparent;
  349. }
  350. .tab-item {
  351. flex-shrink: 0;
  352. text-align: center;
  353. font-size: 26rpx;
  354. line-height: 40rpx;
  355. color: #666666;
  356. padding: 28rpx 8rpx 24rpx;
  357. margin: 0 25rpx;
  358. position: relative;
  359. transition: color 0.2s;
  360. box-sizing: border-box;
  361. white-space: nowrap;
  362. &--active {
  363. color: #151515;
  364. font-weight: bold;
  365. &::after {
  366. content: '';
  367. position: absolute;
  368. bottom: 16rpx;
  369. left: 50%;
  370. transform: translateX(-50%);
  371. width: 40rpx;
  372. height: 6rpx;
  373. background: #ff8a00;
  374. border-radius: 3rpx;
  375. }
  376. }
  377. }
  378. .type-filter-row {
  379. display: flex;
  380. flex-direction: row;
  381. align-items: center;
  382. flex-wrap: wrap;
  383. gap: 16rpx;
  384. padding: 8rpx 30rpx 20rpx;
  385. box-sizing: border-box;
  386. background: #ffffff;
  387. }
  388. .type-pill {
  389. padding: 14rpx 32rpx;
  390. border-radius: 999rpx;
  391. font-size: 24rpx;
  392. color: #333333;
  393. background: #f2f3f5;
  394. line-height: 1.2;
  395. }
  396. .type-pill--active {
  397. background: #fff4e6;
  398. color: #f47d1f;
  399. font-weight: 500;
  400. }
  401. .content {
  402. flex: 1;
  403. min-height: 0;
  404. position: relative;
  405. z-index: 1;
  406. padding: 24rpx 30rpx;
  407. padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  408. box-sizing: border-box;
  409. }
  410. .content--dimmed {
  411. pointer-events: none;
  412. }
  413. .coupon-list {
  414. position: relative;
  415. .coupon-card-bg {
  416. position: absolute;
  417. top: 0;
  418. left: 0;
  419. width: 100%;
  420. height: 100%;
  421. z-index: 1;
  422. }
  423. .coupon-card-bgLeft {
  424. position: absolute;
  425. top: 0;
  426. left: 0;
  427. width: auto;
  428. height: 190rpx;
  429. z-index: 1;
  430. }
  431. .coupon-card-content {
  432. position: relative;
  433. z-index: 3;
  434. display: flex;
  435. align-items: center;
  436. }
  437. .coupon-card {
  438. display: flex;
  439. align-items: center;
  440. margin-bottom: 24rpx;
  441. overflow: hidden;
  442. position: relative;
  443. box-sizing: border-box;
  444. position: relative;
  445. z-index: 3;
  446. &:last-child {
  447. margin-bottom: 0;
  448. }
  449. &__left {
  450. width: 200rpx;
  451. padding: 32rpx 0;
  452. display: flex;
  453. flex-direction: column;
  454. align-items: center;
  455. justify-content: center;
  456. .amount-wrapper {
  457. display: flex;
  458. align-items: baseline;
  459. margin-bottom: 8rpx;
  460. .amount-number {
  461. font-size: 64rpx;
  462. font-weight: bold;
  463. color: #F47D1F;
  464. line-height: 1;
  465. }
  466. .amount-unit {
  467. font-size: 28rpx;
  468. color: #F47D1F;
  469. margin-left: 4rpx;
  470. }
  471. }
  472. .condition-text {
  473. font-size: 22rpx;
  474. color: #F47D1F;
  475. }
  476. }
  477. &__divider {
  478. width: 2rpx;
  479. height: 100%;
  480. position: relative;
  481. .dash-line {
  482. width: 2rpx;
  483. height: 100%;
  484. // background-image: linear-gradient(to bottom, #FFD9C2 0%, #FFD9C2 50%, transparent 50%, transparent 100%);
  485. background-size: 2rpx 12rpx;
  486. background-repeat: repeat-y;
  487. }
  488. }
  489. &__right {
  490. flex: 1;
  491. display: flex;
  492. align-items: center;
  493. justify-content: space-between;
  494. padding: 32rpx 24rpx;
  495. .coupon-info {
  496. flex: 1;
  497. display: flex;
  498. flex-direction: column;
  499. .coupon-name {
  500. font-size: 28rpx;
  501. font-weight: bold;
  502. color: #151515;
  503. margin-bottom: 12rpx;
  504. }
  505. .coupon-rules {
  506. font-size: 22rpx;
  507. color: #999999;
  508. margin-bottom: 12rpx;
  509. display: flex;
  510. align-items: center;
  511. .arrow {
  512. font-size: 28rpx;
  513. margin-left: 4rpx;
  514. }
  515. }
  516. .coupon-expire {
  517. font-size: 22rpx;
  518. color: #999999;
  519. }
  520. }
  521. }
  522. // 已使用状态
  523. &--used {
  524. background: #F8F8F8;
  525. .coupon-card__left {
  526. .amount-number,
  527. .amount-unit,
  528. .condition-text {
  529. color: #CCCCCC;
  530. }
  531. }
  532. .coupon-card__right {
  533. .coupon-info {
  534. .coupon-name,
  535. .coupon-rules,
  536. .coupon-expire {
  537. color: #CCCCCC;
  538. }
  539. }
  540. }
  541. }
  542. // 已过期状态
  543. &--expired {
  544. background: #F8F8F8;
  545. .coupon-card__left {
  546. .amount-number,
  547. .amount-unit,
  548. .condition-text {
  549. color: #CCCCCC;
  550. }
  551. }
  552. .coupon-card__right {
  553. .coupon-info {
  554. .coupon-name,
  555. .coupon-rules,
  556. .coupon-expire {
  557. color: #CCCCCC;
  558. }
  559. }
  560. }
  561. }
  562. }
  563. }
  564. .hover-active {
  565. opacity: 0.8;
  566. transform: scale(0.98);
  567. }
  568. .empty-state {
  569. display: flex;
  570. flex-direction: column;
  571. align-items: center;
  572. justify-content: center;
  573. padding-top: 200rpx;
  574. .empty-icon {
  575. width: 300rpx;
  576. height: 280rpx;
  577. margin-bottom: 40rpx;
  578. }
  579. .empty-text {
  580. font-size: 28rpx;
  581. color: #AAAAAA;
  582. }
  583. }
  584. </style>