...
|
...
|
@@ -50,16 +50,28 @@ func (l *MiniUserLoginLogic) MiniUserLogin(req *types.MiniUserLoginRequest) (res |
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
if loginInfo.User == nil {
|
|
|
return nil, xerr.NewErrMsgErr("用户不存在", err)
|
|
|
}
|
|
|
var userJwtToken = tool.UserToken{}
|
|
|
if loginInfo.User != nil {
|
|
|
userJwtToken.UserId = loginInfo.User.Id
|
|
|
userJwtToken.CompanyId = loginInfo.User.CompanyId
|
|
|
userJwtToken.ClientType = "mini"
|
|
|
|
|
|
var users []*domain.User
|
|
|
conn := l.svcCtx.DefaultDBConn()
|
|
|
_, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
|
|
|
MustWithKV("phone", loginInfo.Phone).
|
|
|
MustWithKV("auditStatus", []int{domain.UserAuditStatusWait, domain.UserAuditStatusPassed}).
|
|
|
WithFindOnly())
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
token, err = userJwtToken.GenerateToken(l.svcCtx.Config.MiniAuth.AccessSecret, l.svcCtx.Config.MiniAuth.AccessExpire)
|
|
|
if len(users) > 0 {
|
|
|
for _, user := range users {
|
|
|
if user.Enable == domain.UserEnable && user.AuditStatus == domain.UserAuditStatusPassed {
|
|
|
loginInfo.User = user
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
if loginInfo.User == nil {
|
|
|
loginInfo.User = users[0]
|
|
|
}
|
|
|
}
|
|
|
token, err = generateToken(l.svcCtx, loginInfo.User)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("登录失败", err)
|
|
|
}
|
...
|
...
|
@@ -69,17 +81,35 @@ func (l *MiniUserLoginLogic) MiniUserLogin(req *types.MiniUserLoginRequest) (res |
|
|
Success: true,
|
|
|
}
|
|
|
if loginInfo.User == nil {
|
|
|
resp.Token = ""
|
|
|
resp.Success = false
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func generateToken(svcCtx *svc.ServiceContext, user *domain.User) (token string, err error) {
|
|
|
var userJwtToken = tool.UserToken{}
|
|
|
if user != nil {
|
|
|
userJwtToken.UserId = user.Id
|
|
|
userJwtToken.CompanyId = user.CompanyId
|
|
|
userJwtToken.ClientType = "mini"
|
|
|
}
|
|
|
token, err = userJwtToken.GenerateToken(svcCtx.Config.MiniAuth.AccessSecret, svcCtx.Config.MiniAuth.AccessExpire)
|
|
|
if err != nil {
|
|
|
return "", xerr.NewErrMsgErr("登录失败", err)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
type WxClientLogin struct {
|
|
|
l *MiniUserLoginLogic
|
|
|
}
|
|
|
|
|
|
func (c WxClientLogin) WechatPhoneLogin(r domain.WechatLoginRequest) (*domain.LoginInfo, error) {
|
|
|
code := r.Code
|
|
|
response := &domain.LoginInfo{
|
|
|
Phone: "",
|
|
|
}
|
|
|
miniprogram := wechat.NewWechat().GetMiniProgram(&miniConfig.Config{
|
|
|
AppID: c.l.svcCtx.Config.Wechat.AppID,
|
|
|
AppSecret: c.l.svcCtx.Config.Wechat.AppSecret,
|
...
|
...
|
@@ -87,25 +117,12 @@ func (c WxClientLogin) WechatPhoneLogin(r domain.WechatLoginRequest) (*domain.Lo |
|
|
})
|
|
|
authResult, err := miniprogram.GetAuth().GetPhoneNumber(code)
|
|
|
if err != nil || authResult.ErrCode != 0 || authResult.PhoneInfo.PhoneNumber == "" {
|
|
|
return nil, xerr.NewCodeErrMsg(xerr.ErrWxMiniAuthFailError, nil, fmt.Sprintf("发起授权请求失败1 err : %v , code : %s , authResult : %+v", err, code, authResult))
|
|
|
return response, xerr.NewCodeErrMsg(xerr.ErrWxMiniAuthFailError, nil, fmt.Sprintf("发起授权请求失败1 err : %v , code : %s , authResult : %+v", err, code, authResult))
|
|
|
}
|
|
|
var (
|
|
|
users []*domain.User
|
|
|
phone = authResult.PhoneInfo.PhoneNumber
|
|
|
)
|
|
|
conn := c.l.svcCtx.DefaultDBConn()
|
|
|
_, users, err = c.l.svcCtx.UserRepository.Find(c.l.ctx, conn, domain.NewQueryOptions().
|
|
|
MustWithKV("phone", phone).
|
|
|
MustWithKV("auditStatus", []int{domain.UserAuditStatusPassed}))
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
response := &domain.LoginInfo{
|
|
|
Phone: phone,
|
|
|
}
|
|
|
if len(users) != 0 {
|
|
|
response.User = users[0]
|
|
|
}
|
|
|
response.Phone = phone
|
|
|
return response, nil
|
|
|
}
|
|
|
|
...
|
...
|
@@ -119,25 +136,17 @@ func (c WxClientLogin) PhonePasswordLogin(phone string, password string) (*domai |
|
|
|
|
|
func (c WxClientLogin) PhoneSmsCodeLogin(phone string, code string) (*domain.LoginInfo, error) {
|
|
|
var (
|
|
|
users []*domain.User
|
|
|
err error
|
|
|
err error
|
|
|
skipCheckSmsCode bool = false
|
|
|
)
|
|
|
if _, err = c.l.svcCtx.SmsService.CheckSmsCode(c.l.ctx, smslib.RequestCheckSmsCode{Phone: phone, Code: code}); err != nil {
|
|
|
return nil, xerr.NewErrMsgErr(err.Error(), err)
|
|
|
if c.l.svcCtx.Config.DebugSmsCode != "" && c.l.svcCtx.Config.DebugSmsCode == code {
|
|
|
skipCheckSmsCode = true
|
|
|
}
|
|
|
conn := c.l.svcCtx.DefaultDBConn()
|
|
|
_, users, err = c.l.svcCtx.UserRepository.Find(c.l.ctx, conn, domain.NewQueryOptions().
|
|
|
MustWithKV("phone", phone).
|
|
|
MustWithKV("auditStatus", []int{domain.UserAuditStatusPassed}).
|
|
|
WithFindOnly())
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
if _, err = c.l.svcCtx.SmsService.CheckSmsCode(c.l.ctx, smslib.RequestCheckSmsCode{Phone: phone, Code: code}); err != nil && !skipCheckSmsCode {
|
|
|
return nil, xerr.NewErrMsgErr(err.Error(), err)
|
|
|
}
|
|
|
response := &domain.LoginInfo{
|
|
|
Phone: phone,
|
|
|
}
|
|
|
if len(users) != 0 {
|
|
|
response.User = users[0]
|
|
|
}
|
|
|
return response, nil
|
|
|
} |
...
|
...
|
|