2022-11-24 23:37:34 +08:00
|
|
|
|
// Package sys
|
|
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
2023-02-23 17:53:04 +08:00
|
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// @Author Ms <133814250@qq.com>
|
|
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
|
|
|
|
package sys
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"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/os/gtime"
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
"hotgo/internal/consts"
|
|
|
|
|
"hotgo/internal/dao"
|
2023-05-10 23:54:50 +08:00
|
|
|
|
"hotgo/internal/library/payment"
|
2023-06-02 20:29:08 +08:00
|
|
|
|
"hotgo/internal/library/storager"
|
2023-05-12 16:20:22 +08:00
|
|
|
|
"hotgo/internal/library/token"
|
2023-05-10 23:54:50 +08:00
|
|
|
|
"hotgo/internal/library/wechat"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
"hotgo/internal/model"
|
|
|
|
|
"hotgo/internal/model/entity"
|
|
|
|
|
"hotgo/internal/model/input/sysin"
|
|
|
|
|
"hotgo/internal/service"
|
2023-05-29 20:24:13 +08:00
|
|
|
|
"hotgo/utility/simple"
|
2022-11-24 23:37:34 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type sSysConfig struct{}
|
|
|
|
|
|
|
|
|
|
func NewSysConfig() *sSysConfig {
|
|
|
|
|
return &sSysConfig{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
service.RegisterSysConfig(NewSysConfig())
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 20:29:08 +08:00
|
|
|
|
// InitConfig 初始化一些系统启动就需要用到的配置
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sSysConfig) InitConfig(ctx context.Context) {
|
|
|
|
|
wx, err := s.GetWechat(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Fatalf(ctx, "init wechat conifg fail:%+v", err)
|
|
|
|
|
}
|
|
|
|
|
wechat.SetConfig(wx)
|
|
|
|
|
|
|
|
|
|
pay, err := s.GetPay(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Fatalf(ctx, "init pay conifg fail:%+v", err)
|
|
|
|
|
}
|
|
|
|
|
payment.SetConfig(pay)
|
|
|
|
|
|
2023-06-02 20:29:08 +08:00
|
|
|
|
upload, err := s.GetUpload(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Fatalf(ctx, "init upload conifg fail:%+v", err)
|
|
|
|
|
}
|
|
|
|
|
storager.SetConfig(upload)
|
|
|
|
|
|
2023-05-12 16:20:22 +08:00
|
|
|
|
tk, err := s.GetLoadToken(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.Log().Fatalf(ctx, "init token conifg fail:%+v", err)
|
|
|
|
|
}
|
|
|
|
|
token.SetConfig(tk)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-14 23:55:16 +08:00
|
|
|
|
// GetLogin 获取登录配置
|
|
|
|
|
func (s *sSysConfig) GetLogin(ctx context.Context) (conf *model.LoginConfig, err error) {
|
|
|
|
|
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "login"})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = gconv.Scan(models.List, &conf)
|
2023-05-12 16:20:22 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
// GetWechat 获取微信配置
|
|
|
|
|
func (s *sSysConfig) GetWechat(ctx context.Context) (conf *model.WechatConfig, err error) {
|
|
|
|
|
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "wechat"})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = gconv.Scan(models.List, &conf)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-01-18 16:23:39 +08:00
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
// GetPay 获取支付配置
|
|
|
|
|
func (s *sSysConfig) GetPay(ctx context.Context) (conf *model.PayConfig, err error) {
|
|
|
|
|
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "pay"})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = gconv.Scan(models.List, &conf)
|
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetSms 获取短信配置
|
|
|
|
|
func (s *sSysConfig) GetSms(ctx context.Context) (conf *model.SmsConfig, err error) {
|
|
|
|
|
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "sms"})
|
|
|
|
|
if err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = gconv.Scan(models.List, &conf)
|
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetGeo 获取地理配置
|
|
|
|
|
func (s *sSysConfig) GetGeo(ctx context.Context) (conf *model.GeoConfig, err error) {
|
|
|
|
|
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "geo"})
|
|
|
|
|
if err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = gconv.Scan(models.List, &conf)
|
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetUpload 获取上传配置
|
|
|
|
|
func (s *sSysConfig) GetUpload(ctx context.Context) (conf *model.UploadConfig, err error) {
|
|
|
|
|
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "upload"})
|
|
|
|
|
if err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = gconv.Scan(models.List, &conf)
|
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// GetSmtp 获取邮件配置
|
|
|
|
|
func (s *sSysConfig) GetSmtp(ctx context.Context) (conf *model.EmailConfig, err error) {
|
|
|
|
|
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "smtp"})
|
|
|
|
|
if err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
if err = gconv.Scan(models.List, &conf); err != nil {
|
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conf.Addr = fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 20:29:34 +08:00
|
|
|
|
// GetBasic 获取基础配置
|
|
|
|
|
func (s *sSysConfig) GetBasic(ctx context.Context) (conf *model.BasicConfig, err error) {
|
|
|
|
|
models, err := s.GetConfigByGroup(ctx, sysin.GetConfigInp{Group: "basic"})
|
|
|
|
|
if err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2023-02-08 20:29:34 +08:00
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = gconv.Scan(models.List, &conf)
|
|
|
|
|
return
|
2023-02-08 20:29:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-14 23:55:16 +08:00
|
|
|
|
// GetLoadTCP 获取本地tcp配置
|
|
|
|
|
func (s *sSysConfig) GetLoadTCP(ctx context.Context) (conf *model.TCPConfig, err error) {
|
|
|
|
|
err = g.Cfg().MustGet(ctx, "tcp").Scan(&conf)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetLoadCache 获取本地缓存配置
|
|
|
|
|
func (s *sSysConfig) GetLoadCache(ctx context.Context) (conf *model.CacheConfig, err error) {
|
|
|
|
|
err = g.Cfg().MustGet(ctx, "cache").Scan(&conf)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetLoadGenerate 获取本地生成配置
|
|
|
|
|
func (s *sSysConfig) GetLoadGenerate(ctx context.Context) (conf *model.GenerateConfig, err error) {
|
|
|
|
|
err = g.Cfg().MustGet(ctx, "hggen").Scan(&conf)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetLoadToken 获取本地token配置
|
|
|
|
|
func (s *sSysConfig) GetLoadToken(ctx context.Context) (conf *model.TokenConfig, err error) {
|
|
|
|
|
err = g.Cfg().MustGet(ctx, "token").Scan(&conf)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
|
// GetLoadLog 获取本地日志配置
|
|
|
|
|
func (s *sSysConfig) GetLoadLog(ctx context.Context) (conf *model.LogConfig, err error) {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = g.Cfg().MustGet(ctx, "hotgo.log").Scan(&conf)
|
|
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 11:49:21 +08:00
|
|
|
|
// GetLoadServeLog 获取本地服务日志配置
|
|
|
|
|
func (s *sSysConfig) GetLoadServeLog(ctx context.Context) (conf *model.ServeLogConfig, err error) {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = g.Cfg().MustGet(ctx, "hotgo.serveLog").Scan(&conf)
|
|
|
|
|
return
|
2023-01-25 11:49:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 23:37:34 +08:00
|
|
|
|
// GetConfigByGroup 获取指定分组的配置
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sSysConfig) GetConfigByGroup(ctx context.Context, in sysin.GetConfigInp) (res *sysin.GetConfigModel, err error) {
|
2022-11-24 23:37:34 +08:00
|
|
|
|
if in.Group == "" {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = gerror.New("分组不能为空")
|
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
2023-05-29 20:24:13 +08:00
|
|
|
|
var models []*entity.SysConfig
|
2023-05-10 23:54:50 +08:00
|
|
|
|
if err = dao.SysConfig.Ctx(ctx).Fields("key", "value", "type").Where("group", in.Group).Scan(&models); err != nil {
|
2023-05-29 20:24:13 +08:00
|
|
|
|
err = gerror.Wrapf(err, "获取配置分组[ %v ]失败,请稍后重试!", in.Group)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
res = new(sysin.GetConfigModel)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
if len(models) > 0 {
|
|
|
|
|
res.List = make(g.Map, len(models))
|
|
|
|
|
for _, v := range models {
|
|
|
|
|
val, err := s.ConversionType(ctx, v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
res.List[v.Key] = val
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
2023-05-29 20:24:13 +08:00
|
|
|
|
res.List = simple.FilterMaskDemo(ctx, res.List)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConversionType 转换类型
|
|
|
|
|
func (s *sSysConfig) ConversionType(ctx context.Context, models *entity.SysConfig) (value interface{}, err error) {
|
|
|
|
|
if models == nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = gerror.New("数据不存在")
|
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
2023-01-18 16:23:39 +08:00
|
|
|
|
return consts.ConvType(models.Value, models.Type), nil
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateConfigByGroup 更新指定分组的配置
|
2023-05-10 23:54:50 +08:00
|
|
|
|
func (s *sSysConfig) UpdateConfigByGroup(ctx context.Context, in sysin.UpdateConfigInp) (err error) {
|
2022-11-24 23:37:34 +08:00
|
|
|
|
if in.Group == "" {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = gerror.New("分组不能为空")
|
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
var (
|
|
|
|
|
mod = dao.SysConfig.Ctx(ctx)
|
|
|
|
|
models []*entity.SysConfig
|
|
|
|
|
)
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
|
|
|
|
if err = mod.Where("group", in.Group).Scan(&models); err != nil {
|
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = dao.SysConfig.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
2022-11-24 23:37:34 +08:00
|
|
|
|
for k, v := range in.List {
|
|
|
|
|
row := s.getConfigByKey(k, models)
|
|
|
|
|
// 新增
|
|
|
|
|
if row == nil {
|
|
|
|
|
//row.Id = 0
|
|
|
|
|
//row.Key = k
|
|
|
|
|
//row.Value = gconv.String(v)
|
|
|
|
|
//row.Group = in.Group
|
|
|
|
|
//row.Status = consts.StatusEnabled
|
|
|
|
|
//row.CreatedAt = gtime.Now()
|
|
|
|
|
//row.UpdatedAt = gtime.Now()
|
|
|
|
|
//_, err := dao.SysConfig.Ctx(ctx).Data(row).Insert()
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// err = gerror.Wrap(err, consts.ErrorORM)
|
|
|
|
|
// return err
|
|
|
|
|
//}
|
|
|
|
|
//continue
|
2023-05-10 23:54:50 +08:00
|
|
|
|
err = gerror.Newf("暂不支持从前台添加变量,请先在数据库表[%v]中配置变量:%v", dao.SysConfig.Table(), k)
|
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新
|
2023-05-10 23:54:50 +08:00
|
|
|
|
_, err = dao.SysConfig.Ctx(ctx).Where("id", row.Id).Data(g.Map{"value": v, "updated_at": gtime.Now()}).Update()
|
2022-11-24 23:37:34 +08:00
|
|
|
|
if err != nil {
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return s.syncUpdate(ctx, in)
|
2022-11-24 23:37:34 +08:00
|
|
|
|
})
|
2023-05-10 23:54:50 +08:00
|
|
|
|
return
|
2022-11-24 23:37:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *sSysConfig) getConfigByKey(key string, models []*entity.SysConfig) *entity.SysConfig {
|
|
|
|
|
if len(models) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, v := range models {
|
|
|
|
|
if key == v.Key {
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
|
|
|
|
|
// syncUpdate 同步更新一些加载配置
|
|
|
|
|
func (s *sSysConfig) syncUpdate(ctx context.Context, in sysin.UpdateConfigInp) (err error) {
|
|
|
|
|
switch in.Group {
|
|
|
|
|
case "wechat":
|
|
|
|
|
wx, err := s.GetWechat(ctx)
|
|
|
|
|
if err == nil {
|
|
|
|
|
wechat.SetConfig(wx)
|
|
|
|
|
}
|
|
|
|
|
case "pay":
|
|
|
|
|
pay, err := s.GetPay(ctx)
|
|
|
|
|
if err == nil {
|
|
|
|
|
payment.SetConfig(pay)
|
|
|
|
|
}
|
2023-06-02 20:29:08 +08:00
|
|
|
|
case "upload":
|
|
|
|
|
upload, err := s.GetUpload(ctx)
|
|
|
|
|
if err == nil {
|
|
|
|
|
storager.SetConfig(upload)
|
|
|
|
|
}
|
2023-05-10 23:54:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = gerror.Newf("syncUpdate %v conifg fail:%+v", in.Group, err.Error())
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|