You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.go 3.1 kB

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package wechat
  2. import (
  3. "code.gitea.io/gitea/modules/log"
  4. "code.gitea.io/gitea/modules/setting"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/go-resty/resty/v2"
  8. "strconv"
  9. "time"
  10. )
  11. var (
  12. client *resty.Client
  13. )
  14. const (
  15. GRANT_TYPE = "client_credential"
  16. ACCESS_TOKEN_PATH = "/cgi-bin/token"
  17. QR_CODE_Path = "/cgi-bin/qrcode/create"
  18. ACTION_QR_STR_SCENE = "QR_STR_SCENE"
  19. ERR_CODE_ACCESSTOKEN_EXPIRE = 42001
  20. ERR_CODE_ACCESSTOKEN_INVALID = 40001
  21. )
  22. type AccessTokenResponse struct {
  23. Access_token string
  24. Expires_in int
  25. }
  26. type QRCodeResponse struct {
  27. Ticket string `json:"ticket"`
  28. Expire_Seconds int `json:"expire_seconds"`
  29. Url string `json:"url"`
  30. }
  31. type QRCodeRequest struct {
  32. Action_name string `json:"action_name"`
  33. Action_info ActionInfo `json:"action_info"`
  34. Expire_seconds int `json:"expire_seconds"`
  35. }
  36. type ActionInfo struct {
  37. Scene Scene `json:"scene"`
  38. }
  39. type Scene struct {
  40. Scene_str string `json:"scene_str"`
  41. }
  42. type ErrorResponse struct {
  43. Errcode int
  44. Errmsg string
  45. }
  46. func getWechatRestyClient() *resty.Client {
  47. if client == nil {
  48. client = resty.New()
  49. client.SetTimeout(time.Duration(setting.WechatApiTimeoutSeconds) * time.Second)
  50. }
  51. return client
  52. }
  53. // api doc:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
  54. func callAccessToken() *AccessTokenResponse {
  55. client := getWechatRestyClient()
  56. var result AccessTokenResponse
  57. _, err := client.R().
  58. SetQueryParam("grant_type", GRANT_TYPE).
  59. SetQueryParam("appid", setting.WechatAppId).
  60. SetQueryParam("secret", setting.WechatAppSecret).
  61. SetResult(&result).
  62. Get(setting.WechatApiHost + ACCESS_TOKEN_PATH)
  63. if err != nil {
  64. log.Error("get wechat access token failed,e=%v", err)
  65. return nil
  66. }
  67. return &result
  68. }
  69. //callQRCodeCreate call the wechat api to create qr-code,
  70. // api doc: https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html
  71. func callQRCodeCreate(sceneStr string) (*QRCodeResponse, bool) {
  72. client := getWechatRestyClient()
  73. body := &QRCodeRequest{
  74. Action_name: ACTION_QR_STR_SCENE,
  75. Action_info: ActionInfo{Scene: Scene{Scene_str: sceneStr}},
  76. Expire_seconds: setting.WechatQRCodeExpireSeconds,
  77. }
  78. bodyJson, _ := json.Marshal(body)
  79. var result QRCodeResponse
  80. r, err := client.R().
  81. SetHeader("Content-Type", "application/json").
  82. SetQueryParam("access_token", GetWechatAccessToken()).
  83. SetBody(bodyJson).
  84. SetResult(&result).
  85. Post(setting.WechatApiHost + QR_CODE_Path)
  86. if err != nil {
  87. log.Error("create QR code failed,e=%v", err)
  88. return nil, false
  89. }
  90. errCode := getErrorCodeFromResponse(r)
  91. if errCode == ERR_CODE_ACCESSTOKEN_EXPIRE || errCode == ERR_CODE_ACCESSTOKEN_INVALID {
  92. return nil, true
  93. }
  94. if result.Url == "" {
  95. return nil, false
  96. }
  97. log.Info("%v", r)
  98. return &result, false
  99. }
  100. func getErrorCodeFromResponse(r *resty.Response) int {
  101. a := r.Body()
  102. resultMap := make(map[string]interface{}, 0)
  103. json.Unmarshal(a, &resultMap)
  104. code := resultMap["errcode"]
  105. if code == nil {
  106. return -1
  107. }
  108. c, _ := strconv.Atoi(fmt.Sprint(code))
  109. return c
  110. }