123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /**
- * Copyright (c) 2022 Yansen Zhang
- * wxcomponent is licensed under Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2.
- * You may obtain a copy of Mulan PSL v2 at:
- * http://license.coscl.org.cn/MulanPSL2
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- * See the Mulan PSL v2 for more details.
- **/
-
- 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"
- )
-
- // GetOAuthCodeLink 代公众号发起网页授权, 构造授权链接
- // redirectURI 必须是未经编码的原始网址字符串
- 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()
- }
-
- // MpOAuthAccessTokenResult 网页授权, 通过 code 换取 access_token 返回值
- type MpOAuthAccessTokenResult struct {
- errors.Error
- // AccessToken 接口调用凭证
- AccessToken string `json:"access_token"`
- // ExpiresIn access_token 接口调用凭证超时时间,单位(秒)
- ExpiresIn int `json:"expires_in"`
- // RefreshToken 用户刷新 access_token
- RefreshToken string `json:"refresh_token"`
- // Openid 授权用户唯一标识
- OpenID string `json:"openid"`
- // Scope 用户授权的作用域,使用逗号(,)分隔
- Scope string `json:"scope"`
- }
-
- const apiOAuthAccessToken = "https://api.weixin.qq.com/sns/oauth2/component/access_token"
-
- // GetOAuthAccessToken 网页授权, 通过 code 换取 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
- }
-
- // SnsApiUserInfo 网页授权, 用户信息
- 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"
-
- // GetSnsApiUserInfo 网页授权, 获取用户信息
- 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
- }
|