-
Notifications
You must be signed in to change notification settings - Fork 38
/
msg_controller.go
95 lines (80 loc) · 2.42 KB
/
msg_controller.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
package wechat
import (
"github.com/silenceper/wechat/v2/officialaccount/message"
"github.com/xiusin/pinecms/src/application/controllers/backend"
"github.com/xiusin/pinecms/src/application/models/tables"
"github.com/xiusin/pinecms/src/common/helper"
"strings"
"time"
"xorm.io/xorm"
)
type WechatMagController struct {
backend.BaseController
}
func (c *WechatMagController) Construct() {
c.Table = &tables.WechatLog{}
c.Entries = &[]tables.WechatLog{}
c.BaseController.Construct()
c.OpBefore = c.before
c.OpAfter = c.after
}
func (c *WechatMagController) PostReply() {
var inputs = replyMsg{}
if err := c.Ctx().BindJSON(&inputs); err != nil {
helper.Ajax(err, 1, c.Ctx())
return
} else if len(inputs.AppId) == 0 || len(inputs.OpenId) == 0 {
helper.Ajax("消息参数错误", 1, c.Ctx())
return
}
account, _ := GetOfficialAccount(inputs.AppId)
msg := &message.CustomerMessage{
ToUser: inputs.OpenId,
Msgtype: message.MsgType(inputs.ReplyType),
Text: &message.MediaText{Content: inputs.ReplyContent},
}
if err := account.GetCustomerMessageManager().Send(msg); err != nil {
helper.Ajax(err, 1, c.Ctx())
return
}
//回复消息:演示回复用户发送的消息
c.Orm.InsertOne(&tables.WechatLog{
AppId: inputs.AppId,
OpenId: inputs.OpenId,
Inout: 1,
MsgType: inputs.ReplyType,
Detail: &message.MixMessage{Content: inputs.ReplyContent},
CreatedAt: tables.LocalTime(time.Now()),
})
helper.Ajax("回复成功", 0, c.Ctx())
}
func (c *WechatMagController) before(act int, params interface{}) error {
if act == backend.OpList {
sess := params.(*xorm.Session)
if msgType, ok := c.P.Param["msg_type"]; ok && len(msgType.(string)) > 0 {
types := strings.Split(msgType.(string), ",")
sess.In("msg_type", types)
}
if timeZone, ok := c.P.Param["time_zone"]; ok && len(timeZone.(string)) > 0 {
sess.Where("created_at >= ?", timeZone)
}
}
return nil
}
func (c *WechatMagController) after(act int, params interface{}) error {
if act == backend.OpList {
lists := c.Entries.(*[]tables.WechatLog)
openIds := backend.ArrayCol(*lists, "OpenId")
var fans []tables.WechatMember
c.Orm.In("openid", openIds).Cols("openid", "nickname", "headimgurl").Find(&fans)
fansMap := backend.ArrayColMap(fans, "Openid")
for i, log := range *lists {
member, ok := fansMap[log.OpenId].(tables.WechatMember)
if ok {
log.FansInfo = &member
}
(*lists)[i] = log
}
}
return nil
}