소스 검색

参数校验

喻文俊 3 년 전
부모
커밋
349b3c30eb
3개의 변경된 파일113개의 추가작업 그리고 125개의 파일을 삭제
  1. 4 4
      .env.development
  2. 51 58
      src/views/authentic/product/add.vue
  3. 58 63
      src/views/authentic/product/edit.vue

+ 4 - 4
.env.development

@@ -2,12 +2,12 @@
 ENV = 'development'
 
 # 测试地址 API接口
-VUE_APP_BASE_API = 'https://zplma-b.caimei365.com'
-# VUE_APP_BASE_API = 'http://192.168.2.68:8012'
+# VUE_APP_BASE_API = 'https://zplma-b.caimei365.com'
+VUE_APP_BASE_API = 'http://192.168.2.68:8012'
 
 # 文件上传 API接口地址
-VUE_APP_UPLOAD_API='https://zplma-b.caimei365.com'
-# VUE_APP_UPLOAD_API='http://192.168.2.68:8012'
+# VUE_APP_UPLOAD_API='https://zplma-b.caimei365.com'
+VUE_APP_UPLOAD_API='http://192.168.2.68:8012'
 
 # 二维码生成链接location
 VUE_APP_BASE_SERVER = 'https://www-b.caimei365.com'

+ 51 - 58
src/views/authentic/product/add.vue

@@ -1,12 +1,6 @@
 <template>
   <div v-loading="isLoading" class="addProduct">
-    <el-form
-      ref="addFormRef"
-      label-width="120px"
-      class="addForm"
-      :model="formData"
-      :rules="rules"
-    >
+    <el-form ref="addFormRef" label-width="120px" class="addForm" :model="formData" :rules="rules">
       <el-form-item label="商品名称:" prop="productName">
         <el-input v-model="formData.productName" placeholder="建议输入30个字符效果最佳" />
       </el-form-item>
@@ -36,30 +30,12 @@
         <el-input v-model="formData.certificateImage" class="hiddenInput" />
       </el-form-item>
       <el-form-item label="相关参数:" prop="paramList">
-        <div
-          v-for="(item, index) in formData.paramList"
-          :key="index"
-          class="form-group paramsItem"
-        >
-          <el-input
-            v-model="item.paramName"
-            class="param-title"
-            :placeholder="item.tip.first"
-            maxlength="10"
-          />
-          <el-input
-            v-model="item.paramContent"
-            class="param-info"
-            placeholder="请输入参数信息"
-            maxlength="50"
-          />
-          <span v-if="paramsCount>4" class="closed el-icon-close" @click="handleRemoveParam(index)" />
+        <div v-for="(item, index) in paramList" :key="index" class="form-group paramsItem">
+          <el-input v-model="item.paramName" class="param-title" :placeholder="item.tip.first" maxlength="10" />
+          <el-input v-model="item.paramContent" class="param-info" placeholder="请输入参数信息" maxlength="50" />
+          <span v-if="paramsCount > 4" class="closed el-icon-close" @click="handleRemoveParam(index)" />
         </div>
-        <el-button
-          type="primary"
-          size="mini"
-          @click="handleAddParam"
-        >添加参数</el-button>
+        <el-button type="primary" size="mini" @click="handleAddParam">添加参数</el-button>
       </el-form-item>
       <!-- <el-form-item label="上线状态:">
         <el-select
@@ -118,12 +94,16 @@ export default {
         status: 2, //	上架状态:0下线,1上线 2待审核
         brandId: ''
       },
+      paramList: [], // 参数列表
       rules: {
         certificateImage: [{ required: true, message: '授权牌照不能为空' }],
         paramList: [{ required: true, message: '参数不能为空' }],
         productImage: [{ required: true, message: '商品图片不能为空' }],
         snCode: [{ required: true, message: 'SN码不能为空' }, { validator: valideSNcode }],
-        productName: [{ required: true, message: '商品名称不能为空' }, { max: 50, message: '字数在50字符以内' }],
+        productName: [
+          { required: true, message: '商品名称不能为空' },
+          { max: 50, message: '字数在50字符以内' }
+        ],
         brandId: [{ required: true, validator: valideBrandId, tigger: 'change' }]
       },
       // 参数框的提示语
