addForm.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <ContentWrap v-loading="formLoading">
  3. <el-tabs v-model="activeName">
  4. <el-tab-pane label="商品信息" name="basicInfo">
  5. <BasicInfoForm
  6. ref="BasicInfoRef"
  7. v-model:activeName="activeName"
  8. :propFormData="formData"
  9. />
  10. </el-tab-pane>
  11. <el-tab-pane label="商品详情" name="description">
  12. <DescriptionForm
  13. ref="DescriptionRef"
  14. v-model:activeName="activeName"
  15. :propFormData="formData"
  16. />
  17. </el-tab-pane>
  18. <el-tab-pane label="其他设置" name="otherSettings">
  19. <OtherSettingsForm
  20. ref="OtherSettingsRef"
  21. v-model:activeName="activeName"
  22. :propFormData="formData"
  23. />
  24. </el-tab-pane>
  25. </el-tabs>
  26. <el-form>
  27. <el-form-item style="float: right">
  28. <el-button :loading="formLoading" type="primary" @click="submitForm">保存</el-button>
  29. <el-button @click="close">返回</el-button>
  30. </el-form-item>
  31. </el-form>
  32. </ContentWrap>
  33. </template>
  34. <script lang="ts" name="ProductManagementForm" setup>
  35. import { useTagsViewStore } from '@/store/modules/tagsView'
  36. import { BasicInfoForm, DescriptionForm, OtherSettingsForm } from './components'
  37. import type { SpuType } from '@/api/mall/product/management/type/spuType' // 业务api
  38. import * as managementApi from '@/api/mall/product/management/spu'
  39. const { t } = useI18n() // 国际化
  40. const message = useMessage() // 消息弹窗
  41. const { push, currentRoute } = useRouter() // 路由
  42. const { query } = useRoute() // 查询参数
  43. const { delView } = useTagsViewStore() // 视图操作
  44. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  45. const activeName = ref('basicInfo') // Tag 激活的窗口
  46. const BasicInfoRef = ref<ComponentRef<typeof BasicInfoForm>>() // 商品信息Ref
  47. const DescriptionRef = ref<ComponentRef<typeof DescriptionForm>>() // 商品详情Ref
  48. const OtherSettingsRef = ref<ComponentRef<typeof OtherSettingsForm>>() // 其他设置Ref
  49. const formData = ref<SpuType>({
  50. name: '213', // 商品名称
  51. categoryId: null, // 商品分类
  52. keyword: '213', // 关键字
  53. unit: null, // 单位
  54. picUrl:
  55. 'http://127.0.0.1:48080/admin-api/infra/file/4/get/6ffdf8f5dfc03f7ceec1ff1ebc138adb8b721a057d505ccfb0e61a8783af1371.png', // 商品封面图
  56. sliderPicUrls: [
  57. {
  58. name: 'http://127.0.0.1:48080/admin-api/infra/file/4/get/6ffdf8f5dfc03f7ceec1ff1ebc138adb8b721a057d505ccfb0e61a8783af1371.png',
  59. url: 'http://127.0.0.1:48080/admin-api/infra/file/4/get/6ffdf8f5dfc03f7ceec1ff1ebc138adb8b721a057d505ccfb0e61a8783af1371.png'
  60. }
  61. ], // 商品轮播图
  62. introduction: '213', // 商品简介
  63. deliveryTemplateId: 0, // 运费模版
  64. specType: false, // 商品规格
  65. subCommissionType: false, // 分销类型
  66. skus: [
  67. {
  68. /**
  69. * 商品价格,单位:分
  70. */
  71. price: 0,
  72. /**
  73. * 市场价,单位:分
  74. */
  75. marketPrice: 0,
  76. /**
  77. * 成本价,单位:分
  78. */
  79. costPrice: 0,
  80. /**
  81. * 商品条码
  82. */
  83. barCode: '',
  84. /**
  85. * 图片地址
  86. */
  87. picUrl: '',
  88. /**
  89. * 库存
  90. */
  91. stock: 0,
  92. /**
  93. * 商品重量,单位:kg 千克
  94. */
  95. weight: 0,
  96. /**
  97. * 商品体积,单位:m^3 平米
  98. */
  99. volume: 0,
  100. /**
  101. * 一级分销的佣金,单位:分
  102. */
  103. subCommissionFirstPrice: 0,
  104. /**
  105. * 二级分销的佣金,单位:分
  106. */
  107. subCommissionSecondPrice: 0
  108. }
  109. ],
  110. description: '5425', // 商品详情
  111. sort: 1, // 商品排序
  112. giveIntegral: 1, // 赠送积分
  113. virtualSalesCount: 1, // 虚拟销量
  114. recommendHot: false, // 是否热卖
  115. recommendBenefit: false, // 是否优惠
  116. recommendBest: false, // 是否精品
  117. recommendNew: false, // 是否新品
  118. recommendGood: false // 是否优品
  119. })
  120. /** 获得详情 */
  121. const getDetail = async () => {
  122. const id = query.id as unknown as number
  123. if (!id) {
  124. return
  125. }
  126. }
  127. /** 提交按钮 */
  128. const submitForm = async () => {
  129. // 提交请求
  130. formLoading.value = true
  131. const newSkus = [...formData.value.skus] //复制一份skus保存失败时使用
  132. // TODO 三个表单逐一校验,如果有一个表单校验不通过则切换到对应表单,如果有两个及以上的情况则切换到最前面的一个并弹出提示消息
  133. // 校验各表单
  134. try {
  135. await unref(BasicInfoRef)?.validate()
  136. await unref(DescriptionRef)?.validate()
  137. await unref(OtherSettingsRef)?.validate()
  138. // 处理掉一些无关数据
  139. formData.value.skus.forEach((item) => {
  140. // 给sku name赋值
  141. item.name = formData.value.name
  142. // 多规格情况移除skus相关属性值value
  143. if (formData.value.specType) {
  144. item.properties.forEach((item2) => {
  145. delete item2.value
  146. })
  147. }
  148. })
  149. // 处理轮播图列表
  150. const newSliderPicUrls = []
  151. formData.value.sliderPicUrls.forEach((item) => {
  152. // 如果是前端选的图
  153. if (typeof item === 'object') {
  154. newSliderPicUrls.push(item.url)
  155. } else {
  156. newSliderPicUrls.push(item)
  157. }
  158. })
  159. formData.value.sliderPicUrls = newSliderPicUrls
  160. // 校验都通过后提交表单
  161. const data = formData.value as SpuType
  162. // 移除skus.
  163. const id = query.id as unknown as number
  164. if (!id) {
  165. await managementApi.createSpu(data)
  166. message.success(t('common.createSuccess'))
  167. } else {
  168. await managementApi.updateSpu(data)
  169. message.success(t('common.updateSuccess'))
  170. }
  171. } catch (e) {
  172. console.log(e)
  173. console.log(newSkus)
  174. } finally {
  175. formLoading.value = false
  176. }
  177. }
  178. /**
  179. * 重置表单
  180. */
  181. const resetForm = async () => {
  182. formData.value = {
  183. name: '', // 商品名称
  184. categoryId: 0, // 商品分类
  185. keyword: '', // 关键字
  186. unit: '', // 单位
  187. picUrl: '', // 商品封面图
  188. sliderPicUrls: [], // 商品轮播图
  189. introduction: '', // 商品简介
  190. deliveryTemplateId: 0, // 运费模版
  191. selectRule: '',
  192. specType: false, // 商品规格
  193. subCommissionType: false, // 分销类型
  194. description: '', // 商品详情
  195. sort: 1, // 商品排序
  196. giveIntegral: 1, // 赠送积分
  197. virtualSalesCount: 1, // 虚拟销量
  198. recommendHot: false, // 是否热卖
  199. recommendBenefit: false, // 是否优惠
  200. recommendBest: false, // 是否精品
  201. recommendNew: false, // 是否新品
  202. recommendGood: false // 是否优品
  203. }
  204. }
  205. /** 关闭按钮 */
  206. const close = () => {
  207. resetForm()
  208. delView(unref(currentRoute))
  209. push('/product/product-management')
  210. }
  211. /** 初始化 */
  212. onMounted(() => {
  213. getDetail()
  214. })
  215. </script>