createUserLogic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package user
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "perms-system-server/internal/consts"
  7. authHelper "perms-system-server/internal/logic/auth"
  8. "perms-system-server/internal/middleware"
  9. userModel "perms-system-server/internal/model/user"
  10. "perms-system-server/internal/response"
  11. "perms-system-server/internal/svc"
  12. "perms-system-server/internal/types"
  13. "perms-system-server/internal/util"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. "golang.org/x/crypto/bcrypt"
  16. )
  17. type CreateUserLogic struct {
  18. logx.Logger
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. }
  22. func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
  23. return &CreateUserLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. }
  28. }
  29. func (l *CreateUserLogic) CreateUser(req *types.CreateUserReq) (resp *types.IdResp, err error) {
  30. productCode := middleware.GetProductCode(l.ctx)
  31. if err := authHelper.RequireProductAdminFor(l.ctx, productCode); err != nil {
  32. return nil, err
  33. }
  34. if len(req.Password) < 6 {
  35. return nil, response.ErrBadRequest("密码长度不能少于6个字符")
  36. }
  37. if len(req.Password) > 72 {
  38. return nil, response.ErrBadRequest("密码长度不能超过72个字符")
  39. }
  40. if len(req.Username) > 64 {
  41. return nil, response.ErrBadRequest("用户名长度不能超过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. hashedPwd, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
  56. if err != nil {
  57. return nil, err
  58. }
  59. now := time.Now().Unix()
  60. result, err := l.svcCtx.SysUserModel.Insert(l.ctx, &userModel.SysUser{
  61. Username: req.Username,
  62. Password: string(hashedPwd),
  63. Nickname: req.Nickname,
  64. Email: req.Email,
  65. Phone: req.Phone,
  66. Remark: req.Remark,
  67. DeptId: req.DeptId,
  68. IsSuperAdmin: consts.IsSuperAdminNo,
  69. MustChangePassword: consts.MustChangePasswordNo,
  70. Status: consts.StatusEnabled,
  71. CreateTime: now,
  72. UpdateTime: now,
  73. })
  74. if err != nil {
  75. if strings.Contains(err.Error(), "1062") || strings.Contains(err.Error(), "Duplicate entry") {
  76. return nil, response.ErrConflict("用户名已存在")
  77. }
  78. return nil, err
  79. }
  80. id, _ := result.LastInsertId()
  81. return &types.IdResp{Id: id}, nil
  82. }