|
@@ -3,11 +3,30 @@ package controllers
|
3
|
3
|
import (
|
4
|
4
|
"encoding/base64"
|
5
|
5
|
"io/ioutil"
|
|
6
|
+ "net/http"
|
|
7
|
+ "wechat-conf/models"
|
|
8
|
+ "wechat-conf/models/model"
|
|
9
|
+ "wechat-conf/service/autoreply"
|
6
|
10
|
"wechat-conf/utils"
|
7
|
11
|
|
|
12
|
+ "github.com/kinisky564477/wechat/component"
|
|
13
|
+
|
8
|
14
|
"github.com/zjxpcyc/wechat/core"
|
9
|
15
|
)
|
10
|
16
|
|
|
17
|
+// WechatController 用户
|
|
18
|
+type WechatController struct {
|
|
19
|
+ BaseController
|
|
20
|
+ serv *autoreply.AutoreplyServ
|
|
21
|
+}
|
|
22
|
+
|
|
23
|
+// Constructor 初始化 Controller
|
|
24
|
+// @Title Constructor
|
|
25
|
+// @Description 初始化 Controller, 系统自动调用
|
|
26
|
+func (c *WechatController) Constructor() {
|
|
27
|
+ c.serv = autoreply.NewAutoreplyServ(c.Context)
|
|
28
|
+}
|
|
29
|
+
|
11
|
30
|
const (
|
12
|
31
|
INFOTYPE_TICKET = "component_verify_ticket"
|
13
|
32
|
INFOTYPE_AUTHORIZED = "authorized"
|
|
@@ -16,7 +35,7 @@ const (
|
16
|
35
|
)
|
17
|
36
|
|
18
|
37
|
// ComponentPush 第三方平台推送
|
19
|
|
-func (c *BaseController) ComponentPush() {
|
|
38
|
+func (c *WechatController) ComponentPush() {
|
20
|
39
|
r := c.Ctx.Request
|
21
|
40
|
defer r.Body.Close()
|
22
|
41
|
con, _ := ioutil.ReadAll(r.Body)
|
|
@@ -70,5 +89,75 @@ func (c *BaseController) ComponentPush() {
|
70
|
89
|
// 取消授权
|
71
|
90
|
break
|
72
|
91
|
}
|
|
92
|
+}
|
|
93
|
+
|
|
94
|
+// WechatInfo 微信接入
|
|
95
|
+func (c *WechatController) WechatInfo() {
|
|
96
|
+ method := c.Ctx.Input.Method()
|
|
97
|
+ if method == http.MethodGet {
|
|
98
|
+ echostr := c.GetString("echostr")
|
|
99
|
+ c.ResponseRaw([]byte(echostr))
|
|
100
|
+ } else {
|
|
101
|
+ c.WxReceive()
|
|
102
|
+ }
|
|
103
|
+}
|
|
104
|
+
|
|
105
|
+// WxReceive 微信消息接收
|
|
106
|
+func (c *WechatController) WxReceive() {
|
|
107
|
+ appid := c.GetString(":appid")
|
|
108
|
+ wechat, err := utils.Component.GetWxClient(appid)
|
|
109
|
+ if err != nil {
|
|
110
|
+ utils.LogError("获取微信失败: " + err.Error())
|
|
111
|
+ }
|
|
112
|
+ val, err := wechat.TransformMessage(string(c.Ctx.Input.RequestBody))
|
|
113
|
+ if err != nil {
|
|
114
|
+ utils.LogError("读取微信服务发送内容失败: " + err.Error())
|
|
115
|
+ c.ResponseRaw([]byte(""))
|
|
116
|
+ }
|
73
|
117
|
|
|
118
|
+ var replay = new(model.TaAutoReply)
|
|
119
|
+ switch val["MsgType"] {
|
|
120
|
+ case "text":
|
|
121
|
+ content := val["Content"]
|
|
122
|
+ // 获取数据库存储返回类型
|
|
123
|
+ replay, err = c.serv.GetAutoReplayByAppID(appid, content)
|
|
124
|
+ break
|
|
125
|
+ case "event":
|
|
126
|
+ if val["Event"] == "subscribe" {
|
|
127
|
+ replay, err = c.serv.GetSubscribeByAppID(appid)
|
|
128
|
+ }
|
|
129
|
+ break
|
|
130
|
+ }
|
|
131
|
+ if err != nil {
|
|
132
|
+ utils.LogError("获取微信自动回复信息失败: " + err.Error())
|
|
133
|
+ c.ResponseRaw([]byte(""))
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+ if replay == nil {
|
|
137
|
+ c.ResponseRaw([]byte(""))
|
|
138
|
+ }
|
|
139
|
+ openID := val["FromUserName"]
|
|
140
|
+ message, err := c.getReplayMessage(replay, openID, wechat)
|
|
141
|
+ if err != nil {
|
|
142
|
+ utils.LogError("转换回复信息失败: " + err.Error())
|
|
143
|
+ c.ResponseRaw([]byte(""))
|
|
144
|
+ }
|
|
145
|
+ c.ResponseRaw(message, false)
|
|
146
|
+ c.DestroyContext()
|
|
147
|
+ c.StopRun()
|
|
148
|
+}
|
|
149
|
+
|
|
150
|
+func (c *WechatController) getReplayMessage(replay *model.TaAutoReply, openid string, wx *component.WxClient) ([]byte, error) {
|
|
151
|
+ switch replay.MessageType {
|
|
152
|
+ case models.MESSAGE_TYPE_CONTENT:
|
|
153
|
+ msg, err := wx.ResponseMessageText(openid, replay.MessageParagraph)
|
|
154
|
+ return msg, err
|
|
155
|
+ case models.MESSAGE_TYPE_IMG:
|
|
156
|
+ msg, err := wx.ResponseMessageImage(openid, replay.MessageImg)
|
|
157
|
+ return msg, err
|
|
158
|
+ // case models.MESSAGE_TYPE_PARAGRAPH:
|
|
159
|
+ // msg, err := wx.ResponseMessageNews(openid, replay.MessageImg)
|
|
160
|
+ // return msg, err
|
|
161
|
+ }
|
|
162
|
+ return nil, nil
|
74
|
163
|
}
|