SelectCouponModal.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <BasicModal type="bottom" v-model:open="getOpen" :isMack="true">
  3. <view class="select-coupon-modal">
  4. <!-- 顶部标题栏 -->
  5. <view class="select-coupon-modal__header">
  6. <text class="header-title">优惠券</text>
  7. <view class="header-close" @click="handleClose">
  8. <image :src="getFileUrl('img/icon/close.png')" mode="aspectFill" class="close-icon"></image>
  9. </view>
  10. </view>
  11. <!-- 优惠券列表:仅查看模式下不展示选中态、点击不触发选择 -->
  12. <scroll-view
  13. v-if="couponList.length > 0"
  14. class="select-coupon-modal__list"
  15. scroll-y
  16. :lower-threshold="100"
  17. @scrolltolower="onScrollToLower"
  18. >
  19. <view
  20. v-for="(coupon, index) in couponList"
  21. :key="'coupon-row-' + index + '-' + String(coupon.id ?? '')"
  22. class="coupon-card"
  23. :class="{ 'coupon-card--view-only': viewOnly }"
  24. @click="handleCardClick(coupon, index)"
  25. >
  26. <!-- 左侧金额信息:数字大号,“元/折”小号 -->
  27. <view class="coupon-card__left">
  28. <view class="amount-row">
  29. <text class="amount-num">{{ coupon.amount }}</text>
  30. <text class="amount-unit">{{ coupon.amountUnit || '元' }}</text>
  31. </view>
  32. <text class="condition-text">{{ coupon.conditionText || ((coupon.minAmount && Number(coupon.minAmount) > 0) ? ('满' + coupon.minAmount + '可用') : '无门槛') }}</text>
  33. </view>
  34. <!-- 中间信息 -->
  35. <view class="coupon-card__center">
  36. <text class="name-text">{{ coupon.name }}</text>
  37. <text class="expire-text">{{ formatExpireLine(coupon) }}</text>
  38. </view>
  39. <!-- 右侧单选(仅在选择模式下显示) -->
  40. <view class="coupon-card__right" v-if="!viewOnly">
  41. <image :src="getFileUrl('img/icon/sele1.png')" mode="widthFix" class="selected-icon" v-show="!isSelected(coupon)"></image>
  42. <image :src="getFileUrl('img/icon/sele2.png')" mode="widthFix" class="selected-icon" v-show="isSelected(coupon)"></image>
  43. </view>
  44. </view>
  45. <view v-if="listLoading || !listHasMore" class="coupon-list-footer">
  46. <text v-if="listLoading" class="coupon-list-footer__text">加载中...</text>
  47. <text v-else class="coupon-list-footer__text">没有更多了</text>
  48. </view>
  49. </scroll-view>
  50. <!-- 无数据时显示 -->
  51. <view class="select-coupon-modal__empty" v-else>
  52. <text class="empty-text">您可以去应用市场下载U店在哪APP领取优惠劵</text>
  53. </view>
  54. </view>
  55. </BasicModal>
  56. </template>
  57. <script setup>
  58. import { computed, ref } from 'vue';
  59. import BasicModal from '@/components/Modal/BasicModal.vue';
  60. import { getFileUrl } from '@/utils/file.js';
  61. const props = defineProps({
  62. open: {
  63. type: Boolean,
  64. default: false
  65. },
  66. couponList: {
  67. type: Array,
  68. default: () => []
  69. },
  70. selectedCouponId: {
  71. type: [Number, String],
  72. default: null
  73. },
  74. // 仅查看模式:来自左下角优惠券入口,只展示列表,不可选择
  75. viewOnly: {
  76. type: Boolean,
  77. default: false
  78. },
  79. listLoading: {
  80. type: Boolean,
  81. default: false
  82. },
  83. listHasMore: {
  84. type: Boolean,
  85. default: true
  86. }
  87. });
  88. const emit = defineEmits(['update:open', 'select', 'close', 'load-more']);
  89. const getOpen = computed({
  90. get: () => props.open,
  91. set: (val) => emit('update:open', val)
  92. });
  93. const formatExpireLine = (coupon) => {
  94. if (Number(coupon?.longTermValid) === 1) return '长期有效';
  95. const d = coupon?.expirationTime ?? coupon?.expireDate;
  96. const f = formatExpireDate(d);
  97. return f ? `${f}到期` : '';
  98. };
  99. // 格式化到期日期
  100. const formatExpireDate = (date) => {
  101. if (!date) return '';
  102. // 如果是字符串格式的日期,转换为 YYYY/MM/DD 格式
  103. if (typeof date === 'string') {
  104. const d = new Date(date);
  105. if (!isNaN(d.getTime())) {
  106. const year = d.getFullYear();
  107. const month = String(d.getMonth() + 1).padStart(2, '0');
  108. const day = String(d.getDate()).padStart(2, '0');
  109. return `${year}/${month}/${day}`;
  110. }
  111. // 如果已经是 YYYY/MM/DD 格式,直接返回
  112. if (date.includes('/')) {
  113. return date;
  114. }
  115. }
  116. return date;
  117. };
  118. // 判断优惠券是否被选中(统一转字符串比较,避免 number/string 不一致)
  119. const isSelected = (coupon) => {
  120. if (props.selectedCouponId == null || props.selectedCouponId === '') return false;
  121. return String(coupon.id) === String(props.selectedCouponId);
  122. };
  123. // 卡片点击:仅查看模式下不触发选择;否则按单选逻辑
  124. const handleCardClick = (coupon, index) => {
  125. if (props.viewOnly) return;
  126. const newSelectedId = isSelected(coupon) ? null : coupon.id;
  127. emit('select', { coupon, index, selectedId: newSelectedId });
  128. };
  129. const onScrollToLower = () => {
  130. if (props.listLoading || !props.listHasMore) return;
  131. emit('load-more');
  132. };
  133. // 处理关闭
  134. const handleClose = () => {
  135. getOpen.value = false;
  136. emit('close');
  137. };
  138. </script>
  139. <style scoped lang="scss">
  140. .select-coupon-modal {
  141. width: 100%;
  142. background: #F2F4F8;
  143. border-radius: 24rpx 24rpx 0 0;
  144. padding: 0;
  145. box-sizing: border-box;
  146. height: 55vh;
  147. max-height: 88vh;
  148. display: flex;
  149. flex-direction: column;
  150. overflow: hidden;
  151. &__header {
  152. position: relative;
  153. display: flex;
  154. align-items: center;
  155. justify-content: center;
  156. height: 88rpx;
  157. background: #F2F4F8;
  158. .header-title {
  159. font-size: 32rpx;
  160. font-weight: bold;
  161. color: #151515;
  162. }
  163. .header-close {
  164. position: absolute;
  165. right: 30rpx;
  166. top: 50%;
  167. transform: translateY(-50%);
  168. width: 60rpx;
  169. height: 60rpx;
  170. display: flex;
  171. align-items: center;
  172. justify-content: center;
  173. .close-icon {
  174. width: 26rpx;
  175. height: 26rpx;
  176. }
  177. }
  178. }
  179. &__list {
  180. flex: 1;
  181. padding: 24rpx 30rpx;
  182. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  183. box-sizing: border-box;
  184. overflow-y: auto;
  185. min-height: 400rpx;
  186. }
  187. .coupon-list-footer {
  188. padding: 24rpx 0 32rpx;
  189. text-align: center;
  190. }
  191. .coupon-list-footer__text {
  192. font-size: 24rpx;
  193. color: #999999;
  194. }
  195. &__empty {
  196. flex: 1;
  197. display: flex;
  198. align-items: center;
  199. justify-content: center;
  200. padding: 80rpx 30rpx;
  201. min-height: 400rpx;
  202. background: transparent;
  203. margin: 24rpx 30rpx 100rpx 30rpx;
  204. box-sizing: border-box;
  205. .empty-text {
  206. font-size: 30rpx;
  207. color: #666666;
  208. }
  209. }
  210. }
  211. .coupon-card {
  212. height: 191rpx;
  213. background: #FFFFFF;
  214. // border: 1rpx solid #E5E5E5;
  215. display: flex;
  216. align-items: center;
  217. border-radius: 24rpx;
  218. padding: 28rpx 24rpx;
  219. margin-bottom: 22rpx;
  220. box-sizing: border-box;
  221. transition: all 0.3s;
  222. &:last-child {
  223. margin-bottom: 0;
  224. }
  225. &:active {
  226. opacity: 0.8;
  227. }
  228. &--view-only:active {
  229. opacity: 1;
  230. }
  231. &__left {
  232. display: flex;
  233. flex-direction: column;
  234. align-items: center;
  235. margin-right: 24rpx;
  236. min-width: 120rpx;
  237. text-align: center;
  238. .amount-row {
  239. display: flex;
  240. align-items: baseline;
  241. justify-content: center;
  242. margin-bottom: 8rpx;
  243. }
  244. .amount-num {
  245. font-size: 50rpx;
  246. font-weight: 600;
  247. color: #F47D1F;
  248. line-height: 1.2;
  249. }
  250. .amount-unit {
  251. font-size: 28rpx;
  252. font-weight: 500;
  253. color: #F47D1F;
  254. margin-left: 2rpx;
  255. }
  256. .condition-text {
  257. font-size: 24rpx;
  258. color: #F47D1F;
  259. line-height: 1.2;
  260. }
  261. }
  262. &__center {
  263. flex: 1;
  264. display: flex;
  265. flex-direction: column;
  266. align-items: flex-start;
  267. margin-right: 20rpx;
  268. .name-text {
  269. font-size: 28rpx;
  270. color: #151515;
  271. font-weight: bold;
  272. line-height: 1.4;
  273. margin-bottom: 8rpx;
  274. }
  275. .expire-text {
  276. font-size: 22rpx;
  277. color: #666666;
  278. line-height: 1.4;
  279. margin-top: 20rpx;
  280. }
  281. }
  282. &__right {
  283. display: flex;
  284. align-items: center;
  285. justify-content: center;
  286. min-width: 60rpx;
  287. .selected-icon {
  288. width: 38rpx;
  289. height: 38rpx;
  290. }
  291. }
  292. }
  293. </style>