diff --git a/server/initialize/plugin.go b/server/initialize/plugin.go index 02f6f1b9ddc7d7c42593bfe95af8a7bc9057b195..00bbc54f8a00f41f5d34c0de8a251019cc41a4e3 100644 --- a/server/initialize/plugin.go +++ b/server/initialize/plugin.go @@ -2,6 +2,7 @@ package initialize import ( "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify" //email "github.com/flipped-aurora/gva-plug-email" // 在线仓库模式 "github.com/flipped-aurora/gin-vue-admin/server/plugin/email" // 本地插件仓库地址模式 "github.com/flipped-aurora/gin-vue-admin/server/plugin/example_plugin" @@ -30,4 +31,10 @@ func InstallPlugin(PublicGroup *gin.RouterGroup, PrivateGroup *gin.RouterGroup) global.GVA_CONFIG.Email.Port, global.GVA_CONFIG.Email.IsSSL, )) + + // 钉钉通知,暂时开放权限 + PluginInit(PublicGroup, notify.CreateDDPlug( + "https://oapi.dingtalk.com/robot/send", + "8ded23f91917dc4f6275f44ba5ef243e6ed1d2cc74de83f01a6f5f5f39905671", + "SECaecf452bd6e671ab0d47469c3ad933e32fcc47b335333049a1b8961606192f38")) } diff --git a/server/initialize/router.go b/server/initialize/router.go index 3b856420ff9e1d341dd813b39528f3ebcb69dd28..d04ced5b30fc254bc7801562250179461fde4a5e 100644 --- a/server/initialize/router.go +++ b/server/initialize/router.go @@ -1,12 +1,6 @@ package initialize import ( - - //email "github.com/flipped-aurora/gva-plug-email" // 在线仓库模式 - "github.com/flipped-aurora/gin-vue-admin/server/plugin/email" // 本地插件仓库地址模式 - "github.com/flipped-aurora/gin-vue-admin/server/plugin/example_plugin" - "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify" - "net/http" _ "github.com/flipped-aurora/gin-vue-admin/server/docs" @@ -64,27 +58,8 @@ func Routers() *gin.Engine { // Code generated by github.com/flipped-aurora/gin-vue-admin/server End; DO NOT EDIT. } - - // 添加开放权限的插件 示例 - PluginInit(PublicGroup, example_plugin.ExamplePlugin) - - // 钉钉通知,暂时开放权限 - PluginInit(PublicGroup, notify.CreateDDPlug()) - - // 添加跟角色挂钩权限的插件 示例 本地示例模式于在线仓库模式注意上方的import 可以自行切换 效果相同 - PluginInit(PrivateGroup, email.CreateEmailPlug( - global.GVA_CONFIG.Email.To, - global.GVA_CONFIG.Email.From, - global.GVA_CONFIG.Email.Host, - global.GVA_CONFIG.Email.Secret, - global.GVA_CONFIG.Email.Nickname, - global.GVA_CONFIG.Email.Port, - global.GVA_CONFIG.Email.IsSSL, - )) - InstallPlugin(PublicGroup, PrivateGroup) // 安装插件 - global.GVA_LOG.Info("router register success") return Router } diff --git a/server/plugin/notify/README.MD b/server/plugin/notify/README.MD index b0b5cc19bd5a6d390b3ac02aec6a0a7031b77a05..e2e28e73a1421a2c82bf278929512cd7e7fe1baf 100644 --- a/server/plugin/notify/README.MD +++ b/server/plugin/notify/README.MD @@ -14,18 +14,45 @@ 在`plugin/notify/global/global.go` 文件中配置钉钉通知的URL ,Token 等 ```go -var GlobalConfig_ = &config.DingDing{ - Url: "https://oapi.dingtalk.com/robot/send", - Token: "xxx", - Secret: "xxx", + // 在gin-vue-admin 主程序的initialize中的plugin的InstallPlugin 函数中写入如下代码 + PluginInit(PublicGroup, notify.CreateDDPlug( + URL, + Token, + 密钥)) } ``` -### 3. 使用说明 +### 3方法API +```go + +可调用方法 +//content 发送的内容 +//atMobiles 需要艾特的人的手机号 +//isAtAll 是否艾特全体 +SendTextMessage(content string,atMobiles []string,isAtAll bool) + +//content 发送的内容 +//title 内容标题 +//picUrl 配图 +//messageUrl 点击跳转路径 +//atMobiles 需要艾特的人的手机号 +//isAtAll 是否艾特全体 +SendLinkMessage(content,title,picUrl,messageUrl string) + +//content 发送的内容(markdown语法) +//title 内容标题 +//atMobiles 需要艾特的人的手机号 +//isAtAll 是否艾特全体 +SendMarkdownMessage(content,title string,atMobiles []string,isAtAll bool) + +``` + +### 4. 使用示例 在代码中调用 `SendTextMessage` 方法即可 ```go +// 示例 func NotifyController(c *gin.Context) { - if err := service.ServiceGroupApp.SendTextMessage("test"); err != nil { + if err := service.ServiceGroupApp.SendTextMessage("test",[]string{"手机号"},是否艾特全体); err != nil { global.GVA_LOG.Error("发送失败!", zap.Any("err", err)) response.FailWithMessage("发送失败", c) } else { @@ -34,5 +61,5 @@ func NotifyController(c *gin.Context) { } ``` -### 方法API + diff --git a/server/plugin/notify/api/api.go b/server/plugin/notify/api/api.go index 4874a814377aa8735d5b9dd7052e492de429c548..54782ea06f29985f842409168bae918f59bdddc6 100644 --- a/server/plugin/notify/api/api.go +++ b/server/plugin/notify/api/api.go @@ -11,8 +11,9 @@ import ( type Api struct { } -func (s *Api) NotifyController(c *gin.Context) { - if err := service.ServiceGroupApp.SendTextMessage("test"); err != nil { +// +func (s *Api) SendTextMessage(c *gin.Context) { + if err := service.ServiceGroupApp.SendTextMessage("notify", []string{}, false); err != nil { global.GVA_LOG.Error("发送失败!", zap.Any("err", err)) response.FailWithMessage("发送失败", c) } else { diff --git a/server/plugin/notify/config/dingding.go b/server/plugin/notify/config/dingding.go index 9cf0d24d64e85c46fe707c524f7f1434f960f7de..7ce67b1fc4d642f080f70e2525057f993acc69b7 100644 --- a/server/plugin/notify/config/dingding.go +++ b/server/plugin/notify/config/dingding.go @@ -2,6 +2,6 @@ package config type DingDing struct { Url string `mapstructure:"url" json:"url" yaml:"url"` // Url - Token string `mapstructure:"token" json:"token" yaml:"token"` // Token + Token string `mapstructure:"token" json:"token" yaml:"token"` // access_token Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥 } diff --git a/server/plugin/notify/global/global.go b/server/plugin/notify/global/global.go index 376f7ef806d1f87dbaf8293bc991bd674894d157..12e15ca4d6ecaf3e8aea881af0a0ae97a2a86b62 100644 --- a/server/plugin/notify/global/global.go +++ b/server/plugin/notify/global/global.go @@ -2,8 +2,4 @@ package global import "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/config" -var GlobalConfig_ = &config.DingDing{ - Url: "https://oapi.dingtalk.com/robot/send", - Token: "77d18293e221b3b7bdae7330d19a213c3cbb20fa07de5f4c273c0363ae475c34", - Secret: "SEC38425b7bd57c6bca9ce20611c41b437994ed65e0ddbd42e840ffa7a8c7da2106", -} +var GlobalConfig_ = &config.DingDing{} diff --git a/server/plugin/notify/main.go b/server/plugin/notify/main.go index 5813b6ce059154eb34255fb7ba54efa7e7c334d5..2c2d939151f438ac512a8cf7fc5ce02ffc017f8a 100644 --- a/server/plugin/notify/main.go +++ b/server/plugin/notify/main.go @@ -1,6 +1,7 @@ package notify import ( + "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/global" "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/router" "github.com/gin-gonic/gin" ) @@ -11,7 +12,10 @@ type ddPlugin struct { Url string } -func CreateDDPlug() *ddPlugin { +func CreateDDPlug(url, Token, Secret string) *ddPlugin { + global.GlobalConfig_.Url = url + global.GlobalConfig_.Token = Token + global.GlobalConfig_.Secret = Secret return &ddPlugin{} } diff --git a/server/plugin/notify/router/router.go b/server/plugin/notify/router/router.go index 61842b5ea7c26738895bbf763caa8535151d5779..b500d52c55ef5abd48a605f42dcf0e8155ea799f 100644 --- a/server/plugin/notify/router/router.go +++ b/server/plugin/notify/router/router.go @@ -11,8 +11,8 @@ type NotifyRouter struct { func (s *NotifyRouter) InitRouter(Router *gin.RouterGroup) { router := Router.Use(middleware.OperationRecord()) - var Controller = api.ApiGroupApp.Api.NotifyController + var SendTextMessage = api.ApiGroupApp.Api.SendTextMessage { - router.POST("dingding", Controller) + router.POST("sendTextMessage", SendTextMessage) } } diff --git a/server/plugin/notify/service/notify.go b/server/plugin/notify/service/notify.go index e21508d14d40e811257c545d073c01689f327510..07a2654085162cae17e5151cf78a8bc73357a9dc 100644 --- a/server/plugin/notify/service/notify.go +++ b/server/plugin/notify/service/notify.go @@ -22,20 +22,50 @@ type NotifyService struct { //@description: 钉钉通知测试 //@return: err error -func (e *NotifyService) SendTextMessage(content string) (err error) { +func (e *NotifyService) SendTextMessage(content string, atMobiles []string, isAtAll bool) (err error) { msg := map[string]interface{}{ "msgtype": "text", "text": map[string]string{ "content": content, }, - //"at": map[string]interface{}{ - // "atMobiles": atMobiles, - // "isAtAll": isAtAll, - //}, + "at": map[string]interface{}{ + "atMobiles": atMobiles, + "isAtAll": isAtAll, + }, + } + return SendMessage(msg) +} + +func (e *NotifyService) SendLinkMessage(content, title, picUrl, messageUrl string) (err error) { + msg := map[string]interface{}{ + "msgtype": "link", + "link": map[string]string{ + "text": content, + "title": title, + "picUrl": picUrl, + "messageUrl": messageUrl, + }, } return SendMessage(msg) } +func (e *NotifyService) SendMarkdownMessage(content, title string, atMobiles []string, isAtAll bool) (err error) { + msg := map[string]interface{}{ + "msgtype": "markdown", + "markdown": map[string]string{ + "text": content, + "title": title, + }, + "at": map[string]interface{}{ + "atMobiles": atMobiles, + "isAtAll": isAtAll, + }, + } + return SendMessage(msg) +} + +// 其余方法请参考 https://developers.dingtalk.com/document/robots/custom-robot-access?spm=ding_open_doc.document.0.0.7f8710afbfzduV#topic-2026027 + func SendMessage(msg interface{}) error { body := bytes.NewBuffer(nil) err := json.NewEncoder(body).Encode(msg)