index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <template>
  2. <el-dialog v-model="dialogVisible" :title="dialogTitle" width="70%" class="dialog">
  3. <ContentWrap v-loading="formLoading">
  4. <div class="left">
  5. <img :src="picUrl" @error="handleError" />
  6. <el-tabs v-model="activeName" tab-position="left">
  7. <el-tab-pane label="基础设置" name="info" />
  8. <el-tab-pane label="价格库存" name="sku" />
  9. <el-tab-pane label="物流设置" name="delivery" />
  10. <el-tab-pane label="商品详情" name="description" />
  11. <el-tab-pane label="其它设置" name="other" />
  12. </el-tabs>
  13. </div>
  14. <div class="right">
  15. <div v-show="activeName == 'info'">
  16. <InfoForm ref="infoRef" :is-detail="isDetail" :propFormData="formData" />
  17. </div>
  18. <div v-show="activeName == 'sku'">
  19. <SkuForm ref="skuRef" :is-detail="isDetail" :propFormData="formData" />
  20. </div>
  21. <div v-show="activeName == 'delivery'">
  22. <DeliveryForm ref="deliveryRef" :is-detail="isDetail" :propFormData="formData" />
  23. </div>
  24. <div v-show="activeName == 'description'">
  25. <DescriptionForm ref="descriptionRef" :is-detail="isDetail" :propFormData="formData" />
  26. </div>
  27. <div v-show="activeName == 'other'">
  28. <OtherForm ref="otherRef" :is-detail="isDetail" :propFormData="formData" />
  29. </div>
  30. </div>
  31. <el-form style="clear: both;">
  32. <el-form-item style="float: right">
  33. <el-button @click="dialogVisible = false">返回</el-button>
  34. <el-button v-if="!isDetail" :loading="formLoading" type="primary" @click="submitForm">
  35. 保存
  36. </el-button>
  37. </el-form-item>
  38. </el-form>
  39. </ContentWrap>
  40. </el-dialog>
  41. <!-- </div> -->
  42. </template>
  43. <script lang="ts" setup>
  44. import { cloneDeep } from 'lodash-es'
  45. import { useTagsViewStore } from '@/store/modules/tagsView'
  46. import * as ProductSpuApi from '@/api/mall/product/spu'
  47. import InfoForm from './InfoForm.vue'
  48. import DescriptionForm from './DescriptionForm.vue'
  49. import OtherForm from './OtherForm.vue'
  50. import SkuForm from './SkuForm.vue'
  51. import DeliveryForm from './DeliveryForm.vue'
  52. import { convertToInteger, floatToFixed2, formatToFraction } from '@/utils'
  53. defineOptions({ name: 'ProductSpuForm' })
  54. const { t } = useI18n() // 国际化
  55. const message = useMessage() // 消息弹窗
  56. const { push, currentRoute } = useRouter() // 路由
  57. const { params, name } = useRoute() // 查询参数
  58. const { delView } = useTagsViewStore() // 视图操作
  59. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  60. const activeName = ref('info') // Tag 激活的窗口
  61. const isDetail = ref(false) // 是否查看详情
  62. const infoRef = ref() // 商品信息 Ref
  63. const skuRef = ref() // 商品规格 Ref
  64. const deliveryRef = ref() // 物流设置 Ref
  65. const descriptionRef = ref() // 商品详情 Ref
  66. const otherRef = ref() // 其他设置 Ref
  67. // SPU 表单数据
  68. const formData = ref<ProductSpuApi.Spu>({
  69. name: '', // 商品名称
  70. categoryId: undefined, // 商品分类
  71. keyword: '', // 关键字
  72. picUrl: '', // 商品封面图
  73. sliderPicUrls: [], // 商品轮播图
  74. introduction: '', // 商品简介
  75. deliveryTypes: [], // 配送方式数组
  76. deliveryTemplateId: undefined, // 运费模版
  77. brandId: undefined, // 商品品牌
  78. specType: false, // 商品规格
  79. subCommissionType: false, // 分销类型
  80. skus: [
  81. {
  82. price: 0, // 商品价格
  83. marketPrice: 0, // 市场价
  84. costPrice: 0, // 成本价
  85. barCode: '', // 商品条码
  86. picUrl: '', // 图片地址
  87. stock: 0, // 库存
  88. weight: 0, // 商品重量
  89. volume: 0, // 商品体积
  90. firstBrokeragePrice: 0, // 一级分销的佣金
  91. secondBrokeragePrice: 0 // 二级分销的佣金
  92. }
  93. ],
  94. description: '', // 商品详情
  95. sort: 0, // 商品排序
  96. giveIntegral: 0, // 赠送积分
  97. virtualSalesCount: 0 // 虚拟销量
  98. })
  99. const addFormData = ref<ProductSpuApi.Spu>({
  100. name: '', // 商品名称
  101. categoryId: undefined, // 商品分类
  102. keyword: '', // 关键字
  103. picUrl: '', // 商品封面图
  104. sliderPicUrls: [], // 商品轮播图
  105. introduction: '', // 商品简介
  106. deliveryTypes: [], // 配送方式数组
  107. deliveryTemplateId: undefined, // 运费模版
  108. brandId: undefined, // 商品品牌
  109. specType: false, // 商品规格
  110. subCommissionType: false, // 分销类型
  111. skus: [
  112. {
  113. price: 0, // 商品价格
  114. marketPrice: 0, // 市场价
  115. costPrice: 0, // 成本价
  116. barCode: '', // 商品条码
  117. picUrl: '', // 图片地址
  118. stock: 0, // 库存
  119. weight: 0, // 商品重量
  120. volume: 0, // 商品体积
  121. firstBrokeragePrice: 0, // 一级分销的佣金
  122. secondBrokeragePrice: 0 // 二级分销的佣金
  123. }
  124. ],
  125. description: '', // 商品详情
  126. sort: 0, // 商品排序
  127. giveIntegral: 0, // 赠送积分
  128. virtualSalesCount: 0 // 虚拟销量
  129. })
  130. const dialogVisible = ref(false) // 弹窗的是否展示
  131. const dialogTitle = ref('') // 弹窗的标题
  132. const productId = ref()
  133. const picUrl = ref("")
  134. const openType = ref("")
  135. const handleError = (e) => {
  136. e.target.src = "https://static.iocoder.cn/mall/a79f5d2ea6bf0c3c11b2127332dfe2df.jpg"
  137. }
  138. /** 打开弹窗 */
  139. const open = async (id : number, type : string, imageUrl : string) => {
  140. dialogVisible.value = true
  141. productId.value = id
  142. picUrl.value = imageUrl
  143. console.log(id, type, imageUrl)
  144. await getDetail()
  145. activeName.value = 'info'
  146. openType.value = type
  147. dialogTitle.value = t('action.' + type)
  148. if (type == "view") {
  149. dialogTitle.value = "查看"
  150. } else if (type == "create") {
  151. formData.value = addFormData.value
  152. }
  153. // 判断打开状态,如果是view那就只能查看 否则可以编辑
  154. if ('view' == openType.value) {
  155. isDetail.value = true
  156. } else {
  157. isDetail.value = false
  158. }
  159. }
  160. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  161. /** 获得详情 */
  162. const getDetail = async () => {
  163. const id = productId.value as any as number
  164. if (id) {
  165. formLoading.value = true
  166. try {
  167. const res = (await ProductSpuApi.getSpu(id)) as ProductSpuApi.Spu
  168. res.skus?.forEach((item) => {
  169. if (isDetail.value) {
  170. item.price = floatToFixed2(item.price)
  171. item.marketPrice = floatToFixed2(item.marketPrice)
  172. item.costPrice = floatToFixed2(item.costPrice)
  173. item.firstBrokeragePrice = floatToFixed2(item.firstBrokeragePrice)
  174. item.secondBrokeragePrice = floatToFixed2(item.secondBrokeragePrice)
  175. } else {
  176. // 回显价格分转元
  177. item.price = formatToFraction(item.price)
  178. item.marketPrice = formatToFraction(item.marketPrice)
  179. item.costPrice = formatToFraction(item.costPrice)
  180. item.firstBrokeragePrice = formatToFraction(item.firstBrokeragePrice)
  181. item.secondBrokeragePrice = formatToFraction(item.secondBrokeragePrice)
  182. }
  183. })
  184. formData.value = res
  185. } finally {
  186. formLoading.value = false
  187. }
  188. }
  189. }
  190. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  191. /** 提交按钮 */
  192. const submitForm = async () => {
  193. // 提交请求
  194. formLoading.value = true
  195. try {
  196. // 校验各表单
  197. await unref(infoRef)?.validate()
  198. await unref(skuRef)?.validate()
  199. await unref(deliveryRef)?.validate()
  200. await unref(descriptionRef)?.validate()
  201. await unref(otherRef)?.validate()
  202. // 深拷贝一份, 这样最终 server 端不满足,不需要影响原始数据
  203. const deepCopyFormData = cloneDeep(unref(formData.value)) as ProductSpuApi.Spu
  204. deepCopyFormData.skus!.forEach((item) => {
  205. // 给sku name赋值
  206. item.name = deepCopyFormData.name
  207. // sku相关价格元转分
  208. item.price = convertToInteger(item.price)
  209. item.marketPrice = convertToInteger(item.marketPrice)
  210. item.costPrice = convertToInteger(item.costPrice)
  211. item.firstBrokeragePrice = convertToInteger(item.firstBrokeragePrice)
  212. item.secondBrokeragePrice = convertToInteger(item.secondBrokeragePrice)
  213. })
  214. // 处理轮播图列表
  215. const newSliderPicUrls : any[] = []
  216. deepCopyFormData.sliderPicUrls!.forEach((item : any) => {
  217. // 如果是前端选的图
  218. typeof item === 'object' ? newSliderPicUrls.push(item.url) : newSliderPicUrls.push(item)
  219. })
  220. deepCopyFormData.sliderPicUrls = newSliderPicUrls
  221. // 校验都通过后提交表单
  222. const data = deepCopyFormData as ProductSpuApi.Spu
  223. const id = productId.value as any as number
  224. if (!id) {
  225. await ProductSpuApi.createSpu(data)
  226. message.success(t('common.createSuccess'))
  227. } else {
  228. await ProductSpuApi.updateSpu(data)
  229. message.success(t('common.updateSuccess'))
  230. }
  231. close()
  232. emit('success')
  233. } finally {
  234. formLoading.value = false
  235. }
  236. }
  237. /** 关闭按钮 */
  238. const close = () => {
  239. dialogVisible.value = false
  240. }
  241. </script>
  242. <style type="text/css">
  243. .el-dialog__body {
  244. padding: unset;
  245. }
  246. </style>
  247. <style lang="scss" scoped>
  248. .left {
  249. width: 96px;
  250. float: left;
  251. img {
  252. width: 96%;
  253. border: 2px solid #e4e7ee;
  254. margin-bottom: -5px;
  255. }
  256. }
  257. .right {
  258. border-left: 2px solid #e4e7ee;
  259. margin-left: -2px;
  260. float: left;
  261. width: calc(100% - 120px);
  262. }
  263. </style>