|
@@ -1,116 +1,42 @@
|
1
|
1
|
package utils
|
2
|
2
|
|
3
|
3
|
import (
|
4
|
|
- "github.com/zjxpcyc/wechat/mini"
|
5
|
|
- "github.com/zjxpcyc/wechat/wx"
|
|
4
|
+ "github.com/astaxie/beego/config"
|
|
5
|
+ "github.com/kinisky564477/wechat/component"
|
6
|
6
|
)
|
7
|
7
|
|
8
|
|
-var wxClients map[string]*wx.Client
|
9
|
|
-var miniClients map[string]*mini.Client
|
|
8
|
+// Component 微信开放平台
|
|
9
|
+var Component *component.ComponentClient
|
10
|
10
|
|
11
|
|
-type WechatUser struct {
|
12
|
|
- OpenID string `json:"openid"`
|
13
|
|
- NickName string `json:"nickname"`
|
14
|
|
- Sex float64 `json:"sex"`
|
15
|
|
- Province string `json:"province"`
|
16
|
|
- City string `json:"city"`
|
17
|
|
- Country string `json:"country"`
|
18
|
|
- HeadImgURL string `json:"headimgurl"`
|
19
|
|
- UnionID string `json:"unionid"`
|
|
11
|
+// ComponentInit 第三方初始化
|
|
12
|
+func ComponentInit() {
|
|
13
|
+ // 初始化第三方
|
|
14
|
+ var cert map[string]string
|
|
15
|
+ wechat, _ := config.NewConfig("ini", appRoot+"/conf/db.conf")
|
|
16
|
+ cert["component_appid"] = wechat.String("appid")
|
|
17
|
+ cert["aeskey"] = wechat.String("aeskey")
|
|
18
|
+ Component = component.NewComponentClient(cert)
|
20
|
19
|
}
|
21
|
20
|
|
22
|
|
-// WxClientSingleton 初始化
|
23
|
|
-func WxClientSingleton(org string, cert map[string]string) {
|
24
|
|
- if wxClients == nil {
|
25
|
|
- wxClients = map[string]*wx.Client{
|
26
|
|
- org: wx.NewClient(cert),
|
27
|
|
- }
|
28
|
|
- } else {
|
29
|
|
- if _, ok := wxClients[org]; !ok {
|
30
|
|
- wxClients[org] = wx.NewClient(cert)
|
31
|
|
- }
|
32
|
|
- }
|
|
21
|
+// RefreshComponentTicket ticket
|
|
22
|
+func RefreshComponentTicket(ticket string) {
|
|
23
|
+ Component.RefreshTicket(ticket)
|
33
|
24
|
}
|
34
|
25
|
|
35
|
|
-// MiniClientSingleton 初始化
|
36
|
|
-func MiniClientSingleton(org string, cert map[string]string) {
|
37
|
|
- if miniClients == nil {
|
38
|
|
- miniClients = map[string]*mini.Client{
|
39
|
|
- org: mini.NewClient(cert),
|
40
|
|
- }
|
41
|
|
- } else {
|
42
|
|
- if _, ok := miniClients[org]; !ok {
|
43
|
|
- miniClients[org] = mini.NewClient(cert)
|
44
|
|
- }
|
45
|
|
- }
|
46
|
|
-}
|
47
|
|
-
|
48
|
|
-// WxClientFor 微信客户端
|
49
|
|
-func WxClientFor(org string) *wx.Client {
|
50
|
|
- return wxClients[org]
|
51
|
|
-}
|
52
|
|
-
|
53
|
|
-// MiniClientFor 小程序客户端
|
54
|
|
-func MiniClientFor(org string) *mini.Client {
|
55
|
|
- return miniClients[org]
|
56
|
|
-}
|
|
26
|
+// WechatInit 微信初始化
|
|
27
|
+func WechatInit(cert map[string]string) *component.WxClient {
|
|
28
|
+ var wechatClient *component.WxClient
|
57
|
29
|
|
58
|
|
-// GetWxAppID 获取微信ID
|
59
|
|
-func GetWxAppID(org string) string {
|
60
|
|
- return wxClients[org].GetAppID()
|
61
|
|
-}
|
|
30
|
+ wechatClient = component.NewWxClient(
|
|
31
|
+ cert,
|
|
32
|
+ Component.GetToken,
|
|
33
|
+ Component.GetCertificate,
|
|
34
|
+ )
|
62
|
35
|
|
63
|
|
-func WechatInit() {
|
64
|
|
- logger := GetDefaultLogger()
|
65
|
|
- wx.SetLogInst(logger)
|
66
|
|
- mini.SetLogInst(logger)
|
|
36
|
+ return wechatClient
|
67
|
37
|
}
|
68
|
38
|
|
69
|
|
-// MapToWechatUser 映射微信人员
|
70
|
|
-func MapToWechatUser(data map[string]interface{}) *WechatUser {
|
71
|
|
- subscribe, has := data["subscribe"]
|
72
|
|
- if has {
|
73
|
|
- if subscribe == nil {
|
74
|
|
- return nil
|
75
|
|
- }
|
76
|
|
-
|
77
|
|
- sub := subscribe.(float64)
|
78
|
|
- if sub == 0 {
|
79
|
|
- return nil
|
80
|
|
- }
|
81
|
|
- }
|
82
|
|
-
|
83
|
|
- user := WechatUser{
|
84
|
|
- OpenID: data["openid"].(string),
|
85
|
|
- }
|
86
|
|
-
|
87
|
|
- if data["sex"] != nil {
|
88
|
|
- user.Sex = data["sex"].(float64)
|
89
|
|
- }
|
90
|
|
-
|
91
|
|
- if data["nickname"] != nil {
|
92
|
|
- user.NickName = data["nickname"].(string)
|
93
|
|
- }
|
94
|
|
-
|
95
|
|
- if data["province"] != nil {
|
96
|
|
- user.Province = data["province"].(string)
|
97
|
|
- }
|
98
|
|
-
|
99
|
|
- if data["city"] != nil {
|
100
|
|
- user.City = data["city"].(string)
|
101
|
|
- }
|
102
|
|
-
|
103
|
|
- if data["country"] != nil {
|
104
|
|
- user.Country = data["country"].(string)
|
105
|
|
- }
|
106
|
|
-
|
107
|
|
- if data["headimgurl"] != nil {
|
108
|
|
- user.HeadImgURL = data["headimgurl"].(string)
|
109
|
|
- }
|
110
|
|
-
|
111
|
|
- if data["unionid"] != nil {
|
112
|
|
- user.UnionID = data["unionid"].(string)
|
113
|
|
- }
|
114
|
|
-
|
115
|
|
- return &user
|
|
39
|
+// AppendWxClient 增加微信实例
|
|
40
|
+func AppendWxClient(wx *component.WxClient) {
|
|
41
|
+ Component.AppendWxClient(wx)
|
116
|
42
|
}
|