作者 zhuangmx

微信绑定openID

@@ -2,10 +2,10 @@ package user @@ -2,10 +2,10 @@ package user
2 2
3 import ( 3 import (
4 "context" 4 "context"
  5 + "fmt"
5 "github.com/silenceper/wechat/v2" 6 "github.com/silenceper/wechat/v2"
6 "github.com/silenceper/wechat/v2/cache" 7 "github.com/silenceper/wechat/v2/cache"
7 miniConfig "github.com/silenceper/wechat/v2/miniprogram/config" 8 miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
8 - "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"  
9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib" 10 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
11 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool" 11 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool"
@@ -53,11 +53,6 @@ func (l *MiniUserLoginLogic) MiniUserLogin(req *types.MiniUserLoginRequest) (res @@ -53,11 +53,6 @@ func (l *MiniUserLoginLogic) MiniUserLogin(req *types.MiniUserLoginRequest) (res
53 53
54 var users []*domain.User 54 var users []*domain.User
55 conn := l.svcCtx.DefaultDBConn() 55 conn := l.svcCtx.DefaultDBConn()
56 - //保存微信用户信息  
57 - err = saveUserWechat(l.svcCtx, l.ctx, conn, loginInfo)  
58 - if err != nil {  
59 - return nil, err  
60 - }  
61 _, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions(). 56 _, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
62 MustWithKV("phone", loginInfo.Phone). 57 MustWithKV("phone", loginInfo.Phone).
63 MustWithKV("auditStatus", []int{domain.UserAuditStatusWait, domain.UserAuditStatusPassed}). 58 MustWithKV("auditStatus", []int{domain.UserAuditStatusWait, domain.UserAuditStatusPassed}).
@@ -92,23 +87,6 @@ func (l *MiniUserLoginLogic) MiniUserLogin(req *types.MiniUserLoginRequest) (res @@ -92,23 +87,6 @@ func (l *MiniUserLoginLogic) MiniUserLogin(req *types.MiniUserLoginRequest) (res
92 return 87 return
93 } 88 }
94 89
95 -// saveUserWechat 保存微信授权信息  
96 -func saveUserWechat(svcCtx *svc.ServiceContext, ctx context.Context, conn transaction.Conn, loginInfo *domain.LoginInfo) error {  
97 - if loginInfo.Phone != "" && loginInfo.OpenId != "" {  
98 - _, err := svcCtx.UserWechatRepository.FindOneByPhone(ctx, conn, loginInfo.Phone)  
99 - if err != nil { // 未存储  
100 - _, err = svcCtx.UserWechatRepository.Insert(ctx, conn, &domain.UserWechat{  
101 - OpenId: loginInfo.OpenId,  
102 - Phone: loginInfo.Phone,  
103 - })  
104 - if err != nil {  
105 - return err  
106 - }  
107 - }  
108 - }  
109 - return nil  
110 -}  
111 -  
112 func generateToken(svcCtx *svc.ServiceContext, user *domain.User) (token string, err error) { 90 func generateToken(svcCtx *svc.ServiceContext, user *domain.User) (token string, err error) {
113 var userJwtToken = tool.UserToken{} 91 var userJwtToken = tool.UserToken{}
114 if user != nil { 92 if user != nil {
@@ -138,25 +116,14 @@ func (c WxClientLogin) WechatPhoneLogin(r domain.WechatLoginRequest) (*domain.Lo @@ -138,25 +116,14 @@ func (c WxClientLogin) WechatPhoneLogin(r domain.WechatLoginRequest) (*domain.Lo
138 AppSecret: c.svcCtx.Config.Wechat.AppSecret, 116 AppSecret: c.svcCtx.Config.Wechat.AppSecret,
139 Cache: cache.NewMemory(), 117 Cache: cache.NewMemory(),
140 }) 118 })
141 - result, err := miniprogram.GetAuth().Code2Session(code)  
142 - if err != nil {  
143 - return nil, xerr.NewErrMsgErr("发起授权请求失败", err)  
144 - }  
145 - plainData, err := miniprogram.GetEncryptor().Decrypt(result.SessionKey, r.EncryptedData, r.IV)  
146 - if err != nil {  
147 - return nil, xerr.NewErrMsgErr("获取授权用户失败", err)  
148 - }  
149 - //增加微信用户openId  
150 - response.OpenId = plainData.OpenID  
151 - response.Phone = plainData.PhoneNumber  
152 - /*authResult, err := miniprogram.GetAuth().GetPhoneNumber(code) 119 + authResult, err := miniprogram.GetAuth().GetPhoneNumber(code)
153 if err != nil || authResult.ErrCode != 0 || authResult.PhoneInfo.PhoneNumber == "" { 120 if err != nil || authResult.ErrCode != 0 || authResult.PhoneInfo.PhoneNumber == "" {
154 return response, xerr.NewCodeErrMsg(xerr.ErrWxMiniAuthFailError, nil, fmt.Sprintf("发起授权请求失败1 err : %v , code : %s , authResult : %+v", err, code, authResult)) 121 return response, xerr.NewCodeErrMsg(xerr.ErrWxMiniAuthFailError, nil, fmt.Sprintf("发起授权请求失败1 err : %v , code : %s , authResult : %+v", err, code, authResult))
155 } 122 }
156 var ( 123 var (
157 phone = authResult.PhoneInfo.PhoneNumber 124 phone = authResult.PhoneInfo.PhoneNumber
158 ) 125 )
159 - response.Phone = phone*/ 126 + response.Phone = phone
160 return response, nil 127 return response, nil
161 } 128 }
162 129
@@ -164,6 +131,23 @@ func (c WxClientLogin) WechatLogin(r domain.WechatLoginRequest) (*domain.LoginIn @@ -164,6 +131,23 @@ func (c WxClientLogin) WechatLogin(r domain.WechatLoginRequest) (*domain.LoginIn
164 return nil, nil 131 return nil, nil
165 } 132 }
166 133
  134 +func (c WxClientLogin) GetOpenId(r domain.WechatLoginRequest) (string, error) {
  135 + miniprogram := wechat.NewWechat().GetMiniProgram(&miniConfig.Config{
  136 + AppID: c.svcCtx.Config.Wechat.AppID,
  137 + AppSecret: c.svcCtx.Config.Wechat.AppSecret,
  138 + Cache: cache.NewMemory(),
  139 + })
  140 + result, err := miniprogram.GetAuth().Code2Session(r.Code)
  141 + if err != nil {
  142 + return "", xerr.NewErrMsgErr("发起授权请求失败", err)
  143 + }
  144 + plainData, err := miniprogram.GetEncryptor().Decrypt(result.SessionKey, r.EncryptedData, r.IV)
  145 + if err != nil {
  146 + return "", xerr.NewErrMsgErr("获取授权用户失败", err)
  147 + }
  148 + return plainData.OpenID, nil
  149 +}
  150 +
167 func (c WxClientLogin) PhonePasswordLogin(phone string, password string) (*domain.LoginInfo, error) { 151 func (c WxClientLogin) PhonePasswordLogin(phone string, password string) (*domain.LoginInfo, error) {
168 panic("implement me") 152 panic("implement me")
169 } 153 }
@@ -3,6 +3,7 @@ package user @@ -3,6 +3,7 @@ package user
3 import ( 3 import (
4 "context" 4 "context"
5 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" 5 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
7 8
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
@@ -27,7 +28,7 @@ func NewMiniWechatBindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Mi @@ -27,7 +28,7 @@ func NewMiniWechatBindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Mi
27 28
28 func (l *MiniWechatBindLogic) MiniWechatBind(req *types.MiniWechatBindRequest) (resp *types.MiniWechatBindResponse, err error) { 29 func (l *MiniWechatBindLogic) MiniWechatBind(req *types.MiniWechatBindRequest) (resp *types.MiniWechatBindResponse, err error) {
29 var loginCreator domain.LoginCreator = WxClientLogin{svcCtx: l.svcCtx, ctx: l.ctx} 30 var loginCreator domain.LoginCreator = WxClientLogin{svcCtx: l.svcCtx, ctx: l.ctx}
30 - loginInfo, err := loginCreator.WechatPhoneLogin(domain.WechatLoginRequest{ 31 + openId, err := loginCreator.GetOpenId(domain.WechatLoginRequest{
31 Code: req.WechatAuthCode, 32 Code: req.WechatAuthCode,
32 EncryptedData: req.WechatEncryptedData, 33 EncryptedData: req.WechatEncryptedData,
33 IV: req.WechatIV, 34 IV: req.WechatIV,
@@ -35,14 +36,30 @@ func (l *MiniWechatBindLogic) MiniWechatBind(req *types.MiniWechatBindRequest) ( @@ -35,14 +36,30 @@ func (l *MiniWechatBindLogic) MiniWechatBind(req *types.MiniWechatBindRequest) (
35 if err != nil { 36 if err != nil {
36 return nil, xerr.NewErrMsgErr("授权失败", err) 37 return nil, xerr.NewErrMsgErr("授权失败", err)
37 } 38 }
  39 + var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
  40 + userId := userToken.UserId
38 var conn = l.svcCtx.DefaultDBConn() 41 var conn = l.svcCtx.DefaultDBConn()
39 - err = saveUserWechat(l.svcCtx, l.ctx, conn, loginInfo) 42 + //获取用户信息
  43 + userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userId)
  44 + if err != nil {
  45 + return nil, xerr.NewErrMsgErr("获取用户信息失败", err)
  46 + }
  47 + _, err = l.svcCtx.UserWechatRepository.FindOneByPhone(l.ctx, conn, userInfo.Phone)
  48 + if err != nil { // 未存储
  49 + _, err = l.svcCtx.UserWechatRepository.Insert(l.ctx, conn, &domain.UserWechat{
  50 + Phone: userInfo.Phone,
  51 + OpenId: openId,
  52 + })
  53 + if err != nil {
  54 + return nil, xerr.NewErrMsgErr("授权失败", err)
  55 + }
  56 + }
40 if err != nil { 57 if err != nil {
41 return nil, xerr.NewErrMsg("保存用户信息失败") 58 return nil, xerr.NewErrMsg("保存用户信息失败")
42 } 59 }
43 return &types.MiniWechatBindResponse{ 60 return &types.MiniWechatBindResponse{
44 Bind: true, 61 Bind: true,
45 - Phone: loginInfo.Phone,  
46 - OpenId: loginInfo.OpenId, 62 + Phone: userInfo.Phone,
  63 + OpenId: openId,
47 }, nil 64 }, nil
48 } 65 }
@@ -9,7 +9,7 @@ import ( @@ -9,7 +9,7 @@ import (
9 9
10 type UserWechat struct { 10 type UserWechat struct {
11 Id int64 `json:"id"` // 唯一标识 11 Id int64 `json:"id"` // 唯一标识
12 - OpenId string `json:"openId" gorm:"unique"` // 微信openId 12 + OpenId string `json:"openId"` // 微信openId
13 Phone string `json:"phone" gorm:"unique"` // 微信手机号 13 Phone string `json:"phone" gorm:"unique"` // 微信手机号
14 CreatedAt int64 `json:",omitempty"` // 创建时间 14 CreatedAt int64 `json:",omitempty"` // 创建时间
15 UpdatedAt int64 `json:",omitempty"` // 更新时间 15 UpdatedAt int64 `json:",omitempty"` // 更新时间
@@ -151,6 +151,7 @@ type ( @@ -151,6 +151,7 @@ type (
151 PhonePasswordLogin(phone string, password string) (*LoginInfo, error) 151 PhonePasswordLogin(phone string, password string) (*LoginInfo, error)
152 PhoneSmsCodeLogin(phone string, code string) (*LoginInfo, error) 152 PhoneSmsCodeLogin(phone string, code string) (*LoginInfo, error)
153 WechatPhoneLogin(r WechatLoginRequest) (*LoginInfo, error) 153 WechatPhoneLogin(r WechatLoginRequest) (*LoginInfo, error)
  154 + GetOpenId(r WechatLoginRequest) (string, error)
154 } 155 }
155 WechatLoginRequest struct { 156 WechatLoginRequest struct {
156 Code string // 授权码 157 Code string // 授权码
@@ -161,6 +162,5 @@ type ( @@ -161,6 +162,5 @@ type (
161 User *User 162 User *User
162 Phone string 163 Phone string
163 Message string 164 Message string
164 - OpenId string  
165 } 165 }
166 ) 166 )