SelectCouponModal.vue 7.3 KB

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