newCoupon.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <template>
  2. <!-- 优惠券管理 - 新建/编辑页面(参考 IssueCoupons;左:类型/名称/折扣或面值/低消/有效期,右:数量/补充说明) -->
  3. <div class="table-box" style="width: 100%; min-height: 100%; background-color: white">
  4. <div class="header">
  5. <el-button @click="goBack"> 返回 </el-button>
  6. <h2 class="title">
  7. {{ type == "add" ? "新建优惠券" : "编辑优惠券" }}
  8. </h2>
  9. </div>
  10. <el-form :model="couponModel" ref="ruleFormRef" :rules="rules" label-width="200px" class="formBox">
  11. <div class="content">
  12. <!-- 左侧:优惠券类型、名称、折扣/面值、低消、有效期(领取后天数) -->
  13. <div class="contentLeft">
  14. <!-- 优惠券类型 -->
  15. <el-form-item label="优惠券类型" prop="couponType">
  16. <el-radio-group v-model="couponModel.couponType" class="radio-group">
  17. <el-radio :value="2"> 折扣券 </el-radio>
  18. <el-radio :value="1"> 满减券 </el-radio>
  19. </el-radio-group>
  20. </el-form-item>
  21. <!-- 优惠券名称 -->
  22. <el-form-item label="优惠券名称" prop="name">
  23. <el-input maxlength="50" v-model="couponModel.name" placeholder="请输入" clearable />
  24. </el-form-item>
  25. <!-- 折扣力度(折扣券) -->
  26. <el-form-item v-if="couponModel.couponType === 2" label="折扣力度" prop="discountRate">
  27. <el-input v-model="couponModel.discountRate" maxlength="4" placeholder="请输入0.1-9.9之间的数字" clearable>
  28. <template #suffix> 折 </template>
  29. </el-input>
  30. </el-form-item>
  31. <!-- 面值(满减券) -->
  32. <el-form-item v-if="couponModel.couponType === 1" label="面值(¥)" prop="nominalValue">
  33. <el-input v-model="couponModel.nominalValue" maxlength="15" placeholder="请输入" clearable />
  34. </el-form-item>
  35. <!-- 是否有低消 -->
  36. <el-form-item label="是否有低消" prop="hasMinimumSpend">
  37. <el-radio-group v-model="couponModel.hasMinimumSpend" class="radio-group">
  38. <el-radio :value="1"> 是 </el-radio>
  39. <el-radio :value="0"> 否 </el-radio>
  40. </el-radio-group>
  41. </el-form-item>
  42. <!-- 最低消费金额(元可用) -->
  43. <el-form-item v-if="couponModel.hasMinimumSpend === 1" label="" prop="minimumSpendingAmount" class="no-label">
  44. <el-input v-model="couponModel.minimumSpendingAmount" maxlength="15" placeholder="请输入" clearable>
  45. <template #suffix> 元可用 </template>
  46. </el-input>
  47. </el-form-item>
  48. <!-- 有效期:长期有效 / 自定义(领取后有效天数) -->
  49. <el-form-item label="有效期" prop="validityMode">
  50. <el-radio-group v-model="couponModel.validityMode" class="radio-group">
  51. <el-radio :value="0"> 长期有效 </el-radio>
  52. <el-radio :value="1"> 自定义 </el-radio>
  53. </el-radio-group>
  54. </el-form-item>
  55. <el-form-item v-if="couponModel.validityMode === 1" label="领取后有效期" prop="validDaysAfterReceive">
  56. <el-input
  57. v-model="couponModel.validDaysAfterReceive"
  58. maxlength="5"
  59. placeholder="请输入正整数"
  60. clearable
  61. inputmode="numeric"
  62. @input="onValidDaysAfterReceiveInput"
  63. >
  64. <template #suffix> 天 </template>
  65. </el-input>
  66. </el-form-item>
  67. </div>
  68. <!-- 右侧:数量、补充说明 -->
  69. <div class="contentRight">
  70. <!-- 数量:不限 / 自定义 -->
  71. <el-form-item label="数量" prop="quantityMode">
  72. <el-radio-group v-model="couponModel.quantityMode" class="radio-group">
  73. <el-radio :value="0"> 不限 </el-radio>
  74. <el-radio :value="1"> 自定义 </el-radio>
  75. </el-radio-group>
  76. </el-form-item>
  77. <el-form-item v-if="couponModel.quantityMode === 1" label="请输入数量" prop="singleQty">
  78. <el-input
  79. v-model="couponModel.singleQty"
  80. maxlength="5"
  81. placeholder="请输入正整数"
  82. clearable
  83. inputmode="numeric"
  84. @input="onSingleQtyInput"
  85. >
  86. <template #suffix> 张 </template>
  87. </el-input>
  88. </el-form-item>
  89. <!-- 补充说明 -->
  90. <el-form-item label="补充说明" prop="supplementaryInstruction">
  91. <el-input
  92. maxlength="300"
  93. v-model="couponModel.supplementaryInstruction"
  94. :rows="4"
  95. type="textarea"
  96. placeholder="请输入"
  97. show-word-limit
  98. />
  99. </el-form-item>
  100. </div>
  101. </div>
  102. </el-form>
  103. <!-- 底部按钮区域 -->
  104. <div class="button-container">
  105. <!-- <el-button @click="() => handleSubmit('draft')"> 存草稿 </el-button> -->
  106. <el-button type="primary" @click="() => handleSubmit()">
  107. {{ type === "add" ? "新建优惠券" : "编辑优惠券" }}
  108. </el-button>
  109. </div>
  110. </div>
  111. </template>
  112. <script setup lang="tsx" name="newCoupon">
  113. /**
  114. * 优惠券管理 - 新建/编辑页面
  115. * 接口入参:longTermValid(1 长期有效 / 0 自定义天数)+ expirationDate(天);
  116. * unlimitedQty(1 不限 / 0 自定义张数)+ singleQty(张)
  117. */
  118. import { ref, reactive, watch, nextTick, onMounted } from "vue";
  119. import { ElMessage } from "element-plus";
  120. import { useRoute, useRouter } from "vue-router";
  121. import type { FormInstance } from "element-plus";
  122. import { getCouponDetail, addDiscountCoupon, editDiscountCoupon } from "@/api/modules/couponManagement";
  123. import { validatePositiveNumber, validatePositiveInteger, validatePriceFormat } from "@/utils/eleValidate";
  124. import { localGet } from "@/utils";
  125. const router = useRouter();
  126. const route = useRoute();
  127. const type = ref<string>("");
  128. const id = ref<string>("");
  129. // 折扣力度校验:0.1-9.9
  130. const validateDiscountRate = (_rule: any, value: any, callback: (err?: Error) => void) => {
  131. if (couponModel.value.couponType !== 2) {
  132. callback();
  133. return;
  134. }
  135. if (value === null || value === undefined || value === "") {
  136. callback(new Error("请输入折扣力度"));
  137. return;
  138. }
  139. const num = Number(value);
  140. if (isNaN(num) || num < 0.1 || num > 9.9) {
  141. callback(new Error("请输入0.1-9.9之间的数字"));
  142. return;
  143. }
  144. callback();
  145. };
  146. const rules = reactive({
  147. name: [{ required: true, message: "请输入优惠券名称" }],
  148. couponType: [{ required: true, message: "请选择优惠券类型" }],
  149. nominalValue: [
  150. {
  151. validator: (_rule: any, value: any, callback: (err?: Error) => void) => {
  152. if (couponModel.value.couponType !== 1) {
  153. callback();
  154. return;
  155. }
  156. if (!value || value.toString().trim() === "") {
  157. callback(new Error("请输入面值"));
  158. return;
  159. }
  160. const next = validatePriceFormat("整数部分最多6位,小数部分最多2位");
  161. next(_rule, value, callback);
  162. },
  163. trigger: "blur"
  164. }
  165. ],
  166. discountRate: [{ validator: validateDiscountRate, trigger: "blur" }],
  167. validityMode: [{ required: true, message: "请选择有效期类型" }],
  168. validDaysAfterReceive: [
  169. {
  170. validator: (_rule: any, value: any, callback: (err?: Error) => void) => {
  171. if (couponModel.value.validityMode !== 1) {
  172. callback();
  173. return;
  174. }
  175. if (value === null || value === undefined || value === "") {
  176. callback(new Error("请输入领取后有效期"));
  177. return;
  178. }
  179. const next = validatePositiveInteger("领取后有效期须为正整数", { required: false });
  180. next(_rule, value, callback);
  181. },
  182. trigger: ["blur", "change"]
  183. }
  184. ],
  185. quantityMode: [{ required: true, message: "请选择数量类型" }],
  186. singleQty: [
  187. {
  188. validator: (_rule: any, value: any, callback: (err?: Error) => void) => {
  189. if (couponModel.value.quantityMode !== 1) {
  190. callback();
  191. return;
  192. }
  193. if (value === null || value === undefined || value === "") {
  194. callback(new Error("请输入数量"));
  195. return;
  196. }
  197. const next = validatePositiveInteger("数量须为正整数", { required: false });
  198. next(_rule, value, callback);
  199. },
  200. trigger: ["blur", "change"]
  201. }
  202. ],
  203. minimumSpendingAmount: [
  204. {
  205. validator: (_rule: any, value: any, callback: (err?: Error) => void) => {
  206. if (couponModel.value.hasMinimumSpend !== 1) {
  207. callback();
  208. return;
  209. }
  210. if (value === null || value === undefined || value === "") {
  211. callback(new Error("请输入最低消费金额"));
  212. return;
  213. }
  214. const next = validatePositiveNumber("最低消费金额必须为正数");
  215. next(_rule, value, callback);
  216. },
  217. trigger: "blur"
  218. },
  219. {
  220. validator: (_rule: any, value: any, callback: (err?: Error) => void) => {
  221. if (couponModel.value.hasMinimumSpend !== 1 || !value) {
  222. callback();
  223. return;
  224. }
  225. const next = validatePriceFormat("整数部分最多6位,小数部分最多2位");
  226. next(_rule, value, callback);
  227. },
  228. trigger: "blur"
  229. }
  230. ]
  231. });
  232. // 1-满减券 2-折扣券(与 ticketManagement index couponTypeEnum 一致)
  233. // validityMode:0 长期有效 → longTermValid=1;1 自定义 → longTermValid=0 + expirationDate=天
  234. // quantityMode:0 不限 → unlimitedQty=1;1 自定义 → unlimitedQty=0 + singleQty=张数
  235. const couponModel = ref<any>({
  236. name: "",
  237. couponType: 2,
  238. nominalValue: "",
  239. discountRate: "",
  240. validityMode: 0,
  241. validDaysAfterReceive: "",
  242. quantityMode: 0,
  243. singleQty: "",
  244. claimRule: "day",
  245. hasMinimumSpend: 0,
  246. minimumSpendingAmount: "",
  247. supplementaryInstruction: ""
  248. });
  249. /** 仅允许数字,限制长度(正整数输入) */
  250. const sanitizeDigits = (raw: string, maxLen: number) =>
  251. String(raw ?? "")
  252. .replace(/\D/g, "")
  253. .slice(0, maxLen);
  254. const onValidDaysAfterReceiveInput = (val: string) => {
  255. couponModel.value.validDaysAfterReceive = sanitizeDigits(val, 5);
  256. };
  257. const onSingleQtyInput = (val: string) => {
  258. couponModel.value.singleQty = sanitizeDigits(val, 5);
  259. };
  260. const isInitializing = ref(true);
  261. watch(
  262. () => couponModel.value.validityMode,
  263. newVal => {
  264. if (isInitializing.value) return;
  265. if (newVal === 0) {
  266. couponModel.value.validDaysAfterReceive = "";
  267. nextTick(() => ruleFormRef.value?.clearValidate("validDaysAfterReceive"));
  268. }
  269. }
  270. );
  271. watch(
  272. () => couponModel.value.quantityMode,
  273. newVal => {
  274. if (isInitializing.value) return;
  275. if (newVal === 0) {
  276. couponModel.value.singleQty = "";
  277. nextTick(() => ruleFormRef.value?.clearValidate("singleQty"));
  278. }
  279. }
  280. );
  281. watch(
  282. () => couponModel.value.hasMinimumSpend,
  283. newVal => {
  284. if (isInitializing.value) return;
  285. if (newVal === 0) {
  286. couponModel.value.minimumSpendingAmount = "";
  287. nextTick(() => ruleFormRef.value?.clearValidate("minimumSpendingAmount"));
  288. }
  289. }
  290. );
  291. // 切换优惠券类型时清空另一类字段并清校验
  292. watch(
  293. () => couponModel.value.couponType,
  294. newVal => {
  295. if (isInitializing.value) return;
  296. if (newVal === 1) {
  297. couponModel.value.discountRate = "";
  298. nextTick(() => ruleFormRef.value?.clearValidate("discountRate"));
  299. } else {
  300. couponModel.value.nominalValue = "";
  301. nextTick(() => ruleFormRef.value?.clearValidate("nominalValue"));
  302. }
  303. }
  304. );
  305. onMounted(async () => {
  306. id.value = (route.query.id as string) || "";
  307. type.value = (route.query.type as string) || "add";
  308. if (type.value !== "add" && id.value) {
  309. const res: any = await getCouponDetail({ counponId: id.value });
  310. const data = res.data || {};
  311. const lt = data.longTermValid;
  312. const isLongByFlag = lt === 1 || lt === "1" || lt === true;
  313. const isLongLegacy =
  314. lt === undefined &&
  315. (() => {
  316. const sd = data.specifiedDay;
  317. const sdNum = Number(sd);
  318. return sd === null || sd === undefined || sd === "" || Number.isNaN(sdNum) || sdNum === 0;
  319. })();
  320. const isLongValidity = isLongByFlag || isLongLegacy;
  321. const expDays = data.expirationDate ?? data.specifiedDay;
  322. const uq = data.unlimitedQty;
  323. const isUnlimitedByFlag = uq === 1 || uq === "1" || uq === true;
  324. const isUnlimitedLegacy =
  325. uq === undefined &&
  326. (data.singleQty === null || data.singleQty === undefined || data.singleQty === "" || Number(data.singleQty) === 0);
  327. const isUnlimitedQty = isUnlimitedByFlag || isUnlimitedLegacy;
  328. couponModel.value = {
  329. ...couponModel.value,
  330. ...data,
  331. couponType: data.couponType ?? 1,
  332. discountRate: data.discountRate != null ? (Number(data.discountRate) / 10).toString() : "",
  333. hasMinimumSpend: Number(data.minimumSpendingAmount) > 0 ? 1 : 0,
  334. validityMode: isLongValidity ? 0 : 1,
  335. validDaysAfterReceive: isLongValidity ? "" : String(expDays ?? ""),
  336. quantityMode: isUnlimitedQty ? 0 : 1,
  337. singleQty: isUnlimitedQty ? "" : String(data.singleQty ?? "")
  338. };
  339. } else {
  340. couponModel.value.couponType = couponModel.value.couponType ?? 2;
  341. couponModel.value.validityMode = 0;
  342. couponModel.value.quantityMode = 0;
  343. couponModel.value.validDaysAfterReceive = "";
  344. couponModel.value.singleQty = "";
  345. }
  346. await nextTick();
  347. ruleFormRef.value?.clearValidate();
  348. isInitializing.value = false;
  349. });
  350. const goBack = () => {
  351. router.go(-1);
  352. };
  353. const ruleFormRef = ref<FormInstance>();
  354. const buildSubmitParams = (submitType?: string) => {
  355. const m = couponModel.value;
  356. const longTermValid = m.validityMode === 0 ? 1 : 0;
  357. const expirationDate = m.validityMode === 0 ? 0 : Number(m.validDaysAfterReceive) || 0;
  358. const unlimitedQty = m.quantityMode === 0 ? 1 : 0;
  359. const singleQty = m.quantityMode === 0 ? 0 : Number(m.singleQty) || 0;
  360. const params: any = {
  361. name: m.name,
  362. couponType: m.couponType,
  363. nominalValue: m.nominalValue,
  364. discountRate: m.discountRate,
  365. longTermValid,
  366. expirationDate,
  367. unlimitedQty,
  368. singleQty,
  369. claimRule: m.claimRule || "day",
  370. attentionCanReceived: 0,
  371. hasMinimumSpend: m.hasMinimumSpend,
  372. minimumSpendingAmount: m.minimumSpendingAmount,
  373. supplementaryInstruction: m.supplementaryInstruction,
  374. beginGetDate: "",
  375. endGetDate: "",
  376. storeId: localGet("createdId"),
  377. couponId: type.value === "edit" ? id.value : "",
  378. couponStatus: submitType ? 0 : 1,
  379. restrictedQuantity: 0,
  380. getStatus: 1,
  381. type: 1
  382. };
  383. return params;
  384. };
  385. const handleSubmit = async (submitType?: string) => {
  386. const params: any = buildSubmitParams(submitType);
  387. // 折扣券:discountRate 后端多为 1-100(如 85 表示 8.5 折),此处按 0.1-9.9 输入则乘 10
  388. if (params.couponType === 2 && params.discountRate !== "" && params.discountRate != null) {
  389. const rate = Number(params.discountRate);
  390. params.discountRate = !isNaN(rate) ? Math.round(rate * 10) : null;
  391. }
  392. if (params.couponType === 1) {
  393. params.discountRate = null;
  394. }
  395. if (!params.hasMinimumSpend) {
  396. params.minimumSpendingAmount = 0;
  397. }
  398. if (submitType) {
  399. if (!couponModel.value.name?.trim()) {
  400. ElMessage.warning("请填写优惠券名称");
  401. return;
  402. }
  403. if (type.value === "add") {
  404. const res: any = await addDiscountCoupon(params);
  405. if (res?.code == 200) {
  406. ElMessage.success("草稿保存成功");
  407. goBack();
  408. }
  409. } else {
  410. params.couponId = id.value;
  411. const res: any = await editDiscountCoupon(params);
  412. if (res?.code == 200) {
  413. ElMessage.success("草稿保存成功");
  414. goBack();
  415. }
  416. }
  417. return;
  418. }
  419. ruleFormRef.value!.validate(async (valid: boolean) => {
  420. if (!valid) return;
  421. if (type.value === "add") {
  422. const res: any = await addDiscountCoupon(params);
  423. if (res?.code == 200) {
  424. ElMessage.success("创建成功");
  425. goBack();
  426. }
  427. } else {
  428. params.couponId = id.value;
  429. const res: any = await editDiscountCoupon(params);
  430. if (res?.code == 200) {
  431. ElMessage.success("修改成功");
  432. goBack();
  433. }
  434. }
  435. });
  436. };
  437. </script>
  438. <style scoped lang="scss">
  439. .table-box {
  440. display: flex;
  441. flex-direction: column;
  442. height: auto !important;
  443. min-height: 100%;
  444. }
  445. .header {
  446. display: flex;
  447. align-items: center;
  448. padding: 20px;
  449. border-bottom: 1px solid #e4e7ed;
  450. }
  451. .title {
  452. flex: 1;
  453. margin: 0;
  454. font-size: 18px;
  455. font-weight: bold;
  456. text-align: center;
  457. }
  458. .content {
  459. display: flex;
  460. flex: 1;
  461. column-gap: 24px;
  462. width: 98%;
  463. margin: 20px auto;
  464. .contentLeft {
  465. width: 50%;
  466. }
  467. .contentRight {
  468. width: 50%;
  469. }
  470. }
  471. .formBox {
  472. display: flex;
  473. flex-direction: column;
  474. width: 100%;
  475. min-height: 100%;
  476. }
  477. .radio-group {
  478. display: flex;
  479. flex-wrap: wrap;
  480. gap: 8px;
  481. &.block {
  482. flex-direction: column;
  483. gap: 4px;
  484. }
  485. }
  486. .claim-rule-item :deep(.el-form-item__label) {
  487. display: flex;
  488. flex-wrap: wrap;
  489. align-items: center;
  490. }
  491. .label-tip {
  492. margin-left: 4px;
  493. font-size: 12px;
  494. font-weight: normal;
  495. color: #909399;
  496. }
  497. .no-label :deep(.el-form-item__label) {
  498. width: 0 !important;
  499. padding: 0;
  500. margin: 0;
  501. }
  502. .button-container {
  503. display: flex;
  504. gap: 12px;
  505. align-items: center;
  506. justify-content: center;
  507. padding: 20px 0;
  508. margin-top: 20px;
  509. }
  510. </style>