后端重构

This commit is contained in:
Yvan
2026-01-14 14:34:49 +08:00
parent f5f77b32fe
commit 30842b42e5
274 changed files with 39754 additions and 1005 deletions
+7
View File
@@ -0,0 +1,7 @@
package api
type ApiGroup struct {
EmailApi
}
var ApiGroupApp = new(ApiGroup)
+50
View File
@@ -0,0 +1,50 @@
package api
import (
"kra/internal/plugin/email/model/response"
"kra/internal/plugin/email/service"
pkgResponse "kra/pkg/response"
"github.com/gin-gonic/gin"
)
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 {
pkgResponse.FailWithMessage("发送失败", c)
return
}
pkgResponse.OkWithMessage("发送成功", c)
}
// SendEmail
// @Tags System
// @Summary 发送邮件
// @Security ApiKeyAuth
// @Produce application/json
// @Param data body response.Email true "发送邮件必须的参数"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"发送成功"}"
// @Router /email/sendEmail [post]
func (s *EmailApi) SendEmail(c *gin.Context) {
var email response.Email
err := c.ShouldBindJSON(&email)
if err != nil {
pkgResponse.FailWithMessage(err.Error(), c)
return
}
err = service.ServiceGroupApp.SendEmail(email.To, email.Subject, email.Body)
if err != nil {
pkgResponse.FailWithMessage("发送失败", c)
return
}
pkgResponse.OkWithMessage("发送成功", c)
}
+13
View File
@@ -0,0 +1,13 @@
package config
// Email 邮件配置
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
IsLoginAuth bool `mapstructure:"is-loginauth" json:"is-loginauth" yaml:"is-loginauth"` // 是否LoginAuth 是否使用LoginAuth认证
}
+5
View File
@@ -0,0 +1,5 @@
package global
import "kra/internal/plugin/email/config"
var GlobalConfig = new(config.Email)
+31
View File
@@ -0,0 +1,31 @@
package email
import (
"kra/internal/plugin/email/global"
"kra/internal/plugin/email/router"
"github.com/gin-gonic/gin"
)
type emailPlugin struct{}
// CreateEmailPlug 创建邮件插件
func CreateEmailPlug(To, From, Host, Secret, Nickname string, Port int, IsSSL bool, IsLoginAuth 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
global.GlobalConfig.IsLoginAuth = IsLoginAuth
return &emailPlugin{}
}
func (*emailPlugin) Register(group *gin.RouterGroup) {
router.RouterGroupApp.InitEmailRouter(group)
}
func (*emailPlugin) RouterPath() string {
return "email"
}
@@ -0,0 +1,8 @@
package response
// Email 邮件请求结构
type Email struct {
To string `json:"to"` // 邮件发送给谁
Subject string `json:"subject"` // 邮件标题
Body string `json:"body"` // 邮件内容
}
+7
View File
@@ -0,0 +1,7 @@
package router
type RouterGroup struct {
EmailRouter
}
var RouterGroupApp = new(RouterGroup)
+20
View File
@@ -0,0 +1,20 @@
package router
import (
"kra/internal/plugin/email/api"
"kra/internal/server/middleware"
"github.com/gin-gonic/gin"
)
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
View File
@@ -0,0 +1,7 @@
package service
type ServiceGroup struct {
EmailService
}
var ServiceGroupApp = new(ServiceGroup)
@@ -0,0 +1,24 @@
package service
import (
"kra/internal/plugin/email/utils"
)
type EmailService struct{}
// EmailTest 发送邮件测试
func (e *EmailService) EmailTest() (err error) {
subject := "test"
body := "test"
err = utils.EmailTest(subject, body)
return err
}
// SendEmail 发送邮件
// @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
}
+102
View File
@@ -0,0 +1,102 @@
package utils
import (
"crypto/tls"
"fmt"
"net/smtp"
"strings"
"github.com/jordan-wright/email"
"kra/internal/plugin/email/global"
)
// Email 发送邮件
func Email(To, subject string, body string) error {
to := strings.Split(To, ",")
return send(to, subject, body)
}
// ErrorToEmail 给email中间件错误发送邮件到指定邮箱
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)
}
// EmailTest Email测试方法
func EmailTest(subject string, body string) error {
to := []string{global.GlobalConfig.To}
return send(to, subject, body)
}
// send Email发送方法
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
isLoginAuth := global.GlobalConfig.IsLoginAuth
var auth smtp.Auth
if isLoginAuth {
auth = LoginAuth(from, secret)
} else {
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
}
// loginAuth 用于IBM、微软邮箱服务器的LOGIN认证方式
type loginAuth struct {
username, password string
}
// LoginAuth 创建LOGIN认证
func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
}
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte{}, nil
}
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:":
return []byte(a.username), nil
case "Password:":
return []byte(a.password), nil
default:
// 邮箱服务器可能发送的其他提示信息
prompt := strings.ToLower(string(fromServer))
if strings.Contains(prompt, "username") || strings.Contains(prompt, "user") {
return []byte(a.username), nil
}
if strings.Contains(prompt, "password") || strings.Contains(prompt, "pass") {
return []byte(a.password), nil
}
}
}
return nil, nil
}