index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <template>
  2. <!-- 优惠券页面 -->
  3. <view class="page">
  4. <!-- 标签页 -->
  5. <view class="tabs">
  6. <view v-for="(tab, index) in tabs" :key="index"
  7. :class="['tab-item', { 'tab-item--active': currentTab === index }]" @click="handleTabChange(index)">
  8. {{ tab }}
  9. </view>
  10. </view>
  11. <!-- 优惠券列表 -->
  12. <scroll-view class="content" scroll-y>
  13. <view class="coupon-list" v-if="filteredCoupons.length > 0">
  14. <view v-for="(coupon, index) in filteredCoupons" :key="coupon.id || index"
  15. :class="['coupon-card', getCouponCardClass(coupon)]">
  16. <image :src="getFileUrl('img/personal/coupon.png')" mode="widthFix" class="coupon-card-bg"></image>
  17. <image :src="getFileUrl('img/personal/couponLeft.png')" mode="heightFix" class="coupon-card-bgLeft"></image>
  18. <view class="coupon-card-content">
  19. <!-- 左侧金额区域 -->
  20. <view class="coupon-card__left">
  21. <view class="amount-wrapper">
  22. <text class="amount-number">{{ coupon.amount }}</text>
  23. <text class="amount-unit">{{ coupon.amountUnit || '元' }}</text>
  24. </view>
  25. <text class="condition-text">{{ coupon.conditionText || (coupon.minAmount > 0 ? '满' + coupon.minAmount + '可用' : '无门槛') }}</text>
  26. </view>
  27. <!-- 右侧信息区域 -->
  28. <view class="coupon-card__right">
  29. <view class="coupon-info">
  30. <text class="coupon-name">{{ coupon.name }}</text>
  31. <view class="coupon-rules" @click="handleShowRules(coupon)">
  32. 使用规则
  33. <text class="arrow">›</text>
  34. </view>
  35. <text class="coupon-expire">{{ coupon.expireDate ? coupon.expireDate + '到期' : '' }}</text>
  36. </view>
  37. <!-- 状态展示 -->
  38. <view class="coupon-action">
  39. <view v-if="coupon.status === 0" class="status-text status-text--unused">未使用</view>
  40. <view v-else-if="coupon.status === 1" class="status-text status-text--expiring">即将过期</view>
  41. <view v-else-if="coupon.status === 2" class="status-text status-text--used">已使用</view>
  42. <view v-else class="status-text status-text--expired">已过期</view>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. <!-- 空状态 -->
  49. <view class="empty-state" v-else>
  50. <image :src="getFileUrl('img/icon/noCoupon.png')" mode="widthFix" class="empty-icon"></image>
  51. <text class="empty-text">暂无优惠券</text>
  52. </view>
  53. </scroll-view>
  54. <!-- 使用规则弹窗 -->
  55. <RulesModal v-model:open="showRulesModal" :couponData="selectedCoupon" @use="handleUseCoupon" />
  56. </view>
  57. </template>
  58. <script setup>
  59. import { onShow } from "@dcloudio/uni-app";
  60. import { ref, computed } from "vue";
  61. import { go } from "@/utils/utils.js";
  62. import { getFileUrl } from "@/utils/file.js";
  63. import RulesModal from "./components/RulesModal.vue";
  64. import * as diningApi from "@/api/dining.js";
  65. // 标签页数据
  66. const tabs = ['未使用', '即将过期', '已使用', '已过期'];
  67. const currentTab = ref(0);
  68. // 弹窗控制
  69. const showRulesModal = ref(false);
  70. const selectedCoupon = ref({
  71. amount: 0,
  72. minAmount: 0,
  73. name: '',
  74. expireDate: '',
  75. validDays: 90,
  76. description: '周一、周五不可用'
  77. });
  78. // 优惠券数据(接口返回)
  79. const couponList = ref([]);
  80. const loading = ref(false);
  81. // 规范化接口返回的优惠券项
  82. function normalizeCouponItem(raw) {
  83. if (!raw || typeof raw !== 'object') return null;
  84. const couponType = Number(raw.couponType) ?? 1;
  85. const nominalValue = Number(raw.nominalValue ?? raw.amount ?? 0) || 0;
  86. const discountRate = Number(raw.discountRate ?? 0) || 0;
  87. const minAmount = Number(raw.minimumSpendingAmount ?? raw.minAmount ?? 0) || 0;
  88. let amount = nominalValue;
  89. let amountUnit = '元';
  90. let conditionText = minAmount > 0 ? `满${minAmount}可用` : '无门槛';
  91. if (couponType === 2 && discountRate > 0) {
  92. amount = discountRate;
  93. amountUnit = '折';
  94. conditionText = minAmount > 0 ? `满${minAmount}可用` : '无门槛';
  95. }
  96. return {
  97. id: raw.id ?? raw.userCouponId ?? raw.couponId ?? '',
  98. amount,
  99. amountUnit,
  100. minAmount,
  101. name: raw.name ?? raw.title ?? raw.couponName ?? '',
  102. expireDate: raw.expirationTime ?? raw.endGetDate ?? raw.expireDate ?? '',
  103. status: currentTab.value,
  104. couponType,
  105. nominalValue,
  106. discountRate,
  107. conditionText
  108. };
  109. }
  110. // 根据当前标签页展示的优惠券(接口按 tabType 分页返回,直接使用)
  111. const filteredCoupons = computed(() => couponList.value);
  112. // 切换标签页
  113. const handleTabChange = (index) => {
  114. currentTab.value = index;
  115. fetchCouponList();
  116. };
  117. // 拉取优惠券列表
  118. const fetchCouponList = async () => {
  119. loading.value = true;
  120. try {
  121. const res = await diningApi.GetUserCouponList({
  122. tabType: currentTab.value, // 0未使用 1即将过期 2已使用 3已过期
  123. page: 1,
  124. size: 20
  125. });
  126. const raw = res?.data ?? res ?? {};
  127. const list = raw?.records ?? raw?.list ?? (Array.isArray(res) ? res : []);
  128. const arr = Array.isArray(list) ? list : [];
  129. couponList.value = arr.map(normalizeCouponItem).filter(Boolean);
  130. } catch (err) {
  131. console.error('获取优惠券列表失败:', err);
  132. uni.showToast({ title: '加载失败', icon: 'none' });
  133. couponList.value = [];
  134. } finally {
  135. loading.value = false;
  136. }
  137. };
  138. // 获取优惠券卡片样式类
  139. const getCouponCardClass = (coupon) => {
  140. if (coupon.status === 2) return 'coupon-card--used';
  141. if (coupon.status === 3) return 'coupon-card--expired';
  142. return '';
  143. };
  144. // 使用优惠券
  145. const handleUseCoupon = (coupon) => {
  146. console.log('使用优惠券:', coupon);
  147. // 跳转到点餐页面
  148. go('/pages/orderFood/index');
  149. };
  150. // 查看使用规则
  151. const handleShowRules = (coupon) => {
  152. selectedCoupon.value = {
  153. ...coupon,
  154. validDays: 90,
  155. description: '周一、周五不可用'
  156. };
  157. showRulesModal.value = true;
  158. };
  159. // 使用 onShow 拉取数据(onLoad 在 uni-app Vue3 组合式 API 下可能不触发,onShow 更可靠)
  160. onShow(() => {
  161. fetchCouponList();
  162. });
  163. </script>
  164. <style lang="scss" scoped>
  165. .page {
  166. height: 100vh;
  167. min-height: 100vh;
  168. background: #F5F5F5;
  169. display: flex;
  170. flex-direction: column;
  171. overflow: hidden;
  172. }
  173. .header {
  174. background: #FFFFFF;
  175. padding: 20rpx 30rpx;
  176. padding-top: calc(20rpx + env(safe-area-inset-top));
  177. .header-title {
  178. font-size: 36rpx;
  179. font-weight: bold;
  180. color: #151515;
  181. text-align: center;
  182. }
  183. }
  184. .tabs {
  185. flex-shrink: 0;
  186. background: #FFFFFF;
  187. display: flex;
  188. align-items: center;
  189. padding: 0 30rpx;
  190. .tab-item {
  191. flex: 1;
  192. text-align: center;
  193. font-size: 28rpx;
  194. color: #666666;
  195. padding: 28rpx 0;
  196. position: relative;
  197. transition: all 0.3s;
  198. &--active {
  199. color: #151515;
  200. font-weight: bold;
  201. &::after {
  202. content: '';
  203. position: absolute;
  204. bottom: 20rpx;
  205. left: 50%;
  206. transform: translateX(-50%);
  207. width: 40rpx;
  208. height: 6rpx;
  209. background: linear-gradient(90deg, #FF8A57 0%, #F47D1F 100%);
  210. border-radius: 3rpx;
  211. }
  212. }
  213. }
  214. }
  215. .content {
  216. flex: 1;
  217. min-height: 0;
  218. padding: 24rpx 30rpx;
  219. padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  220. box-sizing: border-box;
  221. }
  222. .coupon-list {
  223. position: relative;
  224. .coupon-card-bg {
  225. position: absolute;
  226. top: 0;
  227. left: 0;
  228. width: 100%;
  229. height: 100%;
  230. z-index: 1;
  231. }
  232. .coupon-card-bgLeft {
  233. position: absolute;
  234. top: 0;
  235. left: 0;
  236. width: auto;
  237. height: 190rpx;
  238. z-index: 1;
  239. }
  240. .coupon-card-content {
  241. position: relative;
  242. z-index: 3;
  243. display: flex;
  244. align-items: center;
  245. }
  246. .coupon-card {
  247. display: flex;
  248. align-items: center;
  249. margin-bottom: 24rpx;
  250. overflow: hidden;
  251. position: relative;
  252. box-sizing: border-box;
  253. position: relative;
  254. z-index: 3;
  255. &:last-child {
  256. margin-bottom: 0;
  257. }
  258. &__left {
  259. width: 200rpx;
  260. padding: 32rpx 0;
  261. display: flex;
  262. flex-direction: column;
  263. align-items: center;
  264. justify-content: center;
  265. .amount-wrapper {
  266. display: flex;
  267. align-items: baseline;
  268. margin-bottom: 8rpx;
  269. .amount-number {
  270. font-size: 64rpx;
  271. font-weight: bold;
  272. color: #F47D1F;
  273. line-height: 1;
  274. }
  275. .amount-unit {
  276. font-size: 28rpx;
  277. color: #F47D1F;
  278. margin-left: 4rpx;
  279. }
  280. }
  281. .condition-text {
  282. font-size: 22rpx;
  283. color: #F47D1F;
  284. }
  285. }
  286. &__divider {
  287. width: 2rpx;
  288. height: 100%;
  289. position: relative;
  290. .dash-line {
  291. width: 2rpx;
  292. height: 100%;
  293. // background-image: linear-gradient(to bottom, #FFD9C2 0%, #FFD9C2 50%, transparent 50%, transparent 100%);
  294. background-size: 2rpx 12rpx;
  295. background-repeat: repeat-y;
  296. }
  297. }
  298. &__right {
  299. flex: 1;
  300. display: flex;
  301. align-items: center;
  302. justify-content: space-between;
  303. padding: 32rpx 24rpx;
  304. .coupon-info {
  305. flex: 1;
  306. display: flex;
  307. flex-direction: column;
  308. .coupon-name {
  309. font-size: 28rpx;
  310. font-weight: bold;
  311. color: #151515;
  312. margin-bottom: 12rpx;
  313. }
  314. .coupon-rules {
  315. font-size: 22rpx;
  316. color: #999999;
  317. margin-bottom: 12rpx;
  318. display: flex;
  319. align-items: center;
  320. .arrow {
  321. font-size: 28rpx;
  322. margin-left: 4rpx;
  323. }
  324. }
  325. .coupon-expire {
  326. font-size: 22rpx;
  327. color: #999999;
  328. }
  329. }
  330. .coupon-action {
  331. margin-left: 20rpx;
  332. .action-btn {
  333. width: 120rpx;
  334. height: 56rpx;
  335. border-radius: 28rpx;
  336. display: flex;
  337. align-items: center;
  338. justify-content: center;
  339. font-size: 26rpx;
  340. font-weight: 500;
  341. &--use {
  342. // background: linear-gradient(135deg, #FF8A57 0%, #F47D1F 100%);
  343. background: #F47D1F;
  344. color: #FFFFFF;
  345. // box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(255, 107, 53, 0.3);
  346. }
  347. }
  348. .status-text {
  349. font-size: 24rpx;
  350. padding: 8rpx 16rpx;
  351. &--unused,
  352. &--expiring {
  353. color: #999999;
  354. }
  355. &--used {
  356. color: #999999;
  357. }
  358. &--expired {
  359. color: #CCCCCC;
  360. }
  361. }
  362. }
  363. }
  364. // 已使用状态
  365. &--used {
  366. background: #F8F8F8;
  367. .coupon-card__left {
  368. .amount-number,
  369. .amount-unit,
  370. .condition-text {
  371. color: #CCCCCC;
  372. }
  373. }
  374. .coupon-card__right {
  375. .coupon-info {
  376. .coupon-name,
  377. .coupon-rules,
  378. .coupon-expire {
  379. color: #CCCCCC;
  380. }
  381. }
  382. }
  383. }
  384. // 已过期状态
  385. &--expired {
  386. background: #F8F8F8;
  387. .coupon-card__left {
  388. .amount-number,
  389. .amount-unit,
  390. .condition-text {
  391. color: #CCCCCC;
  392. }
  393. }
  394. .coupon-card__right {
  395. .coupon-info {
  396. .coupon-name,
  397. .coupon-rules,
  398. .coupon-expire {
  399. color: #CCCCCC;
  400. }
  401. }
  402. }
  403. }
  404. }
  405. }
  406. .hover-active {
  407. opacity: 0.8;
  408. transform: scale(0.98);
  409. }
  410. .empty-state {
  411. display: flex;
  412. flex-direction: column;
  413. align-items: center;
  414. justify-content: center;
  415. padding-top: 200rpx;
  416. .empty-icon {
  417. width: 300rpx;
  418. height: 280rpx;
  419. margin-bottom: 40rpx;
  420. }
  421. .empty-text {
  422. font-size: 28rpx;
  423. color: #AAAAAA;
  424. }
  425. }
  426. </style>