Browse Source

update

master
dengbiao 4 months ago
parent
commit
f8fa228180
2 changed files with 16 additions and 9 deletions
  1. +11
    -5
      app/hdl/hdl_wx_open.go
  2. +5
    -4
      app/lib/wechat/wechat_api.go

+ 11
- 5
app/hdl/hdl_wx_open.go View File

@@ -148,8 +148,7 @@ func GetPreAuthCode(c *gin.Context) {
return 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 := "http://super.admin.izhyin.com/Wx/getAuthUrlCallBack"
// 对redirectURI进行URL编码 // 对redirectURI进行URL编码
encodedRedirectURI := url.QueryEscape(redirectURI) encodedRedirectURI := url.QueryEscape(redirectURI)
@@ -163,14 +162,21 @@ func GetPreAuthCode(c *gin.Context) {
query.Add("auth_type", "1") query.Add("auth_type", "1")


// 将查询参数附加到基础URL // 将查询参数附加到基础URL
urlStr := baseURL + "?" + query.Encode()
authURL := baseURL + "?" + query.Encode()

// 设置JavaScript重定向 // 设置JavaScript重定向
fmt.Fprintf(w, `
c.Header("Content-Type", "text/html") //TODO::必须指定相应内容为 text/html, 否则浏览器不能正确解析
c.String(200, `

<script> <script>

window.onload = function () { window.onload = function () {

window.location.href = '%s'; window.location.href = '%s';

}; };
</script>`, urlStr)

</script>`, authURL)
} }


func WechatMsgRecieve(c *gin.Context) { func WechatMsgRecieve(c *gin.Context) {


+ 5
- 4
app/lib/wechat/wechat_api.go View File

@@ -31,9 +31,10 @@ func NewWxApiService(masterId, componentAppid, componentAppsecret string) (wxApi
} }
cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, wxApiService.MasterId) cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, wxApiService.MasterId)
cacheComponentVerifyTicket, _ := cache.GetString(cacheKey) cacheComponentVerifyTicket, _ := cache.GetString(cacheKey)
if cacheComponentVerifyTicket != "" {
if cacheComponentVerifyTicket == "" {
return wxApiService, errors.New("微信验证票据ticket未获取到") return wxApiService, errors.New("微信验证票据ticket未获取到")
} }
wxApiService.ComponentVerifyTicket = cacheComponentVerifyTicket
return return
} }


@@ -41,14 +42,14 @@ func NewWxApiService(masterId, componentAppid, componentAppsecret string) (wxApi
func (wxApiService *WxApiService) GetComponentAccessToken() (cacheComponentAccessToken string, err error) { // set方法 func (wxApiService *WxApiService) GetComponentAccessToken() (cacheComponentAccessToken string, err error) { // set方法
cacheKey := fmt.Sprintf(md.MasterComponentAccessToken, wxApiService.MasterId) cacheKey := fmt.Sprintf(md.MasterComponentAccessToken, wxApiService.MasterId)
cacheComponentAccessToken, _ = cache.GetString(cacheKey) cacheComponentAccessToken, _ = cache.GetString(cacheKey)
if cacheComponentAccessToken != "" {
if cacheComponentAccessToken == "" {
url := "https://api.weixin.qq.com/cgi-bin/component/api_component_token" url := "https://api.weixin.qq.com/cgi-bin/component/api_component_token"
params := map[string]string{ params := map[string]string{
"component_appid": wxApiService.ComponentAppid, "component_appid": wxApiService.ComponentAppid,
"component_appsecret": wxApiService.ComponentAppsecret, "component_appsecret": wxApiService.ComponentAppsecret,
"component_verify_ticket": wxApiService.ComponentVerifyTicket, "component_verify_ticket": wxApiService.ComponentVerifyTicket,
} }
postBody, err1 := utils.CurlPost(url, params, nil)
postBody, err1 := utils.CurlPost(url, utils.SerializeStr(params), nil)
if err1 != nil { if err1 != nil {
return cacheComponentAccessToken, err1 return cacheComponentAccessToken, err1
} }
@@ -91,7 +92,7 @@ func (wxApiService *WxApiService) GetPreAuthCode() (preAuthCode string, err erro
params := map[string]string{ params := map[string]string{
"component_appid": wxApiService.ComponentAppid, "component_appid": wxApiService.ComponentAppid,
} }
postBody, err := utils.CurlPost(url, params, nil)
postBody, err := utils.CurlPost(url, utils.SerializeStr(params), nil)
if err != nil { if err != nil {
return return
} }


Loading…
Cancel
Save