user-dialog.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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="school_id">
  13. <ElSelect v-model="formData.school_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 { schoolRelationApi } from '@/api/schoolRelationApi'
  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.SchoolContact>({
  68. id: 0,
  69. name: '', // 关系人,
  70. school_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. school_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. { required: true, message: '请输入微信号', trigger: 'blur' },
  89. { max: 20, message: '长度最多20个字符', trigger: 'blur' }
  90. ],
  91. position: [
  92. { required: true, message: '请输入职位', trigger: 'blur' },
  93. { max: 20, message: '长度最多20个字符', trigger: 'blur' }
  94. ],
  95. memo: [{ max: 255, message: '长度最多255个字符', trigger: 'blur' }]
  96. }
  97. const isEdit = ref(false)
  98. // 初始化表单数据
  99. const initFormData = () => {
  100. isEdit.value = props.type === 'edit' && props.userData
  101. const row = props.userData
  102. Object.assign(formData, {
  103. id: props.userData.id,
  104. name: isEdit.value ? row.name || '' : '',
  105. phone: isEdit.value ? row.phone || '' : '',
  106. school_id: isEdit.value ? row.school_id : 0,
  107. weixin: isEdit.value ? row.weixin || '' : '',
  108. position: isEdit.value ? row.position || '' : '',
  109. memo: isEdit.value ? row.memo || '' : ''
  110. })
  111. }
  112. // 统一监听对话框状态变化
  113. watch(
  114. () => [props.visible, props.type, props.userData],
  115. ([visible]) => {
  116. if (visible) {
  117. initFormData()
  118. nextTick(() => {
  119. formRef.value?.clearValidate()
  120. })
  121. }
  122. },
  123. { immediate: true }
  124. )
  125. // 提交表单
  126. const handleSubmit = async () => {
  127. if (!formRef.value) return
  128. await formRef.value.validate((valid) => {
  129. if (valid) {
  130. console.log(`%c formData == `, 'background:#41b883 ; padding:1px; color:#fff', formData)
  131. if (isEdit.value) {
  132. schoolRelationApi.edit(formData).then(() => {
  133. dialogVisible.value = false
  134. emit('submit')
  135. })
  136. } else {
  137. schoolRelationApi.add(formData).then(() => {
  138. dialogVisible.value = false
  139. emit('submit')
  140. })
  141. }
  142. }
  143. })
  144. }
  145. </script>