AddMemberDialog.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <template>
  2. <el-dialog title="邮箱添加人员" :model-value="props.visible" @update:model-value="(val) => emit('update:visible', val)"
  3. width="480px" :close-on-click-modal="false" :close-on-press-escape="true" class="add-member-dialog"
  4. @close="handleClose">
  5. <div class="add-member-form">
  6. <div class="form-item">
  7. <div class="input-with-button">
  8. <el-input v-model="form.email" placeholder="请输入邮箱地址" clearable :disabled="searching"
  9. @keyup.enter="handleSearch" @clear="handleClear">
  10. <template #prefix>
  11. <el-icon class="input-prefix">
  12. <Message />
  13. </el-icon>
  14. </template>
  15. </el-input>
  16. <el-button type="primary" :loading="searching" :disabled="!form.email || !form.email.trim()"
  17. class="search-btn" @click="handleSearch">
  18. <el-icon v-if="!searching" class="search-icon">
  19. <Search />
  20. </el-icon>
  21. <span>{{ searching ? '搜索中...' : '搜索' }}</span>
  22. </el-button>
  23. </div>
  24. <div v-if="form.email && !isValidEmail" class="error-tip">
  25. <el-icon class="error-icon">
  26. <WarningFilled />
  27. </el-icon>
  28. <span>请输入正确的邮箱格式</span>
  29. </div>
  30. </div>
  31. <transition name="fade-slide">
  32. <div v-if="searchedUser" class="form-item member-info">
  33. <div class="label">
  34. <el-icon class="label-icon">
  35. <User />
  36. </el-icon>
  37. <span>人员信息</span>
  38. </div>
  39. <div class="member-card">
  40. <div class="member-avatar">
  41. <el-icon class="member-avatar-icon">
  42. <UserFilled />
  43. </el-icon>
  44. </div>
  45. <div class="member-details">
  46. <div class="member-name">
  47. <span class="name-label">姓名:</span>
  48. <span class="name-value">{{ searchedUser.name }}</span>
  49. </div>
  50. <div class="member-email">
  51. <span class="email-label">邮箱:</span>
  52. <span class="email-value">{{ form.email }}</span>
  53. </div>
  54. </div>
  55. <div class="member-status">
  56. <el-icon class="status-icon">
  57. <SuccessFilled />
  58. </el-icon>
  59. </div>
  60. </div>
  61. </div>
  62. </transition>
  63. <div v-if="!searchedUser && form.email && isValidEmail && !searching" class="empty-state">
  64. <el-icon class="empty-icon">
  65. <Search />
  66. </el-icon>
  67. <p>点击搜索按钮查找用户</p>
  68. </div>
  69. </div>
  70. <template #footer>
  71. <span class="dialog-footer">
  72. <el-button size="small" @click="handleCancel">取消</el-button>
  73. <el-button type="primary" size="small" :loading="submitting" :disabled="!searchedUser" @click="handleSubmit">
  74. <span>{{ submitting ? '添加中...' : '确认添加' }}</span>
  75. </el-button>
  76. </span>
  77. </template>
  78. </el-dialog>
  79. </template>
  80. <script setup>
  81. import { computed, reactive, ref, watch } from 'vue'
  82. import { Message, Search, SuccessFilled, User, UserFilled, WarningFilled } from '@element-plus/icons-vue'
  83. import { ElMessage } from 'element-plus'
  84. import { searchEmail, addMember } from '/@/api/frontend/me'
  85. defineOptions({ name: 'AddMemberDialog' })
  86. const props = defineProps({
  87. visible: {
  88. type: Boolean,
  89. default: false
  90. },
  91. companyId: {
  92. type: [Number, String],
  93. default: null
  94. }
  95. })
  96. const emit = defineEmits(['update:visible', 'success', 'close'])
  97. const form = reactive({
  98. email: ''
  99. })
  100. const searchedUser = ref(null)
  101. const searching = ref(false)
  102. const submitting = ref(false)
  103. const isValidEmail = computed(() => {
  104. if (!form.email || !form.email.trim()) {
  105. return true
  106. }
  107. const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  108. return emailRegex.test(form.email.trim())
  109. })
  110. watch(
  111. () => props.visible,
  112. newVal => {
  113. if (newVal) {
  114. resetForm()
  115. } else {
  116. resetForm()
  117. }
  118. }
  119. )
  120. const resetForm = () => {
  121. form.email = ''
  122. searchedUser.value = null
  123. searching.value = false
  124. submitting.value = false
  125. }
  126. const handleClear = () => {
  127. searchedUser.value = null
  128. }
  129. const handleSearch = async () => {
  130. if (!form.email || !form.email.trim()) {
  131. ElMessage.warning('请输入邮箱')
  132. return
  133. }
  134. if (!isValidEmail.value) {
  135. ElMessage.error('邮箱格式不正确')
  136. return
  137. }
  138. searching.value = true
  139. try {
  140. const response = await searchEmail({
  141. keyword: form.email.trim()
  142. })
  143. searchedUser.value = response.data[0]
  144. ElMessage.success('搜索成功')
  145. } catch (error) {
  146. ElMessage.error('搜索用户失败,请稍后重试')
  147. searchedUser.value = null
  148. } finally {
  149. searching.value = false
  150. }
  151. }
  152. const handleSubmit = async () => {
  153. if (!searchedUser.value) {
  154. ElMessage.warning('请先搜索用户')
  155. return
  156. }
  157. if (!props.companyId) {
  158. ElMessage.error('企业ID不能为空')
  159. return
  160. }
  161. if (!form.email || !form.email.trim()) {
  162. ElMessage.error('邮箱不能为空')
  163. return
  164. }
  165. // 获取用户姓名,如果搜索返回的数据中没有name,使用email作为默认值
  166. const userName = searchedUser.value.name || searchedUser.value.email || form.email.trim()
  167. submitting.value = true
  168. try {
  169. await addMember({
  170. company_id: String(props.companyId),
  171. email: form.email.trim(),
  172. name: userName
  173. })
  174. ElMessage.success('添加成员成功')
  175. emit('success', searchedUser.value)
  176. emit('update:visible', false)
  177. } catch (error) {
  178. ElMessage.error('添加成员失败,请稍后重试')
  179. } finally {
  180. submitting.value = false
  181. }
  182. }
  183. const handleCancel = () => {
  184. emit('update:visible', false)
  185. }
  186. const handleClose = () => {
  187. resetForm()
  188. emit('close')
  189. }
  190. </script>
  191. <style scoped lang="scss">
  192. ::v-deep .add-member-dialog {
  193. .label-icon,
  194. .input-prefix,
  195. .search-icon,
  196. .error-icon,
  197. .member-avatar-icon,
  198. .status-icon,
  199. .empty-icon,
  200. .submit-icon {
  201. font-size: 16px;
  202. }
  203. .member-avatar-icon {
  204. font-size: 20px;
  205. }
  206. .status-icon {
  207. color: #67c23a;
  208. }
  209. .error-icon {
  210. color: #f56c6c;
  211. }
  212. .empty-icon {
  213. font-size: 22px;
  214. color: #909399;
  215. }
  216. .el-dialog {
  217. border-radius: 8px;
  218. overflow: hidden;
  219. }
  220. .el-dialog__header {
  221. padding: 20px 24px;
  222. background: linear-gradient(135deg, #409eff 0%, #2563eb 100%);
  223. color: #fff;
  224. border-bottom: none;
  225. .el-dialog__title {
  226. font-size: 18px;
  227. font-weight: 600;
  228. color: #fff;
  229. }
  230. .el-dialog__close {
  231. color: #fff;
  232. font-size: 20px;
  233. &:hover {
  234. color: rgba(255, 255, 255, 0.8);
  235. }
  236. }
  237. }
  238. .el-dialog__body {
  239. padding: 24px;
  240. }
  241. .el-dialog__footer {
  242. padding: 16px 24px;
  243. border-top: 1px solid #ebeef5;
  244. background: #fafafa;
  245. }
  246. }
  247. .add-member-form {
  248. .form-item {
  249. margin-bottom: 24px;
  250. &:last-child {
  251. margin-bottom: 0;
  252. }
  253. &.member-info {
  254. margin-top: 28px;
  255. padding-top: 24px;
  256. border-top: 1px solid #ebeef5;
  257. }
  258. }
  259. .label {
  260. display: flex;
  261. align-items: center;
  262. margin-bottom: 10px;
  263. font-size: 14px;
  264. color: #303133;
  265. font-weight: 500;
  266. i {
  267. margin-right: 6px;
  268. font-size: 16px;
  269. color: #409eff;
  270. }
  271. span {
  272. line-height: 1;
  273. }
  274. }
  275. .input-with-button {
  276. display: flex;
  277. gap: 12px;
  278. align-items: flex-start;
  279. .el-input {
  280. flex: 1;
  281. ::v-deep .el-input__inner {
  282. height: 40px;
  283. line-height: 40px;
  284. border-radius: 6px;
  285. transition: all 0.3s ease;
  286. }
  287. }
  288. .search-btn {
  289. flex-shrink: 0;
  290. min-width: 100px;
  291. height: 40px;
  292. border-radius: 6px;
  293. font-weight: 500;
  294. transition: all 0.3s ease;
  295. box-shadow: 0 2px 4px rgba(64, 158, 255, 0.2);
  296. &:hover:not(:disabled) {
  297. transform: translateY(-1px);
  298. box-shadow: 0 4px 8px rgba(64, 158, 255, 0.3);
  299. }
  300. &:active:not(:disabled) {
  301. transform: translateY(0);
  302. }
  303. i {
  304. margin-right: 4px;
  305. }
  306. }
  307. }
  308. .error-tip {
  309. display: flex;
  310. align-items: center;
  311. margin-top: 8px;
  312. font-size: 12px;
  313. color: #f56c6c;
  314. i {
  315. margin-right: 4px;
  316. font-size: 14px;
  317. }
  318. }
  319. .member-card {
  320. display: flex;
  321. align-items: center;
  322. padding: 16px;
  323. background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  324. border-radius: 8px;
  325. border: 1px solid #e4e7ed;
  326. transition: all 0.3s ease;
  327. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  328. &:hover {
  329. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  330. transform: translateY(-2px);
  331. }
  332. .member-avatar {
  333. width: 48px;
  334. height: 48px;
  335. border-radius: 50%;
  336. background: linear-gradient(135deg, #409eff 0%, #2563eb 100%);
  337. display: flex;
  338. align-items: center;
  339. justify-content: center;
  340. margin-right: 16px;
  341. flex-shrink: 0;
  342. box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
  343. i {
  344. font-size: 24px;
  345. color: #fff;
  346. }
  347. }
  348. .member-details {
  349. flex: 1;
  350. min-width: 0;
  351. .member-name,
  352. .member-email {
  353. display: flex;
  354. align-items: center;
  355. margin-bottom: 6px;
  356. font-size: 14px;
  357. &:last-child {
  358. margin-bottom: 0;
  359. }
  360. .name-label,
  361. .email-label {
  362. color: #606266;
  363. margin-right: 4px;
  364. font-weight: 400;
  365. }
  366. .name-value {
  367. font-weight: 600;
  368. color: #303133;
  369. font-size: 15px;
  370. }
  371. .email-value {
  372. color: #606266;
  373. word-break: break-all;
  374. }
  375. }
  376. }
  377. .member-status {
  378. margin-left: 12px;
  379. flex-shrink: 0;
  380. .status-icon {
  381. font-size: 24px;
  382. color: #67c23a;
  383. animation: scaleIn 0.3s ease;
  384. }
  385. }
  386. }
  387. .empty-state {
  388. display: flex;
  389. flex-direction: column;
  390. align-items: center;
  391. justify-content: center;
  392. padding: 40px 20px;
  393. text-align: center;
  394. color: #909399;
  395. i {
  396. font-size: 48px;
  397. margin-bottom: 12px;
  398. color: #c0c4cc;
  399. }
  400. p {
  401. font-size: 14px;
  402. margin: 0;
  403. }
  404. }
  405. }
  406. // 过渡动画
  407. .fade-slide-enter-active,
  408. .fade-slide-leave-active {
  409. transition: all 0.3s ease;
  410. }
  411. .fade-slide-enter {
  412. opacity: 0;
  413. transform: translateY(-10px);
  414. }
  415. .fade-slide-leave-to {
  416. opacity: 0;
  417. transform: translateY(-10px);
  418. }
  419. .dialog-footer {
  420. display: flex;
  421. justify-content: flex-end;
  422. gap: 12px;
  423. .el-button {
  424. min-width: 100px;
  425. height: 40px;
  426. border-radius: 6px;
  427. font-weight: 500;
  428. transition: all 0.3s ease;
  429. &.el-button--primary {
  430. box-shadow: 0 2px 4px rgba(64, 158, 255, 0.2);
  431. &:hover:not(:disabled) {
  432. transform: translateY(-1px);
  433. box-shadow: 0 4px 8px rgba(64, 158, 255, 0.3);
  434. }
  435. &:active:not(:disabled) {
  436. transform: translateY(0);
  437. }
  438. }
  439. i {
  440. margin-right: 4px;
  441. }
  442. }
  443. }
  444. // 动画
  445. @keyframes scaleIn {
  446. from {
  447. transform: scale(0);
  448. opacity: 0;
  449. }
  450. to {
  451. transform: scale(1);
  452. opacity: 1;
  453. }
  454. }
  455. </style>