瀏覽代碼

update

master
dengbiao 4 月之前
父節點
當前提交
c78ad78e1d
共有 4 個檔案被更改,包括 91 行新增1 行删除
  1. +55
    -1
      app/hdl/hdl_wx_open.go
  2. +9
    -0
      app/lib/wechat/md/wechat_api_md.go
  3. +26
    -0
      app/lib/wechat/wechat_api.go
  4. +1
    -0
      app/router/router.go

+ 55
- 1
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) {
</script>`, 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


+ 9
- 0
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:"预授权码"`
}

+ 26
- 0
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
}

+ 1
- 0
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) // 以下接口需要用到数据库


Loading…
取消
儲存