Explorar o código

规范化插件代码

蒋吉兆 %!s(int64=3) %!d(string=hai) anos
pai
achega
7519cc9da9

+ 26 - 2
server/plugin/email/README.MD

@@ -1,4 +1,5 @@
 ## GVA 邮件发送功能插件
+#### 开发者:GIN-VUE-ADMIN 官方
 
 ### 使用步骤
 
@@ -27,8 +28,9 @@
     true,
     ))
 
-#### 2. 配置说明
+### 2. 配置说明
 
+#### 2-1 全局配置结构体说明
     //其中 Form 和 Secret 通常来说就是用户名和密码
 
     type Email struct {
@@ -40,8 +42,17 @@
 	    Port     int     // 端口     请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
 	    IsSSL    bool    // 是否SSL   是否开启SSL
     }
+#### 2-2 入参结构说明
+    //其中 Form 和 Secret 通常来说就是用户名和密码
+
+    type Email struct {
+        To      string `json:"to"`      // 邮件发送给谁
+        Subject string `json:"subject"` // 邮件标题
+        Body    string `json:"body"`    // 邮件内容
+    }
+
 
-### 方法API
+### 3. 方法API
 
     utils.EmailTest(邮件标题,邮件主体) 发送测试邮件
     例:utils.EmailTest("测试邮件","测试邮件")
@@ -49,3 +60,16 @@
     例:utils.ErrorToEmail("测试邮件","测试邮件")
     utils.Email(目标邮箱多个的话用逗号分隔,邮件标题,邮件主体) 发送测试邮件
     例:utils.Email(”a.qq.com,b.qq.com“,"测试邮件","测试邮件")
+
+### 4. 可直接调用的接口
+
+    测试接口: /email/emailTest [post] 已配置swagger
+
+    发送邮件接口接口: /email/emailSend [post] 已配置swagger
+    入参:
+    type Email struct {
+        To      string `json:"to"`      // 邮件发送给谁
+        Subject string `json:"subject"` // 邮件标题
+        Body    string `json:"body"`    // 邮件内容
+    }
+   

+ 19 - 0
server/plugin/email/api/sys_email.go

@@ -3,6 +3,7 @@ package api
 import (
 	"github.com/flipped-aurora/gin-vue-admin/server/global"
 	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
+	email_response "github.com/flipped-aurora/gin-vue-admin/server/plugin/email/model/response"
 	"github.com/flipped-aurora/gin-vue-admin/server/plugin/email/service"
 	"github.com/gin-gonic/gin"
 	"go.uber.org/zap"
@@ -25,3 +26,21 @@ func (s *EmailApi) EmailTest(c *gin.Context) {
 		response.OkWithData("发送成功", c)
 	}
 }
+
+// @Tags System
+// @Summary 发送邮件
+// @Security ApiKeyAuth
+// @Produce  application/json
+// @Param data body email_response.Email true "发送邮件必须的参数"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"发送成功"}"
+// @Router /email/sendEmail [post]
+func (s *EmailApi) SendEmail(c *gin.Context) {
+	var email email_response.Email
+	_ = c.ShouldBindJSON(&email)
+	if err := service.ServiceGroupApp.SendEmail(email.To, email.Subject, email.Body); err != nil {
+		global.GVA_LOG.Error("发送失败!", zap.Any("err", err))
+		response.FailWithMessage("发送失败", c)
+	} else {
+		response.OkWithData("发送成功", c)
+	}
+}

+ 7 - 0
server/plugin/email/model/response/email.go

@@ -0,0 +1,7 @@
+package response
+
+type Email struct {
+	To      string `json:"to"`      // 邮件发送给谁
+	Subject string `json:"subject"` // 邮件标题
+	Body    string `json:"body"`    // 邮件内容
+}

+ 3 - 1
server/plugin/email/router/sys_email.go

