提交 90414970 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

插件优化

上级 1ae17fb3
...@@ -2,6 +2,7 @@ package initialize ...@@ -2,6 +2,7 @@ package initialize
import ( import (
"github.com/flipped-aurora/gin-vue-admin/server/global" "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" // 在线仓库模式 //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/email" // 本地插件仓库地址模式
"github.com/flipped-aurora/gin-vue-admin/server/plugin/example_plugin" "github.com/flipped-aurora/gin-vue-admin/server/plugin/example_plugin"
...@@ -30,4 +31,10 @@ func InstallPlugin(PublicGroup *gin.RouterGroup, PrivateGroup *gin.RouterGroup) ...@@ -30,4 +31,10 @@ func InstallPlugin(PublicGroup *gin.RouterGroup, PrivateGroup *gin.RouterGroup)
global.GVA_CONFIG.Email.Port, global.GVA_CONFIG.Email.Port,
global.GVA_CONFIG.Email.IsSSL, global.GVA_CONFIG.Email.IsSSL,
)) ))
// 钉钉通知,暂时开放权限
PluginInit(PublicGroup, notify.CreateDDPlug(
"https://oapi.dingtalk.com/robot/send",
"8ded23f91917dc4f6275f44ba5ef243e6ed1d2cc74de83f01a6f5f5f39905671",
"SECaecf452bd6e671ab0d47469c3ad933e32fcc47b335333049a1b8961606192f38"))
} }
package initialize package initialize
import ( 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" "net/http"
_ "github.com/flipped-aurora/gin-vue-admin/server/docs" _ "github.com/flipped-aurora/gin-vue-admin/server/docs"
...@@ -64,27 +58,8 @@ func Routers() *gin.Engine { ...@@ -64,27 +58,8 @@ func Routers() *gin.Engine {
// Code generated by github.com/flipped-aurora/gin-vue-admin/server End; DO NOT EDIT. // 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) // 安装插件 InstallPlugin(PublicGroup, PrivateGroup) // 安装插件
global.GVA_LOG.Info("router register success") global.GVA_LOG.Info("router register success")
return Router return Router
} }
...@@ -14,18 +14,45 @@ ...@@ -14,18 +14,45 @@
`plugin/notify/global/global.go` 文件中配置钉钉通知的URL ,Token 等 `plugin/notify/global/global.go` 文件中配置钉钉通知的URL ,Token 等
```go ```go
var GlobalConfig_ = &config.DingDing{ // 在gin-vue-admin 主程序的initialize中的plugin的InstallPlugin 函数中写入如下代码
Url: "https://oapi.dingtalk.com/robot/send", PluginInit(PublicGroup, notify.CreateDDPlug(
Token: "xxx", URL,
Secret: "xxx", 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` 方法即可 在代码中调用 `SendTextMessage` 方法即可
```go ```go
// 示例
func NotifyController(c *gin.Context) { 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)) global.GVA_LOG.Error("发送失败!", zap.Any("err", err))
response.FailWithMessage("发送失败", c) response.FailWithMessage("发送失败", c)
} else { } else {
...@@ -34,5 +61,5 @@ func NotifyController(c *gin.Context) { ...@@ -34,5 +61,5 @@ func NotifyController(c *gin.Context) {
} }
``` ```
### 方法API
...@@ -11,8 +11,9 @@ import ( ...@@ -11,8 +11,9 @@ import (
type Api struct { 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)) global.GVA_LOG.Error("发送失败!", zap.Any("err", err))
response.FailWithMessage("发送失败", c) response.FailWithMessage("发送失败", c)
} else { } else {
......
...@@ -2,6 +2,6 @@ package config ...@@ -2,6 +2,6 @@ package config
type DingDing struct { type DingDing struct {
Url string `mapstructure:"url" json:"url" yaml:"url"` // Url 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"` // 密钥 Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥
} }
...@@ -2,8 +2,4 @@ package global ...@@ -2,8 +2,4 @@ package global
import "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/config" import "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/config"
var GlobalConfig_ = &config.DingDing{ var GlobalConfig_ = &config.DingDing{}
Url: "https://oapi.dingtalk.com/robot/send",
Token: "77d18293e221b3b7bdae7330d19a213c3cbb20fa07de5f4c273c0363ae475c34",
Secret: "SEC38425b7bd57c6bca9ce20611c41b437994ed65e0ddbd42e840ffa7a8c7da2106",
}
package notify package notify
import ( 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/flipped-aurora/gin-vue-admin/server/plugin/notify/router"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
...@@ -11,7 +12,10 @@ type ddPlugin struct { ...@@ -11,7 +12,10 @@ type ddPlugin struct {
Url string 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{} return &ddPlugin{}
} }
......
...@@ -11,8 +11,8 @@ type NotifyRouter struct { ...@@ -11,8 +11,8 @@ type NotifyRouter struct {
func (s *NotifyRouter) InitRouter(Router *gin.RouterGroup) { func (s *NotifyRouter) InitRouter(Router *gin.RouterGroup) {
router := Router.Use(middleware.OperationRecord()) 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)
} }
} }
...@@ -22,20 +22,50 @@ type NotifyService struct { ...@@ -22,20 +22,50 @@ type NotifyService struct {
//@description: 钉钉通知测试 //@description: 钉钉通知测试
//@return: err error //@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{}{ msg := map[string]interface{}{
"msgtype": "text", "msgtype": "text",
"text": map[string]string{ "text": map[string]string{
"content": content, "content": content,
}, },
//"at": map[string]interface{}{ "at": map[string]interface{}{
// "atMobiles": atMobiles, "atMobiles": atMobiles,
// "isAtAll": isAtAll, "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) 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 { func SendMessage(msg interface{}) error {
body := bytes.NewBuffer(nil) body := bytes.NewBuffer(nil)
err := json.NewEncoder(body).Encode(msg) err := json.NewEncoder(body).Encode(msg)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册