Skip to content

Commit 3863460

Browse files
author
蒋吉兆
committed
email提为插件模式
1 parent 5a03870 commit 3863460

File tree

21 files changed

+223
-75
lines changed

21 files changed

+223
-75
lines changed

server/api/v1/system/enter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ var autoCodeService = service.ServiceGroupApp.SystemServiceGroup.AutoCodeService
2525
var autoCodeHistoryService = service.ServiceGroupApp.SystemServiceGroup.AutoCodeHistoryService
2626
var dictionaryService = service.ServiceGroupApp.SystemServiceGroup.DictionaryService
2727
var dictionaryDetailService = service.ServiceGroupApp.SystemServiceGroup.DictionaryDetailService
28-
var emailService = service.ServiceGroupApp.SystemServiceGroup.EmailService
2928
var initDBService = service.ServiceGroupApp.SystemServiceGroup.InitDBService
3029
var jwtService = service.ServiceGroupApp.SystemServiceGroup.JwtService
3130
var baseMenuService = service.ServiceGroupApp.SystemServiceGroup.BaseMenuService

server/core/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func RunWindowsServer() {
1919
initialize.Redis()
2020
}
2121
Router := initialize.Routers()
22+
2223
Router.Static("/form-generator", "./resource/page")
2324

2425
address := fmt.Sprintf(":%d", global.GVA_CONFIG.System.Addr)

server/initialize/plugin.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package initialize
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/utils/plugin"
5+
"github.com/gin-gonic/gin"
6+
)
7+
8+
func PluginInit(group *gin.RouterGroup, Plugin ...plugin.Plugin) {
9+
for i := range Plugin {
10+
PluginGroup := group.Group(Plugin[i].RouterPath())
11+
Plugin[i].Register(PluginGroup)
12+
}
13+
}

server/initialize/router.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package initialize
22

