feat(): notify dingding

This commit is contained in:
Espoir
2021-08-26 21:10:13 +08:00
parent 20876243ed
commit 5fe92dcc89
15 changed files with 266 additions and 16 deletions

View File

@@ -0,0 +1,14 @@
## GVA 钉钉群通知插件
### 使用步骤
#### 1.
config.yaml 文件中配置钉钉通知的URL Token 等
#### 2. 配置说明
### 方法API

View File

@@ -0,0 +1,21 @@
package api
import (
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
"github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/service"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type Api struct {
}
func (s *Api) NotifyController(c *gin.Context) {
if err := service.ServiceGroupApp.Send(); err != nil {
global.GVA_LOG.Error("发送失败!", zap.Any("err", err))
response.FailWithMessage("发送失败", c)
} else {
response.OkWithData("发送成功", c)
}
}

View File

@@ -0,0 +1,7 @@
package api
type ApiGroup struct {
Api
}
var ApiGroupApp = new(ApiGroup)

View File

@@ -0,0 +1,7 @@
package config
type DingDing struct {
Url string `mapstructure:"url" json:"url" yaml:"url"` // Url
Token string `mapstructure:"token" json:"token" yaml:"token"` // Token
Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥
}

View File

@@ -0,0 +1,5 @@
package global
import "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/config"
var GlobalConfig = new(config.DingDing)

View File

@@ -0,0 +1,28 @@
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"
)
type ddPlugin struct {
Secret string
Token string
Url string
}
func CreateDDPlug(Url string, Secret string, Token string) *ddPlugin {
global.GlobalConfig.Secret = Secret
global.GlobalConfig.Token = Token
global.GlobalConfig.Url = Url
return &ddPlugin{}
}
func (*ddPlugin) Register(group *gin.RouterGroup) {
router.RouterGroupApp.InitRouter(group)
}
func (*ddPlugin) RouterPath() string {
return "notify"
}

View File

@@ -0,0 +1,7 @@
package router
type RouterGroup struct {
NotifyRouter
}
var RouterGroupApp = new(RouterGroup)

View File

@@ -0,0 +1,18 @@
package router
import (
"github.com/flipped-aurora/gin-vue-admin/server/middleware"
"github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/api"
"github.com/gin-gonic/gin"
)
type NotifyRouter struct {
}
func (s *NotifyRouter) InitRouter(Router *gin.RouterGroup) {
emailRouter := Router.Use(middleware.OperationRecord())
var Controller = api.ApiGroupApp.Api.NotifyController
{
emailRouter.POST("dingding", Controller)
}
}

View File

@@ -0,0 +1,7 @@
package service
type ServiceGroup struct {
NotifyService
}
var ServiceGroupApp = new(ServiceGroup)

View File

@@ -0,0 +1,114 @@
package service
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/global"
"io/ioutil"
"net/http"
"net/url"
"time"
)
type NotifyService struct {
}
func SendTextMessage(content string) error {
msg := map[string]interface{}{
"msgtype": "text",
"text": map[string]string{
"content": content,
},
//"at": map[string]interface{}{
// "atMobiles": atMobiles,
// "isAtAll": isAtAll,
//},
}
return SendMessage(msg)
}
func SendMessage(msg interface{}) error {
body := bytes.NewBuffer(nil)
err := json.NewEncoder(body).Encode(msg)
if err != nil {
return fmt.Errorf("msg json failed, msg: %v, err: %v", msg, err.Error())
}
value := url.Values{}
value.Set("access_token", global.GlobalConfig.Token)
if global.GlobalConfig.Secret != "" {
t := time.Now().UnixNano() / 1e6
value.Set("timestamp", fmt.Sprintf("%d", t))
value.Set("sign", sign(t, global.GlobalConfig.Secret))
}
request, err := http.NewRequest(http.MethodPost, global.GlobalConfig.Url, body)
if err != nil {
return fmt.Errorf("error request: %v", err.Error())
}
request.URL.RawQuery = value.Encode()
request.Header.Add("Content-Type", "application/json")
res, err := (&http.Client{}).Do(request)
if err != nil {
return fmt.Errorf("send dingTalk message failed, error: %v", err.Error())
}
defer func() { _ = res.Body.Close() }()
result, err := ioutil.ReadAll(res.Body)
if res.StatusCode != 200 {
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, "http code is not 200"))
}
if err != nil {
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, err.Error()))
}
type response struct {
ErrCode int `json:"errcode"`
}
var ret response
if err := json.Unmarshal(result, &ret); err != nil {
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, err.Error()))
}
if ret.ErrCode != 0 {
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, "errcode is not 0"))
}
return nil
}
func httpError(request *http.Request, response *http.Response, body []byte, error string) string {
return fmt.Sprintf(
"http request failure, error: %s, status code: %d, %s %s, body:\n%s",
error,
response.StatusCode,
request.Method,
request.URL.String(),
string(body),
)
}
func sign(t int64, secret string) string {
strToHash := fmt.Sprintf("%d\n%s", t, secret)
hmac256 := hmac.New(sha256.New, []byte(secret))
hmac256.Write([]byte(strToHash))
data := hmac256.Sum(nil)
return base64.StdEncoding.EncodeToString(data)
}
//@author: [Espoir](https://github.com/nightsimon)
//@function: NotifyController
//@description: 钉钉通知测试
//@return: err error
func (e *NotifyService) Send() (err error) {
err = SendTextMessage("test")
if err != nil {
return err
}
return err
}

View File

@@ -0,0 +1 @@
package utils