微信相关接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.0 KiB

package wechat
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"log"
"git.tetele.net/tgo/helper"
)
// 获取小程序码
func GetMiniappQrcode(access_token string, qrcodeParamsMap map[string]interface{}) (string, error) {
getCodeUrl := fmt.Sprintf(GET_MINIAPP_QRCODE, access_token)
checkPath := true
envVersion := "release"
width := 430
autoColor := false
isHyaline := false
if _, exist := qrcodeParamsMap["check_path"]; exist {
checkPath = qrcodeParamsMap["check_path"].(bool)
}
if _, exist := qrcodeParamsMap["env_version"]; exist {
envVersion = helper.ToStr(qrcodeParamsMap["env_version"])
}
if _, exist := qrcodeParamsMap["width"]; exist {
width = helper.ToInt(qrcodeParamsMap["width"])
}
if _, exist := qrcodeParamsMap["auto_color"]; exist {
autoColor = qrcodeParamsMap["auto_color"].(bool)
}
if _, exist := qrcodeParamsMap["is_hyaline"]; exist {
isHyaline = qrcodeParamsMap["is_hyaline"].(bool)
}
requestData := map[string]interface{}{
"scene": qrcodeParamsMap["scene"],
"page": qrcodeParamsMap["page"],
"check_path": checkPath,
"env_version": envVersion,
"width": width,
"auto_color": autoColor,
"is_hyaline": isHyaline,
}
// json.marshal 会转义&字符
/*requestDataJson, err := json.Marshal(requestData)
if err != nil {
return "", err
}*/
bf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(bf)
jsonEncoder.SetEscapeHTML(false)
jsonEncoder.Encode(requestData)
response, err := PostJson(getCodeUrl, bf.Bytes())
log.Println("get mp qrcode response:", string(response), err)
if err != nil {
return "", err
}
responseData := map[string]interface{}{}
err = json.Unmarshal(response, &responseData)
if err != nil {
// 有错,但能解析
log.Println(err)
}
if _, exist := responseData["errcode"]; exist && helper.ToInt(responseData["errcode"]) != 0 {
return "", errors.New(helper.ToStr(responseData["errmsg"]) + ",errcode:" + helper.ToStr(responseData["errcode"]))
}
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(response), nil
}