user-dialog.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <ElDialog
  3. v-model="dialogVisible"
  4. :title="dialogType === 'add' ? '添加餐饮公司关系' : '编辑餐饮公司关系'"
  5. width="30%"
  6. align-center
  7. >
  8. <ElForm ref="formRef" :model="formData" :rules="rules" label-width="80px">
  9. <ElFormItem label="关系人" prop="name">
  10. <ElInput v-model="formData.name" maxlength="20" type="text" />
  11. </ElFormItem>
  12. <ElFormItem label="餐饮公司" prop="company_id">
  13. <ElSelect v-model="formData.company_id" :empty-values="[0]" :value-on-clear="0">
  14. <ElOption
  15. v-for="item in selectList"
  16. :key="item.id"
  17. :value="item.id"
  18. :label="item.name"
  19. />
  20. </ElSelect>
  21. </ElFormItem>
  22. <ElFormItem label="手机号" prop="phone">
  23. <ElInput v-model="formData.phone" maxlength="20" type="text" />
  24. </ElFormItem>
  25. <ElFormItem label="微信号" prop="weixin">
  26. <ElInput v-model="formData.weixin" maxlength="20" type="text" />
  27. </ElFormItem>
  28. <ElFormItem label="职位" prop="position">
  29. <ElInput v-model="formData.position" maxlength="20" type="text" />
  30. </ElFormItem>
  31. <ElFormItem label="备注" prop="memo" >
  32. <ElInput v-model="formData.memo" maxlength="255" type="textarea" :rows="4"/>
  33. </ElFormItem>
  34. </ElForm>
  35. <template #footer>
  36. <div class="dialog-footer">
  37. <ElButton @click="dialogVisible = false">取消</ElButton>
  38. <ElButton type="primary" @click="handleSubmit">提交</ElButton>
  39. </div>
  40. </template>
  41. </ElDialog>
  42. </template>
  43. <script setup lang="ts">
  44. import type { FormInstance, FormRules } from 'element-plus'
  45. import { companyRelationApi } from '@/api/companyRelationApi'
  46. interface Props {
  47. visible: boolean
  48. type: string
  49. userData?: any
  50. selectList: Api.Common.SelectRelationInfo[]
  51. }
  52. interface Emits {
  53. (e: 'update:visible', value: boolean): void
  54. (e: 'submit'): void
  55. }
  56. const props = defineProps<Props>()
  57. const emit = defineEmits<Emits>()
  58. // 对话框显示控制
  59. const dialogVisible = computed({
  60. get: () => props.visible,
  61. set: (value) => emit('update:visible', value)
  62. })
  63. const dialogType = computed(() => props.type)
  64. // 表单实例
  65. const formRef = ref<FormInstance>()
  66. // 表单数据
  67. const formData = reactive<Form.CompanyContact>({
  68. id: 0,
  69. name: '', // 关系人,
  70. company_id: 0, // 餐饮公司ID,
  71. phone: '', // 手机号,
  72. weixin: '', // 微信号,
  73. position: '', // 职位,
  74. memo: '' // 备注,
  75. })
  76. // 表单验证规则
  77. const rules: FormRules = {
  78. name: [
  79. { required: true, message: '请输入关系人', trigger: 'blur' },
  80. { max: 20, message: '长度最多20个字符', trigger: 'blur' }
  81. ],
  82. company_id: [{ required: true, message: '请输入餐饮公司', trigger: 'blur' }],
  83. phone: [
  84. { required: true, message: '请输入手机号', trigger: 'blur' },
  85. { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  86. ],
  87. weixin: [
  88. { max: 20, message: '长度最多20个字符', trigger: 'blur' }
  89. ],
  90. position: [
  91. { required: true, message: '请输入职位', trigger: 'blur' },
  92. { max: 20, message: '长度最多20个字符', trigger: 'blur' }
  93. ],
  94. memo: [{ max: 255, message: '长度最多255个字符', trigger: 'blur' }]
  95. }
  96. const isEdit = ref(false)
  97. // 初始化表单数据
  98. const initFormData = () => {
  99. isEdit.value = props.type === 'edit' && props.userData
  100. const row = props.userData
  101. Object.assign(formData, {
  102. id: props.userData.id,
  103. name: isEdit.value ? row.name || '' : '',
  104. phone: isEdit.value ? row.phone || '' : '',
  105. company_id: isEdit.value ? row.company_id : 0,
  106. weixin: isEdit.value ? row.weixin || '' : '',
  107. position: isEdit.value ? row.position || '' : '',
  108. memo: isEdit.value ? row.memo || '' : ''
  109. })
  110. }
  111. // 统一监听对话框状态变化
  112. watch(
  113. () => [props.visible, props.type, props.userData],
  114. ([visible]) => {
  115. if (visible) {
  116. initFormData()
  117. nextTick(() => {
  118. formRef.value?.clearValidate()
  119. })
  120. }
  121. },
  122. { immediate: true }
  123. )
  124. // 提交表单
  125. const handleSubmit = async () => {
  126. if (!formRef.value) return
  127. await formRef.value.validate((valid) => {
  128. if (valid) {
  129. if (isEdit.value) {
  130. companyRelationApi.edit(formData).then(() => {
  131. dialogVisible.value = false
  132. emit('submit')
  133. })
  134. } else {
  135. companyRelationApi.add(formData).then(() => {
  136. dialogVisible.value = false
  137. emit('submit')
  138. })
  139. }
  140. }
  141. })
  142. }
  143. </script>