index.vue 14 KB

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