Skip to content

Commit

Permalink
fix: 优化第三方登录写法 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx authored Apr 8, 2024
1 parent 131b092 commit c1905a9
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 47 deletions.
39 changes: 31 additions & 8 deletions biz/application/service/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/CloudStriver/cloudmind-sts/biz/infrastructure/config"
"github.com/CloudStriver/cloudmind-sts/biz/infrastructure/consts"
"github.com/CloudStriver/cloudmind-sts/biz/infrastructure/convertor"
usermapper "github.com/CloudStriver/cloudmind-sts/biz/infrastructure/mapper/user"
"github.com/CloudStriver/cloudmind-sts/biz/infrastructure/util/email"
gensts "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/sts"
Expand Down Expand Up @@ -34,17 +33,29 @@ type AuthServiceImpl struct {
UserMongoMapper usermapper.IUserMongoMapper
}

// 添加登录方式
func (s *AuthServiceImpl) AppendAuth(ctx context.Context, req *gensts.AppendAuthReq) (resp *gensts.AppendAuthResp, err error) {
resp = new(gensts.AppendAuthResp)
if err = s.UserMongoMapper.AppendAuth(ctx, req.UserId, convertor.AuthToAuthMapper(req.AuthInfo)); err != nil {
if err = s.UserMongoMapper.AppendAuth(ctx, req.UserId, &usermapper.Auth{
Type: req.AuthType,
AppId: req.AppId,
UnionId: req.UnionId,
PlatformId: req.PlatFormId,
}); err != nil {
return resp, err
}
return resp, nil
}

// 通过某个登录方式登录
func (s *AuthServiceImpl) Login(ctx context.Context, req *gensts.LoginReq) (resp *gensts.LoginResp, err error) {
resp = new(gensts.LoginResp)
user, err := s.UserMongoMapper.FindOneByAuth(ctx, convertor.AuthToAuthMapper(req.Auth))
user, err := s.UserMongoMapper.FindOneByAuth(ctx, &usermapper.Auth{
Type: req.AuthType,
AppId: req.AppId,
UnionId: req.UnionId,
PlatformId: req.PlatFormId,
})
if errors.Is(err, consts.ErrNotFound) {
return resp, nil
}
Expand Down Expand Up @@ -76,6 +87,7 @@ func (s *AuthServiceImpl) CheckEmail(ctx context.Context, req *gensts.CheckEmail
return resp, nil
}

// 重置密码
func (s *AuthServiceImpl) SetPassword(ctx context.Context, req *gensts.SetPasswordReq) (resp *gensts.SetPasswordResp, err error) {
resp = new(gensts.SetPasswordResp)
var user *usermapper.User
Expand All @@ -88,8 +100,7 @@ func (s *AuthServiceImpl) SetPassword(ctx context.Context, req *gensts.SetPasswo
if value != "true" {
return resp, consts.ErrNotPassEmailCheck
}

user, err = s.UserMongoMapper.FindOneByAuth(ctx, &usermapper.Auth{Type: int32(gensts.AuthType_email), AppId: o.EmailOptions.Email})
user, err = s.UserMongoMapper.FindOneByAuth(ctx, &usermapper.Auth{Type: int64(consts.EmailAuthType), AppId: o.EmailOptions.Email})
if err != nil {
return resp, err
}
Expand Down Expand Up @@ -118,6 +129,7 @@ func (s *AuthServiceImpl) SetPassword(ctx context.Context, req *gensts.SetPasswo
return resp, nil
}

// 发送邮件
func (s *AuthServiceImpl) SendEmail(ctx context.Context, req *gensts.SendEmailReq) (resp *gensts.SendEmailResp, err error) {
resp = new(gensts.SendEmailResp)
code, err := email.SendEmail(ctx, s.Config.EmailConf, req.Email, req.Subject)
Expand All @@ -130,9 +142,15 @@ func (s *AuthServiceImpl) SendEmail(ctx context.Context, req *gensts.SendEmailRe
return resp, nil
}

// 注册
func (s *AuthServiceImpl) CreateAuth(ctx context.Context, req *gensts.CreateAuthReq) (resp *gensts.CreateAuthResp, err error) {
resp = new(gensts.CreateAuthResp)
auth := convertor.AuthToAuthMapper(req.AuthInfo)
auth := &usermapper.Auth{
Type: req.AuthType,
AppId: req.AppId,
UnionId: req.UnionId,
PlatformId: req.PlatFormId,
}
_, err = s.UserMongoMapper.FindOneByAuth(ctx, auth)
switch {
case err == nil:
Expand All @@ -142,9 +160,14 @@ func (s *AuthServiceImpl) CreateAuth(ctx context.Context, req *gensts.CreateAuth
default:
return resp, err
}

password := req.Password
if password == "" {
password = consts.DefaultPassword
}
resp.UserId, err = s.UserMongoMapper.Insert(ctx, &usermapper.User{
PassWord: req.UserInfo.GetPassword(),
Role: int32(req.UserInfo.Role),
PassWord: password,
Role: req.Role,
Auths: []*usermapper.Auth{auth},
})
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions biz/application/service/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ func (s *FilterService) ReplaceContent(ctx context.Context, req *gensts.ReplaceC
}

func (s *FilterService) FindAllContent(ctx context.Context, req *gensts.FindAllContentReq) (resp *gensts.FindAllContentResp, err error) {
keywords := make([]*gensts.Keywords, len(req.Contents))
keywords := make([]string, 0)
wg := sync.WaitGroup{}
wg.Add(len(req.Contents))
for i, content := range req.Contents {
go func(i int, content string) {
defer wg.Done()
keywords[i] = &gensts.Keywords{
Keywords: lo.Map[*ToolGood.IllegalWordsSearchResult, string](s.Filter.FindAll(content), func(item *ToolGood.IllegalWordsSearchResult, index int) string {
return item.Keyword
}),
}
keywords = append(keywords, lo.Map[*ToolGood.IllegalWordsSearchResult, string](s.Filter.FindAll(content), func(item *ToolGood.IllegalWordsSearchResult, index int) string {
return item.Keyword
})...)
}(i, content)
}
wg.Wait()
keywords = lo.Uniq(keywords)

return &gensts.FindAllContentResp{
Keywords: keywords,
}, nil
Expand Down
17 changes: 11 additions & 6 deletions biz/infrastructure/consts/field.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package consts

const (
EmailCode = "EmailCode"
Name = "name"
ID = "_id"
CaptchaKey = "CaptchaKey"
PassCheckEmail = "PassCheckEmail"
ReplaceChar = '*'
EmailCode = "EmailCode"
ID = "_id"
PassCheckEmail = "PassCheckEmail"
Type = "type"
AppId = "appId"
UnionId = "unionId"
PlatformId = "platformId"
Auths = "auths"
ReplaceChar = '*'
EmailAuthType = 1
DefaultPassword = "123456789"
)
12 changes: 0 additions & 12 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,2 @@
package convertor

import (
authmapper "github.com/CloudStriver/cloudmind-sts/biz/infrastructure/mapper/user"
gensts "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/sts"
)

func AuthToAuthMapper(u *gensts.AuthInfo) *authmapper.Auth {
return &authmapper.Auth{
Type: int32(u.AuthType),
AppId: u.AppId,
UnionId: u.UnionId,
}
}
26 changes: 14 additions & 12 deletions biz/infrastructure/mapper/user/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ type (
AppendAuth(ctx context.Context, id string, auth *Auth) error // 追加授权信息
}
Auth struct {
Type int32 `bson:"type" json:"type"`
AppId string `bson:"appId" json:"appId"`
UnionId string `bson:"unionId" json:"unionId"`
Type int64 `bson:"type" json:"type"`
AppId string `bson:"appId" json:"appId"`
UnionId string `bson:"unionId" json:"unionId"`
PlatformId string `bson:"platformId" json:"platformId"`
}
User struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
PassWord string `bson:"passWord,omitempty" json:"passWord,omitempty"`
Role int32 `bson:"role,omitempty" json:"role,omitempty"`
Role int64 `bson:"role,omitempty" json:"role,omitempty"`
Auths []*Auth `bson:"auths,omitempty" json:"auths,omitempty"`
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
Expand All @@ -54,7 +55,7 @@ func (m *MongoMapper) AppendAuth(ctx context.Context, id string, auth *Auth) err
return err
}
key := PrefixUserCacheKey + id
_, err = m.conn.UpdateOne(ctx, key, bson.M{"_id": ID}, bson.M{"$push": bson.M{"auths": bson.M{"$each": []*Auth{auth}}}})
_, err = m.conn.UpdateOne(ctx, key, bson.M{consts.ID: ID}, bson.M{"$push": bson.M{consts.Auths: bson.M{"$each": []*Auth{auth}}}})
if err != nil {
return err
}
Expand Down Expand Up @@ -84,7 +85,7 @@ func (m *MongoMapper) UpdateById(ctx context.Context, auth *Auth, id string) (*m
Filters: []interface{}{bson.M{"element.type": auth.Type}},
})

res, err := m.conn.UpdateOne(ctx, key, bson.M{"_id": ID}, update, option)
res, err := m.conn.UpdateOne(ctx, key, bson.M{consts.ID: ID}, update, option)
return res, err
}

Expand All @@ -93,9 +94,10 @@ func (m *MongoMapper) FindOneByAuth(ctx context.Context, auth *Auth) (*User, err
filter := bson.M{
"auths": bson.M{
"$elemMatch": bson.M{
"type": auth.Type,
"appId": auth.AppId,
"unionId": auth.UnionId,
consts.Type: auth.Type,
consts.AppId: auth.AppId,
consts.UnionId: auth.UnionId,
consts.PlatformId: auth.PlatformId,
},
},
}
Expand Down Expand Up @@ -133,7 +135,7 @@ func (m *MongoMapper) FindOne(ctx context.Context, id string) (*User, error) {
}
var data User
key := PrefixUserCacheKey + id
err = m.conn.FindOne(ctx, key, &data, bson.M{"_id": oid})
err = m.conn.FindOne(ctx, key, &data, bson.M{consts.ID: oid})
switch {
case err == nil:
return &data, nil
Expand All @@ -147,7 +149,7 @@ func (m *MongoMapper) FindOne(ctx context.Context, id string) (*User, error) {
func (m *MongoMapper) Update(ctx context.Context, data *User) (*mongo.UpdateResult, error) {
data.UpdateAt = time.Now()
key := PrefixUserCacheKey + data.ID.Hex()
res, err := m.conn.UpdateOne(ctx, key, bson.M{"_id": data.ID}, bson.M{"$set": data})
res, err := m.conn.UpdateOne(ctx, key, bson.M{consts.ID: data.ID}, bson.M{"$set": data})
return res, err
}

Expand All @@ -157,6 +159,6 @@ func (m *MongoMapper) Delete(ctx context.Context, id string) (int64, error) {
return 0, err
}
key := PrefixUserCacheKey + id
res, err := m.conn.DeleteOne(ctx, key, bson.M{"_id": oid})
res, err := m.conn.DeleteOne(ctx, key, bson.M{consts.ID: oid})
return res, err
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/CloudStriver/ToolGood v0.0.0-20240325020152-92c577d6e96d
github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240325034001-88b54ac0de7b
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240408085139-5bcd9d9c4a21
github.com/cloudwego/kitex v0.8.0
github.com/google/wire v0.5.0
github.com/kitex-contrib/obs-opentelemetry v0.2.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/CloudStriver/ToolGood v0.0.0-20240325020152-92c577d6e96d h1:UQZIfnbd6
github.com/CloudStriver/ToolGood v0.0.0-20240325020152-92c577d6e96d/go.mod h1:ajuYgkvZmBaZx+gQF5rhCG3nS4ARFWN6gPKLV55k0j0=
github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af h1:tl3MgdfL4fOkZxFYqqLXYr6dEk5TvT/hGe06uoB+spY=
github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af/go.mod h1:RMjN80WnoDiqHZIsv27u9BxJ9axldr+elFRHgSjhXnY=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240325034001-88b54ac0de7b h1:6zvFftlk5rgfupBg+Ye4w3dScuA6ymUQJICHxI2q30g=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240325034001-88b54ac0de7b/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240408085139-5bcd9d9c4a21 h1:msEIkCNxMJvdIEdIEboQP1TvUh8JD6Dq3BMpquE6YPc=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240408085139-5bcd9d9c4a21/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
Expand Down

0 comments on commit c1905a9

Please sign in to comment.