index.vue 17 KB

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