ExpressForm.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="120px"
  8. v-loading="formLoading"
  9. >
  10. <el-form-item label="公司编码" prop="code">
  11. <el-input v-model="formData.code" placeholder="请输入快递编码" />
  12. </el-form-item>
  13. <el-form-item label="公司名称" prop="name">
  14. <el-input v-model="formData.name" placeholder="请输入快递名称" />
  15. </el-form-item>
  16. <el-form-item label="公司 logo" prop="logo">
  17. <UploadImg v-model="formData.logo" :limit="1" :is-show-tip="false" />
  18. <div style="font-size: 10px" class="pl-10px">推荐 180x180 图片分辨率</div>
  19. </el-form-item>
  20. <el-form-item label="排序" prop="sort">
  21. <el-input-number v-model="formData.sort" controls-position="right" :min="0" />
  22. </el-form-item>
  23. <el-form-item label="开启状态" prop="status">
  24. <el-radio-group v-model="formData.status">
  25. <el-radio
  26. v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
  27. :key="dict.value"
  28. :label="dict.value"
  29. >
  30. {{ dict.label }}
  31. </el-radio>
  32. </el-radio-group>
  33. </el-form-item>
  34. </el-form>
  35. <template #footer>
  36. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  37. <el-button @click="dialogVisible = false">取 消</el-button>
  38. </template>
  39. </Dialog>
  40. </template>
  41. <script lang="ts" setup>
  42. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  43. import { CommonStatusEnum } from '@/utils/constants'
  44. import * as DeliveryExpressApi from '@/api/mall/trade/delivery/express'
  45. defineOptions({ name: 'ExpressForm' })
  46. const { t } = useI18n() // 国际化
  47. const message = useMessage() // 消息弹窗
  48. const dialogVisible = ref(false) // 弹窗的是否展示
  49. const dialogTitle = ref('') // 弹窗的标题
  50. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  51. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  52. const formData = ref({
  53. id: undefined,
  54. code: '',
  55. name: '',
  56. logo: '',
  57. sort: 0,
  58. status: CommonStatusEnum.ENABLE
  59. })
  60. const formRules = reactive({
  61. code: [{ required: true, message: '快递编码不能为空', trigger: 'blur' }],
  62. name: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
  63. logo: [{ required: true, message: '分类图片不能为空', trigger: 'blur' }],
  64. sort: [{ required: true, message: '分类排序不能为空', trigger: 'blur' }],
  65. status: [{ required: true, message: '开启状态不能为空', trigger: 'blur' }]
  66. })
  67. const formRef = ref() // 表单 Ref
  68. /** 打开弹窗 */
  69. const open = async (type: string, id?: number) => {
  70. dialogVisible.value = true
  71. dialogTitle.value = t('action.' + type)
  72. formType.value = type
  73. resetForm()
  74. // 修改时,设置数据
  75. if (id) {
  76. formLoading.value = true
  77. try {
  78. formData.value = await DeliveryExpressApi.getDeliveryExpress(id)
  79. } finally {
  80. formLoading.value = false
  81. }
  82. }
  83. }
  84. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  85. /** 提交表单 */
  86. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  87. const submitForm = async () => {
  88. // 校验表单
  89. if (!formRef) return
  90. const valid = await formRef.value.validate()
  91. if (!valid) return
  92. // 提交请求
  93. formLoading.value = true
  94. try {
  95. const data = formData.value as DeliveryExpressApi.DeliveryExpressVO
  96. if (formType.value === 'create') {
  97. await DeliveryExpressApi.createDeliveryExpress(data)
  98. message.success(t('common.createSuccess'))
  99. } else {
  100. await DeliveryExpressApi.updateDeliveryExpress(data)
  101. message.success(t('common.updateSuccess'))
  102. }
  103. dialogVisible.value = false
  104. // 发送操作成功的事件
  105. emit('success')
  106. } finally {
  107. formLoading.value = false
  108. }
  109. }
  110. /** 重置表单 */
  111. const resetForm = () => {
  112. formData.value = {
  113. id: undefined,
  114. name: '',
  115. picUrl: '',
  116. status: CommonStatusEnum.ENABLE
  117. }
  118. formRef.value?.resetFields()
  119. }
  120. </script>