-
Notifications
You must be signed in to change notification settings - Fork 0
/
weixintools.go
66 lines (62 loc) · 1.96 KB
/
weixintools.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
package tools
import (
"encoding/json"
"github.com/Casxt/TimeLine/config"
"net/http"
"strings"
)
//GetWeiXinUser
func GetWeiXinUser(code string) (OpenID, SessionKey string, UnionID *string, status int, jsonRes map[string]string) {
resp, err := http.Get(strings.Join([]string{`https://api.weixin.qq.com/sns/jscode2session?appid=`, config.WeiXinApp.Id,
`&secret=`, config.WeiXinApp.Secrete,
`&js_code=`, code, `&grant_type=authorization_code`}, ""))
if err != nil {
return "", "", nil, 400, map[string]string{
"State": "Failed",
"Msg": `Connect to weixin Server Failed`,
}
}
if resp.StatusCode != 200 {
}
type UserData struct {
OpenId string `json:"openid"`
SessionKey string `json:"session_key"`
UnionId *string `json:"unionid,omitempty"`
ErrorCode int `json:"errcode"`
ErrorMsg int `json:"errmsg"`
}
var userData UserData
if err := json.NewDecoder(resp.Body).Decode(&userData); err != nil {
return "", "", nil, 400, map[string]string{
"State": "Failed",
"Msg": "Invalid Json when auth with WeiXin",
}
}
switch userData.ErrorCode {
case 0:
return userData.OpenId, userData.SessionKey, userData.UnionId, 200, map[string]string{
"State": "Failed",
"Msg": "Invalid Json when auth with WeiXin",
}
case -1:
return userData.OpenId, userData.SessionKey, userData.UnionId, 500, map[string]string{
"State": "Failed",
"Msg": "WeiXin server are too busy, please try again",
}
case 40029:
return userData.OpenId, userData.SessionKey, userData.UnionId, 400, map[string]string{
"State": "Failed",
"Msg": "Invalid UserCode",
}
case 45011:
return userData.OpenId, userData.SessionKey, userData.UnionId, 400, map[string]string{
"State": "Failed",
"Msg": "request WeiXin server are too fast, please try later",
}
default:
return userData.OpenId, userData.SessionKey, userData.UnionId, 400, map[string]string{
"State": "Failed",
"Msg": "WeiXin server return unknown code",
}
}
}