蒋吉兆 3 år sedan
förälder
incheckning
904149700a

+ 7 - 0
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"))
 }

+ 0 - 25
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
 }

+ 34 - 7
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
+
 

+ 3 - 2
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 {

+ 1 - 1
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"` // 密钥
 }

+ 1 - 5
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{}

+ 5 - 1
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{}
 }
 

+ 2 - 2
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)
 	}
 }

+ 35 - 5
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)