index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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" />
  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. // 标签页:0未使用 1即将过期 2已使用 3已过期
  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. // 优惠券列表
  111. const filteredCoupons = computed(() => couponList.value);
  112. // 切换标签页
  113. const handleTabChange = (index) => {
  114. currentTab.value = index;
  115. fetchCouponList();
  116. };
  117. // 拉取优惠券列表(coupon/getUserCouponList)
  118. const fetchCouponList = async () => {
  119. loading.value = true;
  120. try {
  121. const storeId = uni.getStorageSync('currentStoreId') || '';
  122. const res = await diningApi.GetUserCouponList({ storeId, tabType: currentTab.value, page: 1, size: 20 });
  123. const list = Array.isArray(res) ? res : (res?.data ?? res?.records ?? res?.list ?? []);
  124. const arr = Array.isArray(list) ? list : [];
  125. couponList.value = arr.map(normalizeCouponItem).filter(Boolean);
  126. } catch (err) {
  127. console.error('获取优惠券列表失败:', err);
  128. uni.showToast({ title: '加载失败', icon: 'none' });
  129. couponList.value = [];
  130. } finally {
  131. loading.value = false;
  132. }
  133. };
  134. // 获取优惠券卡片样式类
  135. const getCouponCardClass = (coupon) => {
  136. if (coupon?.status === 2) return 'coupon-card--used';
  137. if (coupon?.status === 3) return 'coupon-card--expired';
  138. return '';
  139. };
  140. // 查看使用规则
  141. const handleShowRules = (coupon) => {
  142. selectedCoupon.value = {
  143. ...coupon,
  144. validDays: 90,
  145. description: '周一、周五不可用'
  146. };
  147. showRulesModal.value = true;
  148. };
  149. // 使用 onShow 拉取数据(onLoad 在 uni-app Vue3 组合式 API 下可能不触发,onShow 更可靠)
  150. onShow(() => {
  151. fetchCouponList();
  152. });
  153. </script>
  154. <style lang="scss" scoped>
  155. .page {
  156. height: 100vh;
  157. min-height: 100vh;
  158. background: #F5F5F5;
  159. display: flex;
  160. flex-direction: column;
  161. overflow: hidden;
  162. }
  163. .header {
  164. background: #FFFFFF;
  165. padding: 20rpx 30rpx;
  166. padding-top: calc(20rpx + env(safe-area-inset-top));
  167. .header-title {
  168. font-size: 36rpx;
  169. font-weight: bold;
  170. color: #151515;
  171. text-align: center;
  172. }
  173. }
  174. .tabs {
  175. flex-shrink: 0;
  176. background: #FFFFFF;
  177. display: flex;
  178. align-items: center;
  179. padding: 0 30rpx;
  180. .tab-item {
  181. flex: 1;
  182. text-align: center;
  183. font-size: 28rpx;
  184. color: #666666;
  185. padding: 28rpx 0;
  186. position: relative;
  187. transition: all 0.3s;
  188. &--active {
  189. color: #151515;
  190. font-weight: bold;
  191. &::after {
  192. content: '';
  193. position: absolute;
  194. bottom: 20rpx;
  195. left: 50%;
  196. transform: translateX(-50%);
  197. width: 40rpx;
  198. height: 6rpx;
  199. background: linear-gradient(90deg, #FF8A57 0%, #F47D1F 100%);
  200. border-radius: 3rpx;
  201. }
  202. }
  203. }
  204. }
  205. .content {
  206. flex: 1;
  207. min-height: 0;
  208. padding: 24rpx 30rpx;
  209. padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  210. box-sizing: border-box;
  211. }
  212. .coupon-list {
  213. position: relative;
  214. .coupon-card-bg {
  215. position: absolute;
  216. top: 0;
  217. left: 0;
  218. width: 100%;
  219. height: 100%;
  220. z-index: 1;
  221. }
  222. .coupon-card-bgLeft {
  223. position: absolute;
  224. top: 0;
  225. left: 0;
  226. width: auto;
  227. height: 190rpx;
  228. z-index: 1;
  229. }
  230. .coupon-card-content {
  231. position: relative;
  232. z-index: 3;
  233. display: flex;
  234. align-items: center;
  235. }
  236. .coupon-card {
  237. display: flex;
  238. align-items: center;
  239. margin-bottom: 24rpx;
  240. overflow: hidden;
  241. position: relative;
  242. box-sizing: border-box;
  243. position: relative;
  244. z-index: 3;
  245. &:last-child {
  246. margin-bottom: 0;
  247. }
  248. &__left {
  249. width: 200rpx;
  250. padding: 32rpx 0;
  251. display: flex;
  252. flex-direction: column;
  253. align-items: center;
  254. justify-content: center;
  255. .amount-wrapper {
  256. display: flex;
  257. align-items: baseline;
  258. margin-bottom: 8rpx;
  259. .amount-number {
  260. font-size: 64rpx;
  261. font-weight: bold;
  262. color: #F47D1F;
  263. line-height: 1;
  264. }
  265. .amount-unit {
  266. font-size: 28rpx;
  267. color: #F47D1F;
  268. margin-left: 4rpx;
  269. }
  270. }
  271. .condition-text {
  272. font-size: 22rpx;
  273. color: #F47D1F;
  274. }
  275. }
  276. &__divider {
  277. width: 2rpx;
  278. height: 100%;
  279. position: relative;
  280. .dash-line {
  281. width: 2rpx;
  282. height: 100%;
  283. // background-image: linear-gradient(to bottom, #FFD9C2 0%, #FFD9C2 50%, transparent 50%, transparent 100%);
  284. background-size: 2rpx 12rpx;
  285. background-repeat: repeat-y;
  286. }
  287. }
  288. &__right {
  289. flex: 1;
  290. display: flex;
  291. align-items: center;
  292. justify-content: space-between;
  293. padding: 32rpx 24rpx;
  294. .coupon-info {
  295. flex: 1;
  296. display: flex;
  297. flex-direction: column;
  298. .coupon-name {
  299. font-size: 28rpx;
  300. font-weight: bold;
  301. color: #151515;
  302. margin-bottom: 12rpx;
  303. }
  304. .coupon-rules {
  305. font-size: 22rpx;
  306. color: #999999;
  307. margin-bottom: 12rpx;
  308. display: flex;
  309. align-items: center;
  310. .arrow {
  311. font-size: 28rpx;
  312. margin-left: 4rpx;
  313. }
  314. }
  315. .coupon-expire {
  316. font-size: 22rpx;
  317. color: #999999;
  318. }
  319. }
  320. .coupon-action {
  321. margin-left: 20rpx;
  322. .action-btn {
  323. width: 120rpx;
  324. height: 56rpx;
  325. border-radius: 28rpx;
  326. display: flex;
  327. align-items: center;
  328. justify-content: center;
  329. font-size: 26rpx;
  330. font-weight: 500;
  331. &--use {
  332. // background: linear-gradient(135deg, #FF8A57 0%, #F47D1F 100%);
  333. background: #F47D1F;
  334. color: #FFFFFF;
  335. // box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(255, 107, 53, 0.3);
  336. }
  337. }
  338. .status-text {
  339. font-size: 24rpx;
  340. padding: 8rpx 16rpx;
  341. &--unused,
  342. &--expiring {
  343. color: #999999;
  344. }
  345. &--used {
  346. color: #999999;
  347. }
  348. &--expired {
  349. color: #CCCCCC;
  350. }
  351. }
  352. }
  353. }
  354. // 已使用状态
  355. &--used {
  356. background: #F8F8F8;
  357. .coupon-card__left {
  358. .amount-number,
  359. .amount-unit,
  360. .condition-text {
  361. color: #CCCCCC;
  362. }
  363. }
  364. .coupon-card__right {
  365. .coupon-info {
  366. .coupon-name,
  367. .coupon-rules,
  368. .coupon-expire {
  369. color: #CCCCCC;
  370. }
  371. }
  372. }
  373. }
  374. // 已过期状态
  375. &--expired {
  376. background: #F8F8F8;
  377. .coupon-card__left {
  378. .amount-number,
  379. .amount-unit,
  380. .condition-text {
  381. color: #CCCCCC;
  382. }
  383. }
  384. .coupon-card__right {
  385. .coupon-info {
  386. .coupon-name,
  387. .coupon-rules,
  388. .coupon-expire {
  389. color: #CCCCCC;
  390. }
  391. }
  392. }
  393. }
  394. }
  395. }
  396. .hover-active {
  397. opacity: 0.8;
  398. transform: scale(0.98);
  399. }
  400. .empty-state {
  401. display: flex;
  402. flex-direction: column;
  403. align-items: center;
  404. justify-content: center;
  405. padding-top: 200rpx;
  406. .empty-icon {
  407. width: 300rpx;
  408. height: 280rpx;
  409. margin-bottom: 40rpx;
  410. }
  411. .empty-text {
  412. font-size: 28rpx;
  413. color: #AAAAAA;
  414. }
  415. }
  416. </style>