addAndEdit.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <div v-loading="loading">
  3. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  4. <el-form-item label="卡号" prop="cardNumber">
  5. <el-input
  6. disabled
  7. v-model="form.cardNumber"
  8. placeholder="点击按钮生成卡号"
  9. />
  10. <el-button
  11. size="small"
  12. type="primary"
  13. style="margin-top: 10px"
  14. @click="createCardNumber"
  15. >
  16. 生成卡号
  17. </el-button>
  18. </el-form-item>
  19. <el-form-item label="密码" prop="cardPassword">
  20. <el-input
  21. disabled
  22. v-model="form.cardPassword"
  23. placeholder="点击按钮生成密码"
  24. />
  25. <el-button
  26. size="small"
  27. type="primary"
  28. @click="createPsd"
  29. style="margin-top: 10px"
  30. >
  31. 生成密码
  32. </el-button>
  33. </el-form-item>
  34. <el-form-item label="到期时间" prop="expirationDate">
  35. <el-date-picker
  36. v-model="form.expirationDate"
  37. style="width: 100%"
  38. value-format="yyyy-MM-dd HH:mm:ss"
  39. placeholder="选择到期时间"
  40. type="datetime"
  41. :picker-options="pickerOptions"
  42. ></el-date-picker>
  43. </el-form-item>
  44. <el-form-item label="所属卡券" prop="couponId">
  45. <el-input
  46. v-model="form.coupon"
  47. placeholder="点击选择所属卡券"
  48. @focus="selectCoupon"
  49. />
  50. </el-form-item>
  51. </el-form>
  52. <div style="text-align: right">
  53. <el-button type="primary" @click="handleConfirm">确 定</el-button>
  54. <el-button @click="handleCancel">取 消</el-button>
  55. </div>
  56. <!-- 选择卡券弹窗 -->
  57. <el-dialog title="选择卡券" :visible.sync="dlgVisible" append-to-body>
  58. <SelectCoupon
  59. :selectedIds="selectedIds"
  60. @close="handleClose"
  61. @confirm="handleSelect"
  62. />
  63. </el-dialog>
  64. </div>
  65. </template>
  66. <script>
  67. import { createRandomNumberByTime, createRandomNumberForPsd } from "@/utils";
  68. import SelectCoupon from "@/components/SelectCoupon";
  69. export default {
  70. dicts: ["coupon_type"],
  71. components: { SelectCoupon },
  72. props: {
  73. data: {
  74. type: Object,
  75. default: () => {},
  76. },
  77. loading: {
  78. type: Boolean,
  79. default: false,
  80. },
  81. },
  82. data() {
  83. return {
  84. // 卡券弹窗相关参数
  85. dlgVisible: false,
  86. selectedIds: "",
  87. form: {
  88. cardNumber: "",
  89. cardPassword: "",
  90. expirationDate: "",
  91. couponId: "",
  92. couponNumber: "",
  93. couponTitle: "",
  94. coupon: "",
  95. },
  96. rules: {
  97. cardNumber: [
  98. { required: true, trigger: "blur", message: "请生成卡号" },
  99. ],
  100. cardPassword: [
  101. { required: true, trigger: "blur", message: "请生成密码" },
  102. ],
  103. expirationDate: [
  104. { required: true, trigger: "blur", message: "请选择到期时间" },
  105. ],
  106. },
  107. pickerOptions: {
  108. disabledDate(time) {
  109. return time.getTime() < Date.now() - 8.64e7;
  110. },
  111. shortcuts: [
  112. {
  113. text: "明天",
  114. onClick(picker) {
  115. const date = new Date();
  116. date.setTime(date.getTime() + 3600 * 1000 * 24);
  117. picker.$emit("pick", date);
  118. },
  119. },
  120. {
  121. text: "一周后",
  122. onClick(picker) {
  123. const date = new Date();
  124. date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
  125. picker.$emit("pick", date);
  126. },
  127. },
  128. {
  129. text: "一个月后",
  130. onClick(picker) {
  131. const date = new Date();
  132. date.setTime(date.getTime() + 3600 * 1000 * 24 * 30);
  133. picker.$emit("pick", date);
  134. },
  135. },
  136. ],
  137. },
  138. };
  139. },
  140. watch: {
  141. data: {
  142. handler(newVal) {
  143. if (newVal) {
  144. this.form = {
  145. id: newVal.id || "",
  146. cardNumber: newVal.cardNumber || "",
  147. cardPassword: newVal.cardPassword || "",
  148. expirationDate: newVal.expirationDate || "",
  149. couponId: newVal.couponId || "",
  150. couponNumber: newVal.couponNumber || "",
  151. couponTitle: newVal.couponTitle || "",
  152. coupon:
  153. newVal.couponNumber && newVal.couponTitle
  154. ? `${newVal.couponTitle}(${newVal.couponNumber})`
  155. : "",
  156. };
  157. this.$nextTick(() => {
  158. this.$refs.form && this.$refs.form.clearValidate();
  159. });
  160. }
  161. },
  162. },
  163. },
  164. methods: {
  165. // 确认选择卡券
  166. handleSelect(val) {
  167. console.log(val);
  168. this.form.coupon = `${val[0].title}(${val[0].couponNumber})`;
  169. this.form.couponId = val[0].id;
  170. this.form.couponNumber = val[0].couponNumber;
  171. this.form.couponTitle = val[0].title;
  172. },
  173. // 选择卡券
  174. selectCoupon() {
  175. this.dlgVisible = true;
  176. this.selectedIds = this.form.couponNumber;
  177. },
  178. // 关闭卡券弹窗
  179. handleClose() {
  180. this.dlgVisible = false;
  181. this.selectedIds = "";
  182. },
  183. // 生成卡号
  184. createCardNumber() {
  185. // console.log(createRandomNumberByTime());
  186. this.form.cardNumber = createRandomNumberByTime();
  187. },
  188. // 生成密码
  189. createPsd() {
  190. this.form.cardPassword = createRandomNumberForPsd();
  191. },
  192. // 确定
  193. handleConfirm() {
  194. this.$refs.form.validate((valid) => {
  195. if (valid) {
  196. this.$emit("confirm", this.form);
  197. }
  198. });
  199. },
  200. // 取消
  201. handleCancel() {
  202. this.form = {
  203. cardNumber: "",
  204. cardPassword: "",
  205. expirationDate: "",
  206. couponId: "",
  207. couponNumber: "",
  208. couponTitle: "",
  209. coupon: "",
  210. };
  211. this.$nextTick(() => {
  212. this.$refs.form && this.$refs.form.clearValidate();
  213. });
  214. this.$emit("cancel");
  215. },
  216. },
  217. };
  218. </script>
  219. <style lang="scss" scoped></style>