init project

This commit is contained in:
2025-03-28 17:14:57 +08:00
parent 76ca33962e
commit 4d08921f92
357 changed files with 54458 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
package api
import "git.echol.cn/loser/xiecheng_server/plugin/announcement/service"
var (
Api = new(api)
serviceInfo = service.Service.Info
)
type api struct{ Info info }

View File

@@ -0,0 +1,183 @@
package api
import (
"git.echol.cn/loser/xiecheng_server/global"
"git.echol.cn/loser/xiecheng_server/model/common/response"
"git.echol.cn/loser/xiecheng_server/plugin/announcement/model"
"git.echol.cn/loser/xiecheng_server/plugin/announcement/model/request"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
var Info = new(info)
type info struct{}
// CreateInfo 创建公告
// @Tags Info
// @Summary 创建公告
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Info true "创建公告"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /info/createInfo [post]
func (a *info) CreateInfo(c *gin.Context) {
var info model.Info
err := c.ShouldBindJSON(&info)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err = serviceInfo.CreateInfo(&info)
if err != nil {
global.GVA_LOG.Error("创建失败!", zap.Error(err))
response.FailWithMessage("创建失败", c)
return
}
response.OkWithMessage("创建成功", c)
}
// DeleteInfo 删除公告
// @Tags Info
// @Summary 删除公告
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Info true "删除公告"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /info/deleteInfo [delete]
func (a *info) DeleteInfo(c *gin.Context) {
ID := c.Query("ID")
err := serviceInfo.DeleteInfo(ID)
if err != nil {
global.GVA_LOG.Error("删除失败!", zap.Error(err))
response.FailWithMessage("删除失败", c)
return
}
response.OkWithMessage("删除成功", c)
}
// DeleteInfoByIds 批量删除公告
// @Tags Info
// @Summary 批量删除公告
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /info/deleteInfoByIds [delete]
func (a *info) DeleteInfoByIds(c *gin.Context) {
IDs := c.QueryArray("IDs[]")
if err := serviceInfo.DeleteInfoByIds(IDs); err != nil {
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
response.FailWithMessage("批量删除失败", c)
return
}
response.OkWithMessage("批量删除成功", c)
}
// UpdateInfo 更新公告
// @Tags Info
// @Summary 更新公告
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Info true "更新公告"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /info/updateInfo [put]
func (a *info) UpdateInfo(c *gin.Context) {
var info model.Info
err := c.ShouldBindJSON(&info)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err = serviceInfo.UpdateInfo(info)
if err != nil {
global.GVA_LOG.Error("更新失败!", zap.Error(err))
response.FailWithMessage("更新失败", c)
return
}
response.OkWithMessage("更新成功", c)
}
// FindInfo 用id查询公告
// @Tags Info
// @Summary 用id查询公告
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query model.Info true "用id查询公告"
// @Success 200 {object} response.Response{data=model.Info,msg=string} "查询成功"
// @Router /info/findInfo [get]
func (a *info) FindInfo(c *gin.Context) {
ID := c.Query("ID")
reinfo, err := serviceInfo.GetInfo(ID)
if err != nil {
global.GVA_LOG.Error("查询失败!", zap.Error(err))
response.FailWithMessage("查询失败", c)
return
}
response.OkWithData(reinfo, c)
}
// GetInfoList 分页获取公告列表
// @Tags Info
// @Summary 分页获取公告列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.InfoSearch true "分页获取公告列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /info/getInfoList [get]
func (a *info) GetInfoList(c *gin.Context) {
var pageInfo request.InfoSearch
err := c.ShouldBindQuery(&pageInfo)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
list, total, err := serviceInfo.GetInfoInfoList(pageInfo)
if err != nil {
global.GVA_LOG.Error("获取失败!", zap.Error(err))
response.FailWithMessage("获取失败", c)
return
}
response.OkWithDetailed(response.PageResult{
List: list,
Total: total,
Page: pageInfo.Page,
PageSize: pageInfo.PageSize,
}, "获取成功", c)
}
// GetInfoDataSource 获取Info的数据源
// @Tags Info
// @Summary 获取Info的数据源
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "查询成功"
// @Router /info/getInfoDataSource [get]
func (a *info) GetInfoDataSource(c *gin.Context) {
// 此接口为获取数据源定义的数据
dataSource, err := serviceInfo.GetInfoDataSource()
if err != nil {
global.GVA_LOG.Error("查询失败!", zap.Error(err))
response.FailWithMessage("查询失败", c)
return
}
response.OkWithData(dataSource, c)
}
// GetInfoPublic 不需要鉴权的公告接口
// @Tags Info
// @Summary 不需要鉴权的公告接口
// @accept application/json
// @Produce application/json
// @Param data query request.InfoSearch true "分页获取公告列表"
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /info/getInfoPublic [get]
func (a *info) GetInfoPublic(c *gin.Context) {
// 此接口不需要鉴权 示例为返回了一个固定的消息接口一般本接口用于C端服务需要自己实现业务逻辑
response.OkWithDetailed(gin.H{"info": "不需要鉴权的公告接口信息"}, "获取成功", c)
}

View File

@@ -0,0 +1,4 @@
package config
type Config struct {
}

View File

@@ -0,0 +1,17 @@
package main
import (
"gorm.io/gen"
"path/filepath" //go:generate go mod tidy
//go:generate go mod download
//go:generate go run gen.go
"git.echol.cn/loser/xiecheng_server/plugin/announcement/model"
)
func main() {
g := gen.NewGenerator(gen.Config{OutPath: filepath.Join("..", "..", "..", "announcement", "blender", "model", "dao"), Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface})
g.ApplyBasic(
new(model.Info),
)
g.Execute()
}

View File

@@ -0,0 +1,49 @@
package initialize
import (
"context"
model "git.echol.cn/loser/xiecheng_server/model/system"
"git.echol.cn/loser/xiecheng_server/plugin/plugin-tool/utils"
)
func Api(ctx context.Context) {
entities := []model.SysApi{
{
Path: "/info/createInfo",
Description: "新建公告",
ApiGroup: "公告",
Method: "POST",
},
{
Path: "/info/deleteInfo",
Description: "删除公告",
ApiGroup: "公告",
Method: "DELETE",
},
{
Path: "/info/deleteInfoByIds",
Description: "批量删除公告",
ApiGroup: "公告",
Method: "DELETE",
},
{
Path: "/info/updateInfo",
Description: "更新公告",
ApiGroup: "公告",
Method: "PUT",
},
{
Path: "/info/findInfo",
Description: "根据ID获取公告",
ApiGroup: "公告",
Method: "GET",
},
{
Path: "/info/getInfoList",
Description: "获取公告列表",
ApiGroup: "公告",
Method: "GET",
},
}
utils.RegisterApis(entities...)
}

View File

@@ -0,0 +1,20 @@
package initialize
import (
"context"
"fmt"
"git.echol.cn/loser/xiecheng_server/global"
"git.echol.cn/loser/xiecheng_server/plugin/announcement/model"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func Gorm(ctx context.Context) {
err := global.GVA_DB.WithContext(ctx).AutoMigrate(
new(model.Info),
)
if err != nil {
err = errors.Wrap(err, "注册表失败!")
zap.L().Error(fmt.Sprintf("%+v", err))
}
}

View File

@@ -0,0 +1,22 @@
package initialize
import (
"context"
model "git.echol.cn/loser/xiecheng_server/model/system"
"git.echol.cn/loser/xiecheng_server/plugin/plugin-tool/utils"
)
func Menu(ctx context.Context) {
entities := []model.SysBaseMenu{
{
ParentId: 24,
Path: "anInfo",
Name: "anInfo",
Hidden: false,
Component: "plugin/announcement/view/info.vue",
Sort: 5,
Meta: model.Meta{Title: "公告管理", Icon: "box"},
},
}
utils.RegisterMenus(entities...)
}

View File

@@ -0,0 +1,15 @@
package initialize
import (
"git.echol.cn/loser/xiecheng_server/global"
"git.echol.cn/loser/xiecheng_server/middleware"
"git.echol.cn/loser/xiecheng_server/plugin/announcement/router"
"github.com/gin-gonic/gin"
)
func Router(engine *gin.Engine) {
public := engine.Group(global.GVA_CONFIG.System.RouterPrefix).Group("")
private := engine.Group(global.GVA_CONFIG.System.RouterPrefix).Group("")
private.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
router.Router.Info.Init(public, private)
}

View File

@@ -0,0 +1,17 @@
package initialize
import (
"fmt"
"git.echol.cn/loser/xiecheng_server/global"
"git.echol.cn/loser/xiecheng_server/plugin/announcement/plugin"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func Viper() {
err := global.GVA_VP.UnmarshalKey("announcement", &plugin.Config)
if err != nil {
err = errors.Wrap(err, "初始化配置文件失败!")
zap.L().Error(fmt.Sprintf("%+v", err))
}
}

View File

@@ -0,0 +1,20 @@
package model
import (
"git.echol.cn/loser/xiecheng_server/global"
"gorm.io/datatypes"
)
// Info 公告 结构体
type Info struct {
global.GVA_MODEL
Title string `json:"title" form:"title" gorm:"column:title;comment:公告标题;"` //标题
Content string `json:"content" form:"content" gorm:"column:content;comment:公告内容;type:text;"` //内容
UserID *int `json:"userID" form:"userID" gorm:"column:user_id;comment:发布者;"` //作者
Attachments datatypes.JSON `json:"attachments" form:"attachments" gorm:"column:attachments;comment:相关附件;" swaggertype:"array,object"` //附件
}
// TableName 公告 Info自定义表名 gva_announcements_info
func (Info) TableName() string {
return "gva_announcements_info"
}

View File

@@ -0,0 +1,12 @@
package request
import (
"git.echol.cn/loser/xiecheng_server/model/common/request"
"time"
)
type InfoSearch struct {
StartCreatedAt *time.Time `json:"startCreatedAt" form:"startCreatedAt"`
EndCreatedAt *time.Time `json:"endCreatedAt" form:"endCreatedAt"`
request.PageInfo
}

View File

@@ -0,0 +1,26 @@
package announcement
import (
"context"
"git.echol.cn/loser/xiecheng_server/plugin/announcement/initialize"
interfaces "git.echol.cn/loser/xiecheng_server/utils/plugin/v2"
"github.com/gin-gonic/gin"
)
var _ interfaces.Plugin = (*plugin)(nil)
var Plugin = new(plugin)
type plugin struct{}
func (p *plugin) Register(group *gin.Engine) {
ctx := context.Background()
// 如果需要配置文件请到config.Config中填充配置结构且到下方发放中填入其在config.yaml中的key
// initialize.Viper()
// 安装插件时候自动注册的api数据请到下方法.Api方法中实现
initialize.Api(ctx)
// 安装插件时候自动注册的api数据请到下方法.Menu方法中实现
initialize.Menu(ctx)
initialize.Gorm(ctx)
initialize.Router(group)
}

View File

@@ -0,0 +1,5 @@
package plugin
import "git.echol.cn/loser/xiecheng_server/plugin/announcement/config"
var Config config.Config

View File

@@ -0,0 +1,10 @@
package router
import "git.echol.cn/loser/xiecheng_server/plugin/announcement/api"
var (
Router = new(router)
apiInfo = api.Api.Info
)
type router struct{ Info info }

View File

@@ -0,0 +1,31 @@
package router
import (
"git.echol.cn/loser/xiecheng_server/middleware"
"github.com/gin-gonic/gin"
)
var Info = new(info)
type info struct{}
// Init 初始化 公告 路由信息
func (r *info) Init(public *gin.RouterGroup, private *gin.RouterGroup) {
{
group := private.Group("info").Use(middleware.OperationRecord())
group.POST("createInfo", apiInfo.CreateInfo) // 新建公告
group.DELETE("deleteInfo", apiInfo.DeleteInfo) // 删除公告
group.DELETE("deleteInfoByIds", apiInfo.DeleteInfoByIds) // 批量删除公告
group.PUT("updateInfo", apiInfo.UpdateInfo) // 更新公告
}
{
group := private.Group("info")
group.GET("findInfo", apiInfo.FindInfo) // 根据ID获取公告
group.GET("getInfoList", apiInfo.GetInfoList) // 获取公告列表
}
{
group := public.Group("info")
group.GET("getInfoDataSource", apiInfo.GetInfoDataSource) // 获取公告数据源
group.GET("getInfoPublic", apiInfo.GetInfoPublic) // 获取公告列表
}
}

View File

@@ -0,0 +1,5 @@
package service
var Service = new(service)
type service struct{ Info info }

View File

@@ -0,0 +1,78 @@
package service
import (
"git.echol.cn/loser/xiecheng_server/global"
"git.echol.cn/loser/xiecheng_server/plugin/announcement/model"
"git.echol.cn/loser/xiecheng_server/plugin/announcement/model/request"
)
var Info = new(info)
type info struct{}
// CreateInfo 创建公告记录
// Author [piexlmax](https://github.com/piexlmax)
func (s *info) CreateInfo(info *model.Info) (err error) {
err = global.GVA_DB.Create(info).Error
return err
}
// DeleteInfo 删除公告记录
// Author [piexlmax](https://github.com/piexlmax)
func (s *info) DeleteInfo(ID string) (err error) {
err = global.GVA_DB.Delete(&model.Info{}, "id = ?", ID).Error
return err
}
// DeleteInfoByIds 批量删除公告记录
// Author [piexlmax](https://github.com/piexlmax)
func (s *info) DeleteInfoByIds(IDs []string) (err error) {
err = global.GVA_DB.Delete(&[]model.Info{}, "id in ?", IDs).Error
return err
}
// UpdateInfo 更新公告记录
// Author [piexlmax](https://github.com/piexlmax)
func (s *info) UpdateInfo(info model.Info) (err error) {
err = global.GVA_DB.Model(&model.Info{}).Where("id = ?", info.ID).Updates(&info).Error
return err
}
// GetInfo 根据ID获取公告记录
// Author [piexlmax](https://github.com/piexlmax)
func (s *info) GetInfo(ID string) (info model.Info, err error) {
err = global.GVA_DB.Where("id = ?", ID).First(&info).Error
return
}
// GetInfoInfoList 分页获取公告记录
// Author [piexlmax](https://github.com/piexlmax)
func (s *info) GetInfoInfoList(info request.InfoSearch) (list []model.Info, total int64, err error) {
limit := info.PageSize
offset := info.PageSize * (info.Page - 1)
// 创建db
db := global.GVA_DB.Model(&model.Info{})
var infos []model.Info
// 如果有条件搜索 下方会自动创建搜索语句
if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
}
err = db.Count(&total).Error
if err != nil {
return
}
if limit != 0 {
db = db.Limit(limit).Offset(offset)
}
err = db.Find(&infos).Error
return infos, total, err
}
func (s *info) GetInfoDataSource() (res map[string][]map[string]any, err error) {
res = make(map[string][]map[string]any)
userID := make([]map[string]any, 0)
global.GVA_DB.Table("sys_users").Select("nick_name as label,id as value").Scan(&userID)
res["userID"] = userID
return
}

75
plugin/email/README.MD Normal file
View 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"` // 邮件内容
}

View File

@@ -0,0 +1,7 @@
package api
type ApiGroup struct {
EmailApi
}
var ApiGroupApp = new(ApiGroup)

View File

@@ -0,0 +1,53 @@
package api
import (
"git.echol.cn/loser/xiecheng_server/global"
"git.echol.cn/loser/xiecheng_server/model/common/response"
email_response "git.echol.cn/loser/xiecheng_server/plugin/email/model/response"
"git.echol.cn/loser/xiecheng_server/plugin/email/service"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
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)
}

