RulesModal.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 d = props.couponData?.expireDate;
  108. if (!d) return '';
  109. return `${d} 到期`;
  110. });
  111. const validityText = computed(() => {
  112. const s = props.couponData?.specifiedDay;
  113. if (s == null || s === '') return '—';
  114. const str = String(s).trim();
  115. if (str.includes('天')) return str;
  116. const n = Number(str);
  117. if (!Number.isNaN(n) && str !== '') return `${n}天`;
  118. return str;
  119. });
  120. const supplementText = computed(() => {
  121. const t = props.couponData?.supplementaryInstruction;
  122. if (t != null && String(t).trim() !== '') return String(t).trim();
  123. return '暂无说明';
  124. });
  125. const qrImageSrc = computed(() => {
  126. const raw = props.couponData?.qrCodeUrl ?? props.couponData?.qrcodeUrl ?? '';
  127. if (!raw) return '';
  128. if (typeof raw === 'string' && (raw.startsWith('http') || raw.startsWith('//'))) return raw;
  129. return getFileUrl(raw);
  130. });
  131. const qrPayload = computed(() => {
  132. const c = props.couponData || {};
  133. const id = c.id ?? c.userCouponId ?? c.couponId ?? '';
  134. const code = c.verificationCode ?? c.couponCode ?? '';
  135. const sid = uni.getStorageSync('currentStoreId') || '';
  136. if (code) return String(code);
  137. if (id) return `coupon:${id}:${sid}`;
  138. return 'coupon';
  139. });
  140. const qrCellRpx = computed(() => {
  141. const n = qrMatrix.value.length;
  142. if (!n) return 0;
  143. return qrBoxRpx / n;
  144. });
  145. function buildQrMatrix(text) {
  146. try {
  147. const qr = new UQRCode();
  148. qr.data = String(text || ' ');
  149. qr.size = 200;
  150. qr.make();
  151. const n = qr.moduleCount;
  152. const mods = qr.modules;
  153. if (!n || !mods) return [];
  154. const rows = [];
  155. for (let r = 0; r < n; r++) {
  156. const row = [];
  157. for (let c = 0; c < n; c++) {
  158. const cell = mods[r][c];
  159. const dark =
  160. typeof cell === 'object' && cell !== null ? !!cell.isBlack : !!cell;
  161. row.push(dark);
  162. }
  163. rows.push(row);
  164. }
  165. return rows;
  166. } catch (e) {
  167. console.warn('生成二维码失败:', e);
  168. return [];
  169. }
  170. }
  171. watch(
  172. () => ({
  173. open: props.open,
  174. id: props.couponData?.id,
  175. qrUrl: props.couponData?.qrCodeUrl ?? props.couponData?.qrcodeUrl,
  176. payload: qrPayload.value
  177. }),
  178. ({ open }) => {
  179. if (!open) {
  180. qrMatrix.value = [];
  181. return;
  182. }
  183. if (qrImageSrc.value) {
  184. qrMatrix.value = [];
  185. return;
  186. }
  187. qrMatrix.value = buildQrMatrix(qrPayload.value);
  188. },
  189. { flush: 'post', deep: true }
  190. );
  191. function handleClose() {
  192. getOpen.value = false;
  193. }
  194. </script>
  195. <style lang="scss" scoped>
  196. .rules-modal {
  197. width: 100%;
  198. background: #f5f5f5;
  199. border-radius: 24rpx 24rpx 0 0;
  200. padding: 28rpx 0 calc(28rpx + env(safe-area-inset-bottom));
  201. box-sizing: border-box;
  202. max-height: 85vh;
  203. }
  204. .rules-modal__header {
  205. display: flex;
  206. align-items: center;
  207. justify-content: center;
  208. margin-bottom: 24rpx;
  209. padding: 0 30rpx;
  210. position: relative;
  211. .header-title {
  212. font-size: 34rpx;
  213. font-weight: bold;
  214. color: #151515;
  215. }
  216. .close-btn {
  217. position: absolute;
  218. right: 24rpx;
  219. top: 50%;
  220. transform: translateY(-50%);
  221. width: 56rpx;
  222. height: 56rpx;
  223. display: flex;
  224. align-items: center;
  225. justify-content: center;
  226. }
  227. .close-icon {
  228. width: 28rpx;
  229. height: 28rpx;
  230. position: relative;
  231. &::before,
  232. &::after {
  233. content: '';
  234. position: absolute;
  235. width: 28rpx;
  236. height: 3rpx;
  237. background: #999999;
  238. top: 50%;
  239. left: 50%;
  240. transform-origin: center;
  241. }
  242. &::before {
  243. transform: translate(-50%, -50%) rotate(45deg);
  244. }
  245. &::after {
  246. transform: translate(-50%, -50%) rotate(-45deg);
  247. }
  248. }
  249. }
  250. .rules-modal__body {
  251. padding: 0 24rpx;
  252. box-sizing: border-box;
  253. }
  254. .card {
  255. background: #ffffff;
  256. border-radius: 16rpx;
  257. margin-bottom: 20rpx;
  258. overflow: hidden;
  259. box-sizing: border-box;
  260. &:last-child {
  261. margin-bottom: 0;
  262. }
  263. }
  264. .card-title {
  265. display: block;
  266. font-size: 30rpx;
  267. font-weight: bold;
  268. color: #151515;
  269. margin-bottom: 20rpx;
  270. }
  271. /* 券摘要 */
  272. .card--summary {
  273. display: flex;
  274. flex-direction: row;
  275. align-items: stretch;
  276. min-height: 168rpx;
  277. padding: 0;
  278. }
  279. .summary-left {
  280. width: 220rpx;
  281. flex-shrink: 0;
  282. background: #fff4e6;
  283. display: flex;
  284. flex-direction: column;
  285. align-items: center;
  286. justify-content: center;
  287. padding: 24rpx 16rpx;
  288. box-sizing: border-box;
  289. }
  290. .amount-line {
  291. display: flex;
  292. flex-direction: row;
  293. align-items: baseline;
  294. margin-bottom: 10rpx;
  295. }
  296. .amount-num {
  297. font-size: 56rpx;
  298. font-weight: bold;
  299. color: #f47d1f;
  300. line-height: 1;
  301. }
  302. .amount-unit {
  303. font-size: 28rpx;
  304. color: #f47d1f;
  305. margin-left: 4rpx;
  306. }
  307. .summary-condition {
  308. font-size: 22rpx;
  309. color: #f47d1f;
  310. }
  311. .summary-right {
  312. flex: 1;
  313. min-width: 0;
  314. padding: 28rpx 24rpx;
  315. display: flex;
  316. flex-direction: column;
  317. justify-content: center;
  318. background: #ffffff;
  319. }
  320. .summary-name {
  321. font-size: 28rpx;
  322. font-weight: bold;
  323. color: #151515;
  324. line-height: 1.4;
  325. margin-bottom: 12rpx;
  326. }
  327. .summary-expire {
  328. font-size: 24rpx;
  329. color: #999999;
  330. }
  331. /* 使用凭证 */
  332. .card--voucher {
  333. padding: 28rpx 24rpx 32rpx;
  334. }
  335. .qr-wrap {
  336. display: flex;
  337. align-items: center;
  338. justify-content: center;
  339. padding: 8rpx 0 4rpx;
  340. }
  341. .qr-image {
  342. display: block;
  343. }
  344. .qr-grid {
  345. background: #ffffff;
  346. overflow: hidden;
  347. }
  348. .qr-row {
  349. display: flex;
  350. flex-direction: row;
  351. }
  352. .qr-cell {
  353. flex-shrink: 0;
  354. box-sizing: border-box;
  355. background: #ffffff;
  356. &--dark {
  357. background: #151515;
  358. }
  359. }
  360. .qr-placeholder {
  361. font-size: 26rpx;
  362. color: #aaaaaa;
  363. }
  364. /* 使用须知 */
  365. .card--notice {
  366. padding: 28rpx 24rpx 32rpx;
  367. }
  368. .notice-row {
  369. display: flex;
  370. flex-direction: row;
  371. align-items: flex-start;
  372. justify-content: space-between;
  373. margin-bottom: 20rpx;
  374. font-size: 28rpx;
  375. line-height: 1.5;
  376. &:last-child {
  377. margin-bottom: 0;
  378. }
  379. }
  380. .notice-label {
  381. color: #888888;
  382. flex-shrink: 0;
  383. margin-right: 24rpx;
  384. }
  385. .notice-value {
  386. flex: 1;
  387. text-align: right;
  388. color: #151515;
  389. word-break: break-all;
  390. }
  391. </style>