forked from shenghui0779/sdk-go
/
auth.go
124 lines (106 loc) · 3.67 KB
/
auth.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
package minip
import (
"encoding/json"
"github.com/chenghonour/wechat-sdk/urls"
"github.com/chenghonour/wechat-sdk/wx"
)
// AuthSession 小程序授权Session
type AuthSession struct {
SessionKey string `json:"session_key"`
OpenID string `json:"openid"`
UnionID string `json:"unionid"`
}
// AccessToken 小程序access_token
type AccessToken struct {
Token string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
}
// Watermark 水印
type Watermark struct {
Timestamp int64 `json:"timestamp"`
AppID string `json:"appid"`
}
// AuthInfo 用户信息
type AuthInfo struct {
OpenID string `json:"openId"`
Language string `json:"language"`
City string `json:"city"`
Province string `json:"province"`
AvatarURL string `json:"avatarUrl"`
Nickname string `json:"nickName"`
Gender int `json:"gender"`
Country string `json:"country"`
UnionID string `json:"unionId"`
Watermark Watermark `json:"watermark"`
}
type ResultPhoneNumber struct {
PhoneInfo *PhoneInfo `json:"phone_info"`
}
type PhoneInfo struct {
PhoneNumber string `json:"phoneNumber"` // 用户绑定的手机号(国外手机号会有区号)
PurePhoneNumber string `json:"purePhoneNumber"` // 没有区号的手机号
CountryCode string `json:"countryCode"` // 区号
Watermark Watermark `json:"watermark"` // 数据水印
}
type ParamsPhoneNumber struct {
Code string `json:"code"`
}
func GetPhoneNumber(code string, result *ResultPhoneNumber) wx.Action {
params := &ParamsPhoneNumber{
Code: code,
}
return wx.NewPostAction(urls.MinipPhoneNumber,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
type ParamsEncryptedDataCheck struct {
EncryptedMsgHash string `json:"encrypted_msg_hash"` // 加密数据的sha256,通过Hex(Base16)编码后的字符串
}
type ResultEncryptedDataCheck struct {
Valid bool `json:"vaild"` // 是否是合法的数据
CreateTime int64 `json:"create_time"` // 加密数据生成的时间戳
}
// CheckEncryptedData 用户信息 - 检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
func CheckEncryptedData(encryptedData string, result *ResultEncryptedDataCheck) wx.Action {
params := &ParamsEncryptedDataCheck{
EncryptedMsgHash: wx.SHA256(encryptedData),
}
return wx.NewPostAction(urls.MinipEncryptedDataCheck,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// ResultPaidUnionID 支付用户unionid
type ResultPaidUnionID struct {
UnionID string `json:"unionid"`
}
// GetPaidUnionIDByTransactionID 用户信息 - 用户支付完成后,获取该用户的 UnionId,无需用户授权
func GetPaidUnionIDByTransactionID(openid, transactionID string, result *ResultPaidUnionID) wx.Action {
return wx.NewGetAction(urls.MinipPaidUnion,
wx.WithQuery("openid", openid),
wx.WithQuery("transaction_id", transactionID),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// GetPaidUnionIDByOutTradeNO 用户信息 - 用户支付完成后,获取该用户的 UnionId,无需用户授权
func GetPaidUnionIDByOutTradeNO(openid, mchid, outTradeNO string, result *ResultPaidUnionID) wx.Action {
return wx.NewGetAction(urls.MinipPaidUnion,
wx.WithQuery("openid", openid),
wx.WithQuery("mch_id", mchid),
wx.WithQuery("out_trade_no", outTradeNO),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}