baseFormAddEdit.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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="20"
  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/service/goodStore/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: [
  115. { required: true, message: "请选择规格值", trigger: "change" },
  116. ],
  117. },
  118. };
  119. },
  120. watch: {
  121. showModel: function (val) {
  122. this.showModelThis = val;
  123. if (val) {
  124. this.initForm();
  125. }
  126. },
  127. showModelThis(val) {
  128. if (!val) {
  129. this.$emit("cancel");
  130. }
  131. },
  132. },
  133. methods: {
  134. async initForm() {
  135. this.loading = true;
  136. this.resetFormData();
  137. this.rulesThis = this.rules;
  138. if (this.index + "" === "-1") {
  139. this.title = "添加商品规格值";
  140. } else {
  141. this.title = "修改商品规格值";
  142. }
  143. await this.resetForm();
  144. this.loading = false;
  145. },
  146. async resetForm() {
  147. // 重置
  148. await this.$nextTick(async () => {
  149. if (this.$refs.ruleForm) {
  150. this.$refs.ruleForm.resetFields();
  151. this.$refs.ruleForm.clearValidate();
  152. if (this.index + "" !== "-1") {
  153. this.specVlist = [];
  154. const { specid, spec_name, spec_value_id, spec_value_name } =
  155. this.sitem;
  156. this.ruleForm = {
  157. index: this.index,
  158. specid: specid || "",
  159. spec_name: spec_name || "",
  160. spec_value_id: spec_value_id || "",
  161. spec_value_name: spec_value_name || "",
  162. };
  163. await this.getlist();
  164. console.log(this.ruleForm);
  165. }
  166. }
  167. });
  168. },
  169. resetFormData() {
  170. this.specVlist = [];
  171. this.ruleForm = {
  172. index: "-1",
  173. specid: "",
  174. spec_name: "",
  175. spec_value_id: "",
  176. spec_value_name: "",
  177. };
  178. },
  179. async add_spec() {
  180. const { specid } = this.ruleForm;
  181. if (specid) {
  182. if (this.sinput) {
  183. const { code, data, message } = await asyncRequest.valueadd({
  184. spec_id: specid,
  185. spec_value: this.sinput,
  186. });
  187. if (code === 0) {
  188. await this.getlist();
  189. } else if (code >= 100 && code <= 104) {
  190. await this.logout();
  191. } else {
  192. this.$message.warning(message);
  193. }
  194. } else {
  195. this.$message.warning("请输入规格值!");
  196. }
  197. } else {
  198. this.$message.warning("请选择规格类型!");
  199. }
  200. },
  201. async specidsearchChange(e) {
  202. const { id, spec_name } = e;
  203. this.ruleForm.specid = id || "";
  204. this.ruleForm.spec_name = spec_name || "";
  205. this.$refs.ruleForm.validateField("specid");
  206. await this.getlist();
  207. },
  208. spec_value_id_change(e) {
  209. if (e) {
  210. const { id, spec_value } = this.specVlist.find((i) => i.id === e);
  211. this.ruleForm.spec_value_id = id || "";
  212. this.ruleForm.spec_value_name = spec_value || "";
  213. } else {
  214. this.ruleForm.spec_value_id = "";
  215. this.ruleForm.spec_value_name = "";
  216. }
  217. this.$refs.ruleForm.validateField("spec_value_id");
  218. },
  219. async getlist() {
  220. const { specid } = this.ruleForm;
  221. if (specid) {
  222. const { code, data, message } = await asyncRequest.valueall({
  223. spec_id: specid,
  224. });
  225. if (code === 0) {
  226. this.specVlist = data;
  227. } else if (code >= 100 && code <= 104) {
  228. await this.logout();
  229. } else {
  230. this.$message.warning(message);
  231. }
  232. } else {
  233. this.specVlist = [];
  234. }
  235. },
  236. async submitForm() {
  237. await this.$refs.ruleForm.validate(async (valid) => {
  238. if (valid) {
  239. this.showModelThis = false;
  240. console.log(this.ruleForm);
  241. this.$emit("refresh", this.ruleForm);
  242. } else {
  243. console.log("error submit!!");
  244. return false;
  245. }
  246. });
  247. },
  248. },
  249. };
  250. </script>
  251. <style lang="scss" scoped>
  252. .goodsCostAdd {
  253. .no-data {
  254. text-align: center;
  255. line-height: 32px;
  256. color: #8492a6;
  257. // border: 1px solid #ccc;
  258. border-radius: 6px;
  259. }
  260. }
  261. </style>