user-dialog.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { Component, Inject } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { FormsModule } from '@angular/forms';
  4. import { MatButtonModule } from '@angular/material/button';
  5. import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
  6. import { MatFormFieldModule } from '@angular/material/form-field';
  7. import { MatInputModule } from '@angular/material/input';
  8. import { MatSelectModule } from '@angular/material/select';
  9. import { MatDatepickerModule } from '@angular/material/datepicker';
  10. import { MatNativeDateModule } from '@angular/material/core';
  11. import { MatIconModule } from '@angular/material/icon';
  12. interface User {
  13. id: string;
  14. username: string;
  15. realName: string;
  16. email: string;
  17. phone: string;
  18. role: string;
  19. department: string;
  20. status: 'active' | 'inactive';
  21. createdAt: string;
  22. lastLogin: string;
  23. }
  24. @Component({
  25. selector: 'app-user-dialog',
  26. standalone: true,
  27. imports: [
  28. CommonModule,
  29. FormsModule,
  30. MatButtonModule,
  31. MatDialogModule,
  32. MatFormFieldModule,
  33. MatInputModule,
  34. MatSelectModule,
  35. MatDatepickerModule,
  36. MatNativeDateModule,
  37. MatIconModule
  38. ],
  39. templateUrl: './user-dialog.html',
  40. styleUrl: './user-dialog.scss'
  41. })
  42. export class UserDialogComponent {
  43. user: User;
  44. isEditMode: boolean;
  45. roles: string[] = ['超级管理员', '客服', '设计师', '组长', '财务', '人事'];
  46. departments: string[] = ['管理层', '客户服务部', '设计部', '财务部', '人力资源部', '行政部'];
  47. statuses: { value: string; label: string }[] = [
  48. { value: 'active', label: '激活' },
  49. { value: 'inactive', label: '禁用' }
  50. ];
  51. constructor(
  52. public dialogRef: MatDialogRef<UserDialogComponent>,
  53. @Inject(MAT_DIALOG_DATA) public data: User
  54. ) {
  55. this.isEditMode = !!data.id;
  56. this.user = {
  57. id: data.id || '',
  58. username: data.username || '',
  59. realName: data.realName || '',
  60. email: data.email || '',
  61. phone: data.phone || '',
  62. role: data.role || '',
  63. department: data.department || '',
  64. status: data.status || 'active',
  65. createdAt: data.createdAt || new Date().toISOString().split('T')[0],
  66. lastLogin: data.lastLogin || ''
  67. };
  68. }
  69. onCancel(): void {
  70. this.dialogRef.close();
  71. }
  72. onSave(): void {
  73. if (this.isFormValid()) {
  74. this.dialogRef.close(this.user);
  75. }
  76. }
  77. isFormValid(): boolean {
  78. // 验证用户名
  79. if (!this.user.username.trim() || this.user.username.length < 3) {
  80. alert('用户名至少需要3个字符');
  81. return false;
  82. }
  83. // 验证姓名
  84. if (!this.user.realName.trim()) {
  85. alert('请输入真实姓名');
  86. return false;
  87. }
  88. // 验证邮箱
  89. const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  90. if (!emailRegex.test(this.user.email)) {
  91. alert('请输入有效的邮箱地址');
  92. return false;
  93. }
  94. // 验证手机号
  95. const phoneRegex = /^1[3-9]\d{9}$/;
  96. if (!phoneRegex.test(this.user.phone)) {
  97. alert('请输入有效的手机号码');
  98. return false;
  99. }
  100. // 验证角色和部门
  101. if (!this.user.role || !this.user.department) {
  102. alert('请选择角色和部门');
  103. return false;
  104. }
  105. return true;
  106. }
  107. generateRandomPassword(): void {
  108. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()';
  109. let password = '';
  110. for (let i = 0; i < 12; i++) {
  111. password += chars.charAt(Math.floor(Math.random() * chars.length));
  112. }
  113. alert(`生成的随机密码:${password}\n请妥善保存!`);
  114. }
  115. onRoleChange(): void {
  116. // 超级管理员只能属于管理层
  117. if (this.user.role === '超级管理员') {
  118. this.user.department = '管理层';
  119. }
  120. }
  121. }