createUserLogic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. func (l *CreateUserLogic) CreateUser(req *types.CreateUserReq) (resp *types.IdResp, err error) {
  32. productCode := middleware.GetProductCode(l.ctx)
  33. if err := authHelper.RequireProductAdminFor(l.ctx, productCode); err != nil {
  34. return nil, err
  35. }
  36. if msg := util.ValidatePassword(req.Password); msg != "" {
  37. return nil, response.ErrBadRequest(msg)
  38. }
  39. if !usernameRegexp.MatchString(req.Username) {
  40. return nil, response.ErrBadRequest("用户名只能包含字母、数字和下划线,长度2-64个字符")
  41. }
  42. if len(req.Nickname) > 64 {
  43. return nil, response.ErrBadRequest("昵称长度不能超过64个字符")
  44. }
  45. if len(req.Remark) > 255 {
  46. return nil, response.ErrBadRequest("备注长度不能超过255个字符")
  47. }
  48. if req.Email != "" && !util.IsValidEmail(req.Email) {
  49. return nil, response.ErrBadRequest("邮箱格式不正确")
  50. }
  51. if req.Phone != "" && !util.IsValidPhone(req.Phone) {
  52. return nil, response.ErrBadRequest("手机号格式不正确")
  53. }
  54. if req.DeptId > 0 {
  55. if _, err := l.svcCtx.SysDeptModel.FindOne(l.ctx, req.DeptId); err != nil {
  56. return nil, response.ErrBadRequest("部门不存在")
  57. }
  58. }
  59. hashedPwd, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
  60. if err != nil {
  61. return nil, err
  62. }
  63. now := time.Now().Unix()
  64. result, err := l.svcCtx.SysUserModel.Insert(l.ctx, &userModel.SysUser{
  65. Username: req.Username,
  66. Password: string(hashedPwd),
  67. Nickname: req.Nickname,
  68. Email: req.Email,
  69. Phone: req.Phone,
  70. Remark: req.Remark,
  71. DeptId: req.DeptId,
  72. IsSuperAdmin: consts.IsSuperAdminNo,
  73. MustChangePassword: consts.MustChangePasswordNo,
  74. Status: consts.StatusEnabled,
  75. CreateTime: now,
  76. UpdateTime: now,
  77. })
  78. if err != nil {
  79. if strings.Contains(err.Error(), "1062") || strings.Contains(err.Error(), "Duplicate entry") {
  80. return nil, response.ErrConflict("用户名已存在")
  81. }
  82. return nil, err
  83. }
  84. id, _ := result.LastInsertId()
  85. return &types.IdResp{Id: id}, nil
  86. }