feat: add wx mp encrypt url #6

Merged
jiangyong merged 1 commits from wfd into master 2026-08-29 15:54:41 +08:00
2 changed files with 60 additions and 2 deletions

View File

@ -20,6 +20,7 @@ const (
userPhoneNumberUrl string = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"
getWxACodeUnLimitUrl string = "https://api.weixin.qq.com/wxa/getwxacodeunlimit"
getWxScheme string = "https://api.weixin.qq.com/wxa/generatescheme"
getWxUrlLink string = "https://api.weixin.qq.com/wxa/generate_urllink"
code2UserinfoUrl string = "https://api.weixin.qq.com/sns/userinfo"
oaQrCodeCreateUrl string = "https://api.weixin.qq.com/cgi-bin/qrcode/create"
userInfoByOpenid string = "https://api.weixin.qq.com/cgi-bin/user/info"

View File

@ -2,13 +2,15 @@ package weixin
import (
"bytes"
"cmp"
"encoding/json"
"errors"
"fmt"
"github.com/spf13/cast"
"github.com/tidwall/gjson"
"io"
"net/http"
"github.com/spf13/cast"
"github.com/tidwall/gjson"
)
type MpSdk struct {
@ -151,3 +153,58 @@ func (o *MpSdk) GetScheme(params map[string]interface{}) (string, error) {
}
return g.Get("openlink").String(), nil
}
func (o *MpSdk) GetEncryptUrl(params map[string]any) (string, error) {
newParams := map[string]any{
"env_version": cmp.Or(cast.ToString(params["env_version"]), "release"),
}
if path := cast.ToString(params["path"]); path != "" {
newParams["path"] = path
}
if query := cast.ToString(params["query"]); query != "" {
newParams["query"] = query
}
_, hasExpireType := params["expire_type"]
_, hasExpireTime := params["expire_time"]
_, hasExpireInterval := params["expire_interval"]
if hasExpireType {
newParams["expire_type"] = cast.ToInt(params["expire_type"])
} else if hasExpireInterval {
newParams["expire_type"] = 1
} else if hasExpireTime {
newParams["expire_type"] = 0
}
if hasExpireTime {
newParams["expire_time"] = cast.ToInt64(params["expire_time"])
}
if hasExpireInterval {
newParams["expire_interval"] = cast.ToInt(params["expire_interval"])
}
marshal, err := json.Marshal(newParams)
if err != nil {
return "", err
}
accessToken, err := o.getAccessToken()
if err != nil {
return "", err
}
url := fmt.Sprintf("%s?access_token=%s", getWxUrlLink, accessToken)
res, err := http.Post(url, "application/json", bytes.NewBuffer(marshal))
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
g := gjson.ParseBytes(body)
errcode := g.Get("errcode").Int()
if errcode != 0 {
return "", fmt.Errorf("%d:%s", errcode, g.Get("errmsg"))
}
return g.Get("url_link").String(), nil
}