123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
-
-
- package webpage
-
-
-
- import (
- "encoding/json"
- "net/url"
- "strings"
-
- "gitee.com/yansen_zh/wxcomponent/config"
- "gitee.com/yansen_zh/wxcomponent/errors"
- "gitee.com/yansen_zh/wxcomponent/utils/request"
- )
-
-
-
- func GetOAuthCodeLink(appID, redirectURI, state string, scopes ...string) string {
- params := url.Values{}
- params.Set("appid", appID)
- params.Set("redirect_uri", redirectURI)
- params.Set("response_type", "code")
- params.Set("scope", strings.Join(scopes, ","))
- params.Set("state", state)
- params.Set("component_appid", config.GetAppID())
-
- return "https://open.weixin.qq.com/connect/oauth2/authorize?" + params.Encode()
- }
-
-
- type MpOAuthAccessTokenResult struct {
- errors.Error
-
- AccessToken string `json:"access_token"`
-
- ExpiresIn int `json:"expires_in"`
-
- RefreshToken string `json:"refresh_token"`
-
- OpenID string `json:"openid"`
-
- Scope string `json:"scope"`
- }
-
- const apiOAuthAccessToken = "https://api.weixin.qq.com/sns/oauth2/component/access_token"
-
-
- func GetOAuthAccessToken(appID, code string) (*MpOAuthAccessTokenResult, error) {
- if appID == "" || code == "" {
- return nil, errors.New("获取网页授权Token appID 或者 code 不能为空")
- }
-
- param := url.Values{}
- param.Set("appid", appID)
- param.Set("code", code)
- param.Set("grant_type", "authorization_code")
- param.Set("component_appid", config.GetAppID())
- param.Set("component_access_token", config.GetAccessToken())
-
- resp, e2 := request.GetJSON(apiOAuthAccessToken, ¶m)
- if e2 != nil {
- return nil, e2
- }
-
- result := MpOAuthAccessTokenResult{}
- if err := json.Unmarshal(resp, &result); err != nil {
- return nil, err
- }
-
- return &result, nil
- }
-
-
- type SnsApiUserInfo struct {
- errors.Error
- OpenID string `json:"openid"`
- NickName string `json:"nickname"`
- Sex string `json:"sex"`
- Province string `json:"province"`
- City string `json:"city"`
- Country string `json:"country"`
- HeadImgURL string `json:"headimgurl"`
- Privilege []string `json:"privilege"`
- UnionID string `json:"unionid"`
- }
-
- const apiSnsUserInfo = "https://api.weixin.qq.com/sns/userinfo"
-
-
- func GetSnsApiUserInfo(accessToken, openID, lang string) (*SnsApiUserInfo, error) {
- if accessToken == "" || openID == "" {
- return nil, errors.New("获取网页授权用户信息 accessToken 或者 openID 不能为空")
- }
-
- langStr := lang
- if langStr == "" {
- langStr = "zh_CN"
- }
-
- param := url.Values{}
- param.Set("access_token", accessToken)
- param.Set("openid", openID)
- param.Set("lang", langStr)
-
- resp, e2 := request.GetJSON(apiOAuthAccessToken, ¶m)
- if e2 != nil {
- return nil, e2
- }
-
- result := SnsApiUserInfo{}
- if err := json.Unmarshal(resp, &result); err != nil {
- return nil, err
- }
-
- return &result, nil
- }
|