@@ -12,7 +12,9 @@ type EmailRouter struct {
 func (s *EmailRouter) InitEmailRouter(Router *gin.RouterGroup) {
 	emailRouter := Router.Use(middleware.OperationRecord())
 	var EmailApi = api.ApiGroupApp.EmailApi.EmailTest
+	var SendEmail = api.ApiGroupApp.EmailApi.SendEmail
 	{
-		emailRouter.POST("emailTest", EmailApi) // 发送测试邮件
+		emailRouter.POST("emailTest", EmailApi)  // 发送测试邮件
+		emailRouter.POST("sendEmail", SendEmail) // 发送邮件
 	}
 }

+ 13 - 0
server/plugin/email/service/sys_email.go

@@ -18,3 +18,16 @@ func (e *EmailService) EmailTest() (err error) {
 	err = utils.EmailTest(subject, body)
 	return err
 }
+
+//@author: [maplepie](https://github.com/maplepie)
+//@function: EmailTest
+//@description: 发送邮件测试
+//@return: err error
+//@params to string 	 收件人
+//@params subject string   标题(主题)
+//@params body  string 	 邮件内容
+
+func (e *EmailService) SendEmail(to, subject, body string) (err error) {
+	err = utils.Email(to, subject, body)
+	return err
+}

+ 26 - 17
server/plugin/notify/README.MD

@@ -21,10 +21,24 @@
         密钥))
 }
 ```
-### 3方法API
+
+### 3 参数说明
+#### 3-1 全局参数说明
+
+```go
+	Url    string `mapstructure:"url" json:"url" yaml:"url"`          // Url
+	Token  string `mapstructure:"token" json:"token" yaml:"token"`    // access_token
+	Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥
+```
+#### 3-2 请求入参说明
+```go
+
+
+```
+
+### 3方法API(可调用方法)
 ```go
 
-可调用方法
 //content 发送的内容
 //atMobiles 需要艾特的人的手机号 
 //isAtAll 是否艾特全体
@@ -34,8 +48,6 @@ SendTextMessage(content string,atMobiles []string,isAtAll bool)
 //title 内容标题
 //picUrl 配图
 //messageUrl 点击跳转路径
-//atMobiles 需要艾特的人的手机号 
-//isAtAll 是否艾特全体
 SendLinkMessage(content,title,picUrl,messageUrl string)
 
 //content 发送的内容(markdown语法)
@@ -46,20 +58,17 @@ SendMarkdownMessage(content,title string,atMobiles []string,isAtAll bool)
 
 ```
 
-### 4. 使用示例
+### 4. 可直接调用接口
 
-在代码中调用 `SendTextMessage` 方法即可
-```go
-// 示例
-func NotifyController(c *gin.Context) {
-    if err := service.ServiceGroupApp.SendTextMessage("test",[]string{"手机号"},是否艾特全体); err != nil {
-        global.GVA_LOG.Error("发送失败!", zap.Any("err", err))
-        response.FailWithMessage("发送失败", c)
-    } else {
-        response.OkWithData("发送成功", c)
-    }
-}
+    发送文字消息接口: /notify/sendTextMessage [post] 已配置swagger
+    发送图文链接消息接口: /notify/sendLinkMessage [post] 已配置swagger
+    发送markdown消息接口: /notify/sendMarkdownMessage [post] 已配置swagger
 
