mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 05:12:32 +08:00
模块化上传驱动,使用泛型优化工具库降低冗余
This commit is contained in:
@@ -90,7 +90,6 @@ func (s *sSysAddons) List(ctx context.Context, in sysin.AddonsListInp) (list []*
|
||||
}
|
||||
|
||||
totalCount = len(sks)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -129,7 +128,6 @@ func (s *sSysAddons) Build(ctx context.Context, in sysin.AddonsBuildInp) (err er
|
||||
err = gerror.New("没有找到有效的生成或插件配置,请检查配置文件是否正常")
|
||||
return
|
||||
}
|
||||
|
||||
return addons.Build(ctx, in.Skeleton, genConfig.Addon)
|
||||
}
|
||||
|
||||
|
@@ -54,8 +54,8 @@ func (s *sSysAddonsConfig) GetConfigByGroup(ctx context.Context, in sysin.GetAdd
|
||||
Scan(&models); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
isDemo := g.Cfg().MustGet(ctx, "hotgo.isDemo", false)
|
||||
|
||||
isDemo := g.Cfg().MustGet(ctx, "hotgo.isDemo", false)
|
||||
if len(models) > 0 {
|
||||
res = new(sysin.GetAddonsConfigModel)
|
||||
res.List = make(g.Map, len(models))
|
||||
@@ -71,7 +71,6 @@ func (s *sSysAddonsConfig) GetConfigByGroup(ctx context.Context, in sysin.GetAdd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -105,10 +104,10 @@ func (s *sSysAddonsConfig) UpdateConfigByGroup(ctx context.Context, in sysin.Upd
|
||||
Where("addon_name", in.AddonName).
|
||||
Where("group", in.Group).
|
||||
Scan(&models); err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
err = dao.SysAddonsConfig.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = dao.SysAddonsConfig.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
for k, v := range in.List {
|
||||
row := s.getConfigByKey(k, models)
|
||||
// 新增
|
||||
@@ -130,20 +129,15 @@ func (s *sSysAddonsConfig) UpdateConfigByGroup(ctx context.Context, in sysin.Upd
|
||||
}
|
||||
|
||||
// 更新
|
||||
_, err = dao.SysAddonsConfig.Ctx(ctx).Where("id", row.Id).Data(g.Map{"value": v, "updated_at": gtime.Now()}).Update()
|
||||
if err != nil {
|
||||
if _, err = dao.SysAddonsConfig.Ctx(ctx).Where("id", row.Id).Data(g.Map{"value": v, "updated_at": gtime.Now()}).Update(); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysAddonsConfig) getConfigByKey(key string, models []*entity.SysAddonsConfig) *entity.SysAddonsConfig {
|
||||
@@ -156,6 +150,5 @@ func (s *sSysAddonsConfig) getConfigByKey(key string, models []*entity.SysAddons
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -9,17 +9,12 @@ import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/hgorm/handler"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/form"
|
||||
"hotgo/internal/library/storager"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/format"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
type sSysAttachment struct{}
|
||||
@@ -37,92 +32,41 @@ func (s *sSysAttachment) Model(ctx context.Context, option ...*handler.Option) *
|
||||
return handler.Model(dao.SysAttachment.Ctx(ctx), option...)
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
// Delete 删除附件
|
||||
func (s *sSysAttachment) Delete(ctx context.Context, in sysin.AttachmentDeleteInp) (err error) {
|
||||
_, err = s.Model(ctx).Where("id", in.Id).Delete()
|
||||
if _, err = s.Model(ctx).WherePri(in.Id).Delete(); err != nil {
|
||||
err = gerror.Wrap(err, "删除附件失败,请稍后重试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Edit 修改/新增
|
||||
func (s *sSysAttachment) Edit(ctx context.Context, in sysin.AttachmentEditInp) (err error) {
|
||||
if in.Name == "" {
|
||||
err = gerror.New("标题不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 修改
|
||||
if in.Id > 0 {
|
||||
_, err = s.Model(ctx).Where("id", in.Id).Data(in).Update()
|
||||
return
|
||||
}
|
||||
|
||||
// 新增
|
||||
_, err = dao.SysAttachment.Ctx(ctx).Data(in).Insert()
|
||||
return
|
||||
}
|
||||
|
||||
// Status 更新部门状态
|
||||
func (s *sSysAttachment) Status(ctx context.Context, in sysin.AttachmentStatusInp) (err error) {
|
||||
if in.Id <= 0 {
|
||||
err = gerror.New("ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
if in.Status <= 0 {
|
||||
err = gerror.New("状态不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
if !validate.InSliceInt(consts.StatusSlice, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return
|
||||
}
|
||||
|
||||
// 修改
|
||||
_, err = s.Model(ctx).Where("id", in.Id).Data("status", in.Status).Update()
|
||||
return
|
||||
}
|
||||
|
||||
// MaxSort 最大排序
|
||||
func (s *sSysAttachment) MaxSort(ctx context.Context, in sysin.AttachmentMaxSortInp) (res *sysin.AttachmentMaxSortModel, err error) {
|
||||
if in.Id > 0 {
|
||||
if err = s.Model(ctx).Where("id", in.Id).Order("sort desc").Scan(&res); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if res == nil {
|
||||
res = new(sysin.AttachmentMaxSortModel)
|
||||
}
|
||||
res.Sort = form.DefaultMaxSort(ctx, res.Sort)
|
||||
return
|
||||
}
|
||||
|
||||
// View 获取指定字典类型信息
|
||||
// View 获取附件信息
|
||||
func (s *sSysAttachment) View(ctx context.Context, in sysin.AttachmentViewInp) (res *sysin.AttachmentViewModel, err error) {
|
||||
err = s.Model(ctx).Where("id", in.Id).Scan(&res)
|
||||
if err = s.Model(ctx).WherePri(in.Id).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, "获取附件信息失败,请稍后重试!")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
// List 获取附件列表
|
||||
func (s *sSysAttachment) List(ctx context.Context, in sysin.AttachmentListInp) (list []*sysin.AttachmentListModel, totalCount int, err error) {
|
||||
mod := s.Model(ctx)
|
||||
|
||||
if in.MemberId > 0 {
|
||||
mod = mod.Where("member_id", in.MemberId)
|
||||
mod = mod.Where(dao.SysAttachment.Columns().MemberId, in.MemberId)
|
||||
}
|
||||
|
||||
if in.Drive != "" {
|
||||
mod = mod.Where("drive", in.Drive)
|
||||
mod = mod.Where(dao.SysAttachment.Columns().Drive, in.Drive)
|
||||
}
|
||||
|
||||
if in.Status > 0 {
|
||||
mod = mod.Where("status", in.Status)
|
||||
mod = mod.Where(dao.SysAttachment.Columns().Status, in.Status)
|
||||
}
|
||||
|
||||
totalCount, err = mod.Count()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
err = gerror.Wrap(err, "获取附件数据行失败!")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -130,58 +74,14 @@ func (s *sSysAttachment) List(ctx context.Context, in sysin.AttachmentListInp) (
|
||||
return
|
||||
}
|
||||
|
||||
if err = mod.Page(in.Page, in.PerPage).Order("updated_at desc").Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return
|
||||
}
|
||||
|
||||
conf, err := service.SysConfig().GetUpload(ctx)
|
||||
if err != nil {
|
||||
if err = mod.Page(in.Page, in.PerPage).OrderDesc(dao.SysAttachment.Columns().UpdatedAt).Scan(&list); err != nil {
|
||||
err = gerror.Wrap(err, "获取附件列表失败!")
|
||||
return
|
||||
}
|
||||
|
||||
for _, v := range list {
|
||||
v.SizeFormat = format.FileSize(v.Size)
|
||||
v.FileUrl = service.CommonUpload().LastUrl(ctx, conf, v.FileUrl, v.Drive)
|
||||
v.FileUrl = storager.LastUrl(ctx, v.FileUrl, v.Drive)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Add 新增附件
|
||||
func (s *sSysAttachment) Add(ctx context.Context, meta *sysin.UploadFileMeta, fullPath, drive string) (models *entity.SysAttachment, err error) {
|
||||
var (
|
||||
c = contexts.Get(ctx)
|
||||
user = c.User
|
||||
memberId int64 = 0
|
||||
)
|
||||
|
||||
if user != nil {
|
||||
memberId = user.Id
|
||||
}
|
||||
|
||||
models = &entity.SysAttachment{
|
||||
Id: 0,
|
||||
AppId: c.Module,
|
||||
MemberId: memberId,
|
||||
Drive: drive,
|
||||
Size: meta.Size,
|
||||
Path: fullPath,
|
||||
FileUrl: fullPath,
|
||||
Name: meta.Filename,
|
||||
Kind: meta.Kind,
|
||||
MetaType: meta.MetaType,
|
||||
NaiveType: meta.NaiveType,
|
||||
Ext: meta.Ext,
|
||||
Md5: meta.Md5,
|
||||
Status: consts.StatusEnabled,
|
||||
CreatedAt: gtime.Now(),
|
||||
UpdatedAt: gtime.Now(),
|
||||
}
|
||||
id, err := dao.SysAttachment.Ctx(ctx).Data(models).InsertAndGetId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
models.Id = id
|
||||
return
|
||||
}
|
||||
|
@@ -71,7 +71,7 @@ func (s *sSysBlacklist) Status(ctx context.Context, in sysin.BlacklistStatusInp)
|
||||
return
|
||||
}
|
||||
|
||||
if !validate.InSliceInt(consts.StatusSlice, in.Status) {
|
||||
if !validate.InSlice(consts.StatusSlice, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ import (
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/payment"
|
||||
"hotgo/internal/library/storager"
|
||||
"hotgo/internal/library/token"
|
||||
"hotgo/internal/library/wechat"
|
||||
"hotgo/internal/model"
|
||||
@@ -35,6 +36,7 @@ func init() {
|
||||
service.RegisterSysConfig(NewSysConfig())
|
||||
}
|
||||
|
||||
// InitConfig 初始化一些系统启动就需要用到的配置
|
||||
func (s *sSysConfig) InitConfig(ctx context.Context) {
|
||||
wx, err := s.GetWechat(ctx)
|
||||
if err != nil {
|
||||
@@ -48,12 +50,17 @@ func (s *sSysConfig) InitConfig(ctx context.Context) {
|
||||
}
|
||||
payment.SetConfig(pay)
|
||||
|
||||
upload, err := s.GetUpload(ctx)
|
||||
if err != nil {
|
||||
g.Log().Fatalf(ctx, "init upload conifg fail:%+v", err)
|
||||
}
|
||||
storager.SetConfig(upload)
|
||||
|
||||
tk, err := s.GetLoadToken(ctx)
|
||||
if err != nil {
|
||||
g.Log().Fatalf(ctx, "init token conifg fail:%+v", err)
|
||||
}
|
||||
token.SetConfig(tk)
|
||||
|
||||
}
|
||||
|
||||
// GetLogin 获取登录配置
|
||||
@@ -267,7 +274,6 @@ func (s *sSysConfig) UpdateConfigByGroup(ctx context.Context, in sysin.UpdateCon
|
||||
|
||||
return s.syncUpdate(ctx, in)
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -281,7 +287,6 @@ func (s *sSysConfig) getConfigByKey(key string, models []*entity.SysConfig) *ent
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -298,6 +303,11 @@ func (s *sSysConfig) syncUpdate(ctx context.Context, in sysin.UpdateConfigInp) (
|
||||
if err == nil {
|
||||
payment.SetConfig(pay)
|
||||
}
|
||||
case "upload":
|
||||
upload, err := s.GetUpload(ctx)
|
||||
if err == nil {
|
||||
storager.SetConfig(upload)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
@@ -106,7 +106,7 @@ func (s *sSysCron) Status(ctx context.Context, in sysin.CronStatusInp) (err erro
|
||||
return
|
||||
}
|
||||
|
||||
if !validate.InSliceInt(consts.StatusSlice, in.Status) {
|
||||
if !validate.InSlice(consts.StatusSlice, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return
|
||||
}
|
||||
|
@@ -46,7 +46,6 @@ func (s *sSysCronGroup) Edit(ctx context.Context, in sysin.CronGroupEditInp) (er
|
||||
if _, err = dao.SysCronGroup.Ctx(ctx).Fields(sysin.CronGroupInsertFields{}).Data(in).Insert(); err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -198,7 +198,7 @@ func (s *sSysCurdDemo) Switch(ctx context.Context, in sysin.CurdDemoSwitchInp) (
|
||||
// ...
|
||||
}
|
||||
|
||||
if !validate.InSliceString(fields, in.Key) {
|
||||
if !validate.InSlice(fields, in.Key) {
|
||||
err = gerror.New("开关键名不在白名单")
|
||||
return
|
||||
}
|
||||
|
@@ -44,7 +44,6 @@ func (s *sSysDictData) Edit(ctx context.Context, in sysin.DictDataEditInp) (err
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -76,7 +76,7 @@ func (s *sSysEmsLog) Status(ctx context.Context, in sysin.EmsLogStatusInp) (err
|
||||
return
|
||||
}
|
||||
|
||||
if !validate.InSliceInt(consts.StatusSlice, in.Status) {
|
||||
if !validate.InSlice(consts.StatusSlice, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return
|
||||
}
|
||||
|
@@ -110,7 +110,7 @@ func (s *sSysGenCodes) Status(ctx context.Context, in sysin.GenCodesStatusInp) (
|
||||
return
|
||||
}
|
||||
|
||||
if !validate.InSliceInt(consts.StatusSlice, in.Status) {
|
||||
if !validate.InSlice(consts.StatusSlice, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return
|
||||
}
|
||||
@@ -274,7 +274,6 @@ func (s *sSysGenCodes) ColumnSelect(ctx context.Context, in sysin.GenCodesColumn
|
||||
res[k].Name = fmt.Sprintf("%s (%s)", v.Value, v.Label)
|
||||
res[k].Label = res[k].Name
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -306,6 +305,5 @@ func (s *sSysGenCodes) Build(ctx context.Context, in sysin.GenCodesBuildInp) (er
|
||||
_ = s.Status(ctx, sysin.GenCodesStatusInp{Id: in.Id, Status: consts.GenCodesStatusFail})
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@@ -230,7 +230,6 @@ func (s *sSysLog) AnalysisLog(ctx context.Context) entity.SysLog {
|
||||
Status: consts.StatusEnabled,
|
||||
TakeUpTime: takeUpTime,
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -241,8 +240,7 @@ func (s *sSysLog) View(ctx context.Context, in sysin.LogViewInp) (res *sysin.Log
|
||||
return
|
||||
}
|
||||
|
||||
isDemo := g.Cfg().MustGet(ctx, "hotgo.isDemo", false)
|
||||
if isDemo.Bool() {
|
||||
if g.Cfg().MustGet(ctx, "hotgo.isDemo", false).Bool() {
|
||||
res.HeaderData = gjson.New(`{
|
||||
"none": [
|
||||
"` + consts.DemoTips + `"
|
||||
@@ -364,6 +362,5 @@ func (s *sSysLog) List(ctx context.Context, in sysin.LogListInp) (list []*sysin.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
@@ -107,7 +107,6 @@ func (s *sSysLoginLog) List(ctx context.Context, in sysin.LoginLogListInp) (list
|
||||
v.Os = useragent.GetOs(v.SysLogUserAgent)
|
||||
v.Browser = useragent.GetBrowser(v.SysLogUserAgent)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -71,7 +71,7 @@ func (s *sSysSmsLog) Status(ctx context.Context, in sysin.SmsLogStatusInp) (err
|
||||
return
|
||||
}
|
||||
|
||||
if !validate.InSliceInt(consts.StatusSlice, in.Status) {
|
||||
if !validate.InSlice(consts.StatusSlice, in.Status) {
|
||||
err = gerror.New("状态不正确")
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user