index.vue 10 KB

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