RulesModal.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <template>
  2. <BasicModal type="bottom" v-model:open="getOpen" :is-mack="true">
  3. <view class="rules-modal">
  4. <!-- 标题栏 -->
  5. <view class="rules-modal__header">
  6. <text class="header-title">使用规则</text>
  7. <view class="close-btn" @click="handleClose">
  8. <view class="close-icon" />
  9. </view>
  10. </view>
  11. <view class="rules-modal__body">
  12. <!-- 券摘要卡片:左金额区 + 右名称/到期 -->
  13. <view class="card card--summary">
  14. <view class="summary-left">
  15. <view class="amount-line">
  16. <text class="amount-num">{{ couponData?.amount ?? 0 }}</text>
  17. <text class="amount-unit">{{ couponData?.amountUnit || '元' }}</text>
  18. </view>
  19. <text class="summary-condition">{{ conditionLine }}</text>
  20. </view>
  21. <view class="summary-right">
  22. <text class="summary-name">{{ couponData?.name || '优惠券' }}</text>
  23. <text class="summary-expire">{{ expireLine }}</text>
  24. </view>
  25. </view>
  26. <!-- 使用凭证:二维码 -->
  27. <view class="card card--voucher">
  28. <text class="card-title">使用凭证</text>
  29. <view class="qr-wrap">
  30. <image
  31. v-if="qrImageSrc"
  32. :src="qrImageSrc"
  33. mode="aspectFit"
  34. class="qr-image"
  35. :style="{ width: qrBoxRpx + 'rpx', height: qrBoxRpx + 'rpx' }"
  36. />
  37. <view
  38. v-else-if="qrMatrix.length"
  39. class="qr-grid"
  40. :style="{ width: qrBoxRpx + 'rpx', height: qrBoxRpx + 'rpx' }"
  41. >
  42. <view
  43. v-for="(row, ri) in qrMatrix"
  44. :key="ri"
  45. class="qr-row"
  46. :style="{ height: qrCellRpx + 'rpx' }"
  47. >
  48. <view
  49. v-for="(dark, ci) in row"
  50. :key="ci"
  51. class="qr-cell"
  52. :style="{ width: qrCellRpx + 'rpx', height: qrCellRpx + 'rpx' }"
  53. :class="{ 'qr-cell--dark': dark }"
  54. />
  55. </view>
  56. </view>
  57. <view v-else class="qr-placeholder">暂无核销码</view>
  58. </view>
  59. </view>
  60. <!-- 使用须知 -->
  61. <view class="card card--notice">
  62. <text class="card-title">使用须知</text>
  63. <view class="notice-row">
  64. <text class="notice-label">有效期</text>
  65. <text class="notice-value">{{ validityText }}</text>
  66. </view>
  67. <view class="notice-row">
  68. <text class="notice-label">补充说明</text>
  69. <text class="notice-value">{{ supplementText }}</text>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. </BasicModal>
  75. </template>
  76. <script setup>
  77. import { computed, ref, watch } from 'vue';
  78. import BasicModal from '@/components/Modal/BasicModal.vue';
  79. import { getFileUrl } from '@/utils/file.js';
  80. import UQRCodeModule from 'uqrcodejs';
  81. const UQRCode = UQRCodeModule?.default || UQRCodeModule;
  82. const props = defineProps({
  83. open: {
  84. type: Boolean,
  85. default: false
  86. },
  87. couponData: {
  88. type: Object,
  89. default: () => ({})
  90. }
  91. });
  92. const emit = defineEmits(['update:open']);
  93. const getOpen = computed({
  94. get: () => props.open,
  95. set: (val) => emit('update:open', val)
  96. });
  97. const qrMatrix = ref([]);
  98. /** 二维码展示边长(rpx),约卡片宽 30%~40%,与设计稿一致 */
  99. const qrBoxRpx = 220;
  100. const conditionLine = computed(() => {
  101. const c = props.couponData;
  102. if (c?.conditionText) return c.conditionText;
  103. const m = Number(c?.minAmount) || 0;
  104. return m > 0 ? `满${m}可用` : '无门槛';
  105. });
  106. const expireLine = computed(() => {
  107. const c = props.couponData;
  108. if (Number(c?.longTermValid) === 1) return '长期有效';
  109. const d = c?.expirationTime ?? c?.expireDate;
  110. if (d == null || String(d).trim() === '') return '';
  111. return `${String(d).trim()}到期`;
  112. });
  113. /** 从 specifiedDay / validDays 解析天数,用于「领取后*天有效」 */
  114. function parseReceiveValidDays(c) {
  115. const s = c?.specifiedDay ?? c?.validDays;
  116. if (s == null || String(s).trim() === '') return null;
  117. const str = String(s).trim();
  118. const m1 = str.match(/领取后\s*(\d+)\s*天/);
  119. if (m1) {
  120. const n = Number(m1[1]);
  121. return !Number.isNaN(n) && n > 0 ? n : null;
  122. }
  123. const m2 = str.match(/(\d+)\s*天/);
  124. if (m2) {
  125. const n = Number(m2[1]);
  126. return !Number.isNaN(n) && n > 0 ? n : null;
  127. }
  128. const n = Number(str);
  129. if (!Number.isNaN(n) && n > 0) return n;
  130. return null;
  131. }
  132. const validityText = computed(() => {
  133. const c = props.couponData;
  134. if (Number(c?.longTermValid) === 1) return '长期有效';
  135. const days = parseReceiveValidDays(c);
  136. if (days != null) return `领取后${days}天有效`;
  137. return '—';
  138. });
  139. const supplementText = computed(() => {
  140. const t = props.couponData?.supplementaryInstruction;
  141. if (t != null && String(t).trim() !== '') return String(t).trim();
  142. return '暂无说明';
  143. });
  144. const qrImageSrc = computed(() => {
  145. const raw = props.couponData?.qrCodeUrl ?? props.couponData?.qrcodeUrl ?? '';
  146. if (!raw) return '';
  147. if (typeof raw === 'string' && (raw.startsWith('http') || raw.startsWith('//'))) return raw;
  148. return getFileUrl(raw);
  149. });
  150. const qrPayload = computed(() => {
  151. const c = props.couponData || {};
  152. const id = c.id ?? c.userCouponId ?? c.couponId ?? '';
  153. const code = c.verificationCode ?? c.couponCode ?? '';
  154. const sid = uni.getStorageSync('currentStoreId') || '';
  155. if (code) return String(code);
  156. if (id) return `coupon:${id}:${sid}`;
  157. return 'coupon';
  158. });
  159. const qrCellRpx = computed(() => {
  160. const n = qrMatrix.value.length;
  161. if (!n) return 0;
  162. return qrBoxRpx / n;
  163. });
  164. function buildQrMatrix(text) {
  165. try {
  166. const qr = new UQRCode();
  167. qr.data = String(text || ' ');
  168. qr.size = 200;
  169. qr.make();
  170. const n = qr.moduleCount;
  171. const mods = qr.modules;
  172. if (!n || !mods) return [];
  173. const rows = [];
  174. for (let r = 0; r < n; r++) {
  175. const row = [];
  176. for (let c = 0; c < n; c++) {
  177. const cell = mods[r][c];
  178. const dark =
  179. typeof cell === 'object' && cell !== null ? !!cell.isBlack : !!cell;
  180. row.push(dark);
  181. }
  182. rows.push(row);
  183. }
  184. return rows;
  185. } catch (e) {
  186. console.warn('生成二维码失败:', e);
  187. return [];
  188. }
  189. }
  190. watch(
  191. () => ({
  192. open: props.open,
  193. id: props.couponData?.id,
  194. qrUrl: props.couponData?.qrCodeUrl ?? props.couponData?.qrcodeUrl,
  195. payload: qrPayload.value
  196. }),
  197. ({ open }) => {
  198. if (!open) {
  199. qrMatrix.value = [];
  200. return;
  201. }
  202. if (qrImageSrc.value) {
  203. qrMatrix.value = [];
  204. return;
  205. }
  206. qrMatrix.value = buildQrMatrix(qrPayload.value);
  207. },
  208. { flush: 'post', deep: true }
  209. );
  210. function handleClose() {
  211. getOpen.value = false;
  212. }
  213. </script>
  214. <style lang="scss" scoped>
  215. .rules-modal {
  216. width: 100%;
  217. background: #f5f5f5;
  218. border-radius: 24rpx 24rpx 0 0;
  219. padding: 28rpx 0 calc(28rpx + env(safe-area-inset-bottom));
  220. box-sizing: border-box;
  221. max-height: 85vh;
  222. }
  223. .rules-modal__header {
  224. display: flex;
  225. align-items: center;
  226. justify-content: center;
  227. margin-bottom: 24rpx;
  228. padding: 0 30rpx;
  229. position: relative;
  230. .header-title {
  231. font-size: 34rpx;
  232. font-weight: bold;
  233. color: #151515;
  234. }
  235. .close-btn {
  236. position: absolute;
  237. right: 24rpx;
  238. top: 50%;
  239. transform: translateY(-50%);
  240. width: 56rpx;
  241. height: 56rpx;
  242. display: flex;
  243. align-items: center;
  244. justify-content: center;
  245. }
  246. .close-icon {
  247. width: 28rpx;
  248. height: 28rpx;
  249. position: relative;
  250. &::before,
  251. &::after {
  252. content: '';
  253. position: absolute;
  254. width: 28rpx;
  255. height: 3rpx;
  256. background: #999999;
  257. top: 50%;
  258. left: 50%;
  259. transform-origin: center;
  260. }
  261. &::before {
  262. transform: translate(-50%, -50%) rotate(45deg);
  263. }
  264. &::after {
  265. transform: translate(-50%, -50%) rotate(-45deg);
  266. }
  267. }
  268. }
  269. .rules-modal__body {
  270. padding: 0 24rpx;
  271. box-sizing: border-box;
  272. }
  273. .card {
  274. background: #ffffff;
  275. border-radius: 16rpx;
  276. margin-bottom: 20rpx;
  277. overflow: hidden;
  278. box-sizing: border-box;
  279. &:last-child {
  280. margin-bottom: 0;
  281. }
  282. }
  283. .card-title {
  284. display: block;
  285. font-size: 30rpx;
  286. font-weight: bold;
  287. color: #151515;
  288. margin-bottom: 20rpx;
  289. }
  290. /* 券摘要 */
  291. .card--summary {
  292. display: flex;
  293. flex-direction: row;
  294. align-items: stretch;
  295. min-height: 168rpx;
  296. padding: 0;
  297. }
  298. .summary-left {
  299. width: 220rpx;
  300. flex-shrink: 0;
  301. background: #fff4e6;
  302. display: flex;
  303. flex-direction: column;
  304. align-items: center;
  305. justify-content: center;
  306. padding: 24rpx 16rpx;
  307. box-sizing: border-box;
  308. }
  309. .amount-line {
  310. display: flex;
  311. flex-direction: row;
  312. align-items: baseline;
  313. margin-bottom: 10rpx;
  314. }
  315. .amount-num {
  316. font-size: 56rpx;
  317. font-weight: bold;
  318. color: #f47d1f;
  319. line-height: 1;
  320. }
  321. .amount-unit {
  322. font-size: 28rpx;
  323. color: #f47d1f;
  324. margin-left: 4rpx;
  325. }
  326. .summary-condition {
  327. font-size: 22rpx;
  328. color: #f47d1f;
  329. }
  330. .summary-right {
  331. flex: 1;
  332. min-width: 0;
  333. padding: 28rpx 24rpx;
  334. display: flex;
  335. flex-direction: column;
  336. justify-content: center;
  337. background: #ffffff;
  338. }
  339. .summary-name {
  340. font-size: 28rpx;
  341. font-weight: bold;
  342. color: #151515;
  343. line-height: 1.4;
  344. margin-bottom: 12rpx;
  345. }
  346. .summary-expire {
  347. font-size: 24rpx;
  348. color: #999999;
  349. }
  350. /* 使用凭证 */
  351. .card--voucher {
  352. padding: 28rpx 24rpx 32rpx;
  353. }
  354. .qr-wrap {
  355. display: flex;
  356. align-items: center;
  357. justify-content: center;
  358. padding: 8rpx 0 4rpx;
  359. }
  360. .qr-image {
  361. display: block;
  362. }
  363. .qr-grid {
  364. background: #ffffff;
  365. overflow: hidden;
  366. }
  367. .qr-row {
  368. display: flex;
  369. flex-direction: row;
  370. }
  371. .qr-cell {
  372. flex-shrink: 0;
  373. box-sizing: border-box;
  374. background: #ffffff;
  375. &--dark {
  376. background: #151515;
  377. }
  378. }
  379. .qr-placeholder {
  380. font-size: 26rpx;
  381. color: #aaaaaa;
  382. }
  383. /* 使用须知 */
  384. .card--notice {
  385. padding: 28rpx 24rpx 32rpx;
  386. }
  387. .notice-row {
  388. display: flex;
  389. flex-direction: row;
  390. align-items: flex-start;
  391. justify-content: space-between;
  392. margin-bottom: 20rpx;
  393. font-size: 28rpx;
  394. line-height: 1.5;
  395. &:last-child {
  396. margin-bottom: 0;
  397. }
  398. }
  399. .notice-label {
  400. color: #888888;
  401. flex-shrink: 0;
  402. margin-right: 24rpx;
  403. }
  404. .notice-value {
  405. flex: 1;
  406. text-align: right;
  407. color: #151515;
  408. word-break: break-all;
  409. }
  410. </style>