Kaynağa Gözat

商品管理: fix:根据商品属性动态生成表格值

puhui999 2 yıl önce
ebeveyn
işleme
538d1e0b6c

+ 0 - 1
src/api/mall/product/management/type/spuType.ts

@@ -9,7 +9,6 @@ export interface SpuType {
   sliderPicUrls?: string[] // 商品轮播图
   sliderPicUrls?: string[] // 商品轮播图
   introduction?: string // 商品简介
   introduction?: string // 商品简介
   deliveryTemplateId?: number // 运费模版
   deliveryTemplateId?: number // 运费模版
-  selectRule?: string // 选择规格 TODO 暂时定义
   specType?: boolean // 商品规格
   specType?: boolean // 商品规格
   subCommissionType?: boolean // 分销类型
   subCommissionType?: boolean // 分销类型
   skus?: SkuType[] // sku数组
   skus?: SkuType[] // sku数组

+ 16 - 23
src/views/mall/product/management/components/BasicInfoForm.vue

@@ -60,7 +60,7 @@
       </el-col>
       </el-col>
       <el-col :span="12">
       <el-col :span="12">
         <el-form-item label="商品规格" props="specType">
         <el-form-item label="商品规格" props="specType">
-          <el-radio-group v-model="formData.specType" @change="changeSpecType(formData.specType)">
+          <el-radio-group v-model="formData.specType">
             <el-radio :label="false" class="radio">单规格</el-radio>
             <el-radio :label="false" class="radio">单规格</el-radio>
             <el-radio :label="true">多规格</el-radio>
             <el-radio :label="true">多规格</el-radio>
           </el-radio-group>
           </el-radio-group>
@@ -76,12 +76,17 @@
       </el-col>
       </el-col>
       <!-- 多规格添加-->
       <!-- 多规格添加-->
       <el-col :span="24">
       <el-col :span="24">
-        <el-form-item v-if="formData.specType" label="商品属性" prop="">
-          <el-button class="mr-15px" @click="AttributesAddFormRef.open()">添加规格</el-button>
+        <el-form-item v-if="formData.specType" label="商品属性">
+          <el-button class="mr-15px mb-10px" @click="AttributesAddFormRef.open()"
+            >添加规格
+          </el-button>
           <ProductAttributes :attribute-data="attributeList" />
           <ProductAttributes :attribute-data="attributeList" />
         </el-form-item>
         </el-form-item>
+        <el-form-item v-if="formData.specType" label="批量设置">
+          <SkuList :attributeList="attributeList" :is-batch="true" :prop-form-data="formData" />
+        </el-form-item>
         <el-form-item>
         <el-form-item>
-          <SkuList :sku-data="formData.skus" :subCommissionType="formData.subCommissionType" />
+          <SkuList :attributeList="attributeList" :prop-form-data="formData" />
         </el-form-item>
         </el-form-item>
       </el-col>
       </el-col>
     </el-row>
     </el-row>
@@ -110,14 +115,8 @@ const props = defineProps({
 })
 })
 const AttributesAddFormRef = ref() // 添加商品属性表单
 const AttributesAddFormRef = ref() // 添加商品属性表单
 const ProductManagementBasicInfoRef = ref() // 表单Ref
 const ProductManagementBasicInfoRef = ref() // 表单Ref
-// 属性列表
-const attributeList = ref([
-  {
-    id: 1,
-    name: '颜色',
-    values: [{ id: 1, name: '白色' }]
-  }
-])
+const attributeList = ref([]) // 商品属性列表
+/** 添加商品属性 */
 const addAttribute = (property: any) => {
 const addAttribute = (property: any) => {
   attributeList.value.push(property)
   attributeList.value.push(property)
 }
 }
