BasicInfoForm.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <!-- 情况一:添加/修改 -->
  3. <el-form
  4. v-if="!isDetail"
  5. ref="productSpuBasicInfoRef"
  6. :model="formData"
  7. :rules="rules"
  8. label-width="120px"
  9. >
  10. <el-row>
  11. <el-col :span="12">
  12. <el-form-item label="商品名称" prop="name">
  13. <el-input v-model="formData.name" placeholder="请输入商品名称" />
  14. </el-form-item>
  15. </el-col>
  16. <el-col :span="12">
  17. <!-- TODO @puhui999:只能选根节点 fix: 已完善-->
  18. <el-form-item label="商品分类" prop="categoryId">
  19. <el-tree-select
  20. v-model="formData.categoryId"
  21. :data="categoryList"
  22. :props="defaultProps"
  23. check-strictly
  24. class="w-1/1"
  25. node-key="id"
  26. placeholder="请选择商品分类"
  27. @change="categoryNodeClick"
  28. />
  29. </el-form-item>
  30. </el-col>
  31. <el-col :span="12">
  32. <el-form-item label="商品关键字" prop="keyword">
  33. <el-input v-model="formData.keyword" placeholder="请输入商品关键字" />
  34. </el-form-item>
  35. </el-col>
  36. <el-col :span="12">
  37. <el-form-item label="单位" prop="unit">
  38. <el-select v-model="formData.unit" class="w-1/1" placeholder="请选择单位">
  39. <el-option
  40. v-for="dict in getIntDictOptions(DICT_TYPE.PRODUCT_UNIT)"
  41. :key="dict.value"
  42. :label="dict.label"
  43. :value="dict.value"
  44. />
  45. </el-select>
  46. </el-form-item>
  47. </el-col>
  48. <el-col :span="12">
  49. <el-form-item label="商品简介" prop="introduction">
  50. <el-input
  51. v-model="formData.introduction"
  52. :rows="3"
  53. placeholder="请输入商品简介"
  54. type="textarea"
  55. />
  56. </el-form-item>
  57. </el-col>
  58. <el-col :span="12">
  59. <el-form-item label="商品封面图" prop="picUrl">
  60. <UploadImg v-model="formData.picUrl" height="80px" />
  61. </el-form-item>
  62. </el-col>
  63. <el-col :span="24">
  64. <el-form-item label="商品轮播图" prop="sliderPicUrls">
  65. <UploadImgs v-model:modelValue="formData.sliderPicUrls" />
  66. </el-form-item>
  67. </el-col>
  68. <el-col :span="12">
  69. <el-form-item label="运费模板" prop="deliveryTemplateId">
  70. <el-select v-model="formData.deliveryTemplateId" placeholder="请选择">
  71. <el-option
  72. v-for="item in deliveryTemplateList"
  73. :key="item.id"
  74. :label="item.name"
  75. :value="item.id"
  76. />
  77. </el-select>
  78. <!-- TODO 可能情况:善品录入后选择运费发现下拉选择中没有对应的模版 这里需不需要做添加运费模版后选择的功能 -->
  79. <!-- <el-button class="ml-20px">运费模板</el-button>-->
  80. </el-form-item>
  81. </el-col>
  82. <el-col :span="12">
  83. <el-form-item label="品牌" prop="brandId">
  84. <el-select v-model="formData.brandId" placeholder="请选择">
  85. <el-option
  86. v-for="item in brandList"
  87. :key="item.id"
  88. :label="item.name"
  89. :value="item.id"
  90. />
  91. </el-select>
  92. </el-form-item>
  93. </el-col>
  94. <el-col :span="12">
  95. <el-form-item label="商品规格" props="specType">
  96. <el-radio-group v-model="formData.specType" @change="onChangeSpec">
  97. <el-radio :label="false" class="radio">单规格</el-radio>
  98. <el-radio :label="true">多规格</el-radio>
  99. </el-radio-group>
  100. </el-form-item>
  101. </el-col>
  102. <el-col :span="12">
  103. <el-form-item label="分销类型" props="subCommissionType">
  104. <el-radio-group v-model="formData.subCommissionType" @change="changeSubCommissionType">
  105. <el-radio :label="false">默认设置</el-radio>
  106. <el-radio :label="true" class="radio">自行设置</el-radio>
  107. </el-radio-group>
  108. </el-form-item>
  109. </el-col>
  110. <!-- 多规格添加-->
  111. <el-col :span="24">
  112. <el-form-item v-if="!formData.specType">
  113. <SkuList ref="skuListRef" :prop-form-data="formData" :propertyList="propertyList" />
  114. </el-form-item>
  115. <el-form-item v-if="formData.specType" label="商品属性">
  116. <el-button class="mr-15px mb-10px" @click="attributesAddFormRef.open">添加规格</el-button>
  117. <ProductAttributes :propertyList="propertyList" @success="generateSkus" />
  118. </el-form-item>
  119. <template v-if="formData.specType && propertyList.length > 0">
  120. <el-form-item label="批量设置">
  121. <SkuList :is-batch="true" :prop-form-data="formData" :propertyList="propertyList" />
  122. </el-form-item>
  123. <el-form-item label="属性列表">
  124. <SkuList ref="skuListRef" :prop-form-data="formData" :propertyList="propertyList" />
  125. </el-form-item>
  126. </template>
  127. </el-col>
  128. </el-row>
  129. </el-form>
  130. <!-- 情况二:详情 -->
  131. <Descriptions v-if="isDetail" :data="formData" :schema="allSchemas.detailSchema">
  132. <template #categoryId="{ row }"> {{ categoryString(row.categoryId) }}</template>
  133. <template #brandId="{ row }">
  134. {{ brandList.find((item) => item.id === row.brandId)?.name }}
  135. </template>
  136. <template #deliveryTemplateId="{ row }">
  137. {{ deliveryTemplateList.find((item) => item.id === row.deliveryTemplateId)?.name }}
  138. </template>
  139. <template #specType="{ row }">
  140. {{ row.specType ? '多规格' : '单规格' }}
  141. </template>
  142. <template #subCommissionType="{ row }">
  143. {{ row.subCommissionType ? '自行设置' : '默认设置' }}
  144. </template>
  145. <template #picUrl="{ row }">
  146. <el-image :src="row.picUrl" class="w-60px h-60px" @click="imagePreview(row.picUrl)" />
  147. </template>
  148. <template #sliderPicUrls="{ row }">
  149. <el-image
  150. v-for="(item, index) in row.sliderPicUrls"
  151. :key="index"
  152. :src="item.url"
  153. class="w-60px h-60px mr-10px"
  154. @click="imagePreview(row.sliderPicUrls)"
  155. />
  156. </template>
  157. <template #skus>
  158. <SkuList
  159. ref="skuDetailListRef"
  160. :is-detail="isDetail"
  161. :prop-form-data="formData"
  162. :propertyList="propertyList"
  163. />
  164. </template>
  165. </Descriptions>
  166. <!-- 商品属性添加 Form 表单 -->
  167. <!-- TODO @puhui999: ProductPropertyAddForm 是不是更合适呀 -->
  168. <ProductAttributesAddForm ref="attributesAddFormRef" :propertyList="propertyList" />
  169. </template>
  170. <script lang="ts" name="ProductSpuBasicInfoForm" setup>
  171. import { PropType } from 'vue'
  172. import { isArray } from '@/utils/is'
  173. import { copyValueToTarget } from '@/utils'
  174. import { propTypes } from '@/utils/propTypes'
  175. import { checkSelectedNode, defaultProps, handleTree, treeToString } from '@/utils/tree'
  176. import { createImageViewer } from '@/components/ImageViewer'
  177. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  178. import { UploadImg, UploadImgs } from '@/components/UploadFile'
  179. import { ProductAttributes, ProductAttributesAddForm, SkuList } from './index'
  180. import { basicInfoSchema } from './spu.data'
  181. import type { Spu } from '@/api/mall/product/spu'
  182. import * as ProductCategoryApi from '@/api/mall/product/category'
  183. import { getSimpleBrandList } from '@/api/mall/product/brand'
  184. import { getSimpleTemplateList } from '@/api/mall/trade/delivery/expressTemplate/index'
  185. // ====== 商品详情相关操作 ======
  186. const { allSchemas } = useCrudSchemas(basicInfoSchema)
  187. /** 商品图预览 */
  188. const imagePreview = (args) => {
  189. const urlList = []
  190. if (isArray(args)) {
  191. args.forEach((item) => {
  192. urlList.push(item.url)
  193. })
  194. } else {
  195. urlList.push(args)
  196. }
  197. createImageViewer({
  198. urlList
  199. })
  200. }
  201. // ====== end ======
  202. const message = useMessage() // 消息弹窗
  203. const props = defineProps({
  204. propFormData: {
  205. type: Object as PropType<Spu>,
  206. default: () => {}
  207. },
  208. activeName: propTypes.string.def(''),
  209. isDetail: propTypes.bool.def(false) // 是否作为详情组件
  210. })
  211. const attributesAddFormRef = ref() // 添加商品属性表单
  212. const productSpuBasicInfoRef = ref() // 表单 Ref
  213. const propertyList = ref([]) // 商品属性列表
  214. const skuListRef = ref() // 商品属性列表Ref
  215. /** 调用 SkuList generateTableData 方法*/
  216. const generateSkus = (propertyList) => {
  217. skuListRef.value.generateTableData(propertyList)
  218. }
  219. const formData = reactive<Spu>({
  220. name: '', // 商品名称
  221. categoryId: null, // 商品分类
  222. keyword: '', // 关键字
  223. unit: null, // 单位
  224. picUrl: '', // 商品封面图
  225. sliderPicUrls: [], // 商品轮播图
  226. introduction: '', // 商品简介
  227. deliveryTemplateId: null, // 运费模版
  228. brandId: null, // 商品品牌
  229. specType: false, // 商品规格
  230. subCommissionType: false, // 分销类型
  231. skus: []
  232. })
  233. const rules = reactive({
  234. name: [required],
  235. categoryId: [required],
  236. keyword: [required],
  237. unit: [required],
  238. introduction: [required],
  239. picUrl: [required],
  240. sliderPicUrls: [required],
  241. // deliveryTemplateId: [required],
  242. brandId: [required],
  243. specType: [required],
  244. subCommissionType: [required]
  245. })
  246. /**
  247. * 将传进来的值赋值给 formData
  248. */
  249. watch(
  250. () => props.propFormData,
  251. (data) => {
  252. if (!data) {
  253. return
  254. }
  255. copyValueToTarget(formData, data)
  256. formData.sliderPicUrls = data['sliderPicUrls'].map((item) => ({
  257. url: item
  258. }))
  259. // 只有是多规格才处理
  260. if (!formData.specType) {
  261. return
  262. }
  263. // 直接拿返回的 skus 属性逆向生成出 propertyList
  264. const properties = []
  265. formData.skus.forEach((sku) => {
  266. sku.properties.forEach(({ propertyId, propertyName, valueId, valueName }) => {
  267. // 添加属性
  268. if (!properties.some((item) => item.id === propertyId)) {
  269. properties.push({ id: propertyId, name: propertyName, values: [] })
  270. }
  271. // 添加属性值
  272. const index = properties.findIndex((item) => item.id === propertyId)
  273. if (!properties[index].values.some((value) => value.id === valueId)) {
  274. properties[index].values.push({ id: valueId, name: valueName })
  275. }
  276. })
  277. })
  278. propertyList.value = properties
  279. },
  280. {
  281. immediate: true
  282. }
  283. )
  284. /**
  285. * 表单校验
  286. */
  287. const emit = defineEmits(['update:activeName'])
  288. const validate = async () => {
  289. // 校验 sku
  290. if (!skuListRef.value.validateSku()) {
  291. message.warning('商品相关价格不能低于 0.01 元!!')
  292. throw new Error('商品相关价格不能低于 0.01 元!!')
  293. }
  294. // 校验表单
  295. if (!productSpuBasicInfoRef) return
  296. return await unref(productSpuBasicInfoRef).validate((valid) => {
  297. if (!valid) {
  298. message.warning('商品信息未完善!!')
  299. emit('update:activeName', 'basicInfo')
  300. // 目的截断之后的校验
  301. throw new Error('商品信息未完善!!')
  302. } else {
  303. // 校验通过更新数据
  304. Object.assign(props.propFormData, formData)
  305. }
  306. })
  307. }
  308. defineExpose({ validate })
  309. /** 分销类型 */
  310. const changeSubCommissionType = () => {
  311. // 默认为零,类型切换后也要重置为零
  312. for (const item of formData.skus) {
  313. item.subCommissionFirstPrice = 0
  314. item.subCommissionSecondPrice = 0
  315. }
  316. }
  317. /** 选择规格 */
  318. const onChangeSpec = () => {
  319. // 重置商品属性列表
  320. propertyList.value = []
  321. // 重置sku列表
  322. formData.skus = [
  323. {
  324. price: 0,
  325. marketPrice: 0,
  326. costPrice: 0,
  327. barCode: '',
  328. picUrl: '',
  329. stock: 0,
  330. weight: 0,
  331. volume: 0,
  332. subCommissionFirstPrice: 0,
  333. subCommissionSecondPrice: 0
  334. }
  335. ]
  336. }
  337. const categoryList = ref([]) // 分类树
  338. /**
  339. * 选择分类时触发校验
  340. */
  341. const categoryNodeClick = () => {
  342. if (!checkSelectedNode(categoryList.value, formData.categoryId)) {
  343. formData.categoryId = null
  344. message.warning('必须选择二级及以下节点!!')
  345. }
  346. }
  347. /**
  348. * 获取分类的节点的完整结构
  349. *
  350. * @param categoryId 分类id
  351. */
  352. const categoryString = (categoryId) => {
  353. return treeToString(categoryList.value, categoryId)
  354. }
  355. const brandList = ref([]) // 精简商品品牌列表
  356. const deliveryTemplateList = ref([]) // 运费模版
  357. onMounted(async () => {
  358. // 获得分类树
  359. const data = await ProductCategoryApi.getCategoryList({})
  360. categoryList.value = handleTree(data, 'id', 'parentId')
  361. // 获取商品品牌列表
  362. brandList.value = await getSimpleBrandList()
  363. // 获取运费模版
  364. deliveryTemplateList.value = await getSimpleTemplateList()
  365. })
  366. </script>