mini_wechat_bind_logic.go
2.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
package user
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"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 MiniWechatBindLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniWechatBindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniWechatBindLogic {
return &MiniWechatBindLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniWechatBindLogic) MiniWechatBind(req *types.MiniWechatBindRequest) (resp *types.MiniWechatBindResponse, err error) {
var loginCreator domain.LoginCreator = WxClientLogin{svcCtx: l.svcCtx, ctx: l.ctx}
openId, err := loginCreator.GetOpenId(domain.WechatLoginRequest{
Code: req.WechatAuthCode,
EncryptedData: req.WechatEncryptedData,
IV: req.WechatIV,
})
if err != nil {
return nil, xerr.NewErrMsgErr("授权失败", err)
}
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
userId := userToken.UserId
var conn = l.svcCtx.DefaultDBConn()
//获取用户信息
userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userId)
if err != nil {
return nil, xerr.NewErrMsgErr("获取用户信息失败", err)
}
_, err = l.svcCtx.UserWechatRepository.FindOneByPhone(l.ctx, conn, userInfo.Phone)
if err != nil { // 未存储
_, err = l.svcCtx.UserWechatRepository.Insert(l.ctx, conn, &domain.UserWechat{
Phone: userInfo.Phone,
OpenId: openId,
})
if err != nil {
return nil, xerr.NewErrMsgErr("授权失败", err)
}
}
if err != nil {
return nil, xerr.NewErrMsg("保存用户信息失败")
}
return &types.MiniWechatBindResponse{
Bind: true,
Phone: userInfo.Phone,
OpenId: openId,
}, nil
}