addEdit.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <el-dialog
  3. v-loading="loading"
  4. :title="title"
  5. :center="true"
  6. align="left"
  7. top="12vh"
  8. width="650px"
  9. :close-on-click-modal="false"
  10. :visible.sync="showModelThis"
  11. element-loading-text="拼命加载中"
  12. element-loading-spinner="el-icon-loading"
  13. element-loading-background="rgba(0, 0, 0, 0.8)"
  14. @close="showModelThis = false"
  15. >
  16. <el-card style="margin: -20px 0 0 0">
  17. <el-row :gutter="10">
  18. <el-col :span="24">
  19. <el-form
  20. ref="ruleForm"
  21. :model="ruleForm"
  22. status-icon
  23. :rules="rulesThis"
  24. label-width="95px"
  25. class="demo-ruleForm"
  26. >
  27. <el-form-item label="分类名称" prop="cat_name">
  28. <el-input
  29. v-model="ruleForm.cat_name"
  30. :disabled="id == '007'"
  31. placeholder="分类名称"
  32. maxlength="20"
  33. >
  34. <template slot="prepend" v-if="pid_name">{{
  35. pid_name
  36. }}</template></el-input
  37. >
  38. </el-form-item>
  39. <el-form-item label="分类规格" prop="specs_id">
  40. <el-select
  41. v-model="ruleForm.specs_id"
  42. multiple
  43. :disabled="id == '007'"
  44. style="width: 100%"
  45. placeholder="分类规格"
  46. >
  47. <el-option
  48. v-for="item in specOptions"
  49. :key="item.id"
  50. :label="item.spec_name"
  51. :value="item.id"
  52. >
  53. </el-option>
  54. </el-select>
  55. </el-form-item>
  56. <!-- <el-form-item label="财务核算码" prop="fund_code">
  57. <el-input
  58. v-model="ruleForm.fund_code"
  59. :disabled="id == '007'"
  60. placeholder="财务核算码"
  61. maxlength="50"
  62. />
  63. </el-form-item> -->
  64. <el-form-item label="售后说明" prop="cat_desc">
  65. <el-input
  66. v-model="ruleForm.cat_desc"
  67. :disabled="id == '007'"
  68. placeholder="售后说明"
  69. type="textarea"
  70. :rows="5"
  71. maxlength="2000"
  72. />
  73. </el-form-item>
  74. </el-form>
  75. </el-col>
  76. <el-col :span="24" style="text-align: right">
  77. <el-button v-if="id !== '007'" type="primary" @click="submitForm"
  78. >保 存
  79. </el-button>
  80. <el-button @click="showModelThis = false">{{
  81. id == "007" ? "关 闭" : "取 消"
  82. }}</el-button>
  83. </el-col>
  84. </el-row>
  85. </el-card>
  86. </el-dialog>
  87. </template>
  88. <script>
  89. import asyncRequest from "@/apis/service/goodStore/sort";
  90. import resToken from "@/mixins/resToken";
  91. import { isChinese } from "@/utils/validate";
  92. export default {
  93. name: "sort",
  94. props: ["showModel", "id", "sitem", "titleList"],
  95. mixins: [resToken],
  96. data() {
  97. const validate_fund_code = (rule, value, callback) => {
  98. if (value === "") {
  99. callback();
  100. } else if (isChinese(value)) {
  101. callback(new Error("财务核算码不能包含汉字!"));
  102. } else {
  103. callback();
  104. }
  105. };
  106. return {
  107. loading: false,
  108. title: "添加分类",
  109. pid_name: "",
  110. showModelThis: this.showModel,
  111. specOptions: [],
  112. ruleForm: {
  113. id: "",
  114. cat_name: "",
  115. specs_id: [],
  116. cat_desc: "",
  117. },
  118. rulesThis: this.rules,
  119. rules: {
  120. cat_name: [
  121. { required: true, message: "分类名称不能为空", trigger: "blur" },
  122. ],
  123. specs_id: [
  124. {
  125. type: "array",
  126. required: true,
  127. message: "请至少选择一个分类规格",
  128. trigger: "change",
  129. },
  130. ],
  131. fund_code: [
  132. { required: false, validator: validate_fund_code, trigger: "blur" },
  133. ],
  134. cat_desc: [{ required: false, message: "", trigger: "blur" }],
  135. },
  136. };
  137. },
  138. watch: {
  139. showModel: function (val) {
  140. this.showModelThis = val;
  141. if (val) {
  142. this.initForm();
  143. }
  144. },
  145. showModelThis(val) {
  146. if (!val) {
  147. this.$emit("cancel");
  148. }
  149. },
  150. },
  151. methods: {
  152. async initForm() {
  153. this.loading = true;
  154. this.pid_name = "";
  155. this.titleList.forEach((e, i) => {
  156. this.pid_name += i !== 0 ? `${e.titleName}/` : "";
  157. });
  158. await this.resetForm();
  159. if (this.id === "003") {
  160. this.title = "添加分类";
  161. this.rulesThis = this.rules;
  162. } else if (this.id === "005") {
  163. this.title = "修改分类";
  164. this.rulesThis = this.rules;
  165. await this.initData();
  166. } else {
  167. this.title = "分类详情";
  168. this.rulesThis = {};
  169. await this.initData();
  170. }
  171. await this.getAllSpecs();
  172. this.loading = false;
  173. },
  174. async resetForm() {
  175. // 重置
  176. await this.$nextTick(() => {
  177. if (this.$refs.ruleForm) {
  178. this.$refs.ruleForm.resetFields();
  179. this.$refs.ruleForm.clearValidate();
  180. const { id, cat_name, cat_desc } = this.sitem;
  181. this.ruleForm = {
  182. pid: this.titleList[this.titleList.length - 1].id || "",
  183. id: id || "",
  184. cat_name: cat_name || "",
  185. specs_id: [],
  186. cat_desc: cat_desc || "",
  187. };
  188. }
  189. });
  190. },
  191. async getAllSpecs() {
  192. this.specOptions = [];
  193. let res = await asyncRequest.specstitle({});
  194. if (res && res.code === 0) {
  195. this.specOptions = res.data;
  196. } else if (res && res.code >= 100 && res.code <= 104) {
  197. this.specOptions = [];
  198. await this.logout();
  199. } else {
  200. this.specOptions = [];
  201. this.$message.warning(res.message);
  202. }
  203. },
  204. async initData() {
  205. const res = await asyncRequest.detail({ id: this.sitem.id });
  206. if (res && res.code === 0 && res.data) {
  207. const { pid, id, cat_name, cat_desc, spec, fund_code } = res.data;
  208. let specs_id = [];
  209. if (spec && spec.length > 0) {
  210. spec.forEach((e) => {
  211. specs_id.push(e.id);
  212. });
  213. }
  214. console.log(specs_id);
  215. this.ruleForm = {
  216. pid: pid || "",
  217. id: id || "",
  218. cat_name: cat_name || "",
  219. specs_id: specs_id || [],
  220. fund_code: fund_code || "",
  221. cat_desc: cat_desc || "",
  222. };
  223. this.$refs.ruleForm.resetFields();
  224. this.$refs.ruleForm.clearValidate();
  225. } else if (res && res.code >= 100 && res.code <= 104) {
  226. await this.logout();
  227. } else {
  228. this.$message.warning(res.message);
  229. }
  230. },
  231. async submitForm() {
  232. await this.$refs.ruleForm.validate(async (valid) => {
  233. if (valid) {
  234. this.loading = true;
  235. let model = JSON.parse(JSON.stringify(this.ruleForm));
  236. let res = {};
  237. if (this.id === "003") {
  238. delete model["id"];
  239. res = await asyncRequest.add(model);
  240. } else {
  241. res = await asyncRequest.update(model);
  242. }
  243. this.loading = false;
  244. if (res && res.code === 0) {
  245. const title = this.id === "add" ? "添加成功!" : "修改成功!";
  246. this.$notify.success({
  247. title,
  248. message: "",
  249. });
  250. this.showModelThis = false;
  251. // 刷新
  252. this.$emit("refresh");
  253. } else if (res && res.code >= 100 && res.code <= 104) {
  254. await this.logout();
  255. } else {
  256. this.$message.warning(res.message);
  257. }
  258. } else {
  259. console.log("error submit!!");
  260. return false;
  261. }
  262. });
  263. },
  264. },
  265. };
  266. </script>
  267. <style lang="scss" scoped>
  268. .sort {
  269. }
  270. </style>