index.vue 16 KB

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