-
Notifications
You must be signed in to change notification settings - Fork 702
/
oplatform_api.go
115 lines (101 loc) · 4.81 KB
/
oplatform_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
微信开放平台
移动应用文档:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/Resource_Center_Homepage.html
网站应用文档:https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
第三方平台文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Third_party_platform_appid.html
*/
package wechat
import "github.com/iGoogle-ink/gopay/pkg/xhttp"
// GetOauth2AccessToken 微信第三方登录,code 换取 access_token
// appId:应用唯一标识,在微信开放平台提交应用审核通过后获得
// appSecret:应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
// code:App用户换取access_token的code
// 文档:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Development_Guide.html
func GetOauth2AccessToken(appId, appSecret, code string) (accessToken *Oauth2AccessToken, err error) {
accessToken = new(Oauth2AccessToken)
url := "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code"
_, errs := xhttp.NewClient().Get(url).EndStruct(accessToken)
if len(errs) > 0 {
return nil, errs[0]
}
return accessToken, nil
}
// RefreshOauth2AccessToken 刷新微信第三方登录后,获取到的 access_token
// appId:应用唯一标识,在微信开放平台提交应用审核通过后获得
// refreshToken:填写通过获取 access_token 获取到的 refresh_token 参数
// 文档:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Development_Guide.html
func RefreshOauth2AccessToken(appId, refreshToken string) (accessToken *Oauth2AccessToken, err error) {
accessToken = new(Oauth2AccessToken)
url := "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appId + "&grant_type=refresh_token&refresh_token=" + refreshToken
_, errs := xhttp.NewClient().Get(url).EndStruct(accessToken)
if len(errs) > 0 {
return nil, errs[0]
}
return accessToken, nil
}
// CheckOauth2AccessToken 检验授权凭证(access_token)是否有效
// accessToken:调用接口凭证
// openid:普通用户标识,对该公众帐号唯一,获取 access_token 是获取的
// 文档:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Authorized_API_call_UnionID.html
func CheckOauth2AccessToken(accessToken, openid string) (result *CheckAccessTokenRsp, err error) {
result = new(CheckAccessTokenRsp)
url := "https://api.weixin.qq.com/sns/auth?access_token=" + accessToken + "&openid=" + openid
_, errs := xhttp.NewClient().Get(url).EndStruct(result)
if len(errs) > 0 {
return nil, errs[0]
}
return result, nil
}
// GetOauth2UserInfo 微信开放平台:获取用户个人信息
// accessToken:接口调用凭据
// openId:用户的OpenID
// lang:默认为 zh_CN ,可选填 zh_CN 简体,zh_TW 繁体,en 英语
// 文档:https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Authorized_API_call_UnionID.html
func GetOauth2UserInfo(accessToken, openId string, lang ...string) (userInfo *Oauth2UserInfo, err error) {
userInfo = new(Oauth2UserInfo)
url := "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId
if len(lang) == 1 {
url += "&lang=" + lang[0]
}
_, errs := xhttp.NewClient().Get(url).EndStruct(userInfo)
if len(errs) > 0 {
return nil, errs[0]
}
return userInfo, nil
}
// Deprecated
// 请替换 GetOauth2AccessToken 使用
func GetAppLoginAccessToken(appId, appSecret, code string) (accessToken *Oauth2AccessToken, err error) {
accessToken = new(Oauth2AccessToken)
url := "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code"
_, errs := xhttp.NewClient().Get(url).EndStruct(accessToken)
if len(errs) > 0 {
return nil, errs[0]
}
return accessToken, nil
}
// Deprecated
// 请替换 RefreshOauth2AccessToken 使用
func RefreshAppLoginAccessToken(appId, refreshToken string) (accessToken *RefreshAppLoginAccessTokenRsp, err error) {
accessToken = new(RefreshAppLoginAccessTokenRsp)
url := "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appId + "&grant_type=refresh_token&refresh_token=" + refreshToken
_, errs := xhttp.NewClient().Get(url).EndStruct(accessToken)
if len(errs) > 0 {
return nil, errs[0]
}
return accessToken, nil
}
// Deprecated
// 推荐使用 GetOauth2UserInfo
func GetUserInfoOpen(accessToken, openId string, lang ...string) (userInfo *Oauth2UserInfo, err error) {
userInfo = new(Oauth2UserInfo)
url := "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId
if len(lang) == 1 {
url += "&lang=" + lang[0]
}
_, errs := xhttp.NewClient().Get(url).EndStruct(userInfo)
if len(errs) > 0 {
return nil, errs[0]
}
return userInfo, nil
}