index.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import BasicInfoForm from './BasicInfoForm.vue'
  2. import DescriptionForm from './DescriptionForm.vue'
  3. import OtherSettingsForm from './OtherSettingsForm.vue'
  4. import ProductAttributes from './ProductAttributes.vue'
  5. import ProductPropertyAddForm from './ProductPropertyAddForm.vue'
  6. import SkuList from './SkuList.vue'
  7. import { Spu } from '@/api/mall/product/spu'
  8. interface Properties {
  9. id: number
  10. name: string
  11. values?: Properties[]
  12. }
  13. interface RuleConfig {
  14. name: string // 需要校验的字段
  15. geValue: number // TODO 暂定大于一个数字
  16. }
  17. /**
  18. * 商品通用函数
  19. * @param spu
  20. */
  21. const getPropertyList = (spu: Spu): Properties[] => {
  22. // 直接拿返回的 skus 属性逆向生成出 propertyList
  23. const properties: Properties[] = []
  24. // 只有是多规格才处理
  25. if (spu.specType) {
  26. spu.skus?.forEach((sku) => {
  27. sku.properties?.forEach(({ propertyId, propertyName, valueId, valueName }) => {
  28. // 添加属性
  29. if (!properties?.some((item) => item.id === propertyId)) {
  30. properties.push({ id: propertyId!, name: propertyName!, values: [] })
  31. }
  32. // 添加属性值
  33. const index = properties?.findIndex((item) => item.id === propertyId)
  34. if (!properties[index].values?.some((value) => value.id === valueId)) {
  35. properties[index].values?.push({ id: valueId!, name: valueName! })
  36. }
  37. })
  38. })
  39. }
  40. return properties
  41. }
  42. export {
  43. BasicInfoForm,
  44. DescriptionForm,
  45. OtherSettingsForm,
  46. ProductAttributes,
  47. ProductPropertyAddForm,
  48. SkuList,
  49. getPropertyList,
  50. Properties,
  51. RuleConfig
  52. }