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