-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwechat.go
162 lines (129 loc) · 3.83 KB
/
wechat.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package socialite
import (
"errors"
"fmt"
"github.com/birjemin/socialite/utils"
)
const (
wxAuthorizeURL = "https://open.weixin.qq.com/connect/qrconnect"
wxResponseType = "code"
wxScope = "snsapi_login"
wxTokenURL = "https://api.weixin.qq.com/sns/oauth2/access_token"
wxGrantTypeAuth = "authorization_code"
wxRefreshTokenURL = "https://api.weixin.qq.com/sns/oauth2/refresh_token"
wxGrantTypeRefresh = "refresh_token"
wxUserInfoURL = "https://api.weixin.qq.com/sns/userinfo"
)
// Wechat struct
// @doc: https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
type Wechat struct {
AppID string
AppSecret string
RedirectURL string
HTTPRequest *utils.HTTPClient
}
// wxRespErrorToken response of err
type wxRespErrorToken struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
}
// WxRespToken response of me
type WxRespToken struct {
wxRespErrorToken
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
OpenID string `json:"openid"`
Scope string `json:"scope"`
UnionID string `json:"unionid"`
}
// WxUserInfo user info
type WxUserInfo struct {
wxRespErrorToken
OpenID string `json:"openid"`
Nickname string `json:"nickname"`
Sex int `json:"sex"`
Province string `json:"province"`
City string `json:"city"`
Country string `json:"country"`
HeadImgURL string `json:"headimgurl"`
Privilege interface{} `json:"privilege"`
UnionID string `json:"unionid"`
}
// GetAuthorizeURL get authorize url
func (w *Wechat) GetAuthorizeURL(args ...string) string {
params := make(map[string]string, 5)
params["appid"] = w.AppID
params["response_type"] = wxResponseType
params["redirect_uri"] = w.RedirectURL
params["scope"] = wxScope
length := len(args)
if length > 0 {
params["state"] = args[0]
}
return fmt.Sprintf("%s?%s", wxAuthorizeURL, utils.QuerySortByKeyStr2(params))
}
// Token get token
func (w *Wechat) Token(code string) (interface{}, error) {
return w.doToken(wxTokenURL, code)
}
// doToken handle
func (w *Wechat) doToken(url, code string) (ret *WxRespToken, err error) {
params := map[string]string{
"grant_type": wxGrantTypeAuth,
"appid": w.AppID,
"secret": w.AppSecret,
"code": code,
}
if err := w.HTTPRequest.HTTPGet(url, params); err != nil {
return nil, err
}
ret = new(WxRespToken)
if w.HTTPRequest.GetResponseJSON(ret) != nil {
return nil, err
}
return ret, nil
}
// RefreshToken refresh token
func (w *Wechat) RefreshToken(refreshToken string) (interface{}, error) {
return w.doRefreshToken(wxRefreshTokenURL, refreshToken)
}
// doRefreshToken handle
func (w *Wechat) doRefreshToken(url, refreshToken string) (ret *WxRespToken, err error) {
params := map[string]string{
"grant_type": wxGrantTypeRefresh,
"appid": w.AppID,
"refresh_token": refreshToken,
}
if err := w.HTTPRequest.HTTPGet(url, params); err != nil {
return nil, err
}
ret = new(WxRespToken)
if w.HTTPRequest.GetResponseJSON(ret) != nil {
return nil, err
}
return ret, nil
}
// GetMe get me
func (w *Wechat) GetMe(accessToken string) (interface{}, error) {
return nil, errors.New("can not support")
}
// GetUserInfo get user info
func (w *Wechat) GetUserInfo(accessToken, openID string) (interface{}, error) {
return w.doGetUserInfo(wxUserInfoURL, accessToken, openID)
}
// doGetUserInfo handle
func (w *Wechat) doGetUserInfo(url, accessToken, openID string) (*WxUserInfo, error) {
params := map[string]string{
"access_token": accessToken,
"openid": openID,
}
if err := w.HTTPRequest.HTTPGet(url, params); err != nil {
return nil, err
}
ret := new(WxUserInfo)
if err := w.HTTPRequest.GetResponseJSON(ret); err != nil {
return nil, err
}
return ret, nil
}