webpage.go 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 webpage
  13. // 代公众号发起网页授权
  14. import (
  15. "encoding/json"
  16. "net/url"
  17. "strings"
  18. "gitee.com/yansen_zh/wxcomponent/config"
  19. "gitee.com/yansen_zh/wxcomponent/errors"
  20. "gitee.com/yansen_zh/wxcomponent/utils/request"
  21. )
  22. // GetOAuthCodeLink 代公众号发起网页授权, 构造授权链接
  23. // redirectURI 必须是未经编码的原始网址字符串
  24. func GetOAuthCodeLink(appID, redirectURI, state string, scopes ...string) string {
  25. params := url.Values{}
  26. params.Set("appid", appID)
  27. params.Set("redirect_uri", redirectURI)
  28. params.Set("response_type", "code")
  29. params.Set("scope", strings.Join(scopes, ","))
  30. params.Set("state", state)
  31. params.Set("component_appid", config.GetAppID())
  32. return "https://open.weixin.qq.com/connect/oauth2/authorize?" + params.Encode()
  33. }
  34. // MpOAuthAccessTokenResult 网页授权, 通过 code 换取 access_token 返回值
  35. type MpOAuthAccessTokenResult struct {
  36. errors.Error
  37. // AccessToken 接口调用凭证
  38. AccessToken string `json:"access_token"`
  39. // ExpiresIn access_token 接口调用凭证超时时间,单位(秒)
  40. ExpiresIn int `json:"expires_in"`
  41. // RefreshToken 用户刷新 access_token
  42. RefreshToken string `json:"refresh_token"`
  43. // Openid 授权用户唯一标识
  44. OpenID string `json:"openid"`
  45. // Scope 用户授权的作用域,使用逗号(,)分隔
  46. Scope string `json:"scope"`
  47. }
  48. const apiOAuthAccessToken = "https://api.weixin.qq.com/sns/oauth2/component/access_token"
  49. // GetOAuthAccessToken 网页授权, 通过 code 换取 access_token
  50. func GetOAuthAccessToken(appID, code string) (*MpOAuthAccessTokenResult, error) {
  51. if appID == "" || code == "" {
  52. return nil, errors.New("获取网页授权Token appID 或者 code 不能为空")
  53. }
  54. param := url.Values{}
  55. param.Set("appid", appID)
  56. param.Set("code", code)
  57. param.Set("grant_type", "authorization_code")
  58. param.Set("component_appid", config.GetAppID())
  59. param.Set("component_access_token", config.GetAccessToken())
  60. resp, e2 := request.GetJSON(apiOAuthAccessToken, &param)
  61. if e2 != nil {
  62. return nil, e2
  63. }
  64. result := MpOAuthAccessTokenResult{}
  65. if err := json.Unmarshal(resp, &result); err != nil {
  66. return nil, err
  67. }
  68. return &result, nil
  69. }