mini_user_login_logic.go
6.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package user
import (
"context"
"github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/cache"
miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniUserLoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserLoginLogic {
return &MiniUserLoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniUserLoginLogic) MiniUserLogin(req *types.MiniUserLoginRequest) (resp *types.MiniUserLoginResponse, err error) {
var (
loginInfo *domain.LoginInfo
token string
loginCreator domain.LoginCreator = WxClientLogin{svcCtx: l.svcCtx, ctx: l.ctx}
)
switch req.LoginType {
case domain.LoginTypeWechatLogin:
loginInfo, err = loginCreator.WechatLogin(domain.WechatLoginRequest{Code: req.WechatAuthCode, EncryptedData: req.WechatEncryptedData, IV: req.WechatIV})
case domain.LoginTypeWechatPhoneLogin:
loginInfo, err = loginCreator.WechatPhoneLogin(domain.WechatLoginRequest{Code: req.WechatAuthCode, EncryptedData: req.WechatEncryptedData, IV: req.WechatIV})
case domain.LoginTypePhoneSmsCodeLogin:
loginInfo, err = loginCreator.PhoneSmsCodeLogin(req.Phone, req.SmsCode)
case domain.LoginTypePhonePasswordLogin:
loginInfo, err = loginCreator.PhonePasswordLogin(req.Phone, req.Password)
}
if err != nil {
return
}
var users []*domain.User
conn := l.svcCtx.DefaultDBConn()
//保存微信用户信息
err = saveUserWechat(l.svcCtx, l.ctx, conn, loginInfo)
if err != nil {
return nil, err
}
_, 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
}
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)
}
resp = &types.MiniUserLoginResponse{
Token: token,
Phone: loginInfo.Phone,
Success: true,
}
if loginInfo.User == nil {
resp.Token = ""
resp.Success = false
}
return
}
// saveUserWechat 保存微信授权信息
func saveUserWechat(svcCtx *svc.ServiceContext, ctx context.Context, conn transaction.Conn, loginInfo *domain.LoginInfo) error {
if loginInfo.Phone != "" && loginInfo.OpenId != "" {
_, err := svcCtx.UserWechatRepository.FindOneByPhone(ctx, conn, loginInfo.Phone)
if err != nil { // 未存储
_, err = svcCtx.UserWechatRepository.Insert(ctx, conn, &domain.UserWechat{
OpenId: loginInfo.OpenId,
Phone: loginInfo.Phone,
})
if err != nil {
return err
}
}
}
return nil
}
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 {
svcCtx *svc.ServiceContext
ctx context.Context
}
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.svcCtx.Config.Wechat.AppID,
AppSecret: c.svcCtx.Config.Wechat.AppSecret,
Cache: cache.NewMemory(),
})
result, err := miniprogram.GetAuth().Code2Session(code)
if err != nil {
return nil, xerr.NewErrMsgErr("发起授权请求失败", err)
}
plainData, err := miniprogram.GetEncryptor().Decrypt(result.SessionKey, r.EncryptedData, r.IV)
if err != nil {
return nil, xerr.NewErrMsgErr("获取授权用户失败", err)
}
//增加微信用户openId
response.OpenId = plainData.OpenID
response.Phone = plainData.PhoneNumber
/*authResult, err := miniprogram.GetAuth().GetPhoneNumber(code)
if err != nil || authResult.ErrCode != 0 || authResult.PhoneInfo.PhoneNumber == "" {
return response, xerr.NewCodeErrMsg(xerr.ErrWxMiniAuthFailError, nil, fmt.Sprintf("发起授权请求失败1 err : %v , code : %s , authResult : %+v", err, code, authResult))
}
var (
phone = authResult.PhoneInfo.PhoneNumber
)
response.Phone = phone*/
return response, nil
}
func (c WxClientLogin) WechatLogin(r domain.WechatLoginRequest) (*domain.LoginInfo, error) {
return nil, nil
}
func (c WxClientLogin) PhonePasswordLogin(phone string, password string) (*domain.LoginInfo, error) {
panic("implement me")
}
func (c WxClientLogin) PhoneSmsCodeLogin(phone string, code string) (*domain.LoginInfo, error) {
var (
err error
skipCheckSmsCode bool = false
)
if c.svcCtx.Config.DebugSmsCode != "" && c.svcCtx.Config.DebugSmsCode == code {
skipCheckSmsCode = true
}
if _, err = c.svcCtx.SmsService.CheckSmsCode(c.ctx, smslib.RequestCheckSmsCode{Phone: phone, Code: code}); err != nil && !skipCheckSmsCode {
return nil, xerr.NewErrMsgErr(err.Error(), err)
}
response := &domain.LoginInfo{
Phone: phone,
}
return response, nil
}