webpage.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }
  70. // SnsApiUserInfo 网页授权, 用户信息
  71. type SnsApiUserInfo struct {
  72. errors.Error
  73. OpenID string `json:"openid"`
  74. NickName string `json:"nickname"`
  75. Sex string `json:"sex"`
  76. Province string `json:"province"`
  77. City string `json:"city"`
  78. Country string `json:"country"`
  79. HeadImgURL string `json:"headimgurl"`
  80. Privilege []string `json:"privilege"`
  81. UnionID string `json:"unionid"`
  82. }
  83. const apiSnsUserInfo = "https://api.weixin.qq.com/sns/userinfo"
  84. // GetSnsApiUserInfo 网页授权, 获取用户信息
  85. func GetSnsApiUserInfo(accessToken, openID, lang string) (*SnsApiUserInfo, error) {
  86. if accessToken == "" || openID == "" {
  87. return nil, errors.New("获取网页授权用户信息 accessToken 或者 openID 不能为空")
  88. }
  89. langStr := lang
  90. if langStr == "" {
  91. langStr = "zh_CN"
  92. }
  93. param := url.Values{}
  94. param.Set("access_token", accessToken)
  95. param.Set("openid", openID)
  96. param.Set("lang", langStr)
  97. resp, e2 := request.GetJSON(apiOAuthAccessToken, &param)
  98. if e2 != nil {
  99. return nil, e2
  100. }
  101. result := SnsApiUserInfo{}
  102. if err := json.Unmarshal(resp, &result); err != nil {
  103. return nil, err
  104. }
  105. return &result, nil
  106. }