ContactForm.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="100px"
  8. v-loading="formLoading"
  9. >
  10. <el-form-item label="供应商" prop="supplierId" v-if="ifAlone==null">
  11. <el-select v-model="formData.supplierId" filterable placeholder="请选择">
  12. <el-option
  13. v-for="dict in supplierList"
  14. :key="dict.id"
  15. :label="dict.name"
  16. :value="dict.id"
  17. />
  18. </el-select>
  19. </el-form-item>
  20. <el-form-item label="姓名" prop="name">
  21. <el-input v-model="formData.name" placeholder="请输入姓名" />
  22. </el-form-item>
  23. <el-form-item label="电话" prop="telephone">
  24. <el-input v-model="formData.telephone" placeholder="请输入电话" />
  25. </el-form-item>
  26. <el-form-item label="职务" prop="position">
  27. <el-input v-model="formData.position" placeholder="请输入职务" />
  28. </el-form-item>
  29. <el-form-item label="邮箱" prop="email">
  30. <el-input v-model="formData.email" placeholder="请输入邮箱" />
  31. </el-form-item>
  32. <el-form-item label="产品业务模块" prop="productIds">
  33. <el-select v-model="formData.productIds" filterable placeholder="请选择">
  34. <el-option
  35. v-for="dict in productList"
  36. :key="dict.id"
  37. :label="dict.name"
  38. :value="dict.id"
  39. />
  40. </el-select>
  41. </el-form-item>
  42. <el-form-item label="备注" prop="remark">
  43. <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" />
  44. </el-form-item>
  45. </el-form>
  46. <template #footer>
  47. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  48. <el-button @click="dialogVisible = false">取 消</el-button>
  49. </template>
  50. </Dialog>
  51. </template>
  52. <script setup lang="ts">
  53. import { ContactApi, ContactVO } from '@/api/supplier/contact'
  54. import {SupplierVO} from "@/api/supplier/base";
  55. import * as SupplierBaseApi from "@/api/supplier/base";
  56. import {FormRules} from "element-plus";
  57. import * as SupplierDetailApi from '@/api/supplier/product/supplierdetail'
  58. /** 供应商联系人 表单 */
  59. defineOptions({ name: 'ContactForm' })
  60. const { t } = useI18n() // 国际化
  61. const message = useMessage() // 消息弹窗
  62. const supplierList = ref([] as SupplierVO[])
  63. const productList = ref([] as SupplierProductVo[])
  64. const dialogVisible = ref(false) // 弹窗的是否展示
  65. const dialogTitle = ref('') // 弹窗的标题
  66. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  67. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  68. const formData = ref({
  69. id: undefined,
  70. supplierId: undefined,
  71. name: undefined,
  72. position: undefined,
  73. telephone: undefined,
  74. email: undefined,
  75. productIds: undefined,
  76. productNames: undefined,
  77. remark: undefined,
  78. supplierName: undefined
  79. })
  80. const formRules = reactive<FormRules>({
  81. supplierId: [{ required: true, message: '供应商不能为空', trigger: 'blur' }],
  82. name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
  83. email: [
  84. {
  85. type: 'email',
  86. message: '请输入正确的邮箱地址',
  87. trigger: ['blur', 'change']
  88. }
  89. ],
  90. telephone: [{ required: true, message: '电话不能为空', trigger: 'blur' },
  91. {
  92. pattern: /^(?:(?:\+|00)86)?1(?:3[\d]|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8[\d]|9[189])\d{8}$/,
  93. message: '请输入正确的手机号码',
  94. trigger: 'blur'
  95. }
  96. ]
  97. })
  98. const formRef = ref() // 表单 Ref
  99. const props = defineProps({
  100. supplierId: { type: Number, default: () => null },
  101. ifAlone: { type: Boolean, default: () => null },
  102. })
  103. /** 打开弹窗 */
  104. const open = async (type: string, id?: number) => {
  105. dialogVisible.value = true
  106. dialogTitle.value = t('action.' + type)
  107. formType.value = type
  108. resetForm()
  109. // 修改时,设置数据
  110. if (id) {
  111. formLoading.value = true
  112. try {
  113. formData.value = await ContactApi.getContact(id)
  114. } finally {
  115. formLoading.value = false
  116. }
  117. }
  118. supplierList.value = await SupplierBaseApi.Api.getAll()
  119. productList.value = await SupplierDetailApi.getAll()
  120. }
  121. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  122. /** 提交表单 */
  123. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  124. const submitForm = async () => {
  125. // 校验表单
  126. await formRef.value.validate()
  127. // 提交请求
  128. formLoading.value = true
  129. try {
  130. const data = formData.value as unknown as ContactVO
  131. if (props.ifAlone!=null) {
  132. if (!props.supplierId.receivedParam){
  133. message.error("请先保存供应商基本信息");
  134. return
  135. }
  136. data.supplierId = props.supplierId.receivedParam
  137. }
  138. if (formType.value === 'create') {
  139. await ContactApi.createContact(data)
  140. message.success(t('common.createSuccess'))
  141. } else {
  142. await ContactApi.updateContact(data)
  143. message.success(t('common.updateSuccess'))
  144. }
  145. dialogVisible.value = false
  146. // 发送操作成功的事件
  147. emit('success')
  148. } finally {
  149. formLoading.value = false
  150. }
  151. }
  152. /** 重置表单 */
  153. const resetForm = () => {
  154. formData.value = {
  155. id: undefined,
  156. supplierId: undefined,
  157. name: undefined,
  158. position: undefined,
  159. telephone: undefined,
  160. email: undefined,
  161. productIds: undefined,
  162. productNames: undefined,
  163. remark: undefined
  164. }
  165. formRef.value?.resetFields()
  166. }
  167. </script>