addGoods.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <div class="container">
  3. <Breadcrumb />
  4. <a-card title="添加商品">
  5. <a-form :model="form" :rules="rules" layout="vertical" @submit-success="handleSubmit">
  6. <a-form-item field="name" label="商品名称" :style="{ width: '600px' }">
  7. <a-input v-model="form.name" :max-length="25" allow-clear show-word-limit />
  8. </a-form-item>
  9. <a-form-item field="price" label="价格" :style="{ width: '300px' }">
  10. <a-input-number v-model="form.price" :step="0.01" :precision="2" />
  11. </a-form-item>
  12. <a-form-item field="lepao_count" label="乐跑次数" :style="{ width: '300px' }">
  13. <a-input-number v-model="form.lepao_count" :step="1" :precision="0" />
  14. </a-form-item>
  15. <a-form-item field="num" label="库存数量" :style="{ width: '300px' }">
  16. <a-input-number v-model="form.num" :step="1" :precision="0" />
  17. </a-form-item>
  18. <a-form-item field="state" label="商品状态" :style="{ width: '300px' }">
  19. <a-radio-group v-model="form.state">
  20. <a-radio :value="1">正常</a-radio>
  21. <a-radio :value="0">下架</a-radio>
  22. </a-radio-group>
  23. </a-form-item>
  24. <a-form-item field="content" label="商品详情">
  25. <WangEditor v-model="form.content" @change="contentChange"></WangEditor>
  26. </a-form-item>
  27. <a-form-item>
  28. <a-button html-type="submit" :loading="loading">保存</a-button>
  29. </a-form-item>
  30. </a-form>
  31. </a-card>
  32. </div>
  33. </template>
  34. <script setup>
  35. import { ref, reactive, onMounted } from 'vue'
  36. import { adminGetGoods, addGoods } from '@/api/goods'
  37. import { useRoute } from 'vue-router'
  38. import { Notification } from '@arco-design/web-vue'
  39. import WangEditor from '@/components/Editor/WangEditor.vue'
  40. const loading = ref(false)
  41. const route = useRoute()
  42. const rules = {
  43. name: [
  44. {
  45. required: true,
  46. message: '请输入商品名称',
  47. },
  48. ],
  49. price: [
  50. {
  51. required: true,
  52. message: '请填写商品价格',
  53. }
  54. ],
  55. lepao_count: [
  56. {
  57. required: true,
  58. message: '请填写乐跑次数',
  59. }
  60. ],
  61. num: [
  62. {
  63. required: true,
  64. message: '请填写商品库存',
  65. }
  66. ],
  67. state: [
  68. {
  69. required: true,
  70. message: '请选择商品状态',
  71. }
  72. ]
  73. }
  74. const form = reactive({
  75. name: '',
  76. price: 0.00,
  77. num: 999999,
  78. files: [],
  79. lepao_count: 0,
  80. ic_count: 0,
  81. content: '',
  82. state: 1
  83. })
  84. const contentChange = (value) => {
  85. form.content = value
  86. }
  87. const handleSubmit = async () => {
  88. try {
  89. loading.value = true
  90. let data = {
  91. ...form,
  92. id: route.params.id ?? null
  93. }
  94. data.content = btoa(encodeURI(data.content))
  95. const res = await addGoods(data)
  96. if (!res || res.code !== 0)
  97. return Notification.error({
  98. title: '保存商品失败!',
  99. content: res?.msg ?? '请稍后再试'
  100. })
  101. Notification.success({
  102. title: '保存成功!'
  103. })
  104. // router.push(`/service/orderDetail/${res.data}`)
  105. } catch (error) {
  106. Notification.error({
  107. title: '保存商品失败!',
  108. content: error.message || '请稍后再试'
  109. })
  110. } finally {
  111. loading.value = false
  112. }
  113. }
  114. const getGoodsDetail = async () => {
  115. if (!route.params.id) return
  116. try {
  117. loading.value = true
  118. const res = await adminGetGoods({ id: route.params.id })
  119. if (!res || res.code !== 0)
  120. return Notification.error({
  121. title: '获取商品信息失败!',
  122. content: res?.msg ?? '请稍后再试'
  123. })
  124. const { name, price, num, content, state, lepao_count } = res.data
  125. form.content = decodeURI(atob(content))
  126. form.name = name
  127. form.price = Number(price)
  128. form.lepao_count = lepao_count
  129. form.num = num
  130. form.state = state
  131. } catch (error) {
  132. Notification.error({
  133. title: '获取商品信息失败!',
  134. content: error.message || '请稍后再试'
  135. })
  136. } finally {
  137. loading.value = false
  138. }
  139. }
  140. onMounted(() => {
  141. getGoodsDetail()
  142. })
  143. </script>
  144. <style scoped>
  145. .container {
  146. padding: 0 20px 20px 20px;
  147. }
  148. .table {
  149. margin-top: 15px;
  150. }
  151. </style>