DeliveryExpressTemplateForm.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible" width="85%">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="80px"
  8. v-loading="formLoading"
  9. >
  10. <el-form-item label="模板名称" prop="name">
  11. <el-input v-model="formData.name" placeholder="请输入模板名称" />
  12. </el-form-item>
  13. <el-form-item label="计费方式" prop="chargeMode">
  14. <el-radio-group v-model="formData.chargeMode" @change="changeChargeMode">
  15. <el-radio :label="1">按件数</el-radio>
  16. <el-radio :label="2">按重量</el-radio>
  17. <el-radio :label="3">按体积</el-radio>
  18. </el-radio-group>
  19. </el-form-item>
  20. <el-form-item label="运费" prop="templateCharge">
  21. <el-table border style="width: 100%" :data="formData.templateCharge">
  22. <el-table-column align="center" label="区域">
  23. <template #default="{ row }">
  24. <!-- @芋艿 TODO 数据多,性能有问题 , 如何解决 -->
  25. <el-tree-select
  26. v-model="row.areaId"
  27. :data="areaList"
  28. :props="defaultProps"
  29. node-key="id"
  30. check-strictly
  31. show-checkbox
  32. check-on-click-node
  33. :render-after-expand="false"
  34. />
  35. </template>
  36. </el-table-column>
  37. <el-table-column :label="columnTitle.startCountTitle" prop="startCount">
  38. <template #default="{ row }">
  39. <el-input-number v-model="row.startCount" :min="1" />
  40. </template>
  41. </el-table-column>
  42. <!-- TODO 元转换 分 -->
  43. <el-table-column label="运费(元)" prop="startPrice">
  44. <template #default="{ row }">
  45. <el-input-number v-model="row.startPrice" :min="1" />
  46. </template>
  47. </el-table-column>
  48. <el-table-column :label="columnTitle.extraCountTitle" prop="extraCount">
  49. <template #default="{ row }">
  50. <el-input-number v-model="row.extraCount" :min="1" />
  51. </template>
  52. </el-table-column>
  53. <el-table-column label="续费(元)" prop="extraPrice">
  54. <template #default="{ row }">
  55. <el-input-number v-model="row.extraPrice" :min="1" />
  56. </template>
  57. </el-table-column>
  58. <el-table-column label="操作" align="center">
  59. <template #default="scope">
  60. <el-button link type="danger" @click="deleteChargeArea(scope.$index)">
  61. 删除
  62. </el-button>
  63. </template>
  64. </el-table-column>
  65. </el-table>
  66. </el-form-item>
  67. <el-form-item>
  68. <el-button type="primary" plain @click="addChargeArea()">
  69. <Icon icon="ep:plus" class="mr-5px" /> 添加区域
  70. </el-button>
  71. </el-form-item>
  72. <el-form-item label="包邮区域" prop="templateFree">
  73. <el-table border style="width: 100%" :data="formData.templateFree">
  74. <el-table-column label="区域">
  75. <template #default="{ row }">
  76. <!-- @芋艿 TODO 数据多,性能有问题 , 如何解决 -->
  77. <el-tree-select
  78. v-model="row.areaId"
  79. :data="areaList"
  80. :props="defaultProps"
  81. node-key="id"
  82. check-strictly
  83. show-checkbox
  84. check-on-click-node
  85. :render-after-expand="false"
  86. />
  87. </template>
  88. </el-table-column>
  89. <el-table-column :label="columnTitle.freeCountTitle" prop="freeCount">
  90. <template #default="{ row }">
  91. <el-input-number v-model="row.freeCount" :min="1" />
  92. </template>
  93. </el-table-column>
  94. <el-table-column label="包邮金额(元)" prop="freePrice">
  95. <template #default="{ row }">
  96. <el-input-number v-model="row.freePrice" :min="1" />
  97. </template>
  98. </el-table-column>
  99. <el-table-column label="操作" align="center">
  100. <template #default="scope">
  101. <el-button link type="danger" @click="deleteFreeArea(scope.$index)"> 删除 </el-button>
  102. </template>
  103. </el-table-column>
  104. </el-table>
  105. </el-form-item>
  106. <el-form-item>
  107. <el-button type="primary" plain @click="addFreeArea()">
  108. <Icon icon="ep:plus" class="mr-5px" /> 添加区域
  109. </el-button>
  110. </el-form-item>
  111. <el-form-item label="排序" prop="sort">
  112. <el-input-number v-model="formData.sort" controls-position="right" :min="0" />
  113. </el-form-item>
  114. </el-form>
  115. <template #footer>
  116. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  117. <el-button @click="dialogVisible = false">取 消</el-button>
  118. </template>
  119. </Dialog>
  120. </template>
  121. <script setup lang="ts">
  122. import * as DeliveryExpressTemplateApi from '@/api/mall/trade/delivery/expressTemplate'
  123. import { defaultProps } from '@/utils/tree'
  124. import { getAreaTree } from '@/api/system/area'
  125. const { t } = useI18n() // 国际化
  126. const message = useMessage() // 消息弹窗
  127. const dialogVisible = ref(false) // 弹窗的是否展示
  128. const dialogTitle = ref('') // 弹窗的标题
  129. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  130. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  131. const formData = ref({
  132. id: undefined,
  133. name: '',
  134. chargeMode: 1,
  135. sort: 0,
  136. templateCharge: [],
  137. templateFree: []
  138. })
  139. const columnTitle = ref({
  140. startCountTitle: '首件',
  141. extraCountTitle: '续件',
  142. freeCountTitle: '包邮件数'
  143. })
  144. const formRules = reactive({
  145. name: [{ required: true, message: '模板名称不能为空', trigger: 'blur' }],
  146. chargeMode: [{ required: true, message: '配送计费方式不能为空', trigger: 'blur' }],
  147. sort: [{ required: true, message: '分类排序不能为空', trigger: 'blur' }]
  148. })
  149. const formRef = ref() // 表单 Ref
  150. const areaList = ref([]) //区域数据
  151. /** 打开弹窗 */
  152. const open = async (type: string, id?: number) => {
  153. dialogVisible.value = true
  154. dialogTitle.value = t('action.' + type)
  155. formType.value = type
  156. resetForm()
  157. try {
  158. // 修改时,设置数据
  159. if (id) {
  160. formLoading.value = true
  161. formData.value = await DeliveryExpressTemplateApi.getDeliveryExpressTemplate(id)
  162. }
  163. } finally {
  164. formLoading.value = false
  165. }
  166. }
  167. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  168. /** 提交表单 */
  169. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  170. const submitForm = async () => {
  171. // 校验表单
  172. if (!formRef) return
  173. const valid = await formRef.value.validate()
  174. if (!valid) return
  175. // 提交请求
  176. formLoading.value = true
  177. try {
  178. const data = formData.value as DeliveryExpressTemplateApi.DeliveryExpressTemplateVO
  179. if (formType.value === 'create') {
  180. await DeliveryExpressTemplateApi.createDeliveryExpressTemplate(data)
  181. message.success(t('common.createSuccess'))
  182. } else {
  183. await DeliveryExpressTemplateApi.updateDeliveryExpressTemplate(data)
  184. message.success(t('common.updateSuccess'))
  185. }
  186. dialogVisible.value = false
  187. // 发送操作成功的事件
  188. emit('success')
  189. } finally {
  190. formLoading.value = false
  191. }
  192. }
  193. /** 重置表单 */
  194. const resetForm = () => {
  195. formData.value = {
  196. id: undefined,
  197. name: '',
  198. chargeMode: 1,
  199. templateCharge: [
  200. {
  201. areaId: 1,
  202. startCount: 2,
  203. startPrice: 5,
  204. extraCount: 5,
  205. extraPrice: 10
  206. }
  207. ],
  208. templateFree: [],
  209. sort: 0
  210. }
  211. columnTitle.value = {
  212. startCountTitle: '首件',
  213. extraCountTitle: '续件',
  214. freeCountTitle: '包邮件数'
  215. }
  216. formRef.value?.resetFields()
  217. }
  218. /** 配送计费方法改变 */
  219. const changeChargeMode = (chargeMod: number) => {
  220. if (chargeMod === 1) {
  221. columnTitle.value = {
  222. startCountTitle: '首件',
  223. extraCountTitle: '续件',
  224. freeCountTitle: '包邮件数'
  225. }
  226. }
  227. if (chargeMod === 2) {
  228. columnTitle.value = {
  229. startCountTitle: '首件重量(kg)',
  230. extraCountTitle: '续件重量(kg)',
  231. freeCountTitle: '包邮重量(kg)'
  232. }
  233. }
  234. if (chargeMod === 3) {
  235. columnTitle.value = {
  236. startCountTitle: '首件体积(m³)',
  237. extraCountTitle: '续件体积(m³)',
  238. freeCountTitle: '包邮体积(m³)'
  239. }
  240. }
  241. }
  242. /** 初始化区域数据 */
  243. const initAreaData = async () => {
  244. formLoading.value = true
  245. try {
  246. const data = await getAreaTree(1)
  247. areaList.value = data
  248. } finally {
  249. formLoading.value = false
  250. }
  251. }
  252. /** 添加计费区域 */
  253. const addChargeArea = () => {
  254. const data = formData.value
  255. data.templateCharge.push({
  256. areaId: undefined,
  257. startCount: 1,
  258. startPrice: 1,
  259. extraCount: 1,
  260. extraPrice: 1
  261. })
  262. }
  263. /** 删除计费区域 */
  264. const deleteChargeArea = (index) => {
  265. const data = formData.value
  266. data.templateCharge.splice(index, 1)
  267. }
  268. /** 添加包邮区域 */
  269. const addFreeArea = () => {
  270. const data = formData.value
  271. data.templateFree.push({
  272. areaId: undefined,
  273. freeCount: 1,
  274. freePrice: 1
  275. })
  276. }
  277. /** 删除包邮区域 */
  278. const deleteFreeArea = (index) => {
  279. const data = formData.value
  280. data.templateFree.splice(index, 1)
  281. }
  282. /** 初始化 **/
  283. onMounted(() => {
  284. initAreaData()
  285. })
  286. </script>