forked from shenghui0779/sdk-go
/
oauth.go
86 lines (74 loc) · 2.28 KB
/
oauth.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
package offia
import (
"encoding/json"
"github.com/chenghonour/wechat-sdk/urls"
"github.com/chenghonour/wechat-sdk/wx"
)
// OAuthToken 公众号网页授权Token
type OAuthToken struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int64 `json:"expires_in"`
OpenID string `json:"openid"`
Scope string `json:"scope"`
}
// AccessToken 公众号普通AccessToken
type AccessToken struct {
Token string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
}
// CheckOAuthToken 检验授权凭证(access_token)是否有效
func CheckOAuthToken(openid string) wx.Action {
return wx.NewGetAction(urls.OffiaSnsCheckAccessToken,
wx.WithQuery("openid", openid),
)
}
// ResultOAuthUser 授权用户信息
type ResultOAuthUser struct {
OpenID string `json:"openid"`
UnionID string `json:"unionid"`
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 []string `json:"privilege"`
}
// GetOAuthUser 获取授权用户信息(注意:使用网页授权的access_token)
func GetOAuthUser(openid string, result *ResultOAuthUser) wx.Action {
return wx.NewGetAction(urls.OffiaSnsUserInfo,
wx.WithQuery("openid", openid),
wx.WithQuery("lang", "zh_CN"),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// TicketType JSApi ticket 类型
type TicketType string
// 微信支持的 JSApi ticket
const (
WXCardTicket TicketType = "wx_card"
JSAPITicket TicketType = "jsapi"
)
// JSApiSign JSApi签名
type JSApiSign struct {
Signature string `json:"signature"`
NonceStr string `json:"noncestr"`
Timestamp int64 `json:"timestamp"`
}
// ResultApiTicket 公众号 api ticket
type ResultApiTicket struct {
Ticket string `json:"ticket"`
ExpiresIn int64 `json:"expires_in"`
}
// GetApiTicket 获取 JSApi ticket (注意:使用普通access_token)
func GetApiTicket(ticketType TicketType, result *ResultApiTicket) wx.Action {
return wx.NewGetAction(urls.OffiaCgiBinTicket,
wx.WithQuery("type", string(ticketType)),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}