Skip to content

Commit

Permalink
小程序服务商 - 获取 URL Link 及 查询小程序 url_link 配置 (#576)
Browse files Browse the repository at this point in the history
* 小程序服务商 - 获取 URL Link 及 查询小程序 url_link 配置

* 格式化代码

* 规范调整
  • Loading branch information
yueyue1zhe authored Jun 28, 2022
1 parent e130764 commit ded5a10
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
52 changes: 52 additions & 0 deletions miniprogram/urllink/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package urllink

import (
"fmt"

"github.com/silenceper/wechat/v2/util"
)

const queryURL = "https://api.weixin.qq.com/wxa/query_urllink"

// ULQueryResult 返回的结果
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-link/urllink.query.html 返回值
type ULQueryResult struct {
util.CommonError

URLLinkInfo struct {
Appid string `json:"appid"`
Path string `json:"path"`
Query string `json:"query"`
CreateTime int64 `json:"create_time"`
ExpireTime int64 `json:"expire_time"`
EnvVersion string `json:"env_version"`
CloudBase struct {
Env string `json:"env"`
Domain string `json:"domain"`
Path string `json:"path"`
Query string `json:"query"`
ResourceAppid string `json:"resource_appid"`
} `json:"cloud_base"`
} `json:"url_link_info"`
VisitOpenid string `json:"visit_openid"`
}

// Query 查询小程序 url_link 配置。
func (u *URLLink) Query(urlLink string) (*ULQueryResult, error) {
accessToken, err := u.GetAccessToken()
if err != nil {
return nil, err
}

uri := fmt.Sprintf("%s?access_token=%s", queryURL, accessToken)
response, err := util.PostJSON(uri, map[string]string{"url_link": urlLink})
if err != nil {
return nil, err
}
var resp ULQueryResult
err = util.DecodeWithError(response, &resp, "URLLink.Query")
if err != nil {
return nil, err
}
return &resp, nil
}
35 changes: 35 additions & 0 deletions openplatform/miniprogram/miniprogram.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package miniprogram

import (
"fmt"

miniContext "github.com/silenceper/wechat/v2/miniprogram/context"
"github.com/silenceper/wechat/v2/miniprogram/urllink"
openContext "github.com/silenceper/wechat/v2/openplatform/context"
"github.com/silenceper/wechat/v2/openplatform/miniprogram/basic"
"github.com/silenceper/wechat/v2/openplatform/miniprogram/component"
Expand All @@ -10,6 +14,30 @@ import (
type MiniProgram struct {
AppID string
openContext *openContext.Context

authorizerRefreshToken string
}

// GetAccessToken 获取ak
func (miniProgram *MiniProgram) GetAccessToken() (string, error) {
ak, akErr := miniProgram.openContext.GetAuthrAccessToken(miniProgram.AppID)
if akErr == nil {
return ak, nil
}
if miniProgram.authorizerRefreshToken == "" {
return "", fmt.Errorf("please set the authorizer_refresh_token first")
}
akRes, akResErr := miniProgram.GetComponent().RefreshAuthrToken(miniProgram.AppID, miniProgram.authorizerRefreshToken)
if akResErr != nil {
return "", akResErr
}
return akRes.AccessToken, nil
}

// SetAuthorizerRefreshToken 设置代执操作业务授权账号authorizer_refresh_token
func (miniProgram *MiniProgram) SetAuthorizerRefreshToken(authorizerRefreshToken string) *MiniProgram {
miniProgram.authorizerRefreshToken = authorizerRefreshToken
return miniProgram
}

// NewMiniProgram 实例化
Expand All @@ -30,3 +58,10 @@ func (miniProgram *MiniProgram) GetComponent() *component.Component {
func (miniProgram *MiniProgram) GetBasic() *basic.Basic {
return basic.NewBasic(miniProgram.openContext, miniProgram.AppID)
}

// GetURLLink 小程序URL Link接口 调用前需确认已调用 SetAuthorizerRefreshToken 避免由于缓存中 authorizer_access_token 过期执行中断
func (miniProgram *MiniProgram) GetURLLink() *urllink.URLLink {
return urllink.NewURLLink(&miniContext.Context{
AccessTokenHandle: miniProgram,
})
}

0 comments on commit ded5a10

Please sign in to comment.