feat: passwork login

This commit is contained in:
zzw
2026-09-03 10:33:48 +08:00
parent 533842e140
commit 493416551b
5 changed files with 77 additions and 1 deletions
@@ -68,6 +68,21 @@ func findCredential(credentialType, identifier string) (*dao.UserCredential, err
return &cred, nil
}
func userHasEnabledClient(userId int64, clientCode string) (bool, error) {
var row dao.UserClient
clientModel := model.UserClientModel{}.Init()
if err := clientModel.GetOne(modelbase.Params{
Eq: map[string]string{
"user_id": strconv.FormatInt(userId, utils.NumberTen),
"client_code": clientCode,
"status": strconv.Itoa(int(dao.StatusEnabled)),
},
}, &row); err != nil {
return false, err
}
return row.Id >= utils.NumberOne, nil
}
func loadEnabledUser(userId int64) (*dao.UserRow, error) {
var row dao.UserRow
userModel := model.UserModel{}.Init()
+58 -1
View File
@@ -39,7 +39,7 @@ func (l *LoginLogic) Login(in *user.LoginReq) (*user.Response, error) {
case dao.GrantTypeOpenid:
return l.loginByOpenid(req)
case dao.GrantTypePassword:
return outResponse(utils.ErrorParams, "密码登录暂未开放"), nil
return l.loginByPassword(req)
case dao.GrantTypeSms:
return outResponse(utils.ErrorParams, "验证码登录暂未开放"), nil
default:
@@ -91,3 +91,60 @@ func (l *LoginLogic) loginByOpenid(req validator.LoginValidator) (*user.Response
}
return okResponse(ret), nil
}
func (l *LoginLogic) loginByPassword(req validator.LoginValidator) (*user.Response, error) {
client, err := loadEnabledClient(req.ClientCode)
if err != nil {
l.Errorf("login load client: %v", err)
return failResponse(utils.Fail), nil
}
if !clientAllowsGrant(client.AllowedGrants, dao.GrantTypePassword) {
return outResponse(utils.ErrorParams, "该端不支持密码登录"), nil
}
if len(req.Mobile) != utils.NumberEleven {
return failResponse(utils.ErrorMobileError), nil
}
encryptMobile, encErr := utils.EncryptPhone(req.Mobile)
if encErr != nil {
l.Errorf("login encrypt mobile: %v", encErr)
return failResponse(utils.ErrorEncryptAesError), nil
}
cred, credErr := findCredential(dao.CredentialTypePassword, encryptMobile)
if credErr != nil {
l.Errorf("login find credential: %v", credErr)
return failResponse(utils.Fail), nil
}
if cred == nil {
return failResponse(utils.ErrorNotFund), nil
}
if cred.Status == dao.StatusDisabled {
return outResponse(utils.Fail, "登录凭证已禁用"), nil
}
if !utils.EqualsPassword(req.Password, cred.Secret) {
return failResponse(utils.ErrorPwdError), nil
}
hasClient, clientErr := userHasEnabledClient(cred.UserId, req.ClientCode)
if clientErr != nil {
l.Errorf("login check user client: %v", clientErr)
return failResponse(utils.Fail), nil
}
if !hasClient {
return outResponse(utils.ErrorParams, "该端未开通或已禁用"), nil
}
row, userErr := loadEnabledUser(cred.UserId)
if userErr != nil {
l.Errorf("login load user: %v", userErr)
return failResponse(utils.ErrorNotFund), nil
}
ret, tokenErr := issueLoginToken(l.ctx, l.svcCtx.JWT, l.svcCtx.SaleSvcName, row, client)
if tokenErr != nil {
l.Errorf("login issue token: %v", tokenErr)
return failResponse(utils.Fail), nil
}
return okResponse(ret), nil
}