prodInfo.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <avue-form :option="option" v-model="form" @submit="onSubmit">
  3. <template slot="goodsTypeList">
  4. <el-transfer v-model="form.goodsTypeList" filterable :data="categories_selections"
  5. :titles="['备选项', '已选项']"></el-transfer>
  6. </template>
  7. </avue-form>
  8. </template>
  9. <script lang="ts">
  10. import Vue from 'vue'
  11. import httpx, { AxiosResponse } from '@/utils/httpx'
  12. import formOption from "@/avue/form/prodinfo"
  13. import { ICategory, IProdInfo } from '@/utils/vo'
  14. import { isInteger } from 'lodash'
  15. import { database2real, real2database } from "@/utils/currency"
  16. interface ISelection {
  17. key: number,
  18. label: string,
  19. disabled?: boolean
  20. hidden?: boolean
  21. }
  22. interface ICategoryProdRelation {
  23. goodsId: number
  24. goodsTypeId: number
  25. }
  26. export default Vue.extend({
  27. data() {
  28. return {
  29. form: {
  30. name: "",
  31. desc: "",
  32. price: 0,
  33. deliverPrice: 0,
  34. stockAmount: 0,
  35. // soldAmount: 0,
  36. cover: [],
  37. kind: "",
  38. platform: "",
  39. link: "",
  40. needAddr: true,
  41. goodsTypeList: [],
  42. adminId: 0,
  43. title: "新增商品",
  44. } as IProdInfo,
  45. option: formOption,
  46. categories: [] as ICategory[],
  47. new: false,
  48. id: 0,
  49. }
  50. },
  51. mounted() {
  52. this.getCategoryList("")
  53. const id_str = this.$route.params.id
  54. const id = parseInt(id_str)
  55. if (isNaN(id) || !isInteger(id)) {
  56. if (id_str === "new") {
  57. this.new = true
  58. } else {
  59. this.notFound()
  60. }
  61. } else {
  62. this.new = false
  63. this.id = id
  64. this.form.title = '修改商品'
  65. this.getProductInfo(id)
  66. }
  67. },
  68. methods: {
  69. getProductInfo(id: number) {
  70. return httpx.post(
  71. httpx.makeurl("/goods/editGood"),
  72. id
  73. ).then(({ data }) => {
  74. if ((data?.goodList?.length || 0) === 0) {
  75. this.notFound()
  76. }
  77. const item = data.goodList[0]
  78. const categories_raw: ICategoryProdRelation[] = data?.goodsGoodsTypesEntityList || []
  79. const categories = categories_raw.map(v => v.goodsTypeId)
  80. this.form = {
  81. name: item.name,
  82. desc: item.desc,
  83. price: database2real(item.price), // 数据库内单位为分
  84. deliverPrice: database2real(item.deliverPrice), // 数据库内单位为分
  85. stockAmount: item.stockAmount,
  86. // soldAmount: item.soldAmount,
  87. platform: item.platform,
  88. cover: [item.cover],
  89. kind: item.kind,
  90. link: item.link,
  91. needAddr: item.needAddr,
  92. goodsTypeList: categories,
  93. adminId: item.adminId,
  94. title: `修改商品 ${item.name}`,
  95. }
  96. })
  97. },
  98. getCategoryList(name: string = "") {
  99. return httpx.post(httpx.makeurl("/goodsTypes/queryGoodsTypeList"), {
  100. name: name, limit: 1000, page: 1
  101. }).then(({ data }: AxiosResponse<{ goodsTypeList: ICategory[], total: number }>) => {
  102. this.categories = data.goodsTypeList
  103. })
  104. },
  105. onSubmit(form: IProdInfo, done: Function) {
  106. console.log(form)
  107. const path = this.new ? "/goods/addGood" : "/goods/updateGood"
  108. const id_form: { id?: number } = this.new ? {} : { id: this.id }
  109. const data = Object.assign(form, id_form)
  110. data.price = real2database(data.price)
  111. data.deliverPrice = real2database(data.deliverPrice)
  112. // @ts-ignore
  113. data.cover = data.cover[0] || ""
  114. // @ts-ignore
  115. delete data.title;
  116. console.log(data)
  117. httpx.post(path, data).then((data) => {
  118. this.$message({
  119. message: '操作成功',
  120. type: 'success',
  121. duration: 1500
  122. })
  123. this.$router.back()
  124. })
  125. done()
  126. },
  127. notFound() {
  128. this.$router.replace("/404")
  129. }
  130. },
  131. computed: {
  132. categories_selections(): ISelection[] {
  133. return this.categories.map((v, i) => {
  134. return {
  135. key: v.id,
  136. label: v.name,
  137. }
  138. })
  139. }
  140. },
  141. })
  142. </script>