CreateEnterpriseDialog.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <el-dialog
  3. title="创建企业"
  4. v-model:visible="dialogVisible"
  5. width="420px"
  6. @close="handleClose"
  7. >
  8. <div class="enterprise-form">
  9. <div class="form-item">
  10. <div class="label">当前账号</div>
  11. <el-input
  12. v-model="form.account"
  13. disabled
  14. />
  15. <p class="hint">注:一个账号只能归属一个企业</p>
  16. </div>
  17. <div class="form-item">
  18. <div class="label required">企业名称</div>
  19. <el-input
  20. v-model="form.name"
  21. placeholder="请输入企业名称"
  22. @keyup.enter="handleSubmit"
  23. />
  24. </div>
  25. </div>
  26. <template #footer>
  27. <span class="dialog-footer">
  28. <el-button @click="handleCancel">取消</el-button>
  29. <el-button type="primary" :loading="submitting" @click="handleSubmit">确认</el-button>
  30. </span>
  31. </template>
  32. </el-dialog>
  33. </template>
  34. <script setup>
  35. import { computed, getCurrentInstance, reactive, ref, watch } from 'vue'
  36. defineOptions({ name: 'CreateEnterpriseDialog' })
  37. const props = defineProps({
  38. visible: {
  39. type: Boolean,
  40. default: false
  41. }
  42. })
  43. const emit = defineEmits(['update:visible', 'close'])
  44. const { proxy } = getCurrentInstance()
  45. // mock client user info to decouple from vuex
  46. const mockClientUser = reactive({
  47. email: 'demo_client@example.com'
  48. })
  49. const form = reactive({
  50. account: '',
  51. name: ''
  52. })
  53. const submitting = ref(false)
  54. const dialogVisible = computed({
  55. get: () => props.visible,
  56. set: val => emit('update:visible', val)
  57. })
  58. const clientEmail = computed(() => mockClientUser.email || '')
  59. watch(
  60. () => props.visible,
  61. newVal => {
  62. if (newVal) {
  63. initForm()
  64. ensureClientEmail()
  65. } else {
  66. resetForm()
  67. }
  68. }
  69. )
  70. watch(
  71. clientEmail,
  72. newVal => {
  73. if (newVal && !form.account) {
  74. form.account = newVal
  75. }
  76. },
  77. { immediate: true }
  78. )
  79. const message = proxy?.$message
  80. const initForm = () => {
  81. form.name = ''
  82. ensureClientEmail()
  83. }
  84. const resetForm = () => {
  85. form.name = ''
  86. submitting.value = false
  87. }
  88. const ensureClientEmail = () => {
  89. // use mock email as placeholder until real API is wired
  90. form.account = clientEmail.value || 'client@example.com'
  91. }
  92. const validateForm = () => {
  93. if (!form.name || !form.name.trim()) {
  94. message?.error('请输入企业名称')
  95. return false
  96. }
  97. const trimmedName = form.name.trim()
  98. if (trimmedName.length < 2) {
  99. message?.error('企业名称至少需要2个字符')
  100. return false
  101. }
  102. if (trimmedName.length > 50) {
  103. message?.error('企业名称不能超过50个字符')
  104. return false
  105. }
  106. return true
  107. }
  108. const handleSubmit = async () => {
  109. if (!validateForm()) {
  110. return
  111. }
  112. await createEnterprise()
  113. }
  114. const createEnterprise = async () => {
  115. // TODO: 接入创建企业接口
  116. }
  117. const handleCancel = () => {
  118. dialogVisible.value = false
  119. }
  120. const handleClose = () => {
  121. resetForm()
  122. emit('close')
  123. }
  124. </script>
  125. <style scoped lang="scss">
  126. .enterprise-form {
  127. .form-item {
  128. margin-bottom: 16px;
  129. &:last-child {
  130. margin-bottom: 0;
  131. }
  132. }
  133. .label {
  134. margin-bottom: 6px;
  135. font-size: 13px;
  136. color: #606266;
  137. &.required::after {
  138. content: '*';
  139. color: #f56c6c;
  140. margin-left: 4px;
  141. }
  142. }
  143. .hint {
  144. margin: 4px 0 0;
  145. font-size: 12px;
  146. color: #909399;
  147. }
  148. }
  149. </style>