作者 yangfu

1. 用户注册修改

  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type UserSignUpCommand struct {
  12 + // 企业名称
  13 + Name string `cname:"用户姓名" json:"name" valid:"Required"`
  14 + // 手机号码
  15 + Phone string `cname:"手机号码" json:"phone" valid:"Required"`
  16 + // 密码
  17 + Password string `cname:"密码" json:"password" valid:"Required"`
  18 +}
  19 +
  20 +func (companySignUpCommand *UserSignUpCommand) Valid(validation *validation.Validation) {
  21 + //validation.SetError("CustomValid", "未实现的自定义认证")
  22 +}
  23 +
  24 +func (companySignUpCommand *UserSignUpCommand) ValidateCommand() error {
  25 + valid := validation.Validation{}
  26 + b, err := valid.Valid(companySignUpCommand)
  27 + if err != nil {
  28 + return err
  29 + }
  30 + if !b {
  31 + elem := reflect.TypeOf(companySignUpCommand).Elem()
  32 + for _, validErr := range valid.Errors {
  33 + field, isExist := elem.FieldByName(validErr.Field)
  34 + if isExist {
  35 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  36 + } else {
  37 + return fmt.Errorf(validErr.Message)
  38 + }
  39 + }
  40 + }
  41 + return nil
  42 +}
