index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. min-height: 100vh;
  167. background: #F5F5F5;
  168. display: flex;
  169. flex-direction: column;
  170. }
  171. .header {
  172. background: #FFFFFF;
  173. padding: 20rpx 30rpx;
  174. padding-top: calc(20rpx + env(safe-area-inset-top));
  175. .header-title {
  176. font-size: 36rpx;
  177. font-weight: bold;
  178. color: #151515;
  179. text-align: center;
  180. }
  181. }
  182. .tabs {
  183. background: #FFFFFF;
  184. display: flex;
  185. align-items: center;
  186. padding: 0 30rpx;
  187. .tab-item {
  188. flex: 1;
  189. text-align: center;
  190. font-size: 28rpx;
  191. color: #666666;
  192. padding: 28rpx 0;
  193. position: relative;
  194. transition: all 0.3s;
  195. &--active {
  196. color: #151515;
  197. font-weight: bold;
  198. &::after {
  199. content: '';
  200. position: absolute;
  201. bottom: 20rpx;
  202. left: 50%;
  203. transform: translateX(-50%);
  204. width: 40rpx;
  205. height: 6rpx;
  206. background: linear-gradient(90deg, #FF8A57 0%, #F47D1F 100%);
  207. border-radius: 3rpx;
  208. }
  209. }
  210. }
  211. }
  212. .content {
  213. flex: 1;
  214. padding: 24rpx 30rpx;
  215. padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  216. box-sizing: border-box;
  217. }
  218. .coupon-list {
  219. position: relative;
  220. .coupon-card-bg {
  221. position: absolute;
  222. top: 0;
  223. left: 0;
  224. width: 100%;
  225. height: 100%;
  226. z-index: 1;
  227. }
  228. .coupon-card-bgLeft {
  229. position: absolute;
  230. top: 0;
  231. left: 0;
  232. width: auto;
  233. height: 190rpx;
  234. z-index: 1;
  235. }
  236. .coupon-card-content {
  237. position: relative;
  238. z-index: 3;
  239. display: flex;
  240. align-items: center;
  241. }
  242. .coupon-card {
  243. display: flex;
  244. align-items: center;
  245. margin-bottom: 24rpx;
  246. overflow: hidden;
  247. position: relative;
  248. box-sizing: border-box;
  249. position: relative;
  250. z-index: 3;
  251. &:last-child {
  252. margin-bottom: 0;
  253. }
  254. &__left {
  255. width: 200rpx;
  256. padding: 32rpx 0;
  257. display: flex;
  258. flex-direction: column;
  259. align-items: center;
  260. justify-content: center;
  261. .amount-wrapper {
  262. display: flex;
  263. align-items: baseline;
  264. margin-bottom: 8rpx;
  265. .amount-number {
  266. font-size: 64rpx;
  267. font-weight: bold;
  268. color: #F47D1F;
  269. line-height: 1;
  270. }
  271. .amount-unit {
  272. font-size: 28rpx;
  273. color: #F47D1F;
  274. margin-left: 4rpx;
  275. }
  276. }
  277. .condition-text {
  278. font-size: 22rpx;
  279. color: #F47D1F;
  280. }
  281. }
  282. &__divider {
  283. width: 2rpx;
  284. height: 100%;
  285. position: relative;
  286. .dash-line {
  287. width: 2rpx;
  288. height: 100%;
  289. // background-image: linear-gradient(to bottom, #FFD9C2 0%, #FFD9C2 50%, transparent 50%, transparent 100%);
  290. background-size: 2rpx 12rpx;
  291. background-repeat: repeat-y;
  292. }
  293. }
  294. &__right {
  295. flex: 1;
  296. display: flex;
  297. align-items: center;
  298. justify-content: space-between;
  299. padding: 32rpx 24rpx;
  300. .coupon-info {
  301. flex: 1;
  302. display: flex;
  303. flex-direction: column;
  304. .coupon-name {
  305. font-size: 28rpx;
  306. font-weight: bold;
  307. color: #151515;
  308. margin-bottom: 12rpx;
  309. }
  310. .coupon-rules {
  311. font-size: 22rpx;
  312. color: #999999;
  313. margin-bottom: 12rpx;
  314. display: flex;
  315. align-items: center;
  316. .arrow {
  317. font-size: 28rpx;
  318. margin-left: 4rpx;
  319. }
  320. }
  321. .coupon-expire {
  322. font-size: 22rpx;
  323. color: #999999;
  324. }
  325. }
  326. .coupon-action {
  327. margin-left: 20rpx;
  328. .action-btn {
  329. width: 120rpx;
  330. height: 56rpx;
  331. border-radius: 28rpx;
  332. display: flex;
  333. align-items: center;
  334. justify-content: center;
  335. font-size: 26rpx;
  336. font-weight: 500;
  337. &--use {
  338. // background: linear-gradient(135deg, #FF8A57 0%, #F47D1F 100%);
  339. background: #F47D1F;
  340. color: #FFFFFF;
  341. // box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(255, 107, 53, 0.3);
  342. }
  343. }
  344. .status-text {
  345. font-size: 24rpx;
  346. padding: 8rpx 16rpx;
  347. &--unused,
  348. &--expiring {
  349. color: #999999;
  350. }
  351. &--used {
  352. color: #999999;
  353. }
  354. &--expired {
  355. color: #CCCCCC;
  356. }
  357. }
  358. }
  359. }
  360. // 已使用状态
  361. &--used {
  362. background: #F8F8F8;
  363. .coupon-card__left {
  364. .amount-number,
  365. .amount-unit,
  366. .condition-text {
  367. color: #CCCCCC;
  368. }
  369. }
  370. .coupon-card__right {
  371. .coupon-info {
  372. .coupon-name,
  373. .coupon-rules,
  374. .coupon-expire {
  375. color: #CCCCCC;
  376. }
  377. }
  378. }
  379. }
  380. // 已过期状态
  381. &--expired {
  382. background: #F8F8F8;
  383. .coupon-card__left {
  384. .amount-number,
  385. .amount-unit,
  386. .condition-text {
  387. color: #CCCCCC;
  388. }
  389. }
  390. .coupon-card__right {
  391. .coupon-info {
  392. .coupon-name,
  393. .coupon-rules,
  394. .coupon-expire {
  395. color: #CCCCCC;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. }
  402. .hover-active {
  403. opacity: 0.8;
  404. transform: scale(0.98);
  405. }
  406. .empty-state {
  407. display: flex;
  408. flex-direction: column;
  409. align-items: center;
  410. justify-content: center;
  411. padding-top: 200rpx;
  412. .empty-icon {
  413. width: 300rpx;
  414. height: 280rpx;
  415. margin-bottom: 40rpx;
  416. }
  417. .empty-text {
  418. font-size: 28rpx;
  419. color: #AAAAAA;
  420. }
  421. }
  422. </style>