初始
This commit is contained in:
71
internal/app/common/logic/bigUpload/big_upload.go
Normal file
71
internal/app/common/logic/bigUpload/big_upload.go
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* @desc:大文件上传
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/27 16:25
|
||||
*/
|
||||
|
||||
package bigUpload
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
"github.com/tiger1103/gfast/v3/library/upload_chunk"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterBigUpload(New())
|
||||
}
|
||||
|
||||
func New() *sBigUpload {
|
||||
return &sBigUpload{}
|
||||
}
|
||||
|
||||
type sBigUpload struct{}
|
||||
|
||||
// Upload 上传分片文件
|
||||
func (s *sBigUpload) Upload(ctx context.Context, req *system.BigUploadReq) (res *system.BigUploadRes, err error) {
|
||||
uploadChunk := &upload_chunk.UploadChunk{}
|
||||
result, err := uploadChunk.Upload(req.UploadReq)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = new(system.BigUploadRes)
|
||||
res.UpLoadRes = *result
|
||||
return
|
||||
}
|
||||
|
||||
// UploadCheck 上传文件检查
|
||||
func (s *sBigUpload) UploadCheck(ctx context.Context, req *system.BigUploadCheckReq) (res *system.BigUploadCheckRes, err error) {
|
||||
uploadChunk := &upload_chunk.UploadChunk{}
|
||||
result, err := uploadChunk.CheckChunk(req.UploadReq)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = &system.BigUploadCheckRes{
|
||||
CheckRes: *result,
|
||||
Identifier: req.Identifier,
|
||||
TotalChunks: req.TotalChunks,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UploadMerge 上传文件合并
|
||||
func (s *sBigUpload) UploadMerge(ctx context.Context, req *system.BigUploadMergeReq) (res *system.BigUploadRes, err error) {
|
||||
uploadChunk := &upload_chunk.UploadChunk{}
|
||||
result, err := uploadChunk.MergeChunk(req.UploadReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res = &system.BigUploadRes{
|
||||
UpLoadRes: upload_chunk.UpLoadRes{
|
||||
BaseRes: result.BaseRes,
|
||||
NeedMerge: false,
|
||||
Identifier: req.Identifier,
|
||||
TotalChunks: req.TotalChunks,
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
45
internal/app/common/logic/cache/cache.go
vendored
Normal file
45
internal/app/common/logic/cache/cache.go
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* @desc:缓存处理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/27 16:33
|
||||
*/
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/tiger1103/gfast-cache/cache"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterCache(New())
|
||||
}
|
||||
|
||||
func New() *sCache {
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
cacheContainer *cache.GfCache
|
||||
)
|
||||
prefix := g.Cfg().MustGet(ctx, "system.cache.prefix").String()
|
||||
model := g.Cfg().MustGet(ctx, "system.cache.model").String()
|
||||
if model == consts.CacheModelRedis {
|
||||
// redis
|
||||
cacheContainer = cache.NewRedis(prefix)
|
||||
} else {
|
||||
// memory
|
||||
cacheContainer = cache.New(prefix)
|
||||
}
|
||||
return &sCache{
|
||||
GfCache: cacheContainer,
|
||||
prefix: prefix,
|
||||
}
|
||||
}
|
||||
|
||||
type sCache struct {
|
||||
*cache.GfCache
|
||||
prefix string
|
||||
}
|
76
internal/app/common/logic/captcha/captcha.go
Normal file
76
internal/app/common/logic/captcha/captcha.go
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* @desc:验证码处理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:01
|
||||
*/
|
||||
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterCaptcha(New())
|
||||
}
|
||||
|
||||
func New() *sCaptcha {
|
||||
return &sCaptcha{
|
||||
driver: &base64Captcha.DriverString{
|
||||
//Height: 80,
|
||||
//Width: 240,
|
||||
Height: 100,
|
||||
Width: 260,
|
||||
NoiseCount: 50,
|
||||
ShowLineOptions: 20,
|
||||
Length: 4,
|
||||
Source: "0123456789",
|
||||
//Length: 4,
|
||||
//Source: "abcdefghjkmnpqrstuvwxyz23456789",
|
||||
Fonts: []string{"chromohv.ttf"},
|
||||
},
|
||||
store: base64Captcha.DefaultMemStore,
|
||||
}
|
||||
}
|
||||
|
||||
type sCaptcha struct {
|
||||
driver *base64Captcha.DriverString
|
||||
store base64Captcha.Store
|
||||
}
|
||||
|
||||
var (
|
||||
captcha = sCaptcha{
|
||||
driver: &base64Captcha.DriverString{
|
||||
Height: 100,
|
||||
Width: 260,
|
||||
NoiseCount: 50,
|
||||
ShowLineOptions: 20,
|
||||
Length: 4,
|
||||
Source: "0123456789",
|
||||
//Length: 4,
|
||||
//Source: "abcdefghjkmnpqrstuvwxyz23456789",
|
||||
Fonts: []string{"chromohv.ttf"},
|
||||
},
|
||||
store: base64Captcha.DefaultMemStore,
|
||||
}
|
||||
)
|
||||
|
||||
// GetVerifyImgString 获取字母数字混合验证码
|
||||
func (s *sCaptcha) GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string, err error) {
|
||||
driver := s.driver.ConvertFonts()
|
||||
c := base64Captcha.NewCaptcha(driver, s.store)
|
||||
//idKeyC, base64stringC, _, err = c.Generate()
|
||||
idKeyC, base64stringC, err = c.Generate()
|
||||
return
|
||||
}
|
||||
|
||||
// VerifyString 验证输入的验证码是否正确
|
||||
func (s *sCaptcha) VerifyString(id, answer string) bool {
|
||||
c := base64Captcha.NewCaptcha(s.driver, s.store)
|
||||
answer = gstr.ToLower(answer)
|
||||
return c.Verify(id, answer, true)
|
||||
}
|
16
internal/app/common/logic/logic.go
Normal file
16
internal/app/common/logic/logic.go
Normal file
@ -0,0 +1,16 @@
|
||||
// ==========================================================================
|
||||
// Code generated by GoFrame CLI tool. DO NOT EDIT.
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/bigUpload"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/cache"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/captcha"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/middleware"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/sysConfig"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/sysDictData"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/sysDictType"
|
||||
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/upload"
|
||||
)
|
31
internal/app/common/logic/middleware/middleware.go
Normal file
31
internal/app/common/logic/middleware/middleware.go
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* @desc:中间件处理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:08
|
||||
*/
|
||||
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterMiddleware(New())
|
||||
}
|
||||
|
||||
func New() *sMiddleware {
|
||||
return &sMiddleware{}
|
||||
}
|
||||
|
||||
type sMiddleware struct{}
|
||||
|
||||
func (s *sMiddleware) MiddlewareCORS(r *ghttp.Request) {
|
||||
corsOptions := r.Response.DefaultCORSOptions()
|
||||
// you can set options
|
||||
//corsOptions.AllowDomain = []string{"goframe.org", "baidu.com"}
|
||||
r.Response.CORS(corsOptions)
|
||||
r.Middleware.Next()
|
||||
}
|
178
internal/app/common/logic/sysConfig/sys_config.go
Normal file
178
internal/app/common/logic/sysConfig/sys_config.go
Normal file
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* @desc:配置参数管理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:13
|
||||
*/
|
||||
|
||||
package sysConfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
systemConsts "github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterSysConfig(New())
|
||||
}
|
||||
|
||||
func New() *sSysConfig {
|
||||
return &sSysConfig{}
|
||||
}
|
||||
|
||||
type sSysConfig struct {
|
||||
}
|
||||
|
||||
// List 系统参数列表
|
||||
func (s *sSysConfig) List(ctx context.Context, req *system.ConfigSearchReq) (res *system.ConfigSearchRes, err error) {
|
||||
res = new(system.ConfigSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.SysConfig.Ctx(ctx)
|
||||
if req != nil {
|
||||
if req.ConfigName != "" {
|
||||
m = m.Where("config_name like ?", "%"+req.ConfigName+"%")
|
||||
}
|
||||
if req.ConfigType != "" {
|
||||
m = m.Where("config_type = ", gconv.Int(req.ConfigType))
|
||||
}
|
||||
if req.ConfigKey != "" {
|
||||
m = m.Where("config_key like ?", "%"+req.ConfigKey+"%")
|
||||
}
|
||||
if len(req.DateRange) > 0 {
|
||||
m = m.Where("created_at >= ? AND created_at<=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
}
|
||||
res.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
res.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = systemConsts.PageSize
|
||||
}
|
||||
err = m.Page(req.PageNum, req.PageSize).Order("config_id asc").Scan(&res.List)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysConfig) Add(ctx context.Context, req *system.ConfigAddReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.CheckConfigKeyUnique(ctx, req.ConfigKey)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
_, err = dao.SysConfig.Ctx(ctx).Insert(do.SysConfig{
|
||||
ConfigName: req.ConfigName,
|
||||
ConfigKey: req.ConfigKey,
|
||||
ConfigValue: req.ConfigValue,
|
||||
ConfigType: req.ConfigType,
|
||||
CreateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加系统参数失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysConfigTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// CheckConfigKeyUnique 验证参数键名是否存在
|
||||
func (s *sSysConfig) CheckConfigKeyUnique(ctx context.Context, configKey string, configId ...int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
data := (*entity.SysConfig)(nil)
|
||||
m := dao.SysConfig.Ctx(ctx).Fields(dao.SysConfig.Columns().ConfigId).Where(dao.SysConfig.Columns().ConfigKey, configKey)
|
||||
if len(configId) > 0 {
|
||||
m = m.Where(dao.SysConfig.Columns().ConfigId+" != ?", configId[0])
|
||||
}
|
||||
err = m.Scan(&data)
|
||||
liberr.ErrIsNil(ctx, err, "校验失败")
|
||||
if data != nil {
|
||||
liberr.ErrIsNil(ctx, errors.New("参数键名重复"))
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Get 获取系统参数
|
||||
func (s *sSysConfig) Get(ctx context.Context, id int) (res *system.ConfigGetRes, err error) {
|
||||
res = new(system.ConfigGetRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.SysConfig.Ctx(ctx).WherePri(id).Scan(&res.Data)
|
||||
liberr.ErrIsNil(ctx, err, "获取系统参数失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改系统参数
|
||||
func (s *sSysConfig) Edit(ctx context.Context, req *system.ConfigEditReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.CheckConfigKeyUnique(ctx, req.ConfigKey, req.ConfigId)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
_, err = dao.SysConfig.Ctx(ctx).WherePri(req.ConfigId).Update(do.SysConfig{
|
||||
ConfigName: req.ConfigName,
|
||||
ConfigKey: req.ConfigKey,
|
||||
ConfigValue: req.ConfigValue,
|
||||
ConfigType: req.ConfigType,
|
||||
UpdateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改系统参数失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysConfigTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除系统参数
|
||||
func (s *sSysConfig) Delete(ctx context.Context, ids []int) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.SysConfig.Ctx(ctx).Delete(dao.SysConfig.Columns().ConfigId+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysConfigTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// GetConfigByKey 通过key获取参数(从缓存获取)
|
||||
func (s *sSysConfig) GetConfigByKey(ctx context.Context, key string) (config *entity.SysConfig, err error) {
|
||||
if key == "" {
|
||||
err = gerror.New("参数key不能为空")
|
||||
return
|
||||
}
|
||||
cache := service.Cache()
|
||||
cf := cache.Get(ctx, consts.CacheSysConfigTag+key)
|
||||
if cf != nil && !cf.IsEmpty() {
|
||||
err = gconv.Struct(cf, &config)
|
||||
return
|
||||
}
|
||||
config, err = s.GetByKey(ctx, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if config != nil {
|
||||
cache.Set(ctx, consts.CacheSysConfigTag+key, config, 0, consts.CacheSysConfigTag)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByKey 通过key获取参数(从数据库获取)
|
||||
func (s *sSysConfig) GetByKey(ctx context.Context, key string) (config *entity.SysConfig, err error) {
|
||||
err = dao.SysConfig.Ctx(ctx).Where("config_key", key).Scan(&config)
|
||||
if err != nil {
|
||||
g.Log().Error(ctx, err)
|
||||
err = gerror.New("获取配置失败")
|
||||
}
|
||||
return
|
||||
}
|
173
internal/app/common/logic/sysDictData/sys_dict_data.go
Normal file
173
internal/app/common/logic/sysDictData/sys_dict_data.go
Normal file
@ -0,0 +1,173 @@
|
||||
/*
|
||||
* @desc:字典数据管理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:22
|
||||
*/
|
||||
|
||||
package sysDictData
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
systemConsts "github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterSysDictData(New())
|
||||
}
|
||||
|
||||
func New() *sSysDictData {
|
||||
return &sSysDictData{}
|
||||
}
|
||||
|
||||
type sSysDictData struct {
|
||||
}
|
||||
|
||||
// GetDictWithDataByType 通过字典键类型获取选项
|
||||
func (s *sSysDictData) GetDictWithDataByType(ctx context.Context, req *system.GetDictReq) (dict *system.GetDictRes,
|
||||
err error) {
|
||||
cache := service.Cache()
|
||||
cacheKey := consts.CacheSysDict + "_" + req.DictType
|
||||
//从缓存获取
|
||||
iDict := cache.GetOrSetFuncLock(ctx, cacheKey, func(ctx context.Context) (value interface{}, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//从数据库获取
|
||||
dict = &system.GetDictRes{}
|
||||
//获取类型数据
|
||||
err = dao.SysDictType.Ctx(ctx).Where(dao.SysDictType.Columns().DictType, req.DictType).
|
||||
Where(dao.SysDictType.Columns().Status, 1).Fields(model.DictTypeRes{}).Scan(&dict.Info)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型失败")
|
||||
err = dao.SysDictData.Ctx(ctx).Fields(model.DictDataRes{}).
|
||||
Where(dao.SysDictData.Columns().DictType, req.DictType).
|
||||
Order(dao.SysDictData.Columns().DictSort + " asc," +
|
||||
dao.SysDictData.Columns().DictCode + " asc").
|
||||
Scan(&dict.Values)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
})
|
||||
value = dict
|
||||
return
|
||||
}, 0, consts.CacheSysDictTag)
|
||||
if iDict != nil {
|
||||
err = gconv.Struct(iDict, &dict)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
//设置给定的默认值
|
||||
for _, v := range dict.Values {
|
||||
if req.DefaultValue != "" {
|
||||
if gstr.Equal(req.DefaultValue, v.DictValue) {
|
||||
v.IsDefault = 1
|
||||
} else {
|
||||
v.IsDefault = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取字典数据
|
||||
func (s *sSysDictData) List(ctx context.Context, req *system.DictDataSearchReq) (res *system.DictDataSearchRes, err error) {
|
||||
res = new(system.DictDataSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.SysDictData.Ctx(ctx)
|
||||
if req != nil {
|
||||
if req.DictLabel != "" {
|
||||
m = m.Where(dao.SysDictData.Columns().DictLabel+" like ?", "%"+req.DictLabel+"%")
|
||||
}
|
||||
if req.Status != "" {
|
||||
m = m.Where(dao.SysDictData.Columns().Status+" = ", gconv.Int(req.Status))
|
||||
}
|
||||
if req.DictType != "" {
|
||||
m = m.Where(dao.SysDictData.Columns().DictType+" = ?", req.DictType)
|
||||
}
|
||||
res.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
res.CurrentPage = req.PageNum
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = systemConsts.PageSize
|
||||
}
|
||||
err = m.Page(req.PageNum, req.PageSize).Order(dao.SysDictData.Columns().DictSort + " asc," +
|
||||
dao.SysDictData.Columns().DictCode + " asc").Scan(&res.List)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysDictData) Add(ctx context.Context, req *system.DictDataAddReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.SysDictData.Ctx(ctx).Insert(do.SysDictData{
|
||||
DictSort: req.DictSort,
|
||||
DictLabel: req.DictLabel,
|
||||
DictValue: req.DictValue,
|
||||
DictType: req.DictType,
|
||||
CssClass: req.CssClass,
|
||||
ListClass: req.ListClass,
|
||||
IsDefault: req.IsDefault,
|
||||
Status: req.Status,
|
||||
CreateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加字典数据失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Get 获取字典数据
|
||||
func (s *sSysDictData) Get(ctx context.Context, dictCode uint) (res *system.DictDataGetRes, err error) {
|
||||
res = new(system.DictDataGetRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.SysDictData.Ctx(ctx).WherePri(dictCode).Scan(&res.Dict)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典数据失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改字典数据
|
||||
func (s *sSysDictData) Edit(ctx context.Context, req *system.DictDataEditReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.SysDictData.Ctx(ctx).WherePri(req.DictCode).Update(do.SysDictData{
|
||||
DictSort: req.DictSort,
|
||||
DictLabel: req.DictLabel,
|
||||
DictValue: req.DictValue,
|
||||
DictType: req.DictType,
|
||||
CssClass: req.CssClass,
|
||||
ListClass: req.ListClass,
|
||||
IsDefault: req.IsDefault,
|
||||
Status: req.Status,
|
||||
UpdateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改字典数据失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除字典数据
|
||||
func (s *sSysDictData) Delete(ctx context.Context, ids []int) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.SysDictData.Ctx(ctx).Where(dao.SysDictData.Columns().DictCode+" in(?)", ids).Delete()
|
||||
liberr.ErrIsNil(ctx, err, "删除字典数据失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
186
internal/app/common/logic/sysDictType/sys_dict_type.go
Normal file
186
internal/app/common/logic/sysDictType/sys_dict_type.go
Normal file
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* @desc:字典类型管理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:26
|
||||
*/
|
||||
|
||||
package sysDictType
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
systemConsts "github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterSysDictType(New())
|
||||
}
|
||||
|
||||
func New() *sSysDictType {
|
||||
return &sSysDictType{}
|
||||
}
|
||||
|
||||
type sSysDictType struct {
|
||||
}
|
||||
|
||||
// List 字典类型列表
|
||||
func (s *sSysDictType) List(ctx context.Context, req *system.DictTypeSearchReq) (res *system.DictTypeSearchRes, err error) {
|
||||
res = new(system.DictTypeSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.SysDictType.Ctx(ctx)
|
||||
if req.DictName != "" {
|
||||
m = m.Where(dao.SysDictType.Columns().DictName+" like ?", "%"+req.DictName+"%")
|
||||
}
|
||||
if req.DictType != "" {
|
||||
m = m.Where(dao.SysDictType.Columns().DictType+" like ?", "%"+req.DictType+"%")
|
||||
}
|
||||
if req.Status != "" {
|
||||
m = m.Where(dao.SysDictType.Columns().Status+" = ", gconv.Int(req.Status))
|
||||
}
|
||||
res.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
res.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = systemConsts.PageSize
|
||||
}
|
||||
err = m.Fields(model.SysDictTypeInfoRes{}).Page(req.PageNum, req.PageSize).
|
||||
Order(dao.SysDictType.Columns().DictId + " asc").Scan(&res.DictTypeList)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Add 添加字典类型
|
||||
func (s *sSysDictType) Add(ctx context.Context, req *system.DictTypeAddReq, userId uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.ExistsDictType(ctx, req.DictType)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
_, err = dao.SysDictType.Ctx(ctx).Insert(do.SysDictType{
|
||||
DictName: req.DictName,
|
||||
DictType: req.DictType,
|
||||
Status: req.Status,
|
||||
CreateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加字典类型失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改字典类型
|
||||
func (s *sSysDictType) Edit(ctx context.Context, req *system.DictTypeEditReq, userId uint64) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = s.ExistsDictType(ctx, req.DictType, req.DictId)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
dictType := (*entity.SysDictType)(nil)
|
||||
e := dao.SysDictType.Ctx(ctx).Fields(dao.SysDictType.Columns().DictType).WherePri(req.DictId).Scan(&dictType)
|
||||
liberr.ErrIsNil(ctx, e, "获取字典类型失败")
|
||||
liberr.ValueIsNil(dictType, "字典类型不存在")
|
||||
//修改字典类型
|
||||
_, e = dao.SysDictType.Ctx(ctx).TX(tx).WherePri(req.DictId).Update(do.SysDictType{
|
||||
DictName: req.DictName,
|
||||
DictType: req.DictType,
|
||||
Status: req.Status,
|
||||
UpdateBy: userId,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, e, "修改字典类型失败")
|
||||
//修改字典数据
|
||||
_, e = dao.SysDictData.Ctx(ctx).TX(tx).Data(do.SysDictData{DictType: req.DictType}).
|
||||
Where(dao.SysDictData.Columns().DictType, dictType.DictType).Update()
|
||||
liberr.ErrIsNil(ctx, e, "修改字典数据失败")
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysDictType) Get(ctx context.Context, req *system.DictTypeGetReq) (dictType *entity.SysDictType, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.SysDictType.Ctx(ctx).Where(dao.SysDictType.Columns().DictId, req.DictId).Scan(&dictType)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// ExistsDictType 检查类型是否已经存在
|
||||
func (s *sSysDictType) ExistsDictType(ctx context.Context, dictType string, dictId ...int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.SysDictType.Ctx(ctx).Fields(dao.SysDictType.Columns().DictId).
|
||||
Where(dao.SysDictType.Columns().DictType, dictType)
|
||||
if len(dictId) > 0 {
|
||||
m = m.Where(dao.SysDictType.Columns().DictId+" !=? ", dictId[0])
|
||||
}
|
||||
res, e := m.One()
|
||||
liberr.ErrIsNil(ctx, e, "sql err")
|
||||
if !res.IsEmpty() {
|
||||
liberr.ErrIsNil(ctx, gerror.New("字典类型已存在"))
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除字典类型
|
||||
func (s *sSysDictType) Delete(ctx context.Context, dictIds []int) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
discs := ([]*entity.SysDictType)(nil)
|
||||
err = dao.SysDictType.Ctx(ctx).Fields(dao.SysDictType.Columns().DictType).
|
||||
Where(dao.SysDictType.Columns().DictId+" in (?) ", dictIds).Scan(&discs)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
types := garray.NewStrArray()
|
||||
for _, dt := range discs {
|
||||
types.Append(dt.DictType)
|
||||
}
|
||||
if types.Len() > 0 {
|
||||
_, err = dao.SysDictType.Ctx(ctx).TX(tx).Delete(dao.SysDictType.Columns().DictId+" in (?) ", dictIds)
|
||||
liberr.ErrIsNil(ctx, err, "删除类型失败")
|
||||
_, err = dao.SysDictData.Ctx(ctx).TX(tx).Delete(dao.SysDictData.Columns().DictType+" in (?) ", types.Slice())
|
||||
liberr.ErrIsNil(ctx, err, "删除字典数据失败")
|
||||
}
|
||||
//清除缓存
|
||||
service.Cache().RemoveByTag(ctx, consts.CacheSysDictTag)
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllDictType 获取所有正常状态下的字典类型
|
||||
func (s *sSysDictType) GetAllDictType(ctx context.Context) (list []*entity.SysDictType, err error) {
|
||||
cache := service.Cache()
|
||||
//从缓存获取
|
||||
data := cache.Get(ctx, consts.CacheSysDict+"_dict_type_all")
|
||||
if !data.IsNil() {
|
||||
err = data.Structs(&list)
|
||||
return
|
||||
}
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.SysDictType.Ctx(ctx).Where("status", 1).Order("dict_id ASC").Scan(&list)
|
||||
liberr.ErrIsNil(ctx, err, "获取字典类型数据出错")
|
||||
//缓存
|
||||
cache.Set(ctx, consts.CacheSysDict+"_dict_type_all", list, 0, consts.CacheSysDictTag)
|
||||
})
|
||||
return
|
||||
}
|
201
internal/app/common/logic/upload/upload.go
Normal file
201
internal/app/common/logic/upload/upload.go
Normal file
@ -0,0 +1,201 @@
|
||||
/*
|
||||
* @desc:上传处理
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu<yxh669@qq.com>
|
||||
* @Date: 2022/9/28 9:37
|
||||
*/
|
||||
|
||||
package upload
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/model/entity"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
"github.com/tiger1103/gfast/v3/library/upload"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterUpload(New())
|
||||
}
|
||||
|
||||
func New() *sUpload {
|
||||
return &sUpload{}
|
||||
}
|
||||
|
||||
type sUpload struct{}
|
||||
|
||||
// UploadFiles 上传多文件
|
||||
func (s *sUpload) UploadFiles(ctx context.Context, files []*ghttp.UploadFile, checkFileType string, source int) (result system.UploadMultipleRes, err error) {
|
||||
for _, item := range files {
|
||||
f, e := s.UploadFile(ctx, item, checkFileType, source)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
result = append(result, &f)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UploadFile 上传单文件
|
||||
func (s *sUpload) UploadFile(ctx context.Context, file *ghttp.UploadFile, checkFileType string, source int) (result system.UploadResponse, err error) {
|
||||
|
||||
// 检查文件类型
|
||||
err = s.CheckType(ctx, checkFileType, file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 检查文件大小
|
||||
err = s.CheckSize(ctx, checkFileType, file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uploader := upload.GetUploader(upload.UploaderType(source))
|
||||
if uploader == nil {
|
||||
err = errors.New("没有找到上传适配器")
|
||||
return
|
||||
}
|
||||
return uploader.Upload(ctx, file)
|
||||
}
|
||||
|
||||
// CheckSize 检查上传文件大小
|
||||
func (s *sUpload) CheckSize(ctx context.Context, checkFileType string, file *ghttp.UploadFile) (err error) {
|
||||
|
||||
var (
|
||||
configSize *entity.SysConfig
|
||||
)
|
||||
|
||||
if checkFileType == consts.CheckFileTypeFile {
|
||||
|
||||
//获取上传大小配置
|
||||
configSize, err = s.getUpConfig(ctx, consts.FileSizeKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else if checkFileType == consts.CheckFileTypeImg {
|
||||
|
||||
//获取上传大小配置
|
||||
configSize, err = s.getUpConfig(ctx, consts.ImgSizeKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
return errors.New(fmt.Sprintf("文件检查类型错误:%s|%s", consts.CheckFileTypeFile, consts.CheckFileTypeImg))
|
||||
}
|
||||
|
||||
var rightSize bool
|
||||
rightSize, err = s.checkSize(configSize.ConfigValue, file.Size)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !rightSize {
|
||||
err = gerror.New("上传文件超过最大尺寸:" + configSize.ConfigValue)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CheckType 检查上传文件类型
|
||||
func (s *sUpload) CheckType(ctx context.Context, checkFileType string, file *ghttp.UploadFile) (err error) {
|
||||
|
||||
var (
|
||||
configType *entity.SysConfig
|
||||
)
|
||||
|
||||
if checkFileType == consts.CheckFileTypeFile {
|
||||
//获取上传类型配置
|
||||
configType, err = s.getUpConfig(ctx, consts.FileTypeKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
} else if checkFileType == consts.CheckFileTypeImg {
|
||||
//获取上传类型配置
|
||||
configType, err = s.getUpConfig(ctx, consts.ImgTypeKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
return errors.New(fmt.Sprintf("文件检查类型错误:%s|%s", consts.CheckFileTypeFile, consts.CheckFileTypeImg))
|
||||
}
|
||||
|
||||
rightType := s.checkFileType(file.Filename, configType.ConfigValue)
|
||||
if !rightType {
|
||||
err = gerror.New("上传文件类型错误,只能包含后缀为:" + configType.ConfigValue + "的文件。")
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 获取上传配置
|
||||
func (s *sUpload) getUpConfig(ctx context.Context, key string) (config *entity.SysConfig, err error) {
|
||||
config, err = service.SysConfig().GetConfigByKey(ctx, key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if config == nil {
|
||||
err = gerror.New("上传文件类型未设置,请在后台配置")
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 判断上传文件类型是否合法
|
||||
func (s *sUpload) checkFileType(fileName, typeString string) bool {
|
||||
suffix := gstr.SubStrRune(fileName, gstr.PosRRune(fileName, ".")+1, gstr.LenRune(fileName)-1)
|
||||
imageType := gstr.Split(typeString, ",")
|
||||
rightType := false
|
||||
for _, v := range imageType {
|
||||
if gstr.Equal(suffix, v) {
|
||||
rightType = true
|
||||
break
|
||||
}
|
||||
}
|
||||
return rightType
|
||||
}
|
||||
|
||||
// 检查文件大小是否合法
|
||||
func (s *sUpload) checkSize(configSize string, fileSize int64) (bool, error) {
|
||||
match, err := gregex.MatchString(`^([0-9]+)(?i:([a-z]*))$`, configSize)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(match) == 0 {
|
||||
err = gerror.New("上传文件大小未设置,请在后台配置,格式为(30M,30k,30MB)")
|
||||
return false, err
|
||||
}
|
||||
var cfSize int64
|
||||
switch gstr.ToUpper(match[2]) {
|
||||
case "MB", "M":
|
||||
cfSize = gconv.Int64(match[1]) * 1024 * 1024
|
||||
case "KB", "K":
|
||||
cfSize = gconv.Int64(match[1]) * 1024
|
||||
case "":
|
||||
cfSize = gconv.Int64(match[1])
|
||||
}
|
||||
if cfSize == 0 {
|
||||
err = gerror.New("上传文件大小未设置,请在后台配置,格式为(30M,30k,30MB),最大单位为MB")
|
||||
return false, err
|
||||
}
|
||||
return cfSize >= fileSize, nil
|
||||
}
|
||||
|
||||
// 静态文件夹目录
|
||||
func (s *sUpload) getStaticPath(ctx context.Context) string {
|
||||
value, _ := g.Cfg().Get(ctx, "server.serverRoot")
|
||||
if !value.IsEmpty() {
|
||||
return value.String()
|
||||
}
|
||||
return ""
|
||||
}
|
Reference in New Issue
Block a user