SelectCouponModal.vue 9.0 KB

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