33
import (
4+
"github.com/flipped-aurora/gin-vue-admin/plugin/email"
5+
"github.com/flipped-aurora/gin-vue-admin/plugin/example_plugin"
46
"net/http"
57

68
_ "github.com/flipped-aurora/gin-vue-admin/docs"
79
"github.com/flipped-aurora/gin-vue-admin/global"
810
"github.com/flipped-aurora/gin-vue-admin/middleware"
911
"github.com/flipped-aurora/gin-vue-admin/router"
10-
"github.com/flipped-aurora/gin-vue-admin/router/example"
11-
1212
"github.com/gin-gonic/gin"
1313
"github.com/swaggo/gin-swagger"
1414
"github.com/swaggo/gin-swagger/swaggerFiles"
@@ -37,17 +37,13 @@ func Routers() *gin.Engine {
3737
systemRouter.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
3838
systemRouter.InitInitRouter(PublicGroup) // 自动初始化相关
3939
}
40-
41-
example.PluginInit(PublicGroup)
42-
4340
PrivateGroup := Router.Group("")
4441
PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
4542
{
4643
systemRouter.InitApiRouter(PrivateGroup) // 注册功能api路由
4744
systemRouter.InitJwtRouter(PrivateGroup) // jwt相关路由
4845
systemRouter.InitUserRouter(PrivateGroup) // 注册用户路由
4946
systemRouter.InitMenuRouter(PrivateGroup) // 注册menu路由
50-
systemRouter.InitEmailRouter(PrivateGroup) // 邮件相关路由
5147
systemRouter.InitSystemRouter(PrivateGroup) // system相关路由
5248
systemRouter.InitCasbinRouter(PrivateGroup) // 权限相关路由
5349
systemRouter.InitAutoCodeRouter(PrivateGroup) // 创建自动化代码
@@ -64,6 +60,21 @@ func Routers() *gin.Engine {
6460
autocodeRouter.InitSysAutoCodeExampleRouter(PrivateGroup)
6561
// Code generated by github.com/flipped-aurora/gin-vue-admin End; DO NOT EDIT.
6662
}
63+
64+
// 添加开放权限的插件 示例
65+
PluginInit(PublicGroup, example_plugin.ExamplePlugin)
66+
67+
// 添加跟角色挂钩权限的插件 示例
68+
PluginInit(PrivateGroup, email.CreateEmailPlug(
69+
global.GVA_CONFIG.Email.To,
70+
global.GVA_CONFIG.Email.From,
71+
global.GVA_CONFIG.Email.Host,
72+
global.GVA_CONFIG.Email.Secret,
73+
global.GVA_CONFIG.Email.Nickname,
74+
global.GVA_CONFIG.Email.Port,
75+
global.GVA_CONFIG.Email.IsSSL,
76+
))
77+
6778
global.GVA_LOG.Info("router register success")
6879
return Router
6980
}

server/middleware/email.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package middleware
22

33
import (
4+
"github.com/flipped-aurora/gin-vue-admin/plugin/email/utils"
45
"io/ioutil"
56
"strconv"
67
"time"
@@ -9,7 +10,6 @@ import (
910
"github.com/flipped-aurora/gin-vue-admin/model/system"
1011
"github.com/flipped-aurora/gin-vue-admin/model/system/request"
1112
"github.com/flipped-aurora/gin-vue-admin/service"
12-
"github.com/flipped-aurora/gin-vue-admin/utils"
1313
"github.com/gin-gonic/gin"
1414
"go.uber.org/zap"
1515
)

server/plugin/email/README.MD

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## GVA 邮件发送功能插件
2+
3+
### 使用步骤
4+
5+
#### 1. 前往GVA主程序下的initialize/router.go 在Routers 方法最末尾按照你需要的及安全模式添加本插件
6+
例:
7+
本插件可以采用gva的配置文件 也可以直接写死内容作为配置 建议为gva添加配置文件结构 然后将配置传入
8+
PluginInit(PrivateGroup, email.CreateEmailPlug(
9+
global.GVA_CONFIG.Email.To,
10+
global.GVA_CONFIG.Email.From,
11+
global.GVA_CONFIG.Email.Host,
12+
global.GVA_CONFIG.Email.Secret,
13+
global.GVA_CONFIG.Email.Nickname,
14+
global.GVA_CONFIG.Email.Port,
15+
global.GVA_CONFIG.Email.IsSSL,
16+
))
17+
18+
同样也可以再传入时写死
19+
20+
PluginInit(PrivateGroup, email.CreateEmailPlug(
21+
"a@qq.com",
22+
"b@qq.com",
23+
"smtp.qq.com",
24+
"global.GVA_CONFIG.Email.Secret",
25+
"登录密钥",
26+
465,
27+
true,
28+
))
29+
30+
#### 2. 配置说明
31+
32+
//其中 Form 和 Secret 通常来说就是用户名和密码
33+
34+
type Email struct {
35+
To string // 收件人:多个以英文逗号分隔 例:a@qq.com b@qq.com 正式开发中请把此项目作为参数使用 此处配置主要用于发送错误监控邮件
36+
From string // 发件人 你自己要发邮件的邮箱
37+
Host string // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
38+
Secret string // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
39+
Nickname string // 昵称 发件人昵称 自定义即可 可以不填
40+
Port int // 端口 请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
41+
IsSSL bool // 是否SSL 是否开启SSL
42+
}
43+
44+
### 方法API
45+
46+
utils.EmailTest(邮件标题,邮件主体) 发送测试邮件
47+
例:utils.EmailTest("测试邮件","测试邮件")
48+
utils.ErrorToEmail(邮件标题,邮件主体) 错误监控
49+
例:utils.ErrorToEmail("测试邮件","测试邮件")
50+
utils.Email(目标邮箱多个的话用逗号分隔,邮件标题,邮件主体) 发送测试邮件
51+
例:utils.Email(”a.qq.com,b.qq.com“,"测试邮件","测试邮件")

server/plugin/email/api/enter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package api
2+
3+
type ApiGroup struct {
4+
EmailApi
5+
}
6+
7+
var ApiGroupApp = new(ApiGroup)

server/api/v1/system/sys_email.go renamed to server/plugin/email/api/sys_email.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
package system
1+
package api
22

33
import (
44
"github.com/flipped-aurora/gin-vue-admin/global"
55
"github.com/flipped-aurora/gin-vue-admin/model/common/response"
6+
"github.com/flipped-aurora/gin-vue-admin/plugin/email/service"
67
"github.com/gin-gonic/gin"
78
"go.uber.org/zap"
89
)
910

11+
type EmailApi struct {
12+
}
13+
1014
// @Tags System
1115
// @Summary 发送测试邮件
1216
// @Security ApiKeyAuth
1317
// @Produce application/json
1418
// @Success 200 {string} string "{"success":true,"data":{},"msg":"发送成功"}"
1519
// @Router /email/emailTest [post]
16-
func (s *SystemApi) EmailTest(c *gin.Context) {
17-
if err := emailService.EmailTest(); err != nil {
20+
func (s *EmailApi) EmailTest(c *gin.Context) {
21+
if err := service.ServiceGroupApp.EmailTest(); err != nil {
1822
global.GVA_LOG.Error("发送失败!", zap.Any("err", err))
1923
response.FailWithMessage("发送失败", c)
2024
} else {

server/plugin/email/config/email.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package config
2+
3+
type Email struct {
4+
To string `mapstructure:"to" json:"to" yaml:"to"` // 收件人:多个以英文逗号分隔 例:a@qq.com b@qq.com 正式开发中请把此项目作为参数使用
5+
From string `mapstructure:"from" json:"from" yaml:"from"` // 发件人 你自己要发邮件的邮箱
6+
Host string `mapstructure:"host" json:"host" yaml:"host"` // 服务器地址 例如 smtp.qq.com 请前往QQ或者你要发邮件的邮箱查看其smtp协议
7+
Secret string `mapstructure:"secret" json:"secret" yaml:"secret"` // 密钥 用于登录的密钥 最好不要用邮箱密码 去邮箱smtp申请一个用于登录的密钥
8+
Nickname string `mapstructure:"nickname" json:"nickname" yaml:"nickname"` // 昵称 发件人昵称 通常为自己的邮箱
9+
Port int `mapstructure:"port" json:"port" yaml:"port"` // 端口 请前往QQ或者你要发邮件的邮箱查看其smtp协议 大多为 465
10+
IsSSL bool `mapstructure:"is-ssl" json:"isSSL" yaml:"is-ssl"` // 是否SSL 是否开启SSL
11+
}

server/plugin/email/global/gloabl.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package global
2+
3+
import "github.com/flipped-aurora/gin-vue-admin/plugin/email/config"
4+
5+
var GlobalConfig = new(config.Email)

server/plugin/email/main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package email
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/plugin/email/global"
5+
"github.com/flipped-aurora/gin-vue-admin/plugin/email/router"
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
type emailPlugin struct {
10+
To string
11+
From string
12+
Host string
13+
Secret string
14+
Nickname string
15+
Port int
16+
IsSsl bool
17+
}
18+
19+
func CreateEmailPlug(To, From, Host, Secret, Nickname string, Port int, IsSSL bool) *emailPlugin {
20+
global.GlobalConfig.To = To
21+
global.GlobalConfig.From = From
22+
global.GlobalConfig.Host = Host
23+
global.GlobalConfig.Secret = Secret
24+
global.GlobalConfig.Nickname = Nickname
25+
global.GlobalConfig.Port = Port
26+
global.GlobalConfig.IsSSL = IsSSL
27+
return &emailPlugin{}
28+
29+
}
30+
31+
func (*emailPlugin) Register(group *gin.RouterGroup) {
32+
router.RouterGroupApp.InitEmailRouter(group)
33+
}
34+
35+
func (*emailPlugin) RouterPath() string {
36+
return "email"
37+
}

server/plugin/email/router/enter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package router
2+
3+
type RouterGroup struct {
4+
EmailRouter
5+
}
6+
7+
var RouterGroupApp = new(RouterGroup)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package router
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/middleware"
5+
"github.com/flipped-aurora/gin-vue-admin/plugin/email/api"
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
type EmailRouter struct {
10+
}
11+
12+
func (s *EmailRouter) InitEmailRouter(Router *gin.RouterGroup) {
13+
emailRouter := Router.Use(middleware.OperationRecord())
14+
var EmailApi = api.ApiGroupApp.EmailApi.EmailTest
15+
{
16+
emailRouter.POST("emailTest", EmailApi) // 发送测试邮件
17+
}
18+
}

server/plugin/email/service/enter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package service
2+
3+
type ServiceGroup struct {
4+
EmailService
5+
}
6+
7+
var ServiceGroupApp = new(ServiceGroup)

server/service/system/sys_email.go renamed to server/plugin/email/service/sys_email.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package system
1+
package service
22

33
import (
4-
"github.com/flipped-aurora/gin-vue-admin/utils"
4+
"github.com/flipped-aurora/gin-vue-admin/plugin/email/utils"
55
)
66

77
type EmailService struct {

server/utils/email.go renamed to server/plugin/email/utils/email.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/smtp"
77
"strings"
88

9-
"github.com/flipped-aurora/gin-vue-admin/global"
9+
"github.com/flipped-aurora/gin-vue-admin/plugin/email/global"
1010

1111
"github.com/jordan-wright/email"
1212
)
@@ -17,8 +17,8 @@ import (
1717
//@param: subject string, body string
1818
//@return: error
1919

20-
func Email(subject string, body string) error {
21-
to := strings.Split(global.GVA_CONFIG.Email.To, ",")
20+
func Email(To, subject string, body string) error {
21+
to := strings.Split(To, ",")
2222
return send(to, subject, body)
2323
}
2424

@@ -29,7 +29,7 @@ func Email(subject string, body string) error {
2929
//@return: error
3030

3131
func ErrorToEmail(subject string, body string) error {
32-
to := strings.Split(global.GVA_CONFIG.Email.To, ",")
32+
to := strings.Split(global.GlobalConfig.To, ",")
3333
if to[len(to)-1] == "" { // 判断切片的最后一个元素是否为空,为空则移除
3434
to = to[:len(to)-1]
3535
}
@@ -43,7 +43,7 @@ func ErrorToEmail(subject string, body string) error {
4343
//@return: error
4444

4545
func EmailTest(subject string, body string) error {
46-
to := []string{global.GVA_CONFIG.Email.From}
46+
to := []string{global.GlobalConfig.From}
4747
return send(to, subject, body)
4848
}
4949

@@ -54,12 +54,12 @@ func EmailTest(subject string, body string) error {
5454
//@return: error
5555

5656
func send(to []string, subject string, body string) error {
57-
from := global.GVA_CONFIG.Email.From
58-
nickname := global.GVA_CONFIG.Email.Nickname
59-
secret := global.GVA_CONFIG.Email.Secret
60-
host := global.GVA_CONFIG.Email.Host
61-
port := global.GVA_CONFIG.Email.Port
62-
isSSL := global.GVA_CONFIG.Email.IsSSL
57+
from := global.GlobalConfig.From
58+
nickname := global.GlobalConfig.Nickname
59+
secret := global.GlobalConfig.Secret
60+
host := global.GlobalConfig.Host
61+
port := global.GlobalConfig.Port
62+
isSSL := global.GlobalConfig.IsSSL
6363

6464
auth := smtp.PlainAuth("", from, secret, host)
6565
e := email.NewEmail()

server/plugin/example_plugin/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package example_plugin
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
var ExamplePlugin = new(pluginExample)
8+
9+
type pluginExample struct {
10+
}
11+
12+
func (*pluginExample) Register(group *gin.RouterGroup) {
13+
//如需细分权限 可以在此处use中间件 gva项目包名已改为github模式
14+
//所以整个plugin可以直接独立到外层开启为新的项目 然后用包的形式导入也是可以完整运行的
15+
// 例:
16+
/*
17+
group.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler()).GET("hello", func(context *gin.Context) {
18+
context.JSON(200, "hello world")
19+
})
20+
*/
21+
group.GET("hello", func(context *gin.Context) {
22+
context.JSON(200, "hello world")
23+
})
24+
}
25+
26+
func (*pluginExample) RouterPath() string {
27+
return "group"
28+
}

0 commit comments

Comments
 (0)