@@ -5,6 +5,8 @@ import ( @@ -5,6 +5,8 @@ import (
5 ) 5 )
6 6
7 type UserBaseDto struct { 7 type UserBaseDto struct {
  8 + // 游客用户id
  9 + UserId int64 `json:"userId,omitempty"`
8 // 用户基础数据id 10 // 用户基础数据id
9 UserBaseId int64 `json:"userBaseId,omitempty"` 11 UserBaseId int64 `json:"userBaseId,omitempty"`
10 UserType int `json:"userType"` 12 UserType int `json:"userType"`
@@ -59,6 +59,41 @@ func (authService *AuthService) CompanySignUp(companySignUpCommand *command.Comp @@ -59,6 +59,41 @@ func (authService *AuthService) CompanySignUp(companySignUpCommand *command.Comp
59 return struct{}{}, nil 59 return struct{}{}, nil
60 } 60 }
61 61
  62 +// 个人注册
  63 +func (authService *AuthService) UserSignUp(companySignUpCommand *command.UserSignUpCommand) (interface{}, error) {
  64 + if err := companySignUpCommand.ValidateCommand(); err != nil {
  65 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  66 + }
  67 + transactionContext, err := factory.CreateTransactionContext(nil)
  68 + if err != nil {
  69 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  70 + }
  71 + if err := transactionContext.StartTransaction(); err != nil {
  72 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  73 + }
  74 + defer func() {
  75 + transactionContext.RollbackTransaction()
  76 + }()
  77 +
  78 + signUpPersonService, err := factory.CreatePgSignUpPersonService(map[string]interface{}{
  79 + "transactionContext": transactionContext,
  80 + })
  81 + if err != nil {
  82 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  83 + }
  84 + userInfo := &domain.UserInfo{
  85 + UserName: companySignUpCommand.Name,
  86 + Phone: companySignUpCommand.Phone,
  87 + }
  88 + if _, err = signUpPersonService.SignUp(companySignUpCommand.Phone, companySignUpCommand.Password, userInfo); err != nil {
  89 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  90 + }
  91 + if err := transactionContext.CommitTransaction(); err != nil {
  92 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  93 + }
  94 + return struct{}{}, nil
  95 +}
  96 +
62 // 注销账号 (添加用户时重新激活) 97 // 注销账号 (添加用户时重新激活)
63 func (authService *AuthService) DestroyAccount(destroyAccountCommand *command.DestroyAccountCommand) (interface{}, error) { 98 func (authService *AuthService) DestroyAccount(destroyAccountCommand *command.DestroyAccountCommand) (interface{}, error) {
64 if err := destroyAccountCommand.ValidateCommand(); err != nil { 99 if err := destroyAccountCommand.ValidateCommand(); err != nil {
@@ -331,6 +366,10 @@ func (authService *AuthService) UserInfo(userInfoQuery *query.UserInfoQuery) (in @@ -331,6 +366,10 @@ func (authService *AuthService) UserInfo(userInfoQuery *query.UserInfoQuery) (in
331 } 366 }
332 ubDto := &dto.UserBaseDto{} 367 ubDto := &dto.UserBaseDto{}
333 ubDto.LoadDto(userBase) 368 ubDto.LoadDto(userBase)
  369 + userRepository, _, _ := factory.FastPgUser(transactionContext, 0)
  370 + if user, err := userRepository.FindOne(map[string]interface{}{"userBaseId": userBase.UserBaseId, "userType": domain.UserTypeVisitor}); err == nil {
  371 + ubDto.UserId = user.UserId
  372 + }
334 if err := transactionContext.CommitTransaction(); err != nil { 373 if err := transactionContext.CommitTransaction(); err != nil {
335 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 374 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
336 } 375 }
@@ -15,6 +15,14 @@ func CreateSignUpCompanyService(options map[string]interface{}) (service.PgSignU @@ -15,6 +15,14 @@ func CreateSignUpCompanyService(options map[string]interface{}) (service.PgSignU
15 return domainService.NewPgSignUpCompanyServiceService(transactionContext) 15 return domainService.NewPgSignUpCompanyServiceService(transactionContext)
16 } 16 }
17 17
  18 +func CreatePgSignUpPersonService(options map[string]interface{}) (service.PgSignUpPersonService, error) {
  19 + var transactionContext *pgTransaction.TransactionContext
  20 + if value, ok := options["transactionContext"]; ok {
  21 + transactionContext = value.(*pgTransaction.TransactionContext)
  22 + }
  23 + return domainService.NewPgSignUpPersonService(transactionContext)
  24 +}
  25 +
18 func CreatePgAuthResetPhoneService(options map[string]interface{}) (service.PgAuthResetPhoneService, error) { 26 func CreatePgAuthResetPhoneService(options map[string]interface{}) (service.PgAuthResetPhoneService, error) {
19 var transactionContext *pgTransaction.TransactionContext 27 var transactionContext *pgTransaction.TransactionContext
20 if value, ok := options["transactionContext"]; ok { 28 if value, ok := options["transactionContext"]; ok {
@@ -523,7 +523,7 @@ func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (in @@ -523,7 +523,7 @@ func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (in
523 user := users[i] 523 user := users[i]
524 userDto := &dto.UserDto{} 524 userDto := &dto.UserDto{}
525 var ok bool 525 var ok bool
526 - if company, ok = mapCompany[user.CompanyId]; !ok { 526 + if company, ok = mapCompany[user.CompanyId]; !ok && user.CompanyId > 0 {
527 _, company, err = factory.FastPgCompany(transactionContext, user.CompanyId) 527 _, company, err = factory.FastPgCompany(transactionContext, user.CompanyId)
528 if err != nil { 528 if err != nil {
529 log.Logger.Error(err.Error()) 529 log.Logger.Error(err.Error())
  1 +package service
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  4 +
  5 +// PgSignUpPersonService 个人注册服务
  6 +type PgSignUpPersonService interface {
  7 + SignUp(registerPhone string, password string, userInfo *domain.UserInfo) (*domain.UserBase, error)
  8 +}
  1 +package domainService
  2 +
  3 +import (
  4 + "fmt"
  5 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
  8 + "time"
  9 +)
  10 +
  11 +// PgSignUpPersonService 个人注册服务
  12 +type PgSignUpPersonService struct {
  13 + transactionContext *pgTransaction.TransactionContext
  14 +}
  15 +
  16 +// SignUp 个人注册服务
  17 +//
  18 +// registerPhone 注册人手机号
  19 +// password 密码
  20 +// companyInfo 注册公司信息
  21 +// userInfo 用户信息
  22 +func (ptr *PgSignUpPersonService) SignUp(account string, password string, userInfo *domain.UserInfo) (*domain.UserBase, error) {
  23 + var err error
  24 + // 前置验证
  25 + if len(account) == 0 || len(password) == 0 {
  26 + return nil, fmt.Errorf("账号密码不能为空")
  27 + }
  28 +
  29 + var existsUser *domain.User
  30 + var userBase *domain.UserBase
  31 + createUserAccountService, _ := NewPgCreateUserAccountService(ptr.transactionContext)
  32 + if userBase, err = createUserAccountService.CreateUserAccount(account, password, userInfo); err != nil {
  33 + return nil, err
  34 + }
  35 +
  36 + userBaseRepository, _ := repository.NewUserRepository(ptr.transactionContext)
  37 + if existsUser, err = userBaseRepository.FindOne(map[string]interface{}{"userBaseId": userBase.UserBaseId, "userType": domain.UserTypeVisitor}); err == nil && existsUser != nil {
  38 + return nil, fmt.Errorf("账号已存在")
  39 + }
  40 +
  41 + // 4.创建用户、分配角色、关联组织、账号
  42 + var user *domain.User = &domain.User{
  43 + CompanyId: 0,
  44 + UserType: domain.UserTypeVisitor,
  45 + UserCode: "",
  46 + OrganizationId: 0,
  47 + DepartmentId: 0,
  48 + UserOrg: []*domain.Org{},
  49 + UserRole: []*domain.Role{},
  50 + FavoriteMenus: []string{},
  51 + CooperationInfo: &domain.CooperationInfo{},
  52 + EnableStatus: int(domain.UserStatusEnable),
  53 + UserInfo: userInfo,
  54 + Ext: &domain.Ext{
  55 + UserName: userInfo.UserName,
  56 + Phone: userInfo.Phone,
  57 + },
  58 + CreatedAt: time.Now(),
  59 + UpdatedAt: time.Now(),
  60 + }
  61 + createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
  62 + if user, err = createUserService.CreateUser(nil, user, password); err != nil {
  63 + return nil, err
  64 + }
  65 +
  66 + return userBase, nil
  67 +}
  68 +
  69 +func NewPgSignUpPersonService(transactionContext *pgTransaction.TransactionContext) (*PgSignUpPersonService, error) {
  70 + if transactionContext == nil {
  71 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  72 + } else {
  73 + return &PgSignUpPersonService{
  74 + transactionContext: transactionContext,
  75 + }, nil
  76 + }
  77 +}
@@ -168,6 +168,7 @@ func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) ( @@ -168,6 +168,7 @@ func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (
168 query.SetWhereByQueryOption("user_base_id=?", "userBaseId") 168 query.SetWhereByQueryOption("user_base_id=?", "userBaseId")
169 query.SetWhereByQueryOption("user_code = ?", "userCode") 169 query.SetWhereByQueryOption("user_code = ?", "userCode")
170 query.SetWhereByQueryOption("user_id != ?", "notEqualUserId") 170 query.SetWhereByQueryOption("user_id != ?", "notEqualUserId")
  171 + query.SetWhereByQueryOption("user_type & ? > 0", "userType")
171 if err := query.First(); err != nil { 172 if err := query.First(); err != nil {
172 if err.Error() == "pg: no rows in result set" { 173 if err.Error() == "pg: no rows in result set" {
173 return nil, fmt.Errorf("没有此资源") 174 return nil, fmt.Errorf("没有此资源")
@@ -19,6 +19,14 @@ func (controller *AuthController) CompanySignUp() { @@ -19,6 +19,14 @@ func (controller *AuthController) CompanySignUp() {
19 controller.Response(data, err) 19 controller.Response(data, err)
20 } 20 }
21 21
  22 +func (controller *AuthController) SignUp() {
  23 + authService := service.NewAuthService(nil)
  24 + companySignUpCommand := &command.UserSignUpCommand{}
  25 + controller.Unmarshal(companySignUpCommand)
  26 + data, err := authService.UserSignUp(companySignUpCommand)
  27 + controller.Response(data, err)
  28 +}
  29 +
22 func (controller *AuthController) PhoneAuthCheck() { 30 func (controller *AuthController) PhoneAuthCheck() {
23 authService := service.NewAuthService(nil) 31 authService := service.NewAuthService(nil)
24 phoneAuthCheckCommand := &command.PhoneAuthCheckCommand{} 32 phoneAuthCheckCommand := &command.PhoneAuthCheckCommand{}
@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 7
8 func init() { 8 func init() {
9 web.Router("/auth/company-sign-up", &controllers.AuthController{}, "Post:CompanySignUp") 9 web.Router("/auth/company-sign-up", &controllers.AuthController{}, "Post:CompanySignUp")
  10 + web.Router("/auth/user-sign-up", &controllers.AuthController{}, "Post:SignUp")
10 web.Router("/auth/check-password", &controllers.AuthController{}, "Post:PhoneAuthCheck") 11 web.Router("/auth/check-password", &controllers.AuthController{}, "Post:PhoneAuthCheck")
11 web.Router("/auth/reset-password", &controllers.AuthController{}, "Post:PhoneAuthResetPassword") 12 web.Router("/auth/reset-password", &controllers.AuthController{}, "Post:PhoneAuthResetPassword")
12 web.Router("/auth/change-password", &controllers.AuthController{}, "Post:PhoneAuthChangePassword") 13 web.Router("/auth/change-password", &controllers.AuthController{}, "Post:PhoneAuthChangePassword")