addAndEdit.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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="title">
  5. <el-input v-model="form.title" placeholder="请输入卡券名称" />
  6. </el-form-item>
  7. <el-form-item label="类型" prop="type">
  8. <el-select
  9. v-model="form.type"
  10. placeholder="请选择类型"
  11. style="width: 100%"
  12. >
  13. <el-option
  14. v-for="dict in dict.type.coupon_type"
  15. :key="dict.value"
  16. :label="dict.label"
  17. :value="dict.value"
  18. />
  19. </el-select>
  20. </el-form-item>
  21. <el-form-item label="额度" prop="quota">
  22. <el-input-number
  23. style="width: 100%"
  24. v-model="form.quota"
  25. :precision="2"
  26. :step="1"
  27. :min="0"
  28. :max="9999"
  29. ></el-input-number>
  30. </el-form-item>
  31. <el-form-item label="使用说明" prop="description">
  32. <el-input
  33. type="textarea"
  34. :rows="4"
  35. v-model="form.description"
  36. placeholder="请输入使用说明"
  37. />
  38. </el-form-item>
  39. <el-form-item label="图片" prop="pic">
  40. <image-upload :limit="1" v-model="form.pic" />
  41. </el-form-item>
  42. </el-form>
  43. <div style="text-align: right">
  44. <el-button type="primary" @click="handleConfirm">确 定</el-button>
  45. <el-button @click="handleCancel">取 消</el-button>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. export default {
  51. dicts: ["coupon_type"],
  52. props: {
  53. data: {
  54. type: Object,
  55. default: () => {},
  56. },
  57. loading: {
  58. type: Boolean,
  59. default: false,
  60. },
  61. },
  62. data() {
  63. const validQuota = (rule, value, callback) => {
  64. // console.log("rule-", rule, "value-", value);
  65. if (value <= 0) {
  66. callback(new Error("卡券额度不能为0"));
  67. } else {
  68. callback();
  69. }
  70. };
  71. return {
  72. form: {
  73. title: "",
  74. type: "",
  75. quota: "",
  76. description: "",
  77. pic: "",
  78. },
  79. rules: {
  80. title: [{ required: true, trigger: "blur", message: "请输入卡券名称" }],
  81. type: [{ required: true, trigger: "change", message: "请选择类型" }],
  82. quota: [
  83. { required: true, trigger: "blur", message: "请输入额度" },
  84. { required: true, validator: validQuota, trigger: "blur" },
  85. ],
  86. description: [
  87. { required: true, trigger: "blur", message: "请输入使用说明" },
  88. ],
  89. pic: [{ required: true, trigger: "blur", message: "请上传图片" }],
  90. },
  91. };
  92. },
  93. watch: {
  94. data: {
  95. handler(newVal) {
  96. if (newVal) {
  97. this.form = {
  98. id: newVal.id || "",
  99. title: newVal.title || "",
  100. type: newVal.type || "",
  101. quota: newVal.quota || "",
  102. description: newVal.description || "",
  103. pic: newVal.pic || "",
  104. };
  105. this.$nextTick(() => {
  106. this.$refs.form && this.$refs.form.clearValidate();
  107. });
  108. }
  109. },
  110. },
  111. },
  112. methods: {
  113. // 确定
  114. handleConfirm() {
  115. this.$refs.form.validate((valid) => {
  116. if (valid) {
  117. this.$emit("confirm", this.form);
  118. }
  119. });
  120. },
  121. // 取消
  122. handleCancel() {
  123. this.form = {
  124. title: "",
  125. type: "",
  126. quota: "",
  127. description: "",
  128. pic: "",
  129. };
  130. this.$nextTick(() => {
  131. this.$refs.form && this.$refs.form.clearValidate();
  132. });
  133. this.$emit("cancel");
  134. },
  135. },
  136. };
  137. </script>
  138. <style lang="scss" scoped></style>