feat: 修改密码增加密码登录选项

This commit is contained in:
zzw
2026-10-23 13:55:23 +08:00
parent f5212273c2
commit 6b26a33cf4
10 changed files with 105 additions and 90 deletions
@@ -122,14 +122,14 @@ func (l *StoreUserListLogic) StoreUserList(in *analysis.StoreUserListReq) (*anal
StoreId: row.StoreId,
Name: profile.Name,
Nickname: profile.Name,
Avatar: imageURL(profile.Avatar),
Avatar: profile.Avatar,
Mobile: decryptMobile(profile.Mobile),
Gender: int(profile.Gender),
Birthday: profile.Birthday,
Age: utils.CalcAgeStr(profile.Birthday),
}
if logRow, ok := logs[row.UserId]; ok {
item.Cover = imageURL(logRow.Cover)
item.Cover = logRow.Cover
item.DetectionNumber = logRow.DetectionNumber
item.DetectionTime = logRow.DetectionTime.Format(detectionTimeLayout)
}
@@ -45,7 +45,6 @@ func loadPassedStore(ctx context.Context, storeId int64) (*storepb.InfoResponse,
}
func skinStore(info *storepb.InfoResponse) dao.StoreSkinInfo {
img, _ := utils.BuildImageURL(info.Images).(string)
return dao.StoreSkinInfo{
Id: info.Id,
StoreName: info.Name,
@@ -53,7 +52,7 @@ func skinStore(info *storepb.InfoResponse) dao.StoreSkinInfo {
StoreUserMobile: utils.DecryptPhoneReplace(info.UserMobile),
District: info.DistrictIds,
Address: regionAddress(info),
StoreImg: img,
StoreImg: info.Images,
StoreContact: info.Contact,
StoreContent: info.Content,
OpenTime: info.OpenTime,
@@ -75,14 +74,6 @@ func regionAddress(info *storepb.InfoResponse) string {
return strings.Join(parts, "") + info.Address
}
func imageURL(path string) string {
if path == utils.StringEmpty {
return utils.StringEmpty
}
url, _ := utils.BuildImageURL(path).(string)
return url
}
func dateOnly(value string) string {
if len(value) >= len("2006-01-02") {
return value[:len("2006-01-02")]
@@ -8,7 +8,7 @@ import (
"lone-services/services/equipment/internal/model"
"strconv"
"lone-services/rpc/equipment/pb"
equipment "lone-services/rpc/equipment/pb"
"lone-services/services/equipment/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
+78 -29
View File
@@ -55,27 +55,6 @@ func (l *PasswordLogic) Password(in *user.PasswordReq) (*user.Response, error) {
l.Errorf("password load user: %v", userErr)
return failResponse(utils.ErrorNotFund), nil
}
if row.Mobile == utils.StringEmpty {
return outResponse(utils.ErrorParams, "用户未绑定手机号,无法修改密码"), nil
}
cred, credErr := findCredential(dao.CredentialTypePassword, row.Mobile, subjectType)
if credErr != nil {
l.Errorf("password find credential: %v", credErr)
return failResponse(utils.Fail), nil
}
if cred == nil || cred.UserId != loginUser.ID {
return outResponse(utils.ErrorNotFund, "密码凭证不存在"), nil
}
if cred.Status == dao.StatusDisabled {
return outResponse(utils.Fail, "登录凭证已禁用"), nil
}
if !utils.EqualsPassword(req.OldPassword, cred.Secret) {
return failResponse(utils.ErrorPwdError), nil
}
if req.OldPassword == req.Password {
return outResponse(utils.ErrorParams, "新密码不能与原密码相同"), nil
}
hashed, hashErr := utils.EncryptPassword(req.Password)
if hashErr != nil {
@@ -83,21 +62,91 @@ func (l *PasswordLogic) Password(in *user.PasswordReq) (*user.Response, error) {
return failResponse(utils.ErrorEncryptAesError), nil
}
cred, credErr := findPasswordCredentialByUser(loginUser.ID, subjectType)
if credErr != nil {
l.Errorf("password find credential: %v", credErr)
return failResponse(utils.Fail), nil
}
credModel := model.UserCredentialModel{}.Init()
_, editErr := credModel.Edit(modelbase.Params{
Eq: map[string]string{"id": strconv.FormatInt(cred.Id, utils.NumberTen)},
}, map[string]interface{}{
"secret": hashed,
"status": dao.StatusEnabled,
})
if editErr != nil {
l.Errorf("password update: %v", editErr)
if cred != nil {
if cred.Status == dao.StatusDisabled {
return outResponse(utils.Fail, "登录凭证已禁用"), nil
}
if utils.EqualsPassword(req.Password, cred.Secret) {
return outResponse(utils.ErrorParams, "新密码不能与原密码相同"), nil
}
_, editErr := credModel.Edit(modelbase.Params{
Eq: map[string]string{"id": strconv.FormatInt(cred.Id, utils.NumberTen)},
}, map[string]interface{}{
"secret": hashed,
})
if editErr != nil {
l.Errorf("password update: %v", editErr)
return failResponse(utils.Fail), nil
}
return okResponse(nil), nil
}
if row.Mobile == utils.StringEmpty {
return outResponse(utils.ErrorParams, "未设置手机号不能修改密码"), nil
}
exist, existErr := findCredential(dao.CredentialTypePassword, row.Mobile, subjectType)
if existErr != nil {
l.Errorf("password find by mobile: %v", existErr)
return failResponse(utils.Fail), nil
}
if exist != nil && exist.UserId != loginUser.ID {
return outResponse(utils.Fail, "登录凭证已被其他用户占用"), nil
}
if exist != nil {
_, editErr := credModel.Edit(modelbase.Params{
Eq: map[string]string{"id": strconv.FormatInt(exist.Id, utils.NumberTen)},
}, map[string]interface{}{
"secret": hashed,
})
if editErr != nil {
l.Errorf("password update occupied: %v", editErr)
return failResponse(utils.Fail), nil
}
return okResponse(nil), nil
}
if createErr := credModel.Create(&dao.UserCredential{
UserId: loginUser.ID,
CredentialType: dao.CredentialTypePassword,
Identifier: row.Mobile,
Secret: hashed,
SubjectType: subjectType,
ExtraJson: dao.ExtraJsonEmpty,
Status: dao.StatusEnabled,
}); createErr != nil {
l.Errorf("password create: %v", createErr)
return failResponse(utils.Fail), nil
}
return okResponse(nil), nil
}
func findPasswordCredentialByUser(userId int64, subjectType string) (*dao.UserCredential, error) {
var cred dao.UserCredential
credModel := model.UserCredentialModel{}.Init()
if err := credModel.GetOne(modelbase.Params{
Eq: map[string]string{
"user_id": strconv.FormatInt(userId, utils.NumberTen),
"credential_type": dao.CredentialTypePassword,
"subject_type": subjectType,
},
}, &cred); err != nil {
return nil, err
}
if cred.Id < utils.NumberOne {
return nil, nil
}
return &cred, nil
}
func (l *PasswordLogic) resolveSubjectType(loginUser utils.UserInfo) (string, error) {
if loginUser.ClientCode != utils.StringEmpty {
client, err := loadEnabledClient(loginUser.ClientCode)
+7 -7
View File
@@ -81,17 +81,17 @@ func (p UpdateValidator) GetMessage() validate.ValidatorMessages {
}
type PasswordValidator struct {
OldPassword string `validate:"required,max=64"`
Password string `validate:"required,min=6,max=64"`
Password string `validate:"required,min=6,max=64"`
ConfirmPassword string `validate:"required,eqfield=Password"`
}
func (p PasswordValidator) GetMessage() validate.ValidatorMessages {
return validate.ValidatorMessages{
"OldPassword.required": "原密码不能为空",
"OldPassword.max": "原密码过长",
"Password.required": "新密码不能为空",
"Password.min": "新密码长度不能小于6位",
"Password.max": "新密码过长",
"Password.required": "新密码不能为空",
"Password.min": "新密码长度不能小于6位",
"Password.max": "新密码过长",
"ConfirmPassword.required": "确认密码不能为空",
"ConfirmPassword.eqfield": "确认密码与新密码不一致",
}
}