From c78ad78e1de3d2090d86de41f41c4ce21bad0654 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Tue, 20 Aug 2024 22:02:21 +0800 Subject: [PATCH] update --- app/hdl/hdl_wx_open.go | 56 +++++++++++++++++++++++++++++- app/lib/wechat/md/wechat_api_md.go | 9 +++++ app/lib/wechat/wechat_api.go | 26 ++++++++++++++ app/router/router.go | 1 + 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/app/hdl/hdl_wx_open.go b/app/hdl/hdl_wx_open.go index 655a051..9ee64be 100644 --- a/app/hdl/hdl_wx_open.go +++ b/app/hdl/hdl_wx_open.go @@ -1,6 +1,7 @@ package hdl import ( + "applet/app/cfg" "applet/app/e" "applet/app/lib/wechat" "applet/app/md" @@ -149,7 +150,10 @@ func GetPreAuthCode(c *gin.Context) { } c.Writer.Header().Set("Access-Control-Allow-Origin", "*") - redirectURI := "http://super.admin.izhyin.com/Wx/getAuthUrlCallBack" + redirectURI := "http://super.advertisement.dengbiao.top/api/wxOpen/getAuthUrlCallBack?masterId=" + masterId + if cfg.Prd { + redirectURI = "http://www.baidu.com/api/wxOpen/getAuthUrlCallBack?masterId=" + masterId + } // 对redirectURI进行URL编码 encodedRedirectURI := url.QueryEscape(redirectURI) @@ -179,6 +183,56 @@ func GetPreAuthCode(c *gin.Context) { `, authURL) } +func GetAuthUrlCallBack(c *gin.Context) { + authCode := c.DefaultQuery("auth_code", "") + if authCode == "" { + e.OutErr(c, e.ERR_NOT_FAN, "auth_code获取失败") + return + } + + fmt.Println("auth_code>>>>>>>>", authCode) + + masterId := c.DefaultQuery("masterId", "") + wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db) + wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId)) + if err != nil { + return + } + if wxOpenThirdPartyAppList == nil { + e.OutErr(c, e.ERR_NOT_FAN, "未查询到对应三方应用记录") + return + } + wxApiService, err := wechat.NewWxApiService(masterId, wxOpenThirdPartyAppList.Appid, wxOpenThirdPartyAppList.AppSecret) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + resp, err := wxApiService.GetAuthorizerAccessTokenByAuthCode(authCode) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + + userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db) + userWxAppletList, err := userWxAppletListDb.GetUserWxAppletListByAppId(resp.AuthorizationInfo.AuthorizerAppid) + if err != nil { + return + } + if userWxAppletList == nil { + e.OutErr(c, e.ERR_NOT_FAN, "未查询到小程序应用记录") + return + } + userWxAppletList.AuthorizerRefreshToken = resp.AuthorizationInfo.AuthorizerRefreshToken + _, err = userWxAppletListDb.UpdateUserWxAppletList(userWxAppletList, "authorizer_refresh_token") + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + + c.String(http.StatusOK, "ok") + return +} + func WechatMsgRecieve(c *gin.Context) { return diff --git a/app/lib/wechat/md/wechat_api_md.go b/app/lib/wechat/md/wechat_api_md.go index 4d723c1..9d27fa6 100644 --- a/app/lib/wechat/md/wechat_api_md.go +++ b/app/lib/wechat/md/wechat_api_md.go @@ -9,3 +9,12 @@ type GetPreAuthCode struct { PreAuthCode string `json:"pre_auth_code" example:"预授权码"` ExpiresIn int `json:"expires_in" example:"有效期,单位:秒"` } + +type GetAuthorizerAccessTokenByAuthCode struct { + AuthorizationInfo struct { + AuthorizerAppid string `json:"authorizer_appid" example:"授权方 appid"` + AuthorizerAccessToken string `json:"authorizer_access_token" example:"接口调用令牌"` + ExpiresIn int `json:"expires_in" example:"authorizer_access_token 的有效期(在授权的公众号/小程序具备API权限时,才有此返回值),单位:秒"` + AuthorizerRefreshToken string `json:"authorizer_refresh_token" example:"刷新令牌(在授权的公众号具备API权限时,才有此返回值),刷新令牌主要用于第三方平台获取和刷新已授权用户的 authorizer_access_token。一旦丢失,只能让用户重新授权,才能再次拿到新的刷新令牌。用户重新授权后,之前的刷新令牌会失效"` + } `json:"authorization_info" example:"预授权码"` +} diff --git a/app/lib/wechat/wechat_api.go b/app/lib/wechat/wechat_api.go index f1b6bf8..1ff84b9 100644 --- a/app/lib/wechat/wechat_api.go +++ b/app/lib/wechat/wechat_api.go @@ -108,3 +108,29 @@ func (wxApiService *WxApiService) GetPreAuthCode() (preAuthCode string, err erro preAuthCode = postData.PreAuthCode return } + +// GetAuthorizerAccessTokenByAuthCode 使用授权码获取授权信息 +func (wxApiService *WxApiService) GetAuthorizerAccessTokenByAuthCode(authCode string) (resp md2.GetAuthorizerAccessTokenByAuthCode, err error) { // set方法 + componentAccessToken, err := wxApiService.GetComponentAccessToken() + if err != nil { + return + } + url := "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" + componentAccessToken + params := map[string]string{ + "component_appid": wxApiService.ComponentAppid, + "authorization_code": authCode, + } + postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) + if err != nil { + return + } + err = json.Unmarshal(postBody, &resp) + if err != nil { + return + } + if resp.AuthorizationInfo.AuthorizerAppid == "" { + err = errors.New("获取授权信息失败") + return + } + return +} diff --git a/app/router/router.go b/app/router/router.go index b514f4b..7c17cae 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -56,6 +56,7 @@ func route(r *gin.RouterGroup) { wxOpenNotify.Any("/:APPID/wechatMsgRecieve", hdl.WechatMsgRecieve) wxOpenNotify.Any("/setTicket", hdl.SetTicket) wxOpenNotify.GET("/getPreAuthCode", hdl.GetPreAuthCode) + wxOpenNotify.GET("/getAuthUrlCallBack", hdl.GetAuthUrlCallBack) } r.Use(mw.DB) // 以下接口需要用到数据库