pre_auth_code.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Copyright (c) 2022 Yansen Zhang
  3. * wxcomponent is licensed under Mulan PSL v2.
  4. * You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. * You may obtain a copy of Mulan PSL v2 at:
  6. * http://license.coscl.org.cn/MulanPSL2
  7. * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. * See the Mulan PSL v2 for more details.
  11. **/
  12. package authorization
  13. import (
  14. "encoding/json"
  15. "net/url"
  16. "gitee.com/yansen_zh/wxcomponent/config"
  17. "gitee.com/yansen_zh/wxcomponent/errors"
  18. "gitee.com/yansen_zh/wxcomponent/utils/request"
  19. )
  20. // PreAuthCodeResult 获取预授权码结果
  21. type PreAuthCodeResult struct {
  22. errors.Error
  23. PreAuthCode string `json:"pre_auth_code"`
  24. ExpiresIn int `json:"expires_in"`
  25. }
  26. const (
  27. apiCreatePreauthcode = "https://apies.weixin.qq.com/cgi-bin/component/api_create_preauthcode"
  28. )
  29. // CreatePreAuthCode 获取预授权码
  30. func CreatePreAuthCode() (*PreAuthCodeResult, error) {
  31. queryParam := url.Values{}
  32. queryParam.Set("component_access_token", config.GetAccessToken())
  33. data := map[string]string{
  34. "component_appid": config.GetAppID(),
  35. }
  36. resp, e2 := request.PostJSON(apiCreatePreauthcode, &queryParam, data)
  37. if e2 != nil {
  38. return nil, e2
  39. }
  40. result := PreAuthCodeResult{}
  41. if err := json.Unmarshal(resp, &result); err != nil {
  42. return nil, err
  43. }
  44. return &result, nil
  45. }