View 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
}

View File

@@ -0,0 +1,5 @@
package global
import "git.echol.cn/loser/xiecheng_server/plugin/email/config"
var GlobalConfig = new(config.Email)

28
plugin/email/main.go Normal file
View File

@@ -0,0 +1,28 @@
package email
import (
"git.echol.cn/loser/xiecheng_server/plugin/email/global"
"git.echol.cn/loser/xiecheng_server/plugin/email/router"
"github.com/gin-gonic/gin"
)
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"
}

View File

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

View File

@@ -0,0 +1,7 @@
package router
type RouterGroup struct {
EmailRouter
}
var RouterGroupApp = new(RouterGroup)

View File

@@ -0,0 +1,19 @@
package router
import (
"git.echol.cn/loser/xiecheng_server/middleware"
"git.echol.cn/loser/xiecheng_server/plugin/email/api"
"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) // 发送邮件
}
}

View File

@@ -0,0 +1,7 @@
package service
type ServiceGroup struct {
EmailService
}
var ServiceGroupApp = new(ServiceGroup)

View File

@@ -0,0 +1,32 @@
package service
import (
"git.echol.cn/loser/xiecheng_server/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
}

View File

@@ -0,0 +1,82 @@
package utils
import (
"crypto/tls"
"fmt"
"net/smtp"
"strings"
"git.echol.cn/loser/xiecheng_server/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
}

View File

@@ -0,0 +1,50 @@
package utils
import (
"fmt"
"git.echol.cn/loser/xiecheng_server/global"
"git.echol.cn/loser/xiecheng_server/model/system"
)
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 {
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 {
return
}
err := global.GVA_DB.Create(&parentMenu).Error
if err != nil {
fmt.Println(err)
}
for i := range otherMenus {
pid := parentMenu.ID
otherMenus[i].ParentId = pid
}
err = global.GVA_DB.Create(&otherMenus).Error
if err != nil {
fmt.Println(err)
}
}