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

76 lines
1.8 KiB

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