feat: add wx mp encrypt url

This commit is contained in:
wangfuduo 2026-08-29 15:49:44 +08:00
parent e126b97031
commit e3097efa24
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" userPhoneNumberUrl string = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"
getWxACodeUnLimitUrl string = "https://api.weixin.qq.com/wxa/getwxacodeunlimit" getWxACodeUnLimitUrl string = "https://api.weixin.qq.com/wxa/getwxacodeunlimit"
getWxScheme string = "https://api.weixin.qq.com/wxa/generatescheme" 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" code2UserinfoUrl string = "https://api.weixin.qq.com/sns/userinfo"
oaQrCodeCreateUrl string = "https://api.weixin.qq.com/cgi-bin/qrcode/create" oaQrCodeCreateUrl string = "https://api.weixin.qq.com/cgi-bin/qrcode/create"
userInfoByOpenid string = "https://api.weixin.qq.com/cgi-bin/user/info" userInfoByOpenid string = "https://api.weixin.qq.com/cgi-bin/user/info"

View File

@ -2,13 +2,15 @@ package weixin
import ( import (
"bytes" "bytes"
"cmp"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/spf13/cast"
"github.com/tidwall/gjson"
"io" "io"
"net/http" "net/http"
"github.com/spf13/cast"
"github.com/tidwall/gjson"
) )
type MpSdk struct { type MpSdk struct {
@ -151,3 +153,58 @@ func (o *MpSdk) GetScheme(params map[string]interface{}) (string, error) {
} }
return g.Get("openlink").String(), nil 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
}