baseFormAddEdit.vue 7.1 KB

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