edit.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <div v-loading="isLoading" class="edit-cate-info page-form-container">
  3. <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
  4. <el-form-item prop="name" label="设备名称:">
  5. <el-input v-model="formData.name" placeholder="请输入设备名称" />
  6. </el-form-item>
  7. <el-form-item prop="image" label="设备图片:" class="pd-10">
  8. <el-input v-show="false" v-model="formData.image" />
  9. <upload-image
  10. tip="建议尺寸:542px * 542px"
  11. :image-list="productImageList"
  12. :before-upload="beforeUploadImage"
  13. @success="onUploadImageSuccess"
  14. @remove="onUploadImageRemove"
  15. />
  16. </el-form-item>
  17. <el-form-item v-if="shopType === 2" label="所属品牌:" prop="infoId">
  18. <el-select v-model="formData.infoId" placeholder="请选择品牌" style="width: 100%" filterable clearable>
  19. <el-option v-for="item in brandList" :key="item.infoId" :label="item.brandName" :value="item.infoId" />
  20. </el-select>
  21. </el-form-item>
  22. <el-form-item label="相关参数:" prop="paramList">
  23. <div v-for="item in formData.paramList" :key="item.uuid" class="form-group">
  24. <el-input v-model="item.paramName" class="param-title" :placeholder="item.firstTip || '参数名称'" />
  25. <el-input v-model="item.paramContent" class="param-info" placeholder="请输入参数信息" />
  26. <span v-if="paramCount > 4" class="closed el-icon-close" @click="handleRemoveParam(item)" />
  27. </div>
  28. <el-button type="primary" size="mini" @click="handleAddParam">添加参数</el-button>
  29. </el-form-item>
  30. </el-form>
  31. <div class="submit-btn">
  32. <el-button type="primary" @click="submit">保存</el-button>
  33. <el-button type="warning" @click="navigateBack()">返回</el-button>
  34. </div>
  35. </div>
  36. </template>
  37. <script>
  38. import UploadImage from '@/components/UploadImage'
  39. import { tipList, resetFormData } from './configs'
  40. import { createProductCate, fetchAuthProductFormData } from '@/api/product'
  41. import { mapGetters } from 'vuex'
  42. import { fetchBrandList } from '@/api/brand'
  43. let uuid = 0
  44. export default {
  45. components: {
  46. UploadImage
  47. },
  48. data() {
  49. const paramListValidate = (rules, value, callback) => {
  50. value.every((item) => item.paramName && item.paramContent)
  51. const notEmptyList = value.filter((item) => item.paramName.trim() && item.paramContent.trim())
  52. if (notEmptyList.length === 0) {
  53. callback(new Error('参数列表不能为空'))
  54. } else if (notEmptyList.length < 4) {
  55. callback(new Error('请填写至少4项参数'))
  56. } else {
  57. callback()
  58. }
  59. }
  60. const valideBrandId = (rules, value, callback) => {
  61. if (!value) {
  62. return callback(new Error('所属品牌不能为空'))
  63. }
  64. callback()
  65. }
  66. return {
  67. isLoading: false,
  68. formData: resetFormData(),
  69. rules: {
  70. name: [{ required: true, message: '请输入设备名称', trigger: ['blur'] }],
  71. image: [{ required: true, message: '请上传设备图片', trigger: ['change'] }],
  72. paramList: [
  73. { required: true, message: '请填写设备参数', trigger: ['change'] },
  74. { validator: paramListValidate, trigger: ['change'] }
  75. ],
  76. infoId: [{ required: true, validator: valideBrandId, trigger: 'change' }]
  77. },
  78. brandList: [],
  79. productImageList: []
  80. }
  81. },
  82. computed: {
  83. ...mapGetters(['authUserId', 'shopType', 'proxyInfo']),
  84. // 参数长度
  85. paramCount() {
  86. return this.formData.paramList.length
  87. }
  88. },
  89. created() {
  90. this.initEmptyParamList()
  91. const type = this.$route.query.type
  92. if (this.shopType === 2) {
  93. this.getBrandList()
  94. }
  95. if (type === 'edit') {
  96. this.fetchProductData()
  97. }
  98. },
  99. methods: {
  100. // 提交
  101. async submit() {
  102. this.isLoading = true
  103. try {
  104. await this.$refs.form.validate()
  105. this.createProductCate()
  106. } catch (error) {
  107. console.log(error)
  108. this.isLoading = false
  109. }
  110. },
  111. // 保存设备信息
  112. async createProductCate() {
  113. this.formData.authUserId = this.authUserId
  114. this.formData.createBy = this.authUserId
  115. if (this.shopType === 1) {
  116. this.formData.infoId = this.proxyInfo?.infoId || this.infoId || ''
  117. }
  118. const data = {
  119. ...this.formData,
  120. paramList: this.generageParamList()
  121. }
  122. try {
  123. await createProductCate(data)
  124. this.$message.success('保存设备分类成功')
  125. this.$store.dispatch('tagsView/delView', this.$route)
  126. this.$router.push('device-cate')
  127. } catch (error) {
  128. console.log(error)
  129. } finally {
  130. this.isLoading = false
  131. }
  132. },
  133. // 获取设备数据信息
  134. async fetchProductData() {
  135. try {
  136. const id = this.$route.query.id
  137. const { data } = await fetchAuthProductFormData({ productTypeId: id })
  138. this.initProductData(data)
  139. } catch (error) {
  140. console.log(error)
  141. }
  142. },
  143. // 初始化设备信息
  144. initProductData(data) {
  145. for (const key in this.formData) {
  146. if (Object.hasOwnProperty.call(data, key)) {
  147. if (key === 'paramList') {
  148. this.initParamList(data[key])
  149. } else {
  150. this.formData[key] = data[key]
  151. }
  152. }
  153. }
  154. if (data.image) {
  155. this.productImageList = [{ name: '设备图片', url: this.formData.image }]
  156. }
  157. },
  158. // 获取品牌信息
  159. async getBrandList() {
  160. try {
  161. const { data } = await fetchBrandList({ type: 3, authUserId: this.authUserId })
  162. this.brandList = data
  163. } catch (error) {
  164. console.log(error)
  165. }
  166. },
  167. // 生成最终的设备参数
  168. generageParamList() {
  169. return this.formData.paramList.map((param) => ({
  170. paramContent: param.paramContent,
  171. paramName: param.paramName
  172. }))
  173. },
  174. // 初始化参数列表
  175. initParamList(list) {
  176. if (!list) return
  177. this.formData.paramList.splice(0, list.length, ...list)
  178. },
  179. // 初始化空参数列表
  180. initEmptyParamList() {
  181. for (const item of tipList) {
  182. this.formData.paramList.push({
  183. uuid: ++uuid,
  184. paramContent: '',
  185. paramName: '',
  186. firstTip: item
  187. })
  188. }
  189. },
  190. // 添加一栏参数
  191. handleAddParam() {
  192. this.formData.paramList.push({
  193. uuid: ++uuid,
  194. paramContent: '',
  195. paramName: ''
  196. })
  197. },
  198. // 删除一栏参数
  199. handleRemoveParam(index) {
  200. this.formData.paramList.splice(index, 1)
  201. },
  202. // 设备图片上传
  203. beforeUploadImage(file) {
  204. return true
  205. },
  206. onUploadImageSuccess({ response, file, fileList }) {
  207. this.formData.image = response.data
  208. this.productImageList = fileList
  209. },
  210. onUploadImageRemove({ file, fileList }) {
  211. this.formData.image = ''
  212. this.productImageList = []
  213. }
  214. }
  215. }
  216. </script>
  217. <style lang="scss" scoped>
  218. .edit-cate-info {
  219. .form-group {
  220. position: relative;
  221. margin-bottom: 2%;
  222. .param-title {
  223. width: 30%;
  224. }
  225. .param-info {
  226. width: 68%;
  227. margin-left: 2%;
  228. }
  229. .closed {
  230. position: absolute;
  231. top: 50%;
  232. transform: translateY(-50%);
  233. right: -20px;
  234. cursor: pointer;
  235. color: #ddd;
  236. &:hover {
  237. color: #333;
  238. }
  239. }
  240. }
  241. .submit-btn {
  242. text-align: center;
  243. margin-top: 60px;
  244. .el-button {
  245. width: 140px;
  246. }
  247. }
  248. }
  249. </style>