作者 yangfu

1. 用户注册修改

package command
import (
"fmt"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type UserSignUpCommand struct {
// 企业名称
Name string `cname:"用户姓名" json:"name" valid:"Required"`
// 手机号码
Phone string `cname:"手机号码" json:"phone" valid:"Required"`
// 密码
Password string `cname:"密码" json:"password" valid:"Required"`
}
func (companySignUpCommand *UserSignUpCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (companySignUpCommand *UserSignUpCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(companySignUpCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(companySignUpCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -5,6 +5,8 @@ import (
)
type UserBaseDto struct {
// 游客用户id
UserId int64 `json:"userId,omitempty"`
// 用户基础数据id
UserBaseId int64 `json:"userBaseId,omitempty"`
UserType int `json:"userType"`
... ...
... ... @@ -59,6 +59,41 @@ func (authService *AuthService) CompanySignUp(companySignUpCommand *command.Comp
return struct{}{}, nil
}
// 个人注册
func (authService *AuthService) UserSignUp(companySignUpCommand *command.UserSignUpCommand) (interface{}, error) {
if err := companySignUpCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
signUpPersonService, err := factory.CreatePgSignUpPersonService(map[string]interface{}{
"transactionContext": transactionContext,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
userInfo := &domain.UserInfo{
UserName: companySignUpCommand.Name,
Phone: companySignUpCommand.Phone,
}
if _, err = signUpPersonService.SignUp(companySignUpCommand.Phone, companySignUpCommand.Password, userInfo); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
}
// 注销账号 (添加用户时重新激活)
func (authService *AuthService) DestroyAccount(destroyAccountCommand *command.DestroyAccountCommand) (interface{}, error) {
if err := destroyAccountCommand.ValidateCommand(); err != nil {
... ... @@ -331,6 +366,10 @@ func (authService *AuthService) UserInfo(userInfoQuery *query.UserInfoQuery) (in
}
ubDto := &dto.UserBaseDto{}
ubDto.LoadDto(userBase)
userRepository, _, _ := factory.FastPgUser(transactionContext, 0)
if user, err := userRepository.FindOne(map[string]interface{}{"userBaseId": userBase.UserBaseId, "userType": domain.UserTypeVisitor}); err == nil {
ubDto.UserId = user.UserId
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
... ...
... ... @@ -15,6 +15,14 @@ func CreateSignUpCompanyService(options map[string]interface{}) (service.PgSignU
return domainService.NewPgSignUpCompanyServiceService(transactionContext)
}
func CreatePgSignUpPersonService(options map[string]interface{}) (service.PgSignUpPersonService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return domainService.NewPgSignUpPersonService(transactionContext)
}
func CreatePgAuthResetPhoneService(options map[string]interface{}) (service.PgAuthResetPhoneService, error) {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
... ...
... ... @@ -523,7 +523,7 @@ func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (in
user := users[i]
userDto := &dto.UserDto{}
var ok bool
if company, ok = mapCompany[user.CompanyId]; !ok {
if company, ok = mapCompany[user.CompanyId]; !ok && user.CompanyId > 0 {
_, company, err = factory.FastPgCompany(transactionContext, user.CompanyId)
if err != nil {
log.Logger.Error(err.Error())
... ...
package service
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
// PgSignUpPersonService 个人注册服务
type PgSignUpPersonService interface {
SignUp(registerPhone string, password string, userInfo *domain.UserInfo) (*domain.UserBase, error)
}
... ...
package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/repository"
"time"
)
// PgSignUpPersonService 个人注册服务
type PgSignUpPersonService struct {
transactionContext *pgTransaction.TransactionContext
}
// SignUp 个人注册服务
//
// registerPhone 注册人手机号
// password 密码
// companyInfo 注册公司信息
// userInfo 用户信息
func (ptr *PgSignUpPersonService) SignUp(account string, password string, userInfo *domain.UserInfo) (*domain.UserBase, error) {
var err error
// 前置验证
if len(account) == 0 || len(password) == 0 {
return nil, fmt.Errorf("账号密码不能为空")
}
var existsUser *domain.User
var userBase *domain.UserBase
createUserAccountService, _ := NewPgCreateUserAccountService(ptr.transactionContext)
if userBase, err = createUserAccountService.CreateUserAccount(account, password, userInfo); err != nil {
return nil, err
}
userBaseRepository, _ := repository.NewUserRepository(ptr.transactionContext)
if existsUser, err = userBaseRepository.FindOne(map[string]interface{}{"userBaseId": userBase.UserBaseId, "userType": domain.UserTypeVisitor}); err == nil && existsUser != nil {
return nil, fmt.Errorf("账号已存在")
}
// 4.创建用户、分配角色、关联组织、账号
var user *domain.User = &domain.User{
CompanyId: 0,
UserType: domain.UserTypeVisitor,
UserCode: "",
OrganizationId: 0,
DepartmentId: 0,
UserOrg: []*domain.Org{},
UserRole: []*domain.Role{},
FavoriteMenus: []string{},
CooperationInfo: &domain.CooperationInfo{},
EnableStatus: int(domain.UserStatusEnable),
UserInfo: userInfo,
Ext: &domain.Ext{
UserName: userInfo.UserName,
Phone: userInfo.Phone,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
createUserService, _ := NewPgCreateUserService(ptr.transactionContext)
if user, err = createUserService.CreateUser(nil, user, password); err != nil {
return nil, err
}
return userBase, nil
}
func NewPgSignUpPersonService(transactionContext *pgTransaction.TransactionContext) (*PgSignUpPersonService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PgSignUpPersonService{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -168,6 +168,7 @@ func (repository *UserRepository) FindOne(queryOptions map[string]interface{}) (
query.SetWhereByQueryOption("user_base_id=?", "userBaseId")
query.SetWhereByQueryOption("user_code = ?", "userCode")
query.SetWhereByQueryOption("user_id != ?", "notEqualUserId")
query.SetWhereByQueryOption("user_type & ? > 0", "userType")
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, fmt.Errorf("没有此资源")
... ...
... ... @@ -19,6 +19,14 @@ func (controller *AuthController) CompanySignUp() {
controller.Response(data, err)
}
func (controller *AuthController) SignUp() {
authService := service.NewAuthService(nil)
companySignUpCommand := &command.UserSignUpCommand{}
controller.Unmarshal(companySignUpCommand)
data, err := authService.UserSignUp(companySignUpCommand)
controller.Response(data, err)
}
func (controller *AuthController) PhoneAuthCheck() {
authService := service.NewAuthService(nil)
phoneAuthCheckCommand := &command.PhoneAuthCheckCommand{}
... ...
... ... @@ -7,6 +7,7 @@ import (
func init() {
web.Router("/auth/company-sign-up", &controllers.AuthController{}, "Post:CompanySignUp")
web.Router("/auth/user-sign-up", &controllers.AuthController{}, "Post:SignUp")
web.Router("/auth/check-password", &controllers.AuthController{}, "Post:PhoneAuthCheck")
web.Router("/auth/reset-password", &controllers.AuthController{}, "Post:PhoneAuthResetPassword")
web.Router("/auth/change-password", &controllers.AuthController{}, "Post:PhoneAuthChangePassword")
... ...