@@ -133,6 +113,14 @@ export default {
   computed: {
     ...mapGetters(['authUserId', 'proxyInfo', 'shopType', 'brandId'])
   },
+  watch: {
+    paramList: {
+      deep: true,
+      handler: function() {
+        this.formData.paramList = this.setParamToFormData()
+      }
+    }
+  },
   created() {
     this.formData.authId = this.$route.query.id
     this.formData.authUserId = this.proxyInfo?.authUserId || this.authUserId
@@ -152,6 +140,7 @@ export default {
     },
     // 提交
     submit() {
+      this.formData.paramList = this.setParamToFormData()
       this.$refs.addFormRef.validate(valide => {
         if (valide) {
           this.save()
@@ -164,26 +153,32 @@ export default {
       // createBy先判断是否为代理操作,是就从代理数据中获取,否则直接获取当前登录用户的信息
       this.formData.createBy = this.proxyInfo?.authUserId || this.authUserId
       this.formData.authUserId = this.proxyInfo?.authUserId || this.authUserId
-      saveProduct(this.formData).then(res => {
-        if (res.code !== 0) return
-        const h = this.$createElement
-        this.$notify.success({
-          title: '新增商品',
-          message: h('i', { style: 'color: #333' }, `已添加商品:"${this.formData.productName}"`),
-          duration: 2000
+      saveProduct(this.formData)
+        .then(res => {
+          if (res.code !== 0) return
+          const h = this.$createElement
+          this.$notify.success({
+            title: '新增商品',
+            message: h('i', { style: 'color: #333' }, `已添加商品:"${this.formData.productName}"`),
+            duration: 2000
+          })
+          this.$refs.addFormRef.resetFields()
+          this.clearFormData()
+          this.$store.dispatch('tagsView/delView', this.$route)
+          this.$router.push(`/product/list?id=${this.formData.authId}`)
+        })
+        .finally(() => {
+          this.isLoading = false
         })
-        this.$refs.addFormRef.resetFields()
-        this.clearFormData()
-        this.$store.dispatch('tagsView/delView', this.$route)
-        this.$router.push(`/product/list?id=${this.formData.authId}`)
-      }).finally(() => {
-        this.isLoading = false
-      })
+    },
+    // 保存参数到formdata
+    setParamToFormData() {
+      return this.paramList.filter(item => item.paramContent !== '' && item.paramName !== '')
     },
     // 初始化参数列表
     initParamList() {
       for (let i = 0; i < this.paramsCount; i++) {
-        this.formData.paramList.push({
+        this.paramList.push({
           paramContent: '',
           paramName: '',
           tip: {
@@ -204,7 +199,7 @@ export default {
     // 添加一栏参数
     handleAddParam() {
       this.paramsCount += 1
-      this.formData.paramList.push({
+      this.paramList.push({
         paramContent: '',
         paramName: '',
         tip: {
@@ -216,7 +211,7 @@ export default {
     // 删除一栏参数
     handleRemoveParam(index) {
       this.paramsCount -= 1
-      this.formData.paramList.splice(index, 1)
+      this.paramList.splice(index, 1)
     },
     // 图片上传成功 产品图片
     imageUploadSuccess1(data) {
@@ -227,7 +222,7 @@ export default {
       this.formData.certificateImage = data.data
     },
     clearFormData() {
-      this.formData.paramList = []
+      this.paramList = []
       this.paramsCount = 4
       this.initParamList()
     }
@@ -260,24 +255,22 @@ export default {
     width: 140px;
   }
 }
-.paramsItem{
+.paramsItem {
   position: relative;
-  .closed{
+  .closed {
     position: absolute;
     top: 50%;
     transform: translateY(-50%);
     right: -20px;
     cursor: pointer;
     color: #ddd;
-    &:hover{
+    &:hover {
       color: #333;
     }
   }
-
 }
- .hiddenInput{
-    height: 0;
-    display: none;
-  }
-
+.hiddenInput {
+  height: 0;
+  display: none;
+}
 </style>

+ 58 - 63
src/views/authentic/product/edit.vue

@@ -1,12 +1,6 @@
 <template>
   <div v-loading="isLoading" class="addProduct">
-    <el-form
-      ref="addFormRef"
-      label-width="120px"
-      class="addForm"
-      :model="formData"
-      :rules="rules"
-    >
+    <el-form ref="addFormRef" label-width="120px" class="addForm" :model="formData" :rules="rules">
       <el-form-item label="商品名称:" prop="productName">
         <el-input v-model="formData.productName" placeholder="请输入商品名称" />
       </el-form-item>
@@ -16,12 +10,7 @@
       </el-form-item>
 
       <el-form-item v-if="shopType === 2" label="所属品牌:" prop="brandId">
-        <el-select
-          v-model="formData.brandId"
-          placeholder="请选择品牌"
-          style="width: 100%"
-          filterable
-        >
+        <el-select v-model="formData.brandId" placeholder="请选择品牌" style="width: 100%" filterable>
           <el-option v-for="item in brandList" :key="item.id" :label="item.name" :value="item.id" />
         </el-select>
       </el-form-item>
@@ -35,30 +24,12 @@
         <el-input v-model="formData.certificateImage" class="hiddenInput" />
       </el-form-item>
       <el-form-item label="相关参数:" prop="paramList">
-        <div
-          v-for="(item, index) in formData.paramList"
-          :key="index"
-          class="form-group paramsItem"
-        >
-          <el-input
-            v-model="item.paramName"
-            class="param-title"
-            placeholder="参数名称"
-            maxlength="10"
-          />
-          <el-input
-            v-model="item.paramContent"
-            class="param-info"
-            placeholder="请输入参数信息"
-            maxlength="50"
-          />
-          <span v-if="paramsCount>4" class="closed el-icon-close" @click="handleRemoveParam(index)" />
+        <div v-for="(item, index) in paramList" :key="index" class="form-group paramsItem">
+          <el-input v-model="item.paramName" class="param-title" placeholder="参数名称" maxlength="10" />
+          <el-input v-model="item.paramContent" class="param-info" placeholder="请输入参数信息" maxlength="50" />
+          <span v-if="paramsCount > 4" class="closed el-icon-close" @click="handleRemoveParam(index)" />
         </div>
-        <el-button
-          type="primary"
-          size="mini"
-          @click="handleAddParam"
-        >添加参数</el-button>
+        <el-button type="primary" size="mini" @click="handleAddParam">添加参数</el-button>
       </el-form-item>
       <!-- <el-form-item label="上线状态:">
         <el-select
@@ -102,13 +73,17 @@ export default {
         snCode: '', //	商品SN码
         brandId: ''
       },
+      paramList: [],
       brandList: [],
       rules: {
         certificateImage: [{ required: true, message: '授权牌照不能为空' }],
         paramList: [{ required: true, message: '参数不能为空' }],
         productImage: [{ required: true, message: '商品图片不能为空' }],
         snCode: [{ required: true, message: 'SN码不能为空' }],
-        productName: [{ required: true, message: '商品名称不能为空' }, { max: 50, message: '字数在50字符以内' }]
+        productName: [
+          { required: true, message: '商品名称不能为空' },
+          { max: 50, message: '字数在50字符以内' }
+        ]
       },
       fileList1: [],
       fileList2: []
@@ -117,6 +92,14 @@ export default {
   computed: {
     ...mapGetters(['authUserId', 'proxyInfo', 'shopType', 'brandId'])
   },
+  watch: {
+    paramList: {
+      deep: true,
+      handler: function() {
+        this.formData.paramList = this.setParamToFormData()
+      }
+    }
+  },
   created() {
     this.formData.productId = parseInt(this.$route.query.id)
     this.formData.authUserId = this.proxyInfo?.authUserId || this.authUserId
@@ -132,6 +115,7 @@ export default {
   methods: {
     // 提交
     submit() {
+      this.formData.paramList = this.setParamToFormData()
       this.$refs.addFormRef.validate(valide => {
         if (valide) {
           this.save()
@@ -146,10 +130,14 @@ export default {
         // const { authId, certificateImage, paramList, productId, productImage, productName, snCode, status } = res.data
         for (const key in res.data) {
           if (Object.hasOwnProperty.call(res.data, key)) {
-            this.formData[key] = res.data[key]
+            if (key !== 'paramList') {
+              this.formData[key] = res.data[key]
+            }
           }
         }
-        this.paramsCount = this.formData.paramList.length
+        // 初始化参数
+        this.paramList = res.data.paramList
+        this.paramsCount = this.paramList.length
         if (this.paramsCount < 4) {
           this.initParamList(4 - this.paramsCount)
         }
@@ -182,24 +170,30 @@ export default {
       this.isLoading = true
       this.formData.createBy = this.proxyInfo?.authUserId || this.authUserId
       this.formData.authUserId = this.proxyInfo?.authUserId || this.authUserId
-      saveProduct(this.formData).then(res => {
-        if (res.code !== 0) return
-        const h = this.$createElement
-        this.$notify.success({
-          title: '修改商品',
-          message: h('i', { style: 'color: #333' }, `已修改商品:"${this.formData.productName}"`),
-          duration: 2000
+      saveProduct(this.formData)
+        .then(res => {
+          if (res.code !== 0) return
+          const h = this.$createElement
+          this.$notify.success({
+            title: '修改商品',
+            message: h('i', { style: 'color: #333' }, `已修改商品:"${this.formData.productName}"`),
+            duration: 2000
+          })
+          this.$store.dispatch('tagsView/delView', this.$route)
+          this.$router.back()
+        })
+        .finally(() => {
+          this.isLoading = false
         })
-        this.$store.dispatch('tagsView/delView', this.$route)
-        this.$router.back()
-      }).finally(() => {
-        this.isLoading = false
-      })
+    },
+    // 保存参数到formdata
+    setParamToFormData() {
+      return this.paramList.filter(item => item.paramContent !== '' && item.paramName !== '')
     },
     // 添加一栏参数
     handleAddParam() {
       this.paramsCount += 1
-      this.formData.paramList.push({
+      this.paramList.push({
         paramContent: '',
         paramName: ''
       })
@@ -207,7 +201,7 @@ export default {
     // 删除一栏参数
     handleRemoveParam(index) {
       this.paramsCount -= 1
-      this.formData.paramList.splice(index, 1)
+      this.paramList.splice(index, 1)
     },
     // 图片上传成功 产品图片
     imageUploadSuccess1(data) {
@@ -217,10 +211,13 @@ export default {
     imageUploadSuccess2(data) {
       this.formData.certificateImage = data.data
     },
+    setParamList(data) {
+      this.paramList = data.paramList
+    },
     // 初始化参数列表
     initParamList(count) {
       for (let i = 0; i < count; i++) {
-        this.formData.paramList.push({
+        this.paramList.push({
           paramContent: '',
           paramName: ''
         })
@@ -255,24 +252,22 @@ export default {
     width: 140px;
   }
 }
-.paramsItem{
+.paramsItem {
   position: relative;
-  .closed{
+  .closed {
     position: absolute;
     top: 50%;
     transform: translateY(-50%);
     right: -20px;
     cursor: pointer;
     color: #ddd;
-    &:hover{
+    &:hover {
       color: #333;
     }
   }
-
 }
- .hiddenInput{
-    height: 0;
-    display: none;
-  }
-
+.hiddenInput {
+  height: 0;
+  display: none;
+}
 </style>