From f8fa2281805e8ee104e4ed10a2dfd2fe4761937a Mon Sep 17 00:00:00 2001 From: dengbiao Date: Tue, 20 Aug 2024 21:23:02 +0800 Subject: [PATCH] update --- app/hdl/hdl_wx_open.go | 16 +++++++++++----- app/lib/wechat/wechat_api.go | 9 +++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/hdl/hdl_wx_open.go b/app/hdl/hdl_wx_open.go index d364ee1..655a051 100644 --- a/app/hdl/hdl_wx_open.go +++ b/app/hdl/hdl_wx_open.go @@ -148,8 +148,7 @@ func GetPreAuthCode(c *gin.Context) { return } - var w http.ResponseWriter - w.Header().Set("Access-Control-Allow-Origin", "*") + c.Writer.Header().Set("Access-Control-Allow-Origin", "*") redirectURI := "http://super.admin.izhyin.com/Wx/getAuthUrlCallBack" // 对redirectURI进行URL编码 encodedRedirectURI := url.QueryEscape(redirectURI) @@ -163,14 +162,21 @@ func GetPreAuthCode(c *gin.Context) { query.Add("auth_type", "1") // 将查询参数附加到基础URL - urlStr := baseURL + "?" + query.Encode() + authURL := baseURL + "?" + query.Encode() + // 设置JavaScript重定向 - fmt.Fprintf(w, ` + c.Header("Content-Type", "text/html") //TODO::必须指定相应内容为 text/html, 否则浏览器不能正确解析 + c.String(200, ` + `, urlStr) + + `, authURL) } func WechatMsgRecieve(c *gin.Context) { diff --git a/app/lib/wechat/wechat_api.go b/app/lib/wechat/wechat_api.go index d6a5089..f1b6bf8 100644 --- a/app/lib/wechat/wechat_api.go +++ b/app/lib/wechat/wechat_api.go @@ -31,9 +31,10 @@ func NewWxApiService(masterId, componentAppid, componentAppsecret string) (wxApi } cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, wxApiService.MasterId) cacheComponentVerifyTicket, _ := cache.GetString(cacheKey) - if cacheComponentVerifyTicket != "" { + if cacheComponentVerifyTicket == "" { return wxApiService, errors.New("微信验证票据ticket未获取到") } + wxApiService.ComponentVerifyTicket = cacheComponentVerifyTicket return } @@ -41,14 +42,14 @@ func NewWxApiService(masterId, componentAppid, componentAppsecret string) (wxApi func (wxApiService *WxApiService) GetComponentAccessToken() (cacheComponentAccessToken string, err error) { // set方法 cacheKey := fmt.Sprintf(md.MasterComponentAccessToken, wxApiService.MasterId) cacheComponentAccessToken, _ = cache.GetString(cacheKey) - if cacheComponentAccessToken != "" { + if cacheComponentAccessToken == "" { url := "https://api.weixin.qq.com/cgi-bin/component/api_component_token" params := map[string]string{ "component_appid": wxApiService.ComponentAppid, "component_appsecret": wxApiService.ComponentAppsecret, "component_verify_ticket": wxApiService.ComponentVerifyTicket, } - postBody, err1 := utils.CurlPost(url, params, nil) + postBody, err1 := utils.CurlPost(url, utils.SerializeStr(params), nil) if err1 != nil { return cacheComponentAccessToken, err1 } @@ -91,7 +92,7 @@ func (wxApiService *WxApiService) GetPreAuthCode() (preAuthCode string, err erro params := map[string]string{ "component_appid": wxApiService.ComponentAppid, } - postBody, err := utils.CurlPost(url, params, nil) + postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil) if err != nil { return }