index.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 PropertyAndValues {
  9. id: number
  10. name: string
  11. values?: PropertyAndValues[]
  12. }
  13. interface RuleConfig {
  14. // 需要校验的字段
  15. // 例:name: 'name' 则表示校验 sku.name 的值
  16. // 例:name: 'productConfig.stock' 则表示校验 sku.productConfig.name 的值,此处 productConfig 表示我在 Sku 上扩展的属性
  17. name: string
  18. // 校验规格为一个毁掉函数,其中 arg 为需要校验的字段的值。
  19. // 例:需要校验价格必须大于0.01
  20. // {
  21. // name:'price',
  22. // rule:(arg: number) => arg > 0.01
  23. // }
  24. rule: (arg: any) => boolean
  25. // 校验不通过时的消息提示
  26. message: string
  27. }
  28. /**
  29. * 获得商品的规格列表
  30. *
  31. * @param spu
  32. * @return PropertyAndValues 规格列表
  33. */
  34. const getPropertyList = (spu: Spu): PropertyAndValues[] => {
  35. // 直接拿返回的 skus 属性逆向生成出 propertyList
  36. const properties: PropertyAndValues[] = []
  37. // 只有是多规格才处理
  38. if (spu.specType) {
  39. spu.skus?.forEach((sku) => {
  40. sku.properties?.forEach(({ propertyId, propertyName, valueId, valueName }) => {
  41. // 添加属性
  42. if (!properties?.some((item) => item.id === propertyId)) {
  43. properties.push({ id: propertyId!, name: propertyName!, values: [] })
  44. }
  45. // 添加属性值
  46. const index = properties?.findIndex((item) => item.id === propertyId)
  47. if (!properties[index].values?.some((value) => value.id === valueId)) {
  48. properties[index].values?.push({ id: valueId!, name: valueName! })
  49. }
  50. })
  51. })
  52. }
  53. return properties
  54. }
  55. export {
  56. BasicInfoForm,
  57. DescriptionForm,
  58. OtherSettingsForm,
  59. ProductAttributes,
  60. ProductPropertyAddForm,
  61. SkuList,
  62. getPropertyList,
  63. PropertyAndValues,
  64. RuleConfig
  65. }