@@ -176,10 +175,10 @@ const rules = reactive({
   unit: [required],
   unit: [required],
   introduction: [required],
   introduction: [required],
   picUrl: [required],
   picUrl: [required],
-  sliderPicUrls: [required]
+  sliderPicUrls: [required],
   // deliveryTemplateId: [required],
   // deliveryTemplateId: [required],
-  // specType: [required],
-  // subCommissionType: [required],
+  specType: [required],
+  subCommissionType: [required]
 })
 })
 /**
 /**
  * 将传进来的值赋值给formData
  * 将传进来的值赋值给formData
@@ -215,10 +214,7 @@ const validate = async () => {
   })
   })
 }
 }
 defineExpose({ validate })
 defineExpose({ validate })
-// 选择规格
-const changeSpecType = (specType) => {
-  console.log(specType)
-}
+
 // 分销类型
 // 分销类型
 const changeSubCommissionType = () => {
 const changeSubCommissionType = () => {
   // 默认为零,类型切换后也要重置为零
   // 默认为零,类型切换后也要重置为零
@@ -227,10 +223,7 @@ const changeSubCommissionType = () => {
     item.subCommissionSecondPrice = 0
     item.subCommissionSecondPrice = 0
   }
   }
 }
 }
-// 选择属性确认
-// const confirm = () => {}
-// 添加规格
-// const addRule = () => {}
+
 const categoryList = ref() // 分类树
 const categoryList = ref() // 分类树
 onMounted(async () => {
 onMounted(async () => {
   // 获得分类树
   // 获得分类树

+ 2 - 2
src/views/mall/product/management/components/ProductAttributes.vue

@@ -49,10 +49,10 @@ const inputVisible = computed(() => (index) => {
   if (attributeIndex.value === index) return true
   if (attributeIndex.value === index) return true
 })
 })
 const InputRef = ref() //标签输入框Ref
 const InputRef = ref() //标签输入框Ref
-const attributeList = ref([])
+const attributeList = ref([]) // 商品属性列表
 const props = defineProps({
 const props = defineProps({
   attributeData: {
   attributeData: {
-    type: Object,
+    type: Array,
     default: () => {}
     default: () => {}
   }
   }
 })
 })

+ 3 - 0
src/views/mall/product/management/components/ProductAttributesAddForm.vue

@@ -62,6 +62,9 @@ const submitForm = async () => {
       const propertyId = await PropertyApi.createProperty(data)
       const propertyId = await PropertyApi.createProperty(data)
       emit('success', { id: propertyId, ...formData.value, values: [] })
       emit('success', { id: propertyId, ...formData.value, values: [] })
     } else {
     } else {
+      if (res[0].values === null) {
+        res[0].values = []
+      }
       emit('success', res[0]) // 因为只用一个
       emit('success', res[0]) // 因为只用一个
     }
     }
     message.success(t('common.createSuccess'))
     message.success(t('common.createSuccess'))

+ 119 - 10
src/views/mall/product/management/components/SkuList/index.vue

@@ -1,10 +1,21 @@
 <template>
 <template>
-  <el-table :data="SkuData" border class="tabNumWidth" size="small">
+  <el-table :data="isBatch ? SkuData : formData.skus" border class="tabNumWidth" size="small">
     <el-table-column align="center" fixed="left" label="图片" min-width="100">
     <el-table-column align="center" fixed="left" label="图片" min-width="100">
       <template #default="{ row }">
       <template #default="{ row }">
         <UploadImg v-model="row.picUrl" height="80px" width="100%" />
         <UploadImg v-model="row.picUrl" height="80px" width="100%" />
       </template>
       </template>
     </el-table-column>
     </el-table-column>
+    <template v-if="formData.specType">
+      <!--  根据商品属性动态添加  -->
+      <el-table-column
+        v-for="(item, index) in tableHeaderList"
+        :key="index"
+        :label="item.label"
+        :prop="item.prop"
+        align="center"
+        min-width="120"
+      />
+    </template>
     <el-table-column align="center" label="商品条码" min-width="120">
     <el-table-column align="center" label="商品条码" min-width="120">
       <template #default="{ row }">
       <template #default="{ row }">
         <el-input v-model="row.barCode" :min="0" class="w-100%" />
         <el-input v-model="row.barCode" :min="0" class="w-100%" />
@@ -40,7 +51,7 @@
         <el-input v-model="row.volume" :min="0" class="w-100%" type="number" />
         <el-input v-model="row.volume" :min="0" class="w-100%" type="number" />
       </template>
       </template>
     </el-table-column>
     </el-table-column>
-    <template v-if="subCommissionType">
+    <template v-if="formData.subCommissionType">
       <el-table-column align="center" label="一级返佣(分)" min-width="120">
       <el-table-column align="center" label="一级返佣(分)" min-width="120">
         <template #default="{ row }">
         <template #default="{ row }">
           <el-input v-model="row.subCommissionFirstPrice" :min="0" class="w-100%" type="number" />
           <el-input v-model="row.subCommissionFirstPrice" :min="0" class="w-100%" type="number" />
@@ -52,35 +63,133 @@
         </template>
         </template>
       </el-table-column>
       </el-table-column>
     </template>
     </template>
+    <el-table-column v-if="formData.specType" align="center" fixed="right" label="操作" width="80">
+      <template #default>
+        <el-button v-if="isBatch" link size="small" type="primary">批量添加</el-button>
+        <el-button v-else link size="small" type="primary">删除</el-button>
+      </template>
+    </el-table-column>
   </el-table>
   </el-table>
 </template>
 </template>
 
 
 <script lang="ts" name="index" setup>
 <script lang="ts" name="index" setup>
-import { propTypes } from '@/utils/propTypes'
 import { UploadImg } from '@/components/UploadFile'
 import { UploadImg } from '@/components/UploadFile'
 import { PropType } from 'vue'
 import { PropType } from 'vue'
-import type { SkuType } from '@/api/mall/product/management/type/skuType'
+import { SpuType } from '@/api/mall/product/management/type/spuType'
+import { propTypes } from '@/utils/propTypes'
+import { SkuType } from '@/api/mall/product/management/type/skuType'
 
 
 const props = defineProps({
 const props = defineProps({
-  skuData: {
-    type: Array as PropType<SkuType>,
+  propFormData: {
+    type: Object as PropType<SpuType>,
+    default: () => {}
+  },
+  attributeList: {
+    type: Array,
     default: () => []
     default: () => []
   },
   },
-  subCommissionType: propTypes.bool.def(false) // 分销类型
+  isBatch: propTypes.bool.def(false) // 是否批量操作
 })
 })
-const SkuData = ref<SkuType[]>([])
+const formData = ref<SpuType>() // 表单数据
+// 批量添加时的零时数据
+const SkuData = ref<SkuType[]>([
+  {
+    /**
+     * 商品价格,单位:分
+     */
+    price: 0,
+    /**
+     * 市场价,单位:分
+     */
+    marketPrice: 0,
+    /**
+     * 成本价,单位:分
+     */
+    costPrice: 0,
+    /**
+     * 商品条码
+     */
+    barCode: '',
+    /**
+     * 图片地址
+     */
+    picUrl: '',
+    /**
+     * 库存
+     */
+    stock: 0,
+    /**
+     * 商品重量,单位:kg 千克
+     */
+    weight: 0,
+    /**
+     * 商品体积,单位:m^3 平米
+     */
+    volume: 0
+  }
+])
+const tableHeaderList = ref<{ prop: string; label: string }[]>([])
 /**
 /**
  * 将传进来的值赋值给SkuData
  * 将传进来的值赋值给SkuData
  */
  */
 watch(
 watch(
-  () => props.skuData,
+  () => props.propFormData,
   (data) => {
   (data) => {
     if (!data) return
     if (!data) return
-    SkuData.value = data
+    formData.value = data
   },
   },
   {
   {
     deep: true,
     deep: true,
     immediate: true
     immediate: true
   }
   }
 )
 )
