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 2.9 kB

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. func callAccessToken() *AccessTokenResponse {
  54. client := getWechatRestyClient()
  55. var result AccessTokenResponse
  56. _, err := client.R().
  57. SetQueryParam("grant_type", GRANT_TYPE).
  58. SetQueryParam("appid", setting.WechatAppId).
  59. SetQueryParam("secret", setting.WechatAppSecret).
  60. SetResult(&result).
  61. Get(setting.WechatApiHost + ACCESS_TOKEN_PATH)
  62. if err != nil {
  63. log.Error("get wechat access token failed,e=%v", err)
  64. return nil
  65. }
  66. return &result
  67. }
  68. func callQRCodeCreate(sceneStr string) (*QRCodeResponse, bool) {
  69. client := getWechatRestyClient()
  70. body := &QRCodeRequest{
  71. Action_name: ACTION_QR_STR_SCENE,
  72. Action_info: ActionInfo{Scene: Scene{Scene_str: sceneStr}},
  73. Expire_seconds: setting.WechatQRCodeExpireSeconds,
  74. }
  75. bodyJson, _ := json.Marshal(body)
  76. var result QRCodeResponse
  77. r, err := client.R().
  78. SetHeader("Content-Type", "application/json").
  79. SetQueryParam("access_token", GetWechatAccessToken()).
  80. SetBody(bodyJson).
  81. SetResult(&result).
  82. Post(setting.WechatApiHost + QR_CODE_Path)
  83. if err != nil {
  84. log.Error("create QR code failed,e=%v", err)
  85. return nil, false
  86. }
  87. errCode := getErrorCodeFromResponse(r)
  88. if errCode == ERR_CODE_ACCESSTOKEN_EXPIRE || errCode == ERR_CODE_ACCESSTOKEN_INVALID {
  89. return nil, true
  90. }
  91. if result.Url == "" {
  92. return nil, false
  93. }
  94. log.Info("%v", r)
  95. return &result, false
  96. }
  97. func getErrorCodeFromResponse(r *resty.Response) int {
  98. a := r.Body()
  99. resultMap := make(map[string]interface{}, 0)
  100. json.Unmarshal(a, &resultMap)
  101. code := resultMap["errcode"]
  102. if code == nil {
  103. return -1
  104. }
  105. c, _ := strconv.Atoi(fmt.Sprint(code))
  106. return c
  107. }