Parcourir la source

fix:微信关注

zqbao il y a 9 mois
Parent
commit
15c29e3219
3 fichiers modifiés avec 11 ajouts et 4 suppressions
  1. 6 1
      controllers/wechat.go
  2. 1 0
      models/db.go
  3. 4 3
      services/wechat/wechat.go

+ 6 - 1
controllers/wechat.go

@@ -276,12 +276,17 @@ func (this *WechatController) Subscribe() {
 		br.Msg = "授权码不存在"
 		return
 	}
-	info, err := wechat.GetOpenId(req.Code)
+	info, err := wechat.GetWxUserInfo(req.Code)
 	if err != nil {
 		br.Msg = "获取失败"
 		br.ErrMsg = "获取失败,Err:" + err.Error()
 		return
 	}
+	if info.ErrCode != 0 {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取失败,Err:" + info.ErrMsg
+		return
+	}
 	u := &models.UserRecord{
 		OpenId:  info.OpenId,
 		UnionId: info.UnionId,

+ 1 - 0
models/db.go

@@ -41,6 +41,7 @@ func init() {
 		new(WxToken),
 		new(Banner),
 		new(WxSession),
+		new(UserRecord),
 		new(UserTemplateRecord),
 		new(UserReadRecord),
 		new(UserReadPermission1),

+ 4 - 3
services/wechat/wechat.go

@@ -102,17 +102,18 @@ func GetJsConfig(signUrl string) (jsConf *js.Config, err error) {
 	return
 }
 
-type WxUserToken struct {
+type WxUserInfo struct {
 	OpenId       string
 	UnionId      string
 	AccessToken  string
 	ExpiresIn    string
 	RefreshToken string
 	Scope        string
+	ErrCode      int
+	ErrMsg       string
 }
 
-func GetOpenId(code string) (info *WxUserToken, err error) {
-
+func GetWxUserInfo(code string) (info *WxUserInfo, err error) {
 	httpUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?
 		appid=%s&secret=%s&code=%s&grant_type=authorization_code`