+/** 监听属性列表生成相关参数和表头 */
+watch(
+  () => props.attributeList,
+  (data) => {
+    // 判断代理对象是否为空
+    if (JSON.stringify(data) === '[]') return
+    // 重置表头
+    tableHeaderList.value = []
+    // 重置表数据
+    formData.value!.skus = []
+    SkuData.value = []
+    // 生成表头
+    data.forEach((item, index) => {
+      // name加属性项index区分属性值
+      tableHeaderList.value.push({ prop: `name${index}`, label: item.name })
+    })
+    generateTableData(data)
+  },
+  {
+    deep: true,
+    immediate: true
+  }
+)
+/** 生成表数据 */
+const generateTableData = (data: any[]) => {
+  // const row = {
+  //   price: 0,
+  //   marketPrice: 0,
+  //   costPrice: 0,
+  //   barCode: '',
+  //   picUrl: '',
+  //   stock: 0,
+  //   weight: 0,
+  //   volume: 0
+  // }
+  // 先把所有的属性值取出来
+  const newDataList: any[] = []
+  for (const index in data) {
+    newDataList.push(data[index].values)
+  }
+  console.log(newDataList)
+}
+// const buildRow = (list: any[]) => {
+//   for (const index in data) {
+//     for (const index1 of data[index].values) {
+//       row[`name${index1}`] = data[index].values[index1]
+//     }
+//   }
+// }
 </script>
 </script>