baseFormAddEdit.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. append-to-body
  15. @close="showModelThis = false"
  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. :is-detail="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. style="width: 100%"
  45. filterable
  46. @change="spec_value_id_change"
  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. </el-select>
  55. </div>
  56. <div v-else class="no-data">暂无规格值,请添加!</div>
  57. </el-form-item>
  58. </el-form>
  59. </el-col>
  60. <el-col :span="24" style="text-align: right">
  61. <el-input
  62. v-model="sinput"
  63. style="width: 209px"
  64. class="fl"
  65. :disabled="id == 'edit'"
  66. :size="'small'"
  67. placeholder="规格值名称,如红色"
  68. maxlength="50"
  69. />
  70. <el-button
  71. style="margin: 0 0 0 10px"
  72. icon="el-icon-plus"
  73. :size="'small'"
  74. class="fl"
  75. @click="add_spec"
  76. >新增规格值</el-button>
  77. <el-button
  78. v-if="id !== 'edit'"
  79. :size="'small'"
  80. type="primary"
  81. @click="submitForm"
  82. >保 存
  83. </el-button>
  84. <el-button :size="'small'" @click="showModelThis = false">{{
  85. id == "edit" ? "关 闭" : "取 消"
  86. }}</el-button>
  87. </el-col>
  88. </el-row>
  89. </el-card>
  90. </el-dialog>
  91. </template>
  92. <script>
  93. import asyncRequest from '@/apis/service/goodStore/goodsCost'
  94. import resToken from '@/mixins/resToken'
  95. export default {
  96. name: 'Brand',
  97. mixins: [resToken],
  98. props: ['showModel', 'index', 'sitem'],
  99. data() {
  100. return {
  101. loading: false,
  102. title: '商品规格',
  103. sinput: '',
  104. showModelThis: this.showModel,
  105. specVlist: [],
  106. ruleForm: {},
  107. rulesThis: this.rules,
  108. rules: {
  109. specid: [
  110. {
  111. required: true,
  112. message: '规格类型不能为空',
  113. trigger: 'change'
  114. }
  115. ],
  116. spec_value_id: [{ required: true, message: '请选择规格值', trigger: 'change' }]
  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 } = this.sitem
  155. this.ruleForm = {
  156. index: this.index,
  157. specid: specid || '',
  158. spec_name: spec_name || '',
  159. spec_value_id: spec_value_id || '',
  160. spec_value_name: spec_value_name || ''
  161. }
  162. await this.getlist()
  163. console.log(this.ruleForm)
  164. }
  165. }
  166. })
  167. },
  168. resetFormData() {
  169. this.specVlist = []
  170. this.ruleForm = {
  171. index: '-1',
  172. specid: '',
  173. spec_name: '',
  174. spec_value_id: '',
  175. spec_value_name: ''
  176. }
  177. },
  178. async add_spec() {
  179. const { specid } = this.ruleForm
  180. if (specid) {
  181. if (this.sinput) {
  182. const { code, data, message } = await asyncRequest.valueadd({
  183. spec_id: specid,
  184. spec_value: this.sinput
  185. })
  186. if (code === 0) {
  187. await this.getlist()
  188. } else if (code >= 100 && code <= 104) {
  189. await this.logout()
  190. } else {
  191. this.$message.warning(message)
  192. }
  193. } else {
  194. this.$message.warning('请输入规格值!')
  195. }
  196. } else {
  197. this.$message.warning('请选择规格类型!')
  198. }
  199. },
  200. async specidsearchChange(e) {
  201. const { id, spec_name } = e
  202. this.ruleForm.specid = id || ''
  203. this.ruleForm.spec_name = spec_name || ''
  204. this.$refs.ruleForm.validateField('specid')
  205. await this.getlist()
  206. },
  207. spec_value_id_change(e) {
  208. if (e) {
  209. const { id, spec_value } = this.specVlist.find((i) => i.id === e)
  210. this.ruleForm.spec_value_id = id || ''
  211. this.ruleForm.spec_value_name = spec_value || ''
  212. } else {
  213. this.ruleForm.spec_value_id = ''
  214. this.ruleForm.spec_value_name = ''
  215. }
  216. this.$refs.ruleForm.validateField('spec_value_id')
  217. },
  218. async getlist() {
  219. const { specid } = this.ruleForm
  220. if (specid) {
  221. const { code, data, message } = await asyncRequest.valueall({
  222. spec_id: specid
  223. })
  224. if (code === 0) {
  225. this.specVlist = data
  226. } else if (code >= 100 && code <= 104) {
  227. await this.logout()
  228. } else {
  229. this.$message.warning(message)
  230. }
  231. } else {
  232. this.specVlist = []
  233. }
  234. },
  235. async submitForm() {
  236. await this.$refs.ruleForm.validate(async(valid) => {
  237. if (valid) {
  238. this.showModelThis = false
  239. console.log(this.ruleForm)
  240. this.$emit('refresh', this.ruleForm)
  241. } else {
  242. console.log('error submit!!')
  243. return false
  244. }
  245. })
  246. }
  247. }
  248. }
  249. </script>
  250. <style lang="scss" scoped>
  251. .goodsCostAdd {
  252. .no-data {
  253. text-align: center;
  254. line-height: 32px;
  255. color: #8492a6;
  256. // border: 1px solid #ccc;
  257. border-radius: 6px;
  258. }
  259. }
  260. </style>