createUserLogic.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package user
  2. import (
  3. "context"
  4. "regexp"
  5. "strings"
  6. "time"
  7. "perms-system-server/internal/consts"
  8. authHelper "perms-system-server/internal/logic/auth"
  9. "perms-system-server/internal/middleware"
  10. userModel "perms-system-server/internal/model/user"
  11. "perms-system-server/internal/response"
  12. "perms-system-server/internal/svc"
  13. "perms-system-server/internal/types"
  14. "perms-system-server/internal/util"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. "golang.org/x/crypto/bcrypt"
  17. )
  18. var usernameRegexp = regexp.MustCompile(`^[a-zA-Z0-9_]{2,64}$`)
  19. type CreateUserLogic struct {
  20. logx.Logger
  21. ctx context.Context
  22. svcCtx *svc.ServiceContext
  23. }
  24. func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
  25. return &CreateUserLogic{
  26. Logger: logx.WithContext(ctx),
  27. ctx: ctx,
  28. svcCtx: svcCtx,
  29. }
  30. }
  31. // CreateUser 创建用户。新建系统用户账号,可指定部门归属。仅超管可调用。
  32. func (l *CreateUserLogic) CreateUser(req *types.CreateUserReq) (resp *types.IdResp, err error) {
  33. productCode := middleware.GetProductCode(l.ctx)
  34. if err := authHelper.RequireProductAdminFor(l.ctx, productCode); err != nil {
  35. return nil, err
  36. }
  37. if msg := util.ValidatePassword(req.Password); msg != "" {
  38. return nil, response.ErrBadRequest(msg)
  39. }
  40. if !usernameRegexp.MatchString(req.Username) {
  41. return nil, response.ErrBadRequest("用户名只能包含字母、数字和下划线,长度2-64个字符")
  42. }
  43. if len(req.Nickname) > 64 {
  44. return nil, response.ErrBadRequest("昵称长度不能超过64个字符")
  45. }
  46. if len(req.Remark) > 255 {
  47. return nil, response.ErrBadRequest("备注长度不能超过255个字符")
  48. }
  49. if req.Email != "" && !util.IsValidEmail(req.Email) {
  50. return nil, response.ErrBadRequest("邮箱格式不正确")
  51. }
  52. if req.Phone != "" && !util.IsValidPhone(req.Phone) {
  53. return nil, response.ErrBadRequest("手机号格式不正确")
  54. }
  55. if req.DeptId > 0 {
  56. if _, err := l.svcCtx.SysDeptModel.FindOne(l.ctx, req.DeptId); err != nil {
  57. return nil, response.ErrBadRequest("部门不存在")
  58. }
  59. }
  60. hashedPwd, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
  61. if err != nil {
  62. return nil, err
  63. }
  64. now := time.Now().Unix()
  65. result, err := l.svcCtx.SysUserModel.Insert(l.ctx, &userModel.SysUser{
  66. Username: req.Username,
  67. Password: string(hashedPwd),
  68. Nickname: req.Nickname,
  69. Email: req.Email,
  70. Phone: req.Phone,
  71. Remark: req.Remark,
  72. DeptId: req.DeptId,
  73. IsSuperAdmin: consts.IsSuperAdminNo,
  74. MustChangePassword: consts.MustChangePasswordNo,
  75. Status: consts.StatusEnabled,
  76. CreateTime: now,
  77. UpdateTime: now,
  78. })
  79. if err != nil {
  80. if strings.Contains(err.Error(), "1062") || strings.Contains(err.Error(), "Duplicate entry") {
  81. return nil, response.ErrConflict("用户名已存在")
  82. }
  83. return nil, err
  84. }
  85. id, _ := result.LastInsertId()
  86. return &types.IdResp{Id: id}, nil
  87. }