微信相关接口
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

  1. package wechat
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "log"
  8. "git.tetele.net/tgo/helper"
  9. )
  10. // 获取小程序码
  11. func GetMiniappQrcode(access_token string, qrcodeParamsMap map[string]interface{}) (string, error) {
  12. getCodeUrl := fmt.Sprintf(GET_MINIAPP_QRCODE, access_token)
  13. checkPath := true
  14. envVersion := "release"
  15. width := 430
  16. autoColor := false
  17. isHyaline := false
  18. if _, exist := qrcodeParamsMap["check_path"]; exist {
  19. checkPath = qrcodeParamsMap["check_path"].(bool)
  20. }
  21. if _, exist := qrcodeParamsMap["env_version"]; exist {
  22. envVersion = helper.ToStr(qrcodeParamsMap["env_version"])
  23. }
  24. if _, exist := qrcodeParamsMap["width"]; exist {
  25. width = helper.ToInt(qrcodeParamsMap["width"])
  26. }
  27. if _, exist := qrcodeParamsMap["auto_color"]; exist {
  28. autoColor = qrcodeParamsMap["auto_color"].(bool)
  29. }
  30. if _, exist := qrcodeParamsMap["is_hyaline"]; exist {
  31. isHyaline = qrcodeParamsMap["is_hyaline"].(bool)
  32. }
  33. requestData := map[string]interface{}{
  34. "scene": qrcodeParamsMap["scene"],
  35. "page": qrcodeParamsMap["page"],
  36. "check_path": checkPath,
  37. "env_version": envVersion,
  38. "width": width,
  39. "auto_color": autoColor,
  40. "is_hyaline": isHyaline,
  41. }
  42. // json.marshal 会转义&字符
  43. /*requestDataJson, err := json.Marshal(requestData)
  44. if err != nil {
  45. return "", err
  46. }*/
  47. bf := bytes.NewBuffer([]byte{})
  48. jsonEncoder := json.NewEncoder(bf)
  49. jsonEncoder.SetEscapeHTML(false)
  50. jsonEncoder.Encode(requestData)
  51. response, err := PostJson(getCodeUrl, bf.Bytes())
  52. log.Println("get mp qrcode response:", string(response), err)
  53. if err != nil {
  54. return "", err
  55. }
  56. responseData := map[string]interface{}{}
  57. err = json.Unmarshal(response, &responseData)
  58. if err != nil {
  59. // 有错,但能解析
  60. log.Println(err)
  61. }
  62. if _, exist := responseData["errcode"]; exist && helper.ToInt(responseData["errcode"]) != 0 {
  63. return "", errors.New(helper.ToStr(responseData["errmsg"]) + ",errcode:" + helper.ToStr(responseData["errcode"]))
  64. }
  65. return "data:image/png;base64," + base64.StdEncoding.EncodeToString(response), nil
  66. }