浏览代码

feat:商品信息采集功能调整

caoliqin 2 周之前
父节点
当前提交
565c432d2d
共有 2 个文件被更改,包括 209 次插入57 次删除
  1. 7 7
      src/api/hisStore/storeProductUserTempField.js
  2. 202 50
      src/views/hisStore/storeProductUserTemp/index.vue

+ 7 - 7
src/api/hisStore/storeProductUserTempField.js

@@ -3,7 +3,7 @@ import request from '@/utils/request'
 // 查询商品信息采集模板字段列表
 export function listFsStoreProductUserTempField(query) {
   return request({
-    url: '/FsStoreProductUserTempField/FsStoreProductUserTempField/list',
+    url: '/store/store/storeProductUserTempField/list',
     method: 'get',
     params: query
   })
@@ -12,7 +12,7 @@ export function listFsStoreProductUserTempField(query) {
 // 查询商品信息采集模板字段详细
 export function getFsStoreProductUserTempField(id) {
   return request({
-    url: '/FsStoreProductUserTempField/FsStoreProductUserTempField/' + id,
+    url: '/store/store/storeProductUserTempField/' + id,
     method: 'get'
   })
 }
@@ -20,7 +20,7 @@ export function getFsStoreProductUserTempField(id) {
 // 新增商品信息采集模板字段
 export function addFsStoreProductUserTempField(data) {
   return request({
-    url: '/FsStoreProductUserTempField/FsStoreProductUserTempField',
+    url: '/store/store/storeProductUserTempField',
     method: 'post',
     data: data
   })
@@ -29,7 +29,7 @@ export function addFsStoreProductUserTempField(data) {
 // 修改商品信息采集模板字段
 export function updateFsStoreProductUserTempField(data) {
   return request({
-    url: '/FsStoreProductUserTempField/FsStoreProductUserTempField',
+    url: '/store/store/storeProductUserTempField',
     method: 'put',
     data: data
   })
@@ -38,7 +38,7 @@ export function updateFsStoreProductUserTempField(data) {
 // 删除商品信息采集模板字段
 export function delFsStoreProductUserTempField(id) {
   return request({
-    url: '/FsStoreProductUserTempField/FsStoreProductUserTempField/' + id,
+    url: '/store/store/storeProductUserTempField/' + id,
     method: 'delete'
   })
 }
@@ -46,8 +46,8 @@ export function delFsStoreProductUserTempField(id) {
 // 导出商品信息采集模板字段
 export function exportFsStoreProductUserTempField(query) {
   return request({
-    url: '/FsStoreProductUserTempField/FsStoreProductUserTempField/export',
+    url: '/store/store/storeProductUserTempField/export',
     method: 'get',
     params: query
   })
-}
+}

+ 202 - 50
src/views/hisStore/storeProductUserTemp/index.vue

@@ -122,7 +122,7 @@
         <el-row>
           <el-col :span="12">
             <el-form-item label="排序" prop="sort">
-              <el-input-number v-model="form.sort" :min="0" controls-position="right" style="width: 100%;" />
+              <el-input-number v-model="form.sort" :min="0" controls-position="right" style="width: 200px;" />
             </el-form-item>
           </el-col>
         </el-row>
@@ -145,6 +145,7 @@
         <el-form-item label="选项列表">
           <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openFieldDialog">新增选项</el-button>
           <el-table
+            v-loading="fieldLoading"
             :data="fieldList"
             border
             size="small"
@@ -180,12 +181,18 @@
                   size="mini"
                   type="text"
                   icon="el-icon-delete"
-                  @click="deleteFieldRow(scope.$index)"
-                  v-if="fieldList.length > 0"
+                  @click="deleteFieldRow(scope.row)"
                 >删除</el-button>
               </template>
             </el-table-column>
           </el-table>
+          <pagination
+            v-show="fieldTotal > 0"
+            :total="fieldTotal"
+            :page.sync="fieldQueryParams.pageNum"
+            :limit.sync="fieldQueryParams.pageSize"
+            @pagination="handleFieldPagination"
+          />
         </el-form-item>
       </el-form>
       <div slot="footer" class="dialog-footer">
@@ -204,7 +211,7 @@
           <el-switch v-model="fieldForm.isRequired" :active-value="1" :inactive-value="0" />
         </el-form-item>
         <el-form-item label="类型" prop="fieldType">
-          <el-select v-model="fieldForm.fieldType" placeholder="请选择类型" style="width: 100%;" @change="handleFieldTypeChange">
+          <el-select v-model="fieldForm.fieldType" placeholder="请选择类型" style="width: 200px;" @change="handleFieldTypeChange">
             <el-option
               v-for="item in fieldTypeOptions"
               :key="item.value"
@@ -226,7 +233,7 @@
           />
         </el-form-item>
         <el-form-item label="排序" prop="sort">
-          <el-input-number v-model="fieldForm.sort" :min="0" controls-position="right" style="width: 100%;" />
+          <el-input-number v-model="fieldForm.sort" :min="0" controls-position="right" style="width: 200px;" />
         </el-form-item>
         <el-form-item label="状态" prop="status">
           <el-radio-group v-model="fieldForm.status">
@@ -236,7 +243,7 @@
         </el-form-item>
       </el-form>
       <div slot="footer" class="dialog-footer">
-        <el-button type="primary" @click="submitFieldForm">确 定</el-button>
+        <el-button type="primary" :loading="fieldSubmitLoading" @click="submitFieldForm">确 定</el-button>
         <el-button @click="fieldOpen = false">取 消</el-button>
       </div>
     </el-dialog>
@@ -251,7 +258,11 @@ import {
   addFsStoreProductUserTemp,
   updateFsStoreProductUserTemp
 } from '@/api/hisStore/storeProductUserTemp'
-import { listFsStoreProductUserTempField } from '@/api/hisStore/storeProductUserTempField'
+import {
+  listFsStoreProductUserTempField,
+  addFsStoreProductUserTempField,
+  delFsStoreProductUserTempField
+} from '@/api/hisStore/storeProductUserTempField'
 
 export default {
   name: 'FsStoreProductUserTemp',
@@ -275,7 +286,15 @@ export default {
       title: '',
       open: false,
       fieldOpen: false,
+      fieldSubmitLoading: false,
+      fieldLoading: false,
       fieldList: [],
+      fieldTotal: 0,
+      fieldQueryParams: {
+        pageNum: 1,
+        pageSize: 10,
+        tempId: null
+      },
       statusOptions: [],
       fieldTypeOptions: [
         { label: '文本', value: 'text' },
@@ -311,6 +330,70 @@ export default {
     })
   },
   methods: {
+    parseTemplateId(response) {
+      if (response.id != null && response.id !== '') {
+        return response.id
+      }
+      const data = response.data
+      if (data == null || data === '') {
+        return null
+      }
+      if (typeof data === 'object') {
+        return data.id != null && data.id !== '' ? data.id : null
+      }
+      return data
+    },
+    fetchTemplateIdByName() {
+      return listFsStoreProductUserTemp({
+        name: this.form.name,
+        pageNum: 1,
+        pageSize: 10
+      }).then(response => {
+        const rows = (response.rows || []).filter(item => item.name === this.form.name)
+        if (!rows.length) {
+          return null
+        }
+        rows.sort((a, b) => Number(b.id) - Number(a.id))
+        return rows[0].id
+      })
+    },
+    resolveTemplateIdFromResponse(response) {
+      const id = this.parseTemplateId(response)
+      if (id != null) {
+        return Promise.resolve(id)
+      }
+      return this.fetchTemplateIdByName()
+    },
+    saveTemplateAndGetId() {
+      return addFsStoreProductUserTemp(this.buildTemplatePayload()).then(response => {
+        if (response.code !== 200) {
+          return Promise.reject()
+        }
+        return this.resolveTemplateIdFromResponse(response)
+      })
+    },
+    buildTemplatePayload() {
+      const payload = { ...this.form }
+      if (payload.id == null || payload.id === '') {
+        delete payload.id
+      }
+      return payload
+    },
+    hasTemplateId() {
+      return this.form.id != null && this.form.id !== ''
+    },
+    mapFieldItem(item) {
+      return {
+        id: item.id,
+        tempId: item.tempId,
+        fieldName: item.fieldName,
+        isRequired: item.isRequired != null ? Number(item.isRequired) : 0,
+        fieldType: item.fieldType,
+        fieldOption: item.fieldOption || '',
+        sort: item.sort != null ? Number(item.sort) : 0,
+        status: item.status != null ? Number(item.status) : 1
+      }
+    },
     getFieldTypeLabel(value) {
       const item = this.fieldTypeOptions.find(opt => opt.value === value)
       return item ? item.label : value
@@ -323,26 +406,33 @@ export default {
         this.loading = false
       })
     },
-    loadFieldList(tempId) {
-      if (!tempId) {
+    loadFieldList(tempId, resetPage = true) {
+      if (tempId != null && tempId !== '') {
+        this.fieldQueryParams.tempId = tempId
+        if (resetPage) {
+          this.fieldQueryParams.pageNum = 1
+        }
+      }
+      if (!this.fieldQueryParams.tempId) {
         this.fieldList = []
+        this.fieldTotal = 0
         return Promise.resolve()
       }
+      this.fieldLoading = true
       return listFsStoreProductUserTempField({
-        tempId,
-        pageNum: 1,
-        pageSize: 1000
+        tempId: this.fieldQueryParams.tempId,
+        pageNum: this.fieldQueryParams.pageNum,
+        pageSize: this.fieldQueryParams.pageSize
       }).then(response => {
-        this.fieldList = (response.rows || []).map(item => ({
-          fieldName: item.fieldName,
-          isRequired: item.isRequired != null ? Number(item.isRequired) : 0,
-          fieldType: item.fieldType,
-          fieldOption: item.fieldOption || '',
-          sort: item.sort != null ? Number(item.sort) : 0,
-          status: item.status != null ? Number(item.status) : 1
-        }))
+        this.fieldList = (response.rows || []).map(item => this.mapFieldItem(item))
+        this.fieldTotal = response.total || 0
+      }).finally(() => {
+        this.fieldLoading = false
       })
     },
+    handleFieldPagination() {
+      this.loadFieldList(null, false)
+    },
     cancel() {
       this.open = false
       this.reset()
@@ -356,6 +446,12 @@ export default {
         remark: null
       }
       this.fieldList = []
+      this.fieldTotal = 0
+      this.fieldQueryParams = {
+        pageNum: 1,
+        pageSize: 10,
+        tempId: null
+      }
       this.resetForm('form')
     },
     resetFieldForm() {
@@ -394,11 +490,7 @@ export default {
         this.form = response.data
         this.form.status = this.form.status != null ? Number(this.form.status) : 1
         this.form.sort = this.form.sort != null ? Number(this.form.sort) : 0
-        if (response.data.fieldList && response.data.fieldList.length) {
-          this.fieldList = response.data.fieldList
-        } else {
-          return this.loadFieldList(id)
-        }
+        return this.loadFieldList(id)
       }).then(() => {
         this.open = true
         this.title = '修改商品信息采集模板'
@@ -413,40 +505,100 @@ export default {
         this.fieldForm.fieldOption = ''
       }
     },
+    addTemplateIfNeeded() {
+      if (this.hasTemplateId()) {
+        return Promise.resolve(this.form.id)
+      }
+      return new Promise((resolve, reject) => {
+        this.$refs.form.validate(valid => {
+          if (!valid) {
+            this.$message.warning('请先完善模板基本信息')
+            reject()
+            return
+          }
+          this.saveTemplateAndGetId().then(id => {
+            if (id == null) {
+              this.$message.error('模板已保存,但未获取到模板编号,无法新增选项')
+              reject()
+              return
+            }
+            this.$set(this.form, 'id', id)
+            this.title = '修改商品信息采集模板'
+            resolve(id)
+          }).catch(() => {
+            this.$message.error('模板保存失败,无法新增选项')
+            reject()
+          })
+        })
+      })
+    },
     submitFieldForm() {
       this.$refs.fieldForm.validate(valid => {
-        if (valid) {
-          this.fieldList.push({ ...this.fieldForm })
-          this.fieldList.sort((a, b) => a.sort - b.sort)
-          this.fieldOpen = false
+        if (!valid) {
+          return
         }
+        this.fieldSubmitLoading = true
+        this.addTemplateIfNeeded()
+          .then(templateId => {
+            return addFsStoreProductUserTempField({
+              ...this.fieldForm,
+              tempId: templateId
+            })
+          })
+          .then(response => {
+            if (response.code === 200) {
+              this.msgSuccess('新增选项成功')
+              this.fieldOpen = false
+              return this.loadFieldList(this.form.id)
+            }
+            this.$message.error('新增选项失败')
+          })
+          .catch(() => {})
+          .finally(() => {
+            this.fieldSubmitLoading = false
+          })
       })
     },
-    deleteFieldRow(index) {
-      this.fieldList.splice(index, 1)
+    deleteFieldRow(row) {
+      if (row && row.id) {
+        this.$confirm('是否确认删除该选项?', '警告', {
+          confirmButtonText: '确定',
+          cancelButtonText: '取消',
+          type: 'warning'
+        }).then(() => {
+          return delFsStoreProductUserTempField(row.id)
+        }).then(() => {
+          this.msgSuccess('删除成功')
+          if (this.fieldList.length === 1 && this.fieldQueryParams.pageNum > 1) {
+            this.fieldQueryParams.pageNum -= 1
+          }
+          return this.loadFieldList(null, false)
+        }).catch(() => {})
+      }
     },
     submitForm() {
       this.$refs.form.validate(valid => {
-        if (valid) {
-          if (this.fieldList.length === 0) {
-            this.$message.warning('请至少添加一个选项')
-            return
-          }
-          const params = {
-            ...this.form,
-            fieldList: this.fieldList
-          }
-          const request = params.id != null
-            ? updateFsStoreProductUserTemp(params)
-            : addFsStoreProductUserTemp(params)
-          request.then(response => {
-            if (response.code === 200) {
-              this.msgSuccess(params.id != null ? '修改成功' : '新增成功')
-              this.open = false
-              this.getList()
-            }
-          })
+        if (!valid) {
+          return
         }
+        const isUpdate = this.hasTemplateId()
+        const request = isUpdate
+          ? updateFsStoreProductUserTemp({ ...this.form })
+          : addFsStoreProductUserTemp(this.buildTemplatePayload())
+        request.then(response => {
+          if (response.code === 200) {
+            if (!isUpdate) {
+              this.resolveTemplateIdFromResponse(response).then(id => {
+                if (id != null) {
+                  this.$set(this.form, 'id', id)
+                }
+              })
+            }
+            this.msgSuccess(isUpdate ? '修改成功' : '新增成功')
+            this.open = false
+            this.getList()
+          }
+        })
       })
     },
     handleDelete(row) {