|
@@ -2,11 +2,13 @@ package controllers
|
2
|
2
|
|
3
|
3
|
import (
|
4
|
4
|
"encoding/base64"
|
|
5
|
+ "encoding/json"
|
5
|
6
|
"io/ioutil"
|
6
|
7
|
"net/http"
|
7
|
8
|
"wechat-conf/models"
|
8
|
9
|
"wechat-conf/models/model"
|
9
|
10
|
"wechat-conf/service/autoreply"
|
|
11
|
+ "wechat-conf/service/wechat"
|
10
|
12
|
"wechat-conf/utils"
|
11
|
13
|
|
12
|
14
|
"github.com/kinisky564477/wechat/component"
|
|
@@ -17,7 +19,8 @@ import (
|
17
|
19
|
// WechatController 用户
|
18
|
20
|
type WechatController struct {
|
19
|
21
|
BaseController
|
20
|
|
- serv *autoreply.AutoreplyServ
|
|
22
|
+ serv *autoreply.AutoreplyServ
|
|
23
|
+ wechatServ *wechat.WechatServ
|
21
|
24
|
}
|
22
|
25
|
|
23
|
26
|
// Constructor 初始化 Controller
|
|
@@ -25,6 +28,7 @@ type WechatController struct {
|
25
|
28
|
// @Description 初始化 Controller, 系统自动调用
|
26
|
29
|
func (c *WechatController) Constructor() {
|
27
|
30
|
c.serv = autoreply.NewAutoreplyServ(c.Context)
|
|
31
|
+ c.wechatServ = wechat.NewWechatServ(c.Context)
|
28
|
32
|
}
|
29
|
33
|
|
30
|
34
|
const (
|
|
@@ -40,12 +44,7 @@ func (c *WechatController) ComponentPush() {
|
40
|
44
|
defer r.Body.Close()
|
41
|
45
|
con, _ := ioutil.ReadAll(r.Body)
|
42
|
46
|
|
43
|
|
- // timestamp := c.GetString("timestamp")
|
44
|
|
- // nonce := c.GetString("nonce")
|
45
|
|
-
|
46
|
|
- // token := "testtoken"
|
47
|
47
|
EncodingAESKey := "key"
|
48
|
|
- // appid := "appid"
|
49
|
48
|
|
50
|
49
|
AESKey, err := base64.StdEncoding.DecodeString(EncodingAESKey + "=")
|
51
|
50
|
if err != nil {
|
|
@@ -56,15 +55,18 @@ func (c *WechatController) ComponentPush() {
|
56
|
55
|
val, err := xp.Parse(string(con))
|
57
|
56
|
if err != nil {
|
58
|
57
|
utils.LogError("xml解析失败:", err)
|
|
58
|
+ c.ResponseRaw([]byte(""))
|
59
|
59
|
}
|
60
|
60
|
|
61
|
61
|
msgbyte, err := utils.AesDecrypt([]byte(val["Encrypt"]), AESKey)
|
62
|
62
|
if err != nil {
|
63
|
63
|
utils.LogError("解密失败:", err)
|
|
64
|
+ c.ResponseRaw([]byte(""))
|
64
|
65
|
}
|
65
|
66
|
msg, err := xp.Parse(string(msgbyte))
|
66
|
67
|
if err != nil {
|
67
|
68
|
utils.LogError("msgxml解析失败:", err)
|
|
69
|
+ c.ResponseRaw([]byte(""))
|
68
|
70
|
}
|
69
|
71
|
switch msg["InfoType"] {
|
70
|
72
|
case INFOTYPE_TICKET:
|
|
@@ -77,9 +79,20 @@ func (c *WechatController) ComponentPush() {
|
77
|
79
|
"appid": msg["AuthorizerAppid"],
|
78
|
80
|
"authorization_code": msg["AuthorizationCode"],
|
79
|
81
|
}
|
80
|
|
-
|
81
|
|
- wxclient := utils.WechatInit(cert)
|
|
82
|
+ wxclient := utils.WechatInit(cert, c.wechatServ.UpdateToken)
|
82
|
83
|
utils.AppendWxClient(wxclient)
|
|
84
|
+ // 插入数据库
|
|
85
|
+ mjson, _ := json.Marshal(msg)
|
|
86
|
+ var conf = model.SysWechatConf{
|
|
87
|
+ Appid: msg["AuthorizerAppid"],
|
|
88
|
+ AuthorizationCode: msg["AuthorizationCode"],
|
|
89
|
+ AuthorizationInfo: string(mjson),
|
|
90
|
+ }
|
|
91
|
+ err := c.wechatServ.SaveWechatConf(conf)
|
|
92
|
+ if err != nil {
|
|
93
|
+ utils.LogError("保存微信授权信息失败:", err)
|
|
94
|
+ c.ResponseRaw([]byte(""))
|
|
95
|
+ }
|
83
|
96
|
c.ResponseRaw([]byte("success"))
|
84
|
97
|
break
|
85
|
98
|
case INFOTYPE_UPDATEAUTHORIZED:
|