baseFormAddEdit.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <el-dialog
  3. v-loading="loading"
  4. :title="title"
  5. :center="true"
  6. align="left"
  7. top="5vh"
  8. width="1040px"
  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. append-to-body
  16. >
  17. <el-card style="margin: -20px 0 0 0">
  18. <el-row :gutter="10">
  19. <el-col :span="24">
  20. <el-form
  21. ref="ruleForm"
  22. :model="ruleForm"
  23. status-icon
  24. :size="'small'"
  25. :rules="rulesThis"
  26. label-width="80px"
  27. class="demo-ruleForm"
  28. >
  29. <el-form-item label="规格类型" prop="specid">
  30. <search-spec
  31. :value="ruleForm.specid"
  32. :disabled="sitem.isMust"
  33. :size="'mini'"
  34. :isDetail="false"
  35. :placeholder="'规格类型'"
  36. @searchChange="specidsearchChange"
  37. />
  38. </el-form-item>
  39. <el-form-item label="规格值" prop="spec_value_id">
  40. <div v-if="specVlist.length > 0">
  41. <el-select
  42. v-model="ruleForm.spec_value_id"
  43. placeholder="请选择规格值"
  44. @change="spec_value_id_change"
  45. style="width: 100%"
  46. filterable
  47. >
  48. <el-option
  49. v-for="item in specVlist"
  50. :key="item.id + item.spec_value"
  51. :label="item.spec_value"
  52. :value="item.id"
  53. >{{ item.spec_value }}</el-option
  54. >
  55. </el-select>
  56. </div>
  57. <div class="no-data" v-else>暂无规格值,请添加!</div>
  58. </el-form-item>
  59. </el-form>
  60. </el-col>
  61. <el-col :span="24" style="text-align: right">
  62. <el-input
  63. style="width: 209px"
  64. v-model="sinput"
  65. class="fl"
  66. :disabled="id == 'edit'"
  67. :size="'small'"
  68. placeholder="规格值名称,如红色"
  69. maxlength="20"
  70. />
  71. <el-button
  72. style="margin: 0 0 0 10px"
  73. icon="el-icon-plus"
  74. :size="'small'"
  75. @click="add_spec"
  76. class="fl"
  77. >新增规格值</el-button
  78. >
  79. <el-button
  80. v-if="id !== 'edit'"
  81. :size="'small'"
  82. type="primary"
  83. @click="submitForm"
  84. >保 存
  85. </el-button>
  86. <el-button @click="showModelThis = false" :size="'small'">{{
  87. id == "edit" ? "关 闭" : "取 消"
  88. }}</el-button>
  89. </el-col>
  90. </el-row>
  91. </el-card>
  92. </el-dialog>
  93. </template>
  94. <script>
  95. import asyncRequest from "@/apis/service/goodStore/goodsCost";
  96. import resToken from "@/mixins/resToken";
  97. export default {
  98. name: "brand",
  99. props: ["showModel", "index", "sitem"],
  100. mixins: [resToken],
  101. data() {
  102. return {
  103. loading: false,
  104. title: "商品规格",
  105. sinput: "",
  106. showModelThis: this.showModel,
  107. specVlist: [],
  108. ruleForm: {},
  109. rulesThis: this.rules,
  110. rules: {
  111. specid: [
  112. {
  113. required: true,
  114. message: "规格类型不能为空",
  115. trigger: "change",
  116. },
  117. ],
  118. spec_value_id: [{ required: true, message: "请选择规格值", trigger: "change" }],
  119. },
  120. };
  121. },
  122. watch: {
  123. showModel: function (val) {
  124. this.showModelThis = val;
  125. if (val) {
  126. this.initForm();
  127. }
  128. },
  129. showModelThis(val) {
  130. if (!val) {
  131. this.$emit("cancel");
  132. }
  133. },
  134. },
  135. methods: {
  136. async initForm() {
  137. this.loading = true;
  138. this.resetFormData();
  139. this.rulesThis = this.rules;
  140. if (this.index + "" === "-1") {
  141. this.title = "添加商品规格值";
  142. } else {
  143. this.title = "修改商品规格值";
  144. }
  145. await this.resetForm();
  146. this.loading = false;
  147. },
  148. async resetForm() {
  149. // 重置
  150. await this.$nextTick(async () => {
  151. if (this.$refs.ruleForm) {
  152. this.$refs.ruleForm.resetFields();
  153. this.$refs.ruleForm.clearValidate();
  154. if (this.index + "" !== "-1") {
  155. this.specVlist = [];
  156. const { specid, spec_name, spec_value_id, spec_value_name } = this.sitem;
  157. console.log(this.sitem);
  158. this.ruleForm = {
  159. index: this.index,
  160. specid: specid || "",
  161. spec_name: spec_name || "",
  162. spec_value_id: spec_value_id || "",
  163. spec_value_name: spec_value_name || "",
  164. };
  165. await this.getlist();
  166. console.log(this.ruleForm);
  167. }
  168. }
  169. });
  170. },
  171. resetFormData() {
  172. this.specVlist = [];
  173. this.ruleForm = {
  174. index: "-1",
  175. specid: "",
  176. spec_name: "",
  177. spec_value_id: "",
  178. spec_value_name: "",
  179. };
  180. },
  181. async add_spec() {
  182. const { specid } = this.ruleForm;
  183. if (specid) {
  184. if (this.sinput) {
  185. const { code, data, message } = await asyncRequest.valueadd({
  186. spec_id: specid,
  187. spec_value: this.sinput,
  188. });
  189. if (code === 0) {
  190. await this.getlist();
  191. } else if (code >= 100 && code <= 104) {
  192. await this.logout();
  193. } else {
  194. this.$message.warning(message);
  195. }
  196. } else {
  197. this.$message.warning("请输入规格值!");
  198. }
  199. } else {
  200. this.$message.warning("请选择规格类型!");
  201. }
  202. },
  203. async specidsearchChange(e) {
  204. const { id, spec_name } = e;
  205. this.ruleForm.specid = id || "";
  206. this.ruleForm.spec_name = spec_name || "";
  207. this.$refs.ruleForm.validateField("specid");
  208. await this.getlist();
  209. },
  210. spec_value_id_change(e) {
  211. if (e) {
  212. const { id, spec_value } = this.specVlist.find((i) => i.id === e);
  213. this.ruleForm.spec_value_id = id || "";
  214. this.ruleForm.spec_value_name = spec_value || "";
  215. } else {
  216. this.ruleForm.spec_value_id = "";
  217. this.ruleForm.spec_value_name = "";
  218. }
  219. this.$refs.ruleForm.validateField("spec_value_id");
  220. },
  221. async getlist() {
  222. const { specid } = this.ruleForm;
  223. if (specid) {
  224. const { code, data, message } = await asyncRequest.valueall({
  225. spec_id: specid,
  226. });
  227. if (code === 0) {
  228. this.specVlist = data;
  229. } else if (code >= 100 && code <= 104) {
  230. await this.logout();
  231. } else {
  232. this.$message.warning(message);
  233. }
  234. } else {
  235. this.specVlist = [];
  236. }
  237. },
  238. async submitForm() {
  239. await this.$refs.ruleForm.validate(async (valid) => {
  240. if (valid) {
  241. this.showModelThis = false;
  242. console.log(this.ruleForm);
  243. this.$emit("refresh", this.ruleForm);
  244. } else {
  245. console.log("error submit!!");
  246. return false;
  247. }
  248. });
  249. },
  250. },
  251. };
  252. </script>
  253. <style lang="scss" scoped>
  254. .goodsCostAdd {
  255. .no-data {
  256. text-align: center;
  257. line-height: 32px;
  258. color: #8492a6;
  259. // border: 1px solid #ccc;
  260. border-radius: 6px;
  261. }
  262. }
  263. </style>