SelectCouponModal.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.id || index"
  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">{{ amountNum(coupon) }}</text>
  24. <text class="amount-unit">{{ amountUnit(coupon) }}</text>
  25. </view>
  26. <text class="condition-text">{{ (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">{{ formatExpireDate(coupon.expireDate) }}到期</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">暂无优惠券</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. // 格式化到期日期
  76. const formatExpireDate = (date) => {
  77. if (!date) return '';
  78. // 如果是字符串格式的日期,转换为 YYYY/MM/DD 格式
  79. if (typeof date === 'string') {
  80. const d = new Date(date);
  81. if (!isNaN(d.getTime())) {
  82. const year = d.getFullYear();
  83. const month = String(d.getMonth() + 1).padStart(2, '0');
  84. const day = String(d.getDate()).padStart(2, '0');
  85. return `${year}/${month}/${day}`;
  86. }
  87. // 如果已经是 YYYY/MM/DD 格式,直接返回
  88. if (date.includes('/')) {
  89. return date;
  90. }
  91. }
  92. return date;
  93. };
  94. // 金额数字部分(用于与“元/折”分开展示,单位用小字号)
  95. const amountNum = (coupon) => {
  96. const str = coupon.amountDisplay || (coupon.amount + '元');
  97. return str.length > 0 ? str.slice(0, -1) : '';
  98. };
  99. const amountUnit = (coupon) => {
  100. const str = coupon.amountDisplay || (coupon.amount + '元');
  101. return str.length > 0 ? str.slice(-1) : '元';
  102. };
  103. // 判断优惠券是否被选中(统一转字符串比较,避免 number/string 不一致)
  104. const isSelected = (coupon) => {
  105. if (props.selectedCouponId == null || props.selectedCouponId === '') return false;
  106. return String(coupon.id) === String(props.selectedCouponId);
  107. };
  108. // 卡片点击:仅查看模式下不触发选择;否则按单选逻辑
  109. const handleCardClick = (coupon, index) => {
  110. if (props.viewOnly) return;
  111. const newSelectedId = isSelected(coupon) ? null : coupon.id;
  112. emit('select', { coupon, index, selectedId: newSelectedId });
  113. };
  114. // 处理关闭
  115. const handleClose = () => {
  116. getOpen.value = false;
  117. emit('close');
  118. };
  119. </script>
  120. <style scoped lang="scss">
  121. .select-coupon-modal {
  122. width: 100%;
  123. background: #F2F4F8;
  124. border-radius: 24rpx 24rpx 0 0;
  125. padding: 0;
  126. box-sizing: border-box;
  127. height: 55vh;
  128. max-height: 88vh;
  129. display: flex;
  130. flex-direction: column;
  131. overflow: hidden;
  132. &__header {
  133. position: relative;
  134. display: flex;
  135. align-items: center;
  136. justify-content: center;
  137. height: 88rpx;
  138. background: #F2F4F8;
  139. .header-title {
  140. font-size: 32rpx;
  141. font-weight: bold;
  142. color: #151515;
  143. }
  144. .header-close {
  145. position: absolute;
  146. right: 30rpx;
  147. top: 50%;
  148. transform: translateY(-50%);
  149. width: 60rpx;
  150. height: 60rpx;
  151. display: flex;
  152. align-items: center;
  153. justify-content: center;
  154. .close-icon {
  155. width: 26rpx;
  156. height: 26rpx;
  157. }
  158. }
  159. }
  160. &__list {
  161. flex: 1;
  162. padding: 24rpx 30rpx;
  163. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  164. box-sizing: border-box;
  165. overflow-y: auto;
  166. min-height: 400rpx;
  167. }
  168. &__empty {
  169. flex: 1;
  170. display: flex;
  171. align-items: center;
  172. justify-content: center;
  173. padding: 80rpx 30rpx;
  174. min-height: 400rpx;
  175. background: transparent;
  176. margin: 24rpx 30rpx 100rpx 30rpx;
  177. box-sizing: border-box;
  178. .empty-text {
  179. font-size: 30rpx;
  180. color: #666666;
  181. }
  182. }
  183. }
  184. .coupon-card {
  185. height: 191rpx;
  186. background: #FFFFFF;
  187. // border: 1rpx solid #E5E5E5;
  188. display: flex;
  189. align-items: center;
  190. border-radius: 24rpx;
  191. padding: 28rpx 24rpx;
  192. margin-bottom: 22rpx;
  193. box-sizing: border-box;
  194. transition: all 0.3s;
  195. &:last-child {
  196. margin-bottom: 0;
  197. }
  198. &:active {
  199. opacity: 0.8;
  200. }
  201. &--view-only:active {
  202. opacity: 1;
  203. }
  204. &__left {
  205. display: flex;
  206. flex-direction: column;
  207. align-items: center;
  208. margin-right: 24rpx;
  209. min-width: 120rpx;
  210. text-align: center;
  211. .amount-row {
  212. display: flex;
  213. align-items: baseline;
  214. justify-content: center;
  215. margin-bottom: 8rpx;
  216. }
  217. .amount-num {
  218. font-size: 50rpx;
  219. font-weight: 600;
  220. color: #F47D1F;
  221. line-height: 1.2;
  222. }
  223. .amount-unit {
  224. font-size: 28rpx;
  225. font-weight: 500;
  226. color: #F47D1F;
  227. margin-left: 2rpx;
  228. }
  229. .condition-text {
  230. font-size: 24rpx;
  231. color: #F47D1F;
  232. line-height: 1.2;
  233. }
  234. }
  235. &__center {
  236. flex: 1;
  237. display: flex;
  238. flex-direction: column;
  239. align-items: flex-start;
  240. margin-right: 20rpx;
  241. .name-text {
  242. font-size: 28rpx;
  243. color: #151515;
  244. font-weight: bold;
  245. line-height: 1.4;
  246. margin-bottom: 8rpx;
  247. }
  248. .expire-text {
  249. font-size: 22rpx;
  250. color: #666666;
  251. line-height: 1.4;
  252. margin-top: 20rpx;
  253. }
  254. }
  255. &__right {
  256. display: flex;
  257. align-items: center;
  258. justify-content: center;
  259. min-width: 60rpx;
  260. .selected-icon {
  261. width: 38rpx;
  262. height: 38rpx;
  263. }
  264. }
  265. }
  266. </style>