webpage.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package mp
  2. import (
  3. "net/url"
  4. "strings"
  5. wxerr "gitee.com/yansen_zh/wxcomponent/errors"
  6. )
  7. // MpWebAccessTokenResult 网页授权, 通过 code 换取 access_token 返回值
  8. type MpWebAccessTokenResult struct {
  9. wxerr.Error
  10. // AccessToken 接口调用凭证
  11. AccessToken string `json:"access_token"`
  12. // ExpiresIn access_token 接口调用凭证超时时间,单位(秒)
  13. ExpiresIn int `json:"expires_in"`
  14. // RefreshToken 用户刷新 access_token
  15. RefreshToken string `json:"refresh_token"`
  16. // Openid 授权用户唯一标识
  17. OpenID string `json:"openid"`
  18. // Scope 用户授权的作用域,使用逗号(,)分隔
  19. Scope string `json:"scope"`
  20. }
  21. // GetAuthorizationCodeLink 代公众号发起网页授权, 构造授权链接
  22. // redirectURI 必须是未经编码的原始网址字符串
  23. func GetAuthorizationCodeLink(componentAppID, appID, redirectURI, state string, scopes ...string) string {
  24. params := url.Values{}
  25. params.Set("appid", appID)
  26. params.Set("redirect_uri", redirectURI)
  27. params.Set("response_type", "code")
  28. params.Set("scope", strings.Join(scopes, ","))
  29. params.Set("state", state)
  30. params.Set("component_appid", componentAppID)
  31. return "https://open.weixin.qq.com/connect/oauth2/authorize?" + params.Encode()
  32. }