BasicInfoForm.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <el-form ref="ProductManagementBasicInfoRef" :model="formData" :rules="rules" label-width="120px">
  3. <el-row>
  4. <el-col :span="12">
  5. <el-form-item label="商品名称" prop="name">
  6. <el-input v-model="formData.name" placeholder="请输入商品名称" />
  7. </el-form-item>
  8. </el-col>
  9. <el-col :span="12">
  10. <el-form-item label="商品分类" prop="categoryId">
  11. <el-tree-select
  12. v-model="formData.categoryId"
  13. :data="categoryList"
  14. :props="defaultProps"
  15. check-strictly
  16. node-key="id"
  17. placeholder="请选择商品分类"
  18. />
  19. </el-form-item>
  20. </el-col>
  21. <el-col :span="12">
  22. <el-form-item label="商品关键字" prop="keyword">
  23. <el-input v-model="formData.keyword" placeholder="请输入商品关键字" />
  24. </el-form-item>
  25. </el-col>
  26. <el-col :span="12">
  27. <el-form-item label="单位" prop="unit">
  28. <el-input v-model="formData.unit" placeholder="请输入单位" />
  29. </el-form-item>
  30. </el-col>
  31. <el-col :span="12">
  32. <el-form-item label="商品简介" prop="introduction">
  33. <el-input
  34. v-model="formData.introduction"
  35. :rows="3"
  36. placeholder="请输入商品简介"
  37. type="textarea"
  38. />
  39. </el-form-item>
  40. </el-col>
  41. <el-col :span="12">
  42. <el-form-item label="商品封面图" prop="picUrl">
  43. <UploadImg v-model="formData.picUrl" height="80px" />
  44. </el-form-item>
  45. </el-col>
  46. <el-col :span="24">
  47. <el-form-item label="商品轮播图" prop="sliderPicUrls">
  48. <UploadImgs v-model="formData.sliderPicUrls" />
  49. </el-form-item>
  50. </el-col>
  51. <el-col :span="12">
  52. <el-form-item label="运费模板" prop="deliveryTemplateId">
  53. <el-select v-model="formData.deliveryTemplateId" placeholder="请选择" style="width: 100%">
  54. <el-option v-for="item in []" :key="item.id" :label="item.name" :value="item.id" />
  55. </el-select>
  56. </el-form-item>
  57. </el-col>
  58. <el-col :span="12">
  59. <el-button class="ml-20px">运费模板</el-button>
  60. </el-col>
  61. <el-col :span="12">
  62. <el-form-item label="商品规格" props="specType">
  63. <el-radio-group v-model="formData.specType" @change="changeSpecType(formData.specType)">
  64. <el-radio :label="false" class="radio">单规格</el-radio>
  65. <el-radio :label="true">多规格</el-radio>
  66. </el-radio-group>
  67. </el-form-item>
  68. </el-col>
  69. <el-col :span="12">
  70. <el-form-item label="分销类型" props="subCommissionType">
  71. <el-radio-group v-model="formData.subCommissionType" @change="changeSubCommissionType">
  72. <el-radio :label="false">默认设置</el-radio>
  73. <el-radio :label="true" class="radio">自行设置</el-radio>
  74. </el-radio-group>
  75. </el-form-item>
  76. </el-col>
  77. <!-- 多规格添加-->
  78. <el-col :span="24">
  79. <el-form-item v-if="formData.specType" label="商品属性" prop="">
  80. <el-button class="mr-15px" @click="AttributesAddFormRef.open()">添加规格</el-button>
  81. <ProductAttributes :attribute-data="attributeList" />
  82. </el-form-item>
  83. <el-form-item>
  84. <SkuList :sku-data="formData.skus" :subCommissionType="formData.subCommissionType" />
  85. </el-form-item>
  86. </el-col>
  87. </el-row>
  88. </el-form>
  89. <ProductAttributesAddForm ref="AttributesAddFormRef" @success="addAttribute" />
  90. </template>
  91. <script lang="ts" name="ProductManagementBasicInfoForm" setup>
  92. import { PropType } from 'vue'
  93. import type { SpuType } from '@/api/mall/product/management/type/spuType'
  94. import { UploadImg, UploadImgs } from '@/components/UploadFile'
  95. import SkuList from './SkuList/index.vue'
  96. import ProductAttributesAddForm from './ProductAttributesAddForm.vue'
  97. import ProductAttributes from './ProductAttributes.vue'
  98. import { copyValueToTarget } from '@/utils/object'
  99. // 业务Api
  100. import * as ProductCategoryApi from '@/api/mall/product/category'
  101. import * as PropertyApi from '@/api/mall/product/property'
  102. import { defaultProps, handleTree } from '@/utils/tree'
  103. import { ElInput } from 'element-plus'
  104. const message = useMessage() // 消息弹窗
  105. const props = defineProps({
  106. propFormData: {
  107. type: Object as PropType<SpuType>,
  108. default: () => {}
  109. }
  110. })
  111. const AttributesAddFormRef = ref() // 添加商品属性表单
  112. const ProductManagementBasicInfoRef = ref() // 表单Ref
  113. // 属性列表
  114. const attributeList = ref([
  115. {
  116. id: 1,
  117. name: '颜色',
  118. attributeValues: [{ id: 1, name: '白色' }]
  119. }
  120. ])
  121. const addAttribute = async (propertyId: number) => {
  122. const data = await PropertyApi.getPropertyValuePage({ id: propertyId })
  123. console.log(data)
  124. }
  125. const formData = reactive<SpuType>({
  126. name: '', // 商品名称
  127. categoryId: undefined, // 商品分类
  128. keyword: '', // 关键字
  129. unit: '', // 单位
  130. picUrl: '', // 商品封面图
  131. sliderPicUrls: [], // 商品轮播图
  132. introduction: '', // 商品简介
  133. deliveryTemplateId: 1, // 运费模版
  134. selectRule: '', // 选择规则 TODO 暂定
  135. specType: false, // 商品规格
  136. subCommissionType: false, // 分销类型
  137. skus: [
  138. {
  139. /**
  140. * 商品价格,单位:分
  141. */
  142. price: 0,
  143. /**
  144. * 市场价,单位:分
  145. */
  146. marketPrice: 0,
  147. /**
  148. * 成本价,单位:分
  149. */
  150. costPrice: 0,
  151. /**
  152. * 商品条码
  153. */
  154. barCode: '',
  155. /**
  156. * 图片地址
  157. */
  158. picUrl: '',
  159. /**
  160. * 库存
  161. */
  162. stock: 0,
  163. /**
  164. * 商品重量,单位:kg 千克
  165. */
  166. weight: 0,
  167. /**
  168. * 商品体积,单位:m^3 平米
  169. */
  170. volume: 0
  171. }
  172. ]
  173. })
  174. const rules = reactive({
  175. name: [required],
  176. categoryId: [required],
  177. keyword: [required],
  178. unit: [required],
  179. introduction: [required],
  180. picUrl: [required],
  181. sliderPicUrls: [required]
  182. // deliveryTemplateId: [required],
  183. // specType: [required],
  184. // subCommissionType: [required],
  185. })
  186. /**
  187. * 将传进来的值赋值给formData
  188. */
  189. watch(
  190. () => props.propFormData,
  191. (data) => {
  192. if (!data) return
  193. copyValueToTarget(formData, data)
  194. },
  195. {
  196. deep: true,
  197. immediate: true
  198. }
  199. )
  200. const emit = defineEmits(['update:activeName'])
  201. /**
  202. * 表单校验
  203. */
  204. const validate = async () => {
  205. // 校验表单
  206. if (!ProductManagementBasicInfoRef) return
  207. return await unref(ProductManagementBasicInfoRef).validate((valid) => {
  208. if (!valid) {
  209. message.warning('商品信息未完善!!')
  210. emit('update:activeName', 'basicInfo')
  211. // 目的截断之后的校验
  212. throw new Error('商品信息未完善!!')
  213. } else {
  214. // 校验通过更新数据
  215. Object.assign(props.propFormData, formData)
  216. }
  217. })
  218. }
  219. defineExpose({ validate })
  220. // 选择规格
  221. const changeSpecType = (specType) => {
  222. console.log(specType)
  223. }
  224. // 分销类型
  225. const changeSubCommissionType = () => {
  226. // 默认为零,类型切换后也要重置为零
  227. for (const item of formData.skus) {
  228. item.subCommissionFirstPrice = 0
  229. item.subCommissionSecondPrice = 0
  230. }
  231. }
  232. // 选择属性确认
  233. // const confirm = () => {}
  234. // 添加规格
  235. // const addRule = () => {}
  236. const categoryList = ref() // 分类树
  237. onMounted(async () => {
  238. // 获得分类树
  239. const data = await ProductCategoryApi.getCategoryList({})
  240. categoryList.value = handleTree(data, 'id', 'parentId')
  241. })
  242. </script>