Skip to content

Commit 0bbc230

Browse files
开发微信报警API
1 parent 50e654d commit 0bbc230

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

common/weixin.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package common
2+
3+
import (
4+
"github.com/astaxie/beego"
5+
weixin "github.com/chanyipiaomiao/weixin-kit"
6+
)
7+
8+
var (
9+
corpID = beego.AppConfig.String("weixin::corpID")
10+
appSecret = beego.AppConfig.String("weixin::appSecret")
11+
accessTokenAPI = beego.AppConfig.String("weixin::accessTokenAPI")
12+
sendMessageAPIURL = beego.AppConfig.String("weixin::sendMessageAPIURL")
13+
)
14+
15+
// SendWeixinMessage 发送消息
16+
func SendWeixinMessage(msgType, text, toTag, toUser, toParty string, agentIDFromReq int64) (bool, error) {
17+
var agentID int64
18+
if agentIDFromReq == 0 {
19+
agentIDFromConf, err := beego.AppConfig.Int64("weixin::agentID")
20+
if err != nil {
21+
return false, err
22+
}
23+
agentID = agentIDFromConf
24+
} else {
25+
agentID = agentIDFromReq
26+
}
27+
28+
message := &weixin.Message{
29+
MsgType: msgType, // 目前只支持发送文本消息
30+
ToTag: toTag, // ToTag 是在企业微信后台定义的标签ID,标签里面可以包含很多人,多个请用|分开
31+
ToUser: toUser, // ToUser 是企业微信后台看到的用户的ID,多个请用|分开
32+
ToParty: toParty, // ToParty 是企业微信后台看到的部门的ID,多个请用|分开
33+
AgentID: agentID, // 企业应用的id,整型。可在应用的设置页面查看
34+
Safe: 0, // 表示是否是保密消息,0表示否,1表示是,默认0
35+
Text: &weixin.Text{
36+
Content: text,
37+
},
38+
}
39+
40+
client := &weixin.Client{
41+
AccessTokenAPI: accessTokenAPI,
42+
APIURL: sendMessageAPIURL,
43+
CorpID: corpID,
44+
CorpSecret: appSecret,
45+
Message: message,
46+
}
47+
_, err := client.SendMessage()
48+
if err != nil {
49+
return false, err
50+
}
51+
return true, nil
52+
}

conf/app.conf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,23 @@ jwtokenSignString = TshTGv9QHZbGLT7nqmqfUavjazrArcN8
7878
# 生成的二维码图片目录
7979
qrImageDir = static/download/qr
8080

81+
[weixin]
82+
# 微信相关配置
83+
84+
# 获取accessTokenAPI
85+
accessTokenAPI = https://qyapi.weixin.qq.com/cgi-bin/gettoken
86+
87+
# 发送消息API
88+
sendMessageAPIURL = https://qyapi.weixin.qq.com/cgi-bin/message/send
89+
90+
# corpID 公司的ID
91+
corpID = wwf6f647f2c4fe1f62
92+
93+
# 应用的密钥
94+
appSecret = SQEP1EWJXtdcBijE9898V5GGvos13L0fYJ1rFofXnGo
95+
96+
# 报警应用的ID
97+
agentID = 1000002
98+
8199
include "dev.conf"
82100
include "prod.conf"

controllers/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,8 @@ type TwoStepAuthController struct {
119119
type StorePasswordController struct {
120120
BaseController
121121
}
122+
123+
// WeixinController 发送微信消息管理
124+
type WeixinController struct {
125+
BaseController
126+
}

controllers/weixin.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package controllers
2+
3+
import (
4+
"devops-api/common"
5+
"fmt"
6+
)
7+
8+
// SendMessage 发送消息
9+
func (w *WeixinController) SendMessage() {
10+
msgType := w.GetString("msgType")
11+
toTag := w.GetString("toTag")
12+
toUser := w.GetString("toUser")
13+
toParty := w.GetString("toParty")
14+
text := w.GetString("text")
15+
agentID, err := w.GetInt64("agentID")
16+
if err != nil {
17+
agentID = 0
18+
}
19+
20+
requestID := w.Data["RequestID"].(string)
21+
sendWeixinMessageLog := map[string]interface{}{
22+
"entryType": "SendWeixinMessage",
23+
"requestId": requestID,
24+
}
25+
_, err = common.SendWeixinMessage(msgType, text, toTag, toUser, toParty, agentID)
26+
if err != nil {
27+
sendWeixinMessageLog["statuscode"] = 1
28+
sendWeixinMessageLog["errmsg"] = fmt.Sprintf("%s", err)
29+
common.GetLogger().Error(sendWeixinMessageLog, "发送微信消息")
30+
w.Data["json"] = sendWeixinMessageLog
31+
w.ServeJSON()
32+
return
33+
}
34+
sendWeixinMessageLog["statuscode"] = 0
35+
sendWeixinMessageLog["errmsg"] = ""
36+
sendWeixinMessageLog["result"] = "发送成功"
37+
common.GetLogger().Info(sendWeixinMessageLog, text)
38+
w.Data["json"] = sendWeixinMessageLog
39+
w.ServeJSON()
40+
return
41+
}

0 commit comments

Comments
 (0)