32 Star 271 Fork 97

springrain / gowe

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
WxMpQrcodeApi.go 2.74 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
炫杉 提交于 2021-07-09 15:15 . !1修复二维码地址前缀
package gowe
import (
"encoding/json"
"errors"
)
//WxMpQrCreateTemporary 生成带参数的临时二维码 API
//https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html
//expireSeconds:该二维码有效时间,以秒为单位. 最大不超过2592000(即30天),默认2592000
//sceneStr:场景值ID(字符串形式的ID),字符串类型,长度限制为1到64.这里只使用字符串了,用途更广
func WxMpQrCreateTemporary(wxMpConfig IWxMpConfig, sceneStr string, expireSeconds int) (*WxMpQrCreateResponse, error) {
if len(sceneStr) < 1 {
return nil, errors.New("sceneStr不能为空")
}
if expireSeconds == 0 {
expireSeconds = 2592000
}
apiurl := WxMpAPIURL + "/cgi-bin/qrcode/create?access_token=" + wxMpConfig.GetAccessToken()
params := make(map[string]interface{})
params["expire_seconds"] = expireSeconds
params["action_name"] = "QR_STR_SCENE"
actionInfo := make(map[string]interface{})
scene := make(map[string]interface{})
scene["scene_str"] = sceneStr
actionInfo["scene"] = scene
params["action_info"] = actionInfo
data, err := httpPost(apiurl, params)
// 发送请求
if err != nil {
return nil, err
}
// 尝试解码
res := &WxMpQrCreateResponse{}
err = json.Unmarshal(data, res)
return res, err
}
//WxMpQrCreatePermanent 创建永久的带参数二维码
func WxMpQrCreatePermanent(wxMpConfig IWxMpConfig, sceneStr string) (*WxMpQrCreateResponse, error) {
if len(sceneStr) < 1 {
return nil, errors.New("sceneStr不能为空")
}
apiurl := WxMpAPIURL + "/cgi-bin/qrcode/create?access_token=" + wxMpConfig.GetAccessToken()
params := make(map[string]interface{})
params["action_name"] = "QR_LIMIT_STR_SCENE"
actionInfo := make(map[string]interface{})
scene := make(map[string]interface{})
scene["scene_str"] = sceneStr
actionInfo["scene"] = scene
params["action_info"] = actionInfo
data, err := httpPost(apiurl, params)
// 发送请求
if err != nil {
return nil, err
}
// 尝试解码
res := &WxMpQrCreateResponse{}
err = json.Unmarshal(data, res)
return res, err
}
//WxMpQrShowQrCodeUrl 通过ticket换取二维码地址
func WxMpQrShowQrCodeUrl(wxMpConfig IWxMpConfig, ticket string) (string, error) {
if len(ticket) < 1 {
return "", errors.New("ticket不能为空")
}
return WxMpWeiXinURL + "/cgi-bin/showqrcode?ticket=" + ticket, nil
}
type WxMpQrCreateResponse struct {
Ticket string `json:"ticket"` // 获取的二维码ticket,凭借此ticket可以在有效时间内换取二维码.
ExpireSeconds int `json:"expire_seconds"` // 该二维码有效时间,以秒为单位. 最大不超过2592000(即30天)
URL string `json:"url"` //二维码图片解析后的地址,开发者可根据该地址自行生成需要的二维码图片
}
Go
1
https://gitee.com/chunanyong/gowe.git
git@gitee.com:chunanyong/gowe.git
chunanyong
gowe
gowe
master

搜索帮助

14c37bed 8189591 565d56ea 8189591