Skip to content

Commit 096fa51

Browse files
author
pixel
committed
Merge branch 'develop' of https://github.com/piexlmax/QMPlus into gva_gormv2_dev
2 parents df69c96 + 9b40ea5 commit 096fa51

File tree

12 files changed

+356
-187
lines changed

12 files changed

+356
-187
lines changed

README.md

Lines changed: 170 additions & 178 deletions
Large diffs are not rendered by default.

server/api/v1/sys_email.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package v1
2+
3+
import (
4+
"fmt"
5+
"gin-vue-admin/global/response"
6+
"gin-vue-admin/service"
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
// @Tags system
11+
// @Summary 发送测试邮件
12+
// @Security ApiKeyAuth
13+
// @Produce application/json
14+
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
15+
// @Router /email/emailTest [post]
16+
func EmailTest(c *gin.Context) {
17+
err := service.EmailTest()
18+
if err != nil {
19+
response.FailWithMessage(fmt.Sprintf("发送失败,%v", err), c)
20+
} else {
21+
response.OkWithData("发送成功", c)
22+
}
23+
}

server/config.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ sqlserver:
5555
# 切换本地与七牛云上传,分配头像和文件路径
5656
localupload:
5757
local: false
58-
avatar-path: uploads/avatar
5958
file-path: uploads/file
6059

6160
# 请自行七牛申请对应的 公钥 私钥 bucket 和 域名地址
@@ -105,4 +104,13 @@ zap:
105104
# LowercaseLevelEncoder:小写, LowercaseColorLevelEncoder:小写带颜色,CapitalLevelEncoder: 大写, CapitalColorLevelEncoder: 大写带颜色,
106105
encode_level: 'LowercaseColorLevelEncoder'
107106
stacktrace_key: 'stacktrace'
108-
log_in_console: true
107+
log_in_console: true
108+
109+
email:
110+
email_from: '[email protected]'
111+
email_nick_name: 'test'
112+
email_secret: 'xxx'
113+
email_to: '[email protected]'
114+
email_host: 'smtp.163.com'
115+
email_port: 465
116+
email_isSSL: true

server/config/config.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Server struct {
1313
Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
1414
Zap Zap `mapstructure:"zap" json:"zap" yaml:"zap"`
1515
LocalUpload LocalUpload `mapstructure:"localUpload" json:"localUpload" yaml:"localUpload"`
16+
Email Email `mapstructure:"email" json:"email" yaml:"email"`
1617
}
1718

1819
type System struct {
@@ -79,7 +80,6 @@ type Redis struct {
7980

8081
type LocalUpload struct {
8182
Local bool `mapstructure:"local" json:"local" yaml:"local"`
82-
AvatarPath string `mapstructure:"avatar-path" json:"avatarPath" yaml:"avatar-path"`
8383
FilePath string `mapstructure:"file-path" json:"filePath" yaml:"file-path"`
8484
}
8585

@@ -107,3 +107,13 @@ type Zap struct {
107107
StacktraceKey string `mapstructure:"stacktrace_key" json:"stacktraceKey" yaml:"stacktrace_key"`
108108
LogInConsole bool `mapstructure:"log_in_console" json:"logInConsole" yaml:"log_in_console"`
109109
}
110+
111+
type Email struct {
112+
EmailFrom string `mapstructure:"email_from" json:"emailFrom" yaml:"email_from"`
113+
EmailNickName string `mapstructure:"email_nick_name" json:"emailNickName" yaml:"email_nick_name"`
114+
EmailSecret string `mapstructure:"email_secret" json:"emailSecret" yaml:"email_secret"`
115+
EmailTo string `mapstructure:"email_to" json:"emailTo" yaml:"email_to"`
116+
EmailHost string `mapstructure:"email_host" json:"emailHost" yaml:"email_host"`
117+
EmailPort int `mapstructure:"email_port" json:"emailPort" yaml:"email_port"`
118+
EmailIsSSL bool `mapstructure:"email_isSSL" json:"emailIsSSL" yaml:"email_isSSL"`
119+
}

server/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/go-sql-driver/mysql v1.5.0
2020
github.com/golang/protobuf v1.4.2 // indirect
2121
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
22+
github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84
2223
github.com/json-iterator/go v1.1.10 // indirect
2324
github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible
2425
github.com/lestrrat-go/strftime v1.0.3 // indirect

server/initialize/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
func Routers() *gin.Engine {
1717
var Router = gin.Default()
1818
// 为用户头像和文件提供静态地址
19-
Router.StaticFS(global.GVA_CONFIG.LocalUpload.AvatarPath, http.Dir(global.GVA_CONFIG.LocalUpload.AvatarPath))
2019
Router.StaticFS(global.GVA_CONFIG.LocalUpload.FilePath, http.Dir(global.GVA_CONFIG.LocalUpload.FilePath))
2120
// Router.Use(middleware.LoadTls()) // 打开就能玩https了
2221
global.GVA_LOG.Debug("use middleware logger")
@@ -43,6 +42,7 @@ func Routers() *gin.Engine {
4342
router.InitSysDictionaryDetailRouter(ApiGroup) // 字典详情管理
4443
router.InitSysDictionaryRouter(ApiGroup) // 字典管理
4544
router.InitSysOperationRecordRouter(ApiGroup) // 操作记录
45+
router.InitEmailRouter(ApiGroup) // 邮件相关路由
4646

4747
global.GVA_LOG.Info("router register success")
4848
return Router

server/router/sys_email.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package router
2+
3+
import (
4+
"gin-vue-admin/api/v1"
5+
"gin-vue-admin/middleware"
6+
"github.com/gin-gonic/gin"
7+
)
8+
9+
func InitEmailRouter(Router *gin.RouterGroup) {
10+
UserRouter := Router.Group("email").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
11+
{
12+
UserRouter.POST("emailTest", v1.EmailTest) // 发送测试邮件
13+
}
14+
}

server/service/sys_email.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package service
2+
3+
import (
4+
"gin-vue-admin/utils"
5+
)
6+
7+
// @title EmailTest
8+
// @description 发送邮件测试
9+
// @auth (2020/09/08 13:58
10+
// @return err error
11+
12+
func EmailTest() (err error) {
13+
subject := "test"
14+
body := "test"
15+
err = utils.EmailTest(subject, body)
16+
return err
17+
}

server/utils/email.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package utils
2+
3+
import (
4+
"crypto/tls"
5+
"fmt"
6+
"net/smtp"
7+
"strings"
8+
9+
"gin-vue-admin/global"
10+
11+
"github.com/jordan-wright/email"
12+
)
13+
14+
func Email(subject string, body string) error {
15+
to := strings.Split(global.GVA_CONFIG.Email.EmailTo, ",")
16+
return send(to, subject, body)
17+
}
18+
19+
func EmailTest(subject string, body string) error {
20+
to := []string{global.GVA_CONFIG.Email.EmailFrom}
21+
return send(to, subject, body)
22+
}
23+
24+
func send(to []string, subject string, body string) error {
25+
from := global.GVA_CONFIG.Email.EmailFrom
26+
nickName := global.GVA_CONFIG.Email.EmailNickName
27+
secret := global.GVA_CONFIG.Email.EmailSecret
28+
host := global.GVA_CONFIG.Email.EmailHost
29+
port := global.GVA_CONFIG.Email.EmailPort
30+
isSSL := global.GVA_CONFIG.Email.EmailIsSSL
31+
32+
auth := smtp.PlainAuth("", from, secret, host)
33+
e := email.NewEmail()
34+
if nickName != "" {
35+
e.From = fmt.Sprintf("%s <%s>", nickName, from)
36+
} else {
37+
e.From = from
38+
}
39+
e.To = to
40+
e.Subject = subject
41+
e.HTML = []byte(body)
42+
var err error
43+
hostAddr := fmt.Sprintf("%s:%d", host, port)
44+
if isSSL {
45+
err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: host})
46+
} else {
47+
err = e.Send(hostAddr, auth)
48+
}
49+
return err
50+
}

web/.env.production

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
ENV = 'production'
2-
VUE_APP_BASE_API = '/v1'
2+
VUE_APP_BASE_API = '/api'

0 commit comments

Comments
 (0)