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

77 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. "git.tetele.net/tgo/network"
  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. requestDataJson, err := json.Marshal(requestData)
  43. if err != nil {
  44. return "", err
  45. }
  46. response, err := PostJson(getCodeUrl, requestDataJson)
  47. if err != nil {
  48. return "", err
  49. }
  50. responseData := map[string]interface{}{}
  51. err = json.Unmarshal(response, &responseData)
  52. if err != nil {
  53. // 有错,但能解析
  54. log.Println(err)
  55. }
  56. if _, exist := responseData["errcode"]; exist && helper.ToInt(responseData["errcode"]) != 0 {
  57. return "", errors.New(helper.ToStr(responseData["errmsg"]))
  58. }
  59. return "data:image/png;base64," + base64.StdEncoding.EncodeToString(response), nil
  60. }