✨ Init
This commit is contained in:
75
plugin/email/README.MD
Normal file
75
plugin/email/README.MD
Normal file
@@ -0,0 +1,75 @@
|
||||
## GVA 邮件发送功能插件
|
||||
#### 开发者:GIN-VUE-ADMIN 官方
|
||||
|
||||
### 使用步骤
|
||||
|
||||
#### 1. 前往GVA主程序下的initialize/router.go 在Routers 方法最末尾按照你需要的及安全模式添加本插件
|
||||
例:
|
||||
本插件可以采用gva的配置文件 也可以直接写死内容作为配置 建议为gva添加配置文件结构 然后将配置传入
|
||||
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,
|
||||
))
|
||||
|
||||
同样也可以再传入时写死
|
||||
|
||||
PluginInit(PrivateGroup, email.CreateEmailPlug(
|
||||
"a@qq.com",
|
||||
"b@qq.com",
|
||||
"smtp.qq.com",
|
||||
"global.GVA_CONFIG.Email.Secret",
|
||||
"登录密钥",
|
||||
465,
|
||||
true,
|
||||
))
|
||||
|
||||
### 2. 配置说明
|
||||
|
||||
#### 2-1 全局配置结构体说明
|
||||
//其中 Form 和 Secret 通常来说就是用户名和密码
|
||||
|
||||
type Email struct {
|
||||
To string // 收件人:多个以英文逗号分隔 例:a@qq.com b@qq.com 正式开发中请把此项目作为参数使用 此处配置主要用于发送错误监控邮件
|
||||
From string // 发件人 你自己要发邮件的邮箱
|
||||
Host string // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
|
||||
Secret string // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
|
||||
Nickname string // 昵称 发件人昵称 自定义即可 可以不填
|
||||
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"` // 邮件内容
|
||||
}
|
||||
|
||||
|
||||
### 3. 方法API
|
||||
|
||||
utils.EmailTest(邮件标题,邮件主体) 发送测试邮件
|
||||
例:utils.EmailTest("测试邮件","测试邮件")
|
||||
utils.ErrorToEmail(邮件标题,邮件主体) 错误监控
|
||||
例: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"` // 邮件内容
|
||||
}
|
||||
|
7
plugin/email/api/enter.go
Normal file
7
plugin/email/api/enter.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package api
|
||||
|
||||
type ApiGroup struct {
|
||||
EmailApi
|
||||
}
|
||||
|
||||
var ApiGroupApp = new(ApiGroup)
|
53
plugin/email/api/sys_email.go
Normal file
53
plugin/email/api/sys_email.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"miniapp/global"
|
||||
"miniapp/model/common/response"
|
||||
email_response "miniapp/plugin/email/model/response"
|
||||
"miniapp/plugin/email/service"
|
||||
)
|
||||
|
||||
type EmailApi struct{}
|
||||
|
||||
// EmailTest
|
||||
// @Tags System
|
||||
// @Summary 发送测试邮件
|
||||
// @Security ApiKeyAuth
|
||||
// @Produce application/json
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"发送成功"}"
|
||||
// @Router /email/emailTest [post]
|
||||
func (s *EmailApi) EmailTest(c *gin.Context) {
|
||||
err := service.ServiceGroupApp.EmailTest()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("发送失败!", zap.Error(err))
|
||||
response.FailWithMessage("发送失败", c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage("发送成功", c)
|
||||
}
|
||||
|
||||
// SendEmail
|
||||
// @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
|
||||
err := c.ShouldBindJSON(&email)
|
||||
if err != nil {
|
||||
response.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
err = service.ServiceGroupApp.SendEmail(email.To, email.Subject, email.Body)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("发送失败!", zap.Error(err))
|
||||
response.FailWithMessage("发送失败", c)
|
||||
return
|
||||
}
|
||||
response.OkWithMessage("发送成功", c)
|
||||
}
|
11
plugin/email/config/email.go
Normal file
11
plugin/email/config/email.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
type Email struct {
|
||||
To string `mapstructure:"to" json:"to" yaml:"to"` // 收件人:多个以英文逗号分隔 例:a@qq.com b@qq.com 正式开发中请把此项目作为参数使用
|
||||
From string `mapstructure:"from" json:"from" yaml:"from"` // 发件人 你自己要发邮件的邮箱
|
||||
Host string `mapstructure:"host" json:"host" yaml:"host"` // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
|
||||
Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
|
||||
Nickname string `mapstructure:"nickname" json:"nickname" yaml:"nickname"` // 昵称 发件人昵称 通常为自己的邮箱
|
||||
Port int `mapstructure:"port" json:"port" yaml:"port"` // 端口 请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
|
||||
IsSSL bool `mapstructure:"is-ssl" json:"isSSL" yaml:"is-ssl"` // 是否SSL 是否开启SSL
|
||||
}
|
5
plugin/email/global/gloabl.go
Normal file
5
plugin/email/global/gloabl.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package global
|
||||
|
||||
import "miniapp/plugin/email/config"
|
||||
|
||||
var GlobalConfig = new(config.Email)
|
28
plugin/email/main.go
Normal file
28
plugin/email/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"miniapp/plugin/email/global"
|
||||
"miniapp/plugin/email/router"
|
||||
)
|
||||
|
||||
type emailPlugin struct{}
|
||||
|
||||
func CreateEmailPlug(To, From, Host, Secret, Nickname string, Port int, IsSSL bool) *emailPlugin {
|
||||
global.GlobalConfig.To = To
|
||||
global.GlobalConfig.From = From
|
||||
global.GlobalConfig.Host = Host
|
||||
global.GlobalConfig.Secret = Secret
|
||||
global.GlobalConfig.Nickname = Nickname
|
||||
global.GlobalConfig.Port = Port
|
||||
global.GlobalConfig.IsSSL = IsSSL
|
||||
return &emailPlugin{}
|
||||
}
|
||||
|
||||
func (*emailPlugin) Register(group *gin.RouterGroup) {
|
||||
router.RouterGroupApp.InitEmailRouter(group)
|
||||
}
|
||||
|
||||
func (*emailPlugin) RouterPath() string {
|
||||
return "email"
|
||||
}
|
7
plugin/email/model/response/email.go
Normal file
7
plugin/email/model/response/email.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package response
|
||||
|
||||
type Email struct {
|
||||
To string `json:"to"` // 邮件发送给谁
|
||||
Subject string `json:"subject"` // 邮件标题
|
||||
Body string `json:"body"` // 邮件内容
|
||||
}
|
7
plugin/email/router/enter.go
Normal file
7
plugin/email/router/enter.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package router
|
||||
|
||||
type RouterGroup struct {
|
||||
EmailRouter
|
||||
}
|
||||
|
||||
var RouterGroupApp = new(RouterGroup)
|
19
plugin/email/router/sys_email.go
Normal file
19
plugin/email/router/sys_email.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"miniapp/middleware"
|
||||
"miniapp/plugin/email/api"
|
||||
)
|
||||
|
||||
type EmailRouter struct{}
|
||||
|
||||
func (s *EmailRouter) InitEmailRouter(Router *gin.RouterGroup) {
|
||||
emailRouter := Router.Use(middleware.OperationRecord())
|
||||
EmailApi := api.ApiGroupApp.EmailApi.EmailTest
|
||||
SendEmail := api.ApiGroupApp.EmailApi.SendEmail
|
||||
{
|
||||
emailRouter.POST("emailTest", EmailApi) // 发送测试邮件
|
||||
emailRouter.POST("sendEmail", SendEmail) // 发送邮件
|
||||
}
|
||||
}
|
7
plugin/email/service/enter.go
Normal file
7
plugin/email/service/enter.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package service
|
||||
|
||||
type ServiceGroup struct {
|
||||
EmailService
|
||||
}
|
||||
|
||||
var ServiceGroupApp = new(ServiceGroup)
|
32
plugin/email/service/sys_email.go
Normal file
32
plugin/email/service/sys_email.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"miniapp/plugin/email/utils"
|
||||
)
|
||||
|
||||
type EmailService struct{}
|
||||
|
||||
//@author: [maplepie](https://github.com/maplepie)
|
||||
//@function: EmailTest
|
||||
//@description: 发送邮件测试
|
||||
//@return: err error
|
||||
|
||||
func (e *EmailService) EmailTest() (err error) {
|
||||
subject := "test"
|
||||
body := "test"
|
||||
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
|
||||
}
|
82
plugin/email/utils/email.go
Normal file
82
plugin/email/utils/email.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
|
||||
"miniapp/plugin/email/global"
|
||||
|
||||
"github.com/jordan-wright/email"
|
||||
)
|
||||
|
||||
//@author: [maplepie](https://github.com/maplepie)
|
||||
//@function: Email
|
||||
//@description: Email发送方法
|
||||
//@param: subject string, body string
|
||||
//@return: error
|
||||
|
||||
func Email(To, subject string, body string) error {
|
||||
to := strings.Split(To, ",")
|
||||
return send(to, subject, body)
|
||||
}
|
||||
|
||||
//@author: [SliverHorn](https://github.com/SliverHorn)
|
||||
//@function: ErrorToEmail
|
||||
//@description: 给email中间件错误发送邮件到指定邮箱
|
||||
//@param: subject string, body string
|
||||
//@return: error
|
||||
|
||||
func ErrorToEmail(subject string, body string) error {
|
||||
to := strings.Split(global.GlobalConfig.To, ",")
|
||||
if to[len(to)-1] == "" { // 判断切片的最后一个元素是否为空,为空则移除
|
||||
to = to[:len(to)-1]
|
||||
}
|
||||
return send(to, subject, body)
|
||||
}
|
||||
|
||||
//@author: [maplepie](https://github.com/maplepie)
|
||||
//@function: EmailTest
|
||||
//@description: Email测试方法
|
||||
//@param: subject string, body string
|
||||
//@return: error
|
||||
|
||||
func EmailTest(subject string, body string) error {
|
||||
to := []string{global.GlobalConfig.To}
|
||||
return send(to, subject, body)
|
||||
}
|
||||
|
||||
//@author: [maplepie](https://github.com/maplepie)
|
||||
//@function: send
|
||||
//@description: Email发送方法
|
||||
//@param: subject string, body string
|
||||
//@return: error
|
||||
|
||||
func send(to []string, subject string, body string) error {
|
||||
from := global.GlobalConfig.From
|
||||
nickname := global.GlobalConfig.Nickname
|
||||
secret := global.GlobalConfig.Secret
|
||||
host := global.GlobalConfig.Host
|
||||
port := global.GlobalConfig.Port
|
||||
isSSL := global.GlobalConfig.IsSSL
|
||||
|
||||
auth := smtp.PlainAuth("", from, secret, host)
|
||||
e := email.NewEmail()
|
||||
if nickname != "" {
|
||||
e.From = fmt.Sprintf("%s <%s>", nickname, from)
|
||||
} else {
|
||||
e.From = from
|
||||
}
|
||||
e.To = to
|
||||
e.Subject = subject
|
||||
e.HTML = []byte(body)
|
||||
var err error
|
||||
hostAddr := fmt.Sprintf("%s:%d", host, port)
|
||||
if isSSL {
|
||||
err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: host})
|
||||
} else {
|
||||
err = e.Send(hostAddr, auth)
|
||||
}
|
||||
return err
|
||||
}
|
53
plugin/plugin-tool/utils/check.go
Normal file
53
plugin/plugin-tool/utils/check.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"miniapp/global"
|
||||
"miniapp/model/system"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func RegisterApis(apis ...system.SysApi) {
|
||||
var count int64
|
||||
var apiPaths []string
|
||||
for i := range apis {
|
||||
apiPaths = append(apiPaths, apis[i].Path)
|
||||
}
|
||||
global.GVA_DB.Find(&[]system.SysApi{}, "path in (?)", apiPaths).Count(&count)
|
||||
if count > 0 {
|
||||
fmt.Println("插件已安装或存在同名路由")
|
||||
return
|
||||
}
|
||||
err := global.GVA_DB.Create(&apis).Error
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterMenus(menus ...system.SysBaseMenu) {
|
||||
var count int64
|
||||
var menuNames []string
|
||||
parentMenu := menus[0]
|
||||
otherMenus := menus[1:]
|
||||
for i := range menus {
|
||||
menuNames = append(menuNames, menus[i].Name)
|
||||
}
|
||||
global.GVA_DB.Find(&[]system.SysBaseMenu{}, "name in (?)", menuNames).Count(&count)
|
||||
if count > 0 {
|
||||
fmt.Println("插件已安装或存在同名菜单")
|
||||
return
|
||||
}
|
||||
parentMenu.ParentId = "0"
|
||||
err := global.GVA_DB.Create(&parentMenu).Error
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
for i := range otherMenus {
|
||||
pid := strconv.Itoa(int(parentMenu.ID))
|
||||
otherMenus[i].ParentId = pid
|
||||
}
|
||||
err = global.GVA_DB.Create(&otherMenus).Error
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
84
plugin/ws/ws.go
Normal file
84
plugin/ws/ws.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package ws
|
||||
|
||||
import (
|
||||
"github.com/flipped-aurora/ws/core/biz"
|
||||
"github.com/flipped-aurora/ws/core/data"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"nhooyr.io/websocket"
|
||||
)
|
||||
|
||||
type wsPlugin struct {
|
||||
logger *zap.Logger // 日志输出对象
|
||||
manageBuf int64 // buffer
|
||||
registeredMsgHandler map[int32]func(biz.IMessage) bool // 消息处理
|
||||
checkMap map[string]biz.CheckFunc // 用户校验
|
||||
|
||||
admin biz.IManage
|
||||
adminCase *biz.AdminCase
|
||||
}
|
||||
|
||||
func DefaultRegisteredMsgHandler(admin biz.IManage, logger *zap.Logger) map[int32]func(biz.IMessage) bool {
|
||||
return map[int32]func(msg biz.IMessage) bool{
|
||||
1: func(msg biz.IMessage) bool {
|
||||
// w.admin 里面找到注册客户端的方法
|
||||
client, ok := admin.FindClient(msg.GetTo())
|
||||
if !ok {
|
||||
logger.Info("没有找到该用户")
|
||||
return false
|
||||
}
|
||||
return client.SendMes(msg)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultCheckMap() map[string]biz.CheckFunc {
|
||||
return map[string]biz.CheckFunc{
|
||||
"gva_ws": func(c interface{}) (string, bool) {
|
||||
// 先断言是gin.content
|
||||
cc, ok := c.(*gin.Context)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
token := cc.Query("jwt")
|
||||
// 可以携带jwt
|
||||
if len(token) == 0 {
|
||||
return "", false
|
||||
}
|
||||
// 解析 jwt...
|
||||
|
||||
return token, true
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (w *wsPlugin) Register(g *gin.RouterGroup) {
|
||||
// gva_ws 为身份校验函数
|
||||
g.GET("/ws", w.adminCase.HandlerWS("gva_ws", &websocket.AcceptOptions{
|
||||
InsecureSkipVerify: true,
|
||||
}))
|
||||
g.POST("/sendMsg", w.adminCase.SendMsg("gva_ws"))
|
||||
}
|
||||
|
||||
func (w *wsPlugin) RouterPath() string {
|
||||
return "gva_ws"
|
||||
}
|
||||
|
||||
func GenerateWs(logger *zap.Logger, manageBuf int64, checkMap map[string]biz.CheckFunc) *wsPlugin {
|
||||
m := data.NewManage(manageBuf)
|
||||
t := data.NewTopic()
|
||||
h := data.NewHandle()
|
||||
admin := data.NewAdmin(m, t, h, logger)
|
||||
for s, checkFunc := range checkMap {
|
||||
admin.AddCheckFunc(s, checkFunc)
|
||||
}
|
||||
registeredMsgHandler := DefaultRegisteredMsgHandler(admin, logger)
|
||||
|
||||
for key, handler := range registeredMsgHandler {
|
||||
admin.RegisteredMsgHandler(key, handler)
|
||||
}
|
||||
return &wsPlugin{
|
||||
logger: logger, manageBuf: manageBuf,
|
||||
registeredMsgHandler: registeredMsgHandler, checkMap: checkMap, admin: admin, adminCase: biz.NewAdmin(admin),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user