瀏覽代碼

微信内置浏览器自动授权登录接口

xingzai 2 年之前
父節點
當前提交
ded344e09d
共有 2 個文件被更改,包括 31 次插入19 次删除
  1. 16 4
      controllers/wechat.go
  2. 15 15
      services/wechat.go

+ 16 - 4
controllers/wechat.go

@@ -25,6 +25,7 @@ type MobileWechatController struct {
 // @Title 微信登录小助手接口
 // @Description 微信登录小助手接口
 // @Param   Code   query   string  true       "微信唯一编码code"
+// @Param   IsWechat   query   int  true       "是否微信内置浏览器打开 1是 ,0否 ,不传默认为0 "
 // @Success 200 {object}  models.UserDetailByUserLogin
 // @router /login [get]
 func (this *WechatCommonController) WechatLogin() {
@@ -34,6 +35,7 @@ func (this *WechatCommonController) WechatLogin() {
 		this.ServeJSON()
 	}()
 	code := this.GetString("Code")
+	isWechat, _ := this.GetInt("IsWechat")
 	if code == "" {
 		br.Msg = "参数错误"
 		br.ErrMsg = "Code 为空"
@@ -41,10 +43,16 @@ func (this *WechatCommonController) WechatLogin() {
 	}
 	var token string
 	resp := new(models.UserDetailByUserLogin)
-	WxAccessToken, err := services.GetWxAccessOpenToken(code)
-	if err != nil {
+	var WxAccessToken *services.WxAccessToken
+	var errWx error
+	if isWechat == 0 {
+		WxAccessToken, errWx = services.GetWxAccessOpenToken(code)
+	} else {
+		WxAccessToken, errWx = services.WxGetUserOpenIdByCode(code)
+	}
+	if errWx != nil {
 		br.Msg = "获取用户信息失败"
-		br.ErrMsg = "获取access_token失败,err:" + err.Error()
+		br.ErrMsg = "获取access_token失败,err:" + errWx.Error()
 		return
 	}
 	if WxAccessToken.AccessToken == "" {
@@ -86,7 +94,11 @@ func (this *WechatCommonController) WechatLogin() {
 	items.City = wxUserInfo.City
 	items.Country = wxUserInfo.Country
 	items.Headimgurl = wxUserInfo.Headimgurl
-	items.CreatePlatform = 7
+	if isWechat == 0 {
+		items.CreatePlatform = 7
+	} else {
+		items.CreatePlatform = 8
+	}
 	items.CreateTime = time.Now()
 	if total == 0 {
 		_, err = models.AddUserRecord(items)

+ 15 - 15
services/wechat.go

@@ -49,23 +49,23 @@ type WxToken struct {
 	Id          int64
 }
 
-func WxGetUserOpenIdByCode(code string) (item *WxAccessToken, err error) {
-	if code == "" {
-		err = errors.New("code is empty")
-		return nil, err
-	}
-	requestUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code`
-	requestUrl = fmt.Sprintf(requestUrl, utils.WxAppId, utils.WxAppSecret, code)
-	result, err := http.Get(requestUrl)
-	if err != nil {
-		return nil, err
-	}
-	err = json.Unmarshal(result, &item)
-	return
-}
+//func WxGetUserOpenIdByCode(code string) (item *WxAccessToken, err error) {
+//	if code == "" {
+//		err = errors.New("code is empty")
+//		return nil, err
+//	}
+//	requestUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code`
+//	requestUrl = fmt.Sprintf(requestUrl, utils.WxAppId, utils.WxAppSecret, code)
+//	result, err := http.Get(requestUrl)
+//	if err != nil {
+//		return nil, err
+//	}
+//	err = json.Unmarshal(result, &item)
+//	return
+//}
 
 //小助手
-func WxGetUserOpenIdByCodeXzs(code string) (item *WxAccessToken, err error) {
+func WxGetUserOpenIdByCode(code string) (item *WxAccessToken, err error) {
 	if code == "" {
 		err = errors.New("code is empty")
 		return nil, err