Explorar o código

scope改为snsapi_base

xingzai %!s(int64=3) %!d(string=hai) anos
pai
achega
54d4ab843b
Modificáronse 1 ficheiros con 17 adicións e 13 borrados
  1. 17 13
      controllers/wechat.go

+ 17 - 13
controllers/wechat.go

@@ -1,7 +1,7 @@
 package controllers
 
 import (
-	"fmt"
+	"encoding/json"
 	"hongze/hongze_cygxzs/models"
 	"hongze/hongze_cygxzs/services"
 	"hongze/hongze_cygxzs/utils"
@@ -51,7 +51,20 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 		br.ErrMsg = "获取openid失败,openid:" + item.Openid
 		return
 	}
-	unionId := item.Unionid
+	resp := new(models.UserDetailByUserLogin)
+	wxUserInfo, err := services.WxGetUserInfo(openId, item.AccessToken)
+	if err != nil {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
+		return
+	}
+	if wxUserInfo.Errcode != 0 {
+		userInfoJson, _ := json.Marshal(wxUserInfo)
+		br.Msg = "登录失败"
+		br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
+		return
+	}
+	unionId := wxUserInfo.Unionid
 	if unionId == "" {
 		br.Msg = "获取用户信息失败"
 		br.ErrMsg = "获取unionid失败,unionid:" + item.Openid
@@ -75,16 +88,7 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 			return
 		}
 	}
-	accessToken := item.AccessToken
-	fmt.Println(item)
-	resp := new(models.UserDetailByUserLogin)
-	itemWx, err := services.WxGetUserInfo(openId, accessToken)
-	if err != nil {
-		br.Msg = "获取用户信息失败"
-		br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
-		return
-	}
-	user, err := models.GetWxUserItemByUserUnionId(item.Unionid)
+	user, err := models.GetWxUserItemByUserUnionId(unionId)
 	if err != nil && err.Error() != utils.ErrNoRow() {
 		br.Msg = "获取用户信息失败"
 		br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
@@ -111,7 +115,7 @@ func (this *WechatCommonController) WechatLoginByxzs() {
 			resp.HasPermission = 2
 		}
 	}
-	resp.Headimgurl = itemWx.Headimgurl
+	resp.Headimgurl = wxUserInfo.Headimgurl
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"