-```
+    入参:
+    type Email struct {
+        To      string `json:"to"`      // 邮件发送给谁
+        Subject string `json:"subject"` // 邮件标题
+        Body    string `json:"body"`    // 邮件内容
+    }
 
 

+ 47 - 2
server/plugin/notify/api/api.go

@@ -3,6 +3,7 @@ package api
 import (
 	"github.com/flipped-aurora/gin-vue-admin/server/global"
 	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
+	notify_response "github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/model/response"
 	"github.com/flipped-aurora/gin-vue-admin/server/plugin/notify/service"
 	"github.com/gin-gonic/gin"
 	"go.uber.org/zap"
@@ -11,9 +12,53 @@ import (
 type Api struct {
 }
 
-//
+// @Tags Notify
+// @Summary 发送文字消息接口
+// @Security ApiKeyAuth
+// @Produce  application/json
+// @Param data body notify_response.TextNotify true "发送文字消息的参数"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"发送成功"}"
+// @Router /notify/sendTextMessage [post]
 func (s *Api) SendTextMessage(c *gin.Context) {
-	if err := service.ServiceGroupApp.SendTextMessage("notify", []string{}, false); err != nil {
+	var textNotify notify_response.TextNotify
+	_ = c.ShouldBindJSON(&textNotify)
+	if err := service.ServiceGroupApp.SendTextMessage(textNotify.Content, textNotify.AtMobiles, textNotify.IsAtAll); err != nil {
+		global.GVA_LOG.Error("发送失败!", zap.Any("err", err))
+		response.FailWithMessage("发送失败", c)
+	} else {
+		response.OkWithData("发送成功", c)
+	}
+}
+
+// @Tags Notify
+// @Summary 发送图文链接消息接口
+// @Security ApiKeyAuth
+// @Produce  application/json
+// @Param data body notify_response.LinkNotify true "发送图文链接消息的参数"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"发送成功"}"
+// @Router /notify/sendLinkMessage [post]
+func (s *Api) SendLinkMessage(c *gin.Context) {
+	var linkNotify notify_response.LinkNotify
+	_ = c.ShouldBindJSON(&linkNotify)
+	if err := service.ServiceGroupApp.SendLinkMessage(linkNotify.Content, linkNotify.Title, linkNotify.PicUrl, linkNotify.MessageUrl); err != nil {
+		global.GVA_LOG.Error("发送失败!", zap.Any("err", err))
+		response.FailWithMessage("发送失败", c)
+	} else {
+		response.OkWithData("发送成功", c)
+	}
+}
+
+// @Tags Notify
+// @Summary 发送markdown消息接口
+// @Security ApiKeyAuth
+// @Produce  application/json
+// @Param data body notify_response.MarkdownNotify true "发送markdown消息的参数"
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"发送成功"}"
+// @Router /notify/sendMarkdownMessage [post]
+func (s *Api) SendMarkdownMessage(c *gin.Context) {
+	var markdownNotify notify_response.MarkdownNotify
+	_ = c.ShouldBindJSON(&markdownNotify)
+	if err := service.ServiceGroupApp.SendMarkdownMessage(markdownNotify.Content, markdownNotify.Title, markdownNotify.AtMobiles, markdownNotify.IsAtAll); err != nil {
 		global.GVA_LOG.Error("发送失败!", zap.Any("err", err))
 		response.FailWithMessage("发送失败", c)
 	} else {

+ 21 - 0
server/plugin/notify/model/response/notify.go

@@ -0,0 +1,21 @@
+package response
+
+type TextNotify struct { // 文字信息
+	Content   string   `json:"content"`   // 发送的内容
+	AtMobiles []string `json:"atMobiles"` // 需要艾特的人的手机号
+	IsAtAll   bool     `json:"isAtAll"`   // 是否艾特全体
+}
+
+type LinkNotify struct { // 图文链接信息
+	Content    string `json:"content"`    // 发送的内容
+	Title      string `json:"title"`      // 内容标题
+	PicUrl     string `json:"picUrl"`     // 配图
+	MessageUrl string `json:"messageUrl"` // 点击跳转路径
+}
+
+type MarkdownNotify struct { // markdown信息
+	Title     string   `json:"title"`     // 内容标题
+	Content   string   `json:"content"`   // 发送的内容
+	AtMobiles []string `json:"atMobiles"` // 需要艾特的人的手机号
+	IsAtAll   bool     `json:"isAtAll"`   // 是否艾特全体
+}

+ 25 - 4
server/plugin/notify/service/notify.go

@@ -18,8 +18,11 @@ type NotifyService struct {
 }
 
 //@author: [Espoir](https://github.com/nightsimon)
-//@function: NotifyController
-//@description: 钉钉通知测试
+//@function: SendTextMessage
+//@description: 发送钉钉文字信息
+//@params content string发送的文字内容
+//@params atMobiles []string 艾特的手机号
+//@params isAtAll bool 是否艾特全体
 //@return: err error
 
 func (e *NotifyService) SendTextMessage(content string, atMobiles []string, isAtAll bool) (err error) {
@@ -36,6 +39,15 @@ func (e *NotifyService) SendTextMessage(content string, atMobiles []string, isAt
 	return SendMessage(msg)
 }
 
+//@author: [Espoir](https://github.com/nightsimon)
+//@function: SendLinkMessage
+//@description: 发送钉钉图文链接信息
+//@params content string 发送的文字内容
+//@params title string 发送的标题
+//@params picUrl string 艾特的手机号
+//@params messageUrl string 是否艾特全体
+//@return: err error
+
 func (e *NotifyService) SendLinkMessage(content, title, picUrl, messageUrl string) (err error) {
 	msg := map[string]interface{}{
 		"msgtype": "link",
@@ -49,6 +61,15 @@ func (e *NotifyService) SendLinkMessage(content, title, picUrl, messageUrl strin
 	return SendMessage(msg)
 }
 
+//@author: [Espoir](https://github.com/nightsimon)
+//@function: SendMarkdownMessage
+//@description: 发送钉钉Markdown信息
+//@params content 发送的文字内容
+//@params title 发送的标题
+//@params atMobiles []string 艾特的手机号
+//@params isAtAll bool 是否艾特全体
+//@return: err error
+
 func (e *NotifyService) SendMarkdownMessage(content, title string, atMobiles []string, isAtAll bool) (err error) {
 	msg := map[string]interface{}{
 		"msgtype": "markdown",
@@ -64,8 +85,6 @@ func (e *NotifyService) SendMarkdownMessage(content, title string, atMobiles []s
 	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)
@@ -134,3 +153,5 @@ func sign(t int64, secret string) string {
 	data := hmac256.Sum(nil)
 	return base64.StdEncoding.EncodeToString(data)
 }
+
+//	其余方法请参考 https://developers.dingtalk.com/document/robots/custom-robot-access?spm=ding_open_doc.document.0.0.7f8710afbfzduV#topic-2026027