index.vue 10 KB

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