Skip to content

Commit

Permalink
feat:add urlscheme.query (#553)
Browse files Browse the repository at this point in the history
* add:urlscheme-query

* add:urlscheme-query

Co-authored-by: caoxianfeng <caoxianfeng@rcrai.com>
  • Loading branch information
caoxianfeng and caoxianfeng authored Apr 23, 2022
1 parent 56350c3 commit d5e7c80
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
70 changes: 70 additions & 0 deletions miniprogram/urlscheme/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package urlscheme

import (
"fmt"

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

const (
querySchemeURL = "https://api.weixin.qq.com/wxa/queryscheme?access_token=%s"
)

// QueryScheme 获取小程序访问scheme
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.query.html#参数
type QueryScheme struct {
// 小程序 scheme 码
Scheme string `json:"scheme"`
}

// SchemeInfo scheme 配置
type SchemeInfo struct {
// 小程序 appid。
AppID string `json:"appid"`
// 小程序页面路径。
Path string `json:"path"`
// 小程序页面query。
Query string `json:"query"`
// 创建时间,为 Unix 时间戳。
CreateTime int64 `json:"create_time"`
// 到期失效时间,为 Unix 时间戳,0 表示永久生效
ExpireTime int64 `json:"expire_time"`
// 要打开的小程序版本。正式版为"release",体验版为"trial",开发版为"develop"。
EnvVersion EnvVersion `json:"env_version"`
}

// resQueryScheme 返回结构体
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.query.html#参数
type resQueryScheme struct {
// 通用错误
*util.CommonError
// scheme 配置
SchemeInfo SchemeInfo `json:"scheme_info"`
// 访问该链接的openid,没有用户访问过则为空字符串
VisitOpenid string `json:"visit_openid"`
}

// QueryScheme 查询小程序 scheme 码
func (u *URLScheme) QueryScheme(querySchemeParams QueryScheme) (schemeInfo SchemeInfo, visitOpenid string, err error) {
var accessToken string
accessToken, err = u.GetAccessToken()
if err != nil {
return
}

urlStr := fmt.Sprintf(querySchemeURL, accessToken)
var response []byte
response, err = util.PostJSON(urlStr, querySchemeParams)
if err != nil {
return
}

// 使用通用方法返回错误
var res resQueryScheme
err = util.DecodeWithError(response, &res, "QueryScheme")
if err != nil {
return
}

return res.SchemeInfo, res.VisitOpenid, nil
}
13 changes: 11 additions & 2 deletions miniprogram/urlscheme/urlscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,29 @@ const generateURL = "https://api.weixin.qq.com/wxa/generatescheme"
// TExpireType 失效类型 (指定时间戳/指定间隔)
type TExpireType int

// EnvVersion 要打开的小程序版本
type EnvVersion string

const (
// ExpireTypeTime 指定时间戳后失效
ExpireTypeTime TExpireType = 0

// ExpireTypeInterval 间隔指定天数后失效
ExpireTypeInterval TExpireType = 1

// EnvVersionRelease 正式版为"release"
EnvVersionRelease EnvVersion = "release"
// EnvVersionTrial 体验版为"trial"
EnvVersionTrial EnvVersion = "trial"
// EnvVersionDevelop 开发版为"develop"
EnvVersionDevelop EnvVersion = "develop"
)

// JumpWxa 跳转到的目标小程序信息
type JumpWxa struct {
Path string `json:"path"`
Query string `json:"query"`
// envVersion 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"
EnvVersion string `json:"env_version,omitempty"`
EnvVersion EnvVersion `json:"env_version,omitempty"`
}

// USParams 请求参数
Expand Down

0 comments on commit d5e7c80

Please sign in to comment.