SelectCouponModal.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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>
  13. <view
  14. v-for="(coupon, index) in couponList"
  15. :key="coupon.id || index"
  16. class="coupon-card"
  17. @click="handleSelect(coupon, index)"
  18. >
  19. <!-- 左侧金额信息 -->
  20. <view class="coupon-card__left">
  21. <text class="amount-text">{{ coupon.amount }}元</text>
  22. <text class="condition-text">满{{ coupon.minAmount }}可用</text>
  23. </view>
  24. <!-- 中间信息 -->
  25. <view class="coupon-card__center">
  26. <text class="name-text">{{ coupon.name }}</text>
  27. <text class="expire-text">{{ formatExpireDate(coupon.expireDate) }}到期</text>
  28. </view>
  29. <!-- 右侧复选框 -->
  30. <view class="coupon-card__right">
  31. <image :src="getFileUrl('img/icon/sele1.png')" mode="widthFix" class="selected-icon" v-show="isSelected(coupon)"></image>
  32. <image :src="getFileUrl('img/icon/sele2.png')" mode="widthFix" class="selected-icon" v-show="!isSelected(coupon)"></image>
  33. </view>
  34. </view>
  35. </scroll-view>
  36. </view>
  37. </BasicModal>
  38. </template>
  39. <script setup>
  40. import { computed, ref } from 'vue';
  41. import BasicModal from '@/components/Modal/BasicModal.vue';
  42. import { getFileUrl } from '@/utils/file.js';
  43. const props = defineProps({
  44. open: {
  45. type: Boolean,
  46. default: false
  47. },
  48. couponList: {
  49. type: Array,
  50. default: () => []
  51. },
  52. selectedCouponId: {
  53. type: [Number, String],
  54. default: null
  55. }
  56. });
  57. const emit = defineEmits(['update:open', 'select', 'close']);
  58. const getOpen = computed({
  59. get: () => props.open,
  60. set: (val) => emit('update:open', val)
  61. });
  62. // 格式化到期日期
  63. const formatExpireDate = (date) => {
  64. if (!date) return '';
  65. // 如果是字符串格式的日期,转换为 YYYY/MM/DD 格式
  66. if (typeof date === 'string') {
  67. const d = new Date(date);
  68. if (!isNaN(d.getTime())) {
  69. const year = d.getFullYear();
  70. const month = String(d.getMonth() + 1).padStart(2, '0');
  71. const day = String(d.getDate()).padStart(2, '0');
  72. return `${year}/${month}/${day}`;
  73. }
  74. // 如果已经是 YYYY/MM/DD 格式,直接返回
  75. if (date.includes('/')) {
  76. return date;
  77. }
  78. }
  79. return date;
  80. };
  81. // 判断优惠券是否被选中
  82. const isSelected = (coupon) => {
  83. return props.selectedCouponId !== null && coupon.id === props.selectedCouponId;
  84. };
  85. // 处理选择优惠券
  86. const handleSelect = (coupon, index) => {
  87. // 如果已经选中,则取消选择;否则选中
  88. const newSelectedId = isSelected(coupon) ? null : coupon.id;
  89. emit('select', { coupon, index, selectedId: newSelectedId });
  90. };
  91. // 处理关闭
  92. const handleClose = () => {
  93. getOpen.value = false;
  94. emit('close');
  95. };
  96. </script>
  97. <style scoped lang="scss">
  98. .select-coupon-modal {
  99. width: 100%;
  100. background: #F2F4F8;
  101. border-radius: 24rpx 24rpx 0 0;
  102. padding: 0;
  103. box-sizing: border-box;
  104. max-height: 80vh;
  105. display: flex;
  106. flex-direction: column;
  107. overflow: hidden;
  108. &__header {
  109. position: relative;
  110. display: flex;
  111. align-items: center;
  112. justify-content: center;
  113. height: 88rpx;
  114. background: #F2F4F8;
  115. .header-title {
  116. font-size: 32rpx;
  117. font-weight: bold;
  118. color: #151515;
  119. }
  120. .header-close {
  121. position: absolute;
  122. right: 30rpx;
  123. top: 50%;
  124. transform: translateY(-50%);
  125. width: 60rpx;
  126. height: 60rpx;
  127. display: flex;
  128. align-items: center;
  129. justify-content: center;
  130. .close-icon {
  131. width: 26rpx;
  132. height: 26rpx;
  133. }
  134. }
  135. }
  136. &__list {
  137. flex: 1;
  138. padding: 24rpx 30rpx;
  139. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  140. box-sizing: border-box;
  141. overflow-y: auto;
  142. min-height: 0;
  143. }
  144. }
  145. .coupon-card {
  146. height: 191rpx;
  147. background: #FFFFFF;
  148. // border: 1rpx solid #E5E5E5;
  149. display: flex;
  150. align-items: center;
  151. border-radius: 24rpx;
  152. padding: 28rpx 24rpx;
  153. margin-bottom: 22rpx;
  154. box-sizing: border-box;
  155. transition: all 0.3s;
  156. &:last-child {
  157. margin-bottom: 0;
  158. }
  159. &:active {
  160. opacity: 0.8;
  161. }
  162. &__left {
  163. display: flex;
  164. flex-direction: column;
  165. margin-right: 24rpx;
  166. min-width: 120rpx;
  167. text-align: center;
  168. .amount-text {
  169. font-size: 50rpx;
  170. font-weight: 600;
  171. color: #F47D1F;
  172. line-height: 1.2;
  173. margin-bottom: 8rpx;
  174. }
  175. .condition-text {
  176. font-size: 24rpx;
  177. color: #F47D1F;
  178. line-height: 1.2;
  179. }
  180. }
  181. &__center {
  182. flex: 1;
  183. display: flex;
  184. flex-direction: column;
  185. align-items: flex-start;
  186. margin-right: 20rpx;
  187. .name-text {
  188. font-size: 28rpx;
  189. color: #151515;
  190. font-weight: bold;
  191. line-height: 1.4;
  192. margin-bottom: 8rpx;
  193. }
  194. .expire-text {
  195. font-size: 22rpx;
  196. color: #666666;
  197. line-height: 1.4;
  198. margin-top: 20rpx;
  199. }
  200. }
  201. &__right {
  202. display: flex;
  203. align-items: center;
  204. justify-content: center;
  205. min-width: 60rpx;
  206. .selected-icon {
  207. width: 38rpx;
  208. height: 38rpx;
  209. }
  210. }
  211. }
  212. </style>