mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-11 19:53:55 +08:00
This commit is contained in:
@@ -16,12 +16,15 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/payment"
|
||||
"hotgo/internal/library/wechat"
|
||||
"hotgo/internal/model"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
)
|
||||
|
||||
// MaskDemoField 演示环境下需要隐藏的配置
|
||||
var MaskDemoField = []string{
|
||||
// 邮箱
|
||||
"smtpUser", "smtpPass",
|
||||
@@ -34,10 +37,18 @@ var MaskDemoField = []string{
|
||||
|
||||
// 地图
|
||||
"geoAmapWebKey",
|
||||
|
||||
|
||||
// 短信
|
||||
"smsAliYunAccessKeyID", "smsAliYunAccessKeySecret",
|
||||
"smsTencentSecretId", "smsTencentSecretKey",
|
||||
|
||||
// 支付
|
||||
"payWxPayMchId", "payWxPaySerialNo", "payWxPayAPIv3Key",
|
||||
"payWxPayPrivateKey", "payQQPayMchId", "payQQPayApiKey",
|
||||
|
||||
// 微信
|
||||
"officialAccountAppSecret", "officialAccountToken", "officialAccountEncodingAESKey",
|
||||
"openPlatformAppSecret", "openPlatformToken", "openPlatformEncodingAESKey",
|
||||
}
|
||||
|
||||
type sSysConfig struct{}
|
||||
@@ -50,6 +61,27 @@ func init() {
|
||||
service.RegisterSysConfig(NewSysConfig())
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
// 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)
|
||||
@@ -58,120 +90,120 @@ func (s *sSysConfig) GetLoadCache(ctx context.Context) (conf *model.CacheConfig,
|
||||
|
||||
// GetLoadGenerate 获取本地生成配置
|
||||
func (s *sSysConfig) GetLoadGenerate(ctx context.Context) (conf *model.GenerateConfig, err error) {
|
||||
generate := g.Cfg().MustGet(ctx, "hggen")
|
||||
if err = gconv.Struct(generate, &conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = g.Cfg().MustGet(ctx, "hggen").Scan(&conf)
|
||||
return
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
// 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
|
||||
}
|
||||
err = gconv.Scan(models.List, &conf)
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
if err = gconv.Struct(models.List, &conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
err = gconv.Scan(models.List, &conf)
|
||||
return
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
if err = gconv.Struct(models.List, &conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
err = gconv.Scan(models.List, &conf)
|
||||
return
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
if err = gconv.Struct(models.List, &conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
err = gconv.Scan(models.List, &conf)
|
||||
return
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
if err = gconv.Struct(models.List, &conf); err != nil {
|
||||
return nil, err
|
||||
if err = gconv.Scan(models.List, &conf); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
conf.Addr = fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
||||
|
||||
return conf, nil
|
||||
return
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
if err = gconv.Struct(models.List, &conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
err = gconv.Scan(models.List, &conf)
|
||||
return
|
||||
}
|
||||
|
||||
// GetLoadSSL 获取本地日志配置
|
||||
func (s *sSysConfig) GetLoadSSL(ctx context.Context) (conf *model.SSLConfig, err error) {
|
||||
if err = g.Cfg().MustGet(ctx, "hotgo.ssl").Struct(&conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conf, nil
|
||||
err = g.Cfg().MustGet(ctx, "hotgo.ssl").Scan(&conf)
|
||||
return
|
||||
}
|
||||
|
||||
// GetLoadLog 获取本地日志配置
|
||||
func (s *sSysConfig) GetLoadLog(ctx context.Context) (conf *model.LogConfig, err error) {
|
||||
if err = g.Cfg().MustGet(ctx, "hotgo.log").Struct(&conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conf, nil
|
||||
err = g.Cfg().MustGet(ctx, "hotgo.log").Scan(&conf)
|
||||
return
|
||||
}
|
||||
|
||||
// GetLoadServeLog 获取本地服务日志配置
|
||||
func (s *sSysConfig) GetLoadServeLog(ctx context.Context) (conf *model.ServeLogConfig, err error) {
|
||||
if err = g.Cfg().MustGet(ctx, "hotgo.serveLog").Struct(&conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conf, nil
|
||||
err = g.Cfg().MustGet(ctx, "hotgo.serveLog").Scan(&conf)
|
||||
return
|
||||
}
|
||||
|
||||
// GetConfigByGroup 获取指定分组的配置
|
||||
func (s *sSysConfig) GetConfigByGroup(ctx context.Context, in sysin.GetConfigInp) (*sysin.GetConfigModel, error) {
|
||||
func (s *sSysConfig) GetConfigByGroup(ctx context.Context, in sysin.GetConfigInp) (res *sysin.GetConfigModel, err error) {
|
||||
if in.Group == "" {
|
||||
return nil, gerror.New("分组不能为空")
|
||||
err = gerror.New("分组不能为空")
|
||||
return
|
||||
}
|
||||
var (
|
||||
mod = dao.SysConfig.Ctx(ctx)
|
||||
models []*entity.SysConfig
|
||||
res sysin.GetConfigModel
|
||||
)
|
||||
if err := mod.Fields("key", "value", "type").Where("group", in.Group).Scan(&models); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
isDemo := g.Cfg().MustGet(ctx, "hotgo.isDemo", false)
|
||||
|
||||
var (
|
||||
models []*entity.SysConfig
|
||||
isDemo = g.Cfg().MustGet(ctx, "hotgo.isDemo", false).Bool()
|
||||
)
|
||||
|
||||
if err = dao.SysConfig.Ctx(ctx).Fields("key", "value", "type").Where("group", in.Group).Scan(&models); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
res = new(sysin.GetConfigModel)
|
||||
if len(models) > 0 {
|
||||
res.List = make(g.Map, len(models))
|
||||
for _, v := range models {
|
||||
@@ -180,42 +212,41 @@ func (s *sSysConfig) GetConfigByGroup(ctx context.Context, in sysin.GetConfigInp
|
||||
return nil, err
|
||||
}
|
||||
res.List[v.Key] = val
|
||||
//if isDemo.Bool() && (v.Key == "smtpUser" || v.Key == "smtpPass") {
|
||||
// res.List[v.Key] = consts.DemoTips
|
||||
// res.List[v.Key] = consts.DemoTips
|
||||
//}
|
||||
|
||||
if isDemo.Bool() && gstr.InArray(MaskDemoField, v.Key) {
|
||||
if isDemo && gstr.InArray(MaskDemoField, v.Key) {
|
||||
res.List[v.Key] = consts.DemoTips
|
||||
res.List[v.Key] = consts.DemoTips
|
||||
}
|
||||
}
|
||||
}
|
||||
return &res, nil
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ConversionType 转换类型
|
||||
func (s *sSysConfig) ConversionType(ctx context.Context, models *entity.SysConfig) (value interface{}, err error) {
|
||||
if models == nil {
|
||||
return nil, gerror.New("数据不存在")
|
||||
err = gerror.New("数据不存在")
|
||||
return
|
||||
}
|
||||
return consts.ConvType(models.Value, models.Type), nil
|
||||
}
|
||||
|
||||
// UpdateConfigByGroup 更新指定分组的配置
|
||||
func (s *sSysConfig) UpdateConfigByGroup(ctx context.Context, in sysin.UpdateConfigInp) error {
|
||||
func (s *sSysConfig) UpdateConfigByGroup(ctx context.Context, in sysin.UpdateConfigInp) (err error) {
|
||||
if in.Group == "" {
|
||||
return gerror.New("分组不能为空")
|
||||
err = gerror.New("分组不能为空")
|
||||
return
|
||||
}
|
||||
var (
|
||||
mod = dao.SysConfig.Ctx(ctx)
|
||||
models []*entity.SysConfig
|
||||
)
|
||||
if err := mod.Where("group", in.Group).Scan(&models); err != nil {
|
||||
return err
|
||||
|
||||
if err = mod.Where("group", in.Group).Scan(&models); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err := dao.SysConfig.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = dao.SysConfig.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
for k, v := range in.List {
|
||||
row := s.getConfigByKey(k, models)
|
||||
// 新增
|
||||
@@ -233,24 +264,21 @@ func (s *sSysConfig) UpdateConfigByGroup(ctx context.Context, in sysin.UpdateCon
|
||||
// return err
|
||||
//}
|
||||
//continue
|
||||
return gerror.Newf("暂不支持从前台添加变量,请先在数据库表[%v]中配置变量:%v", dao.SysConfig.Table(), k)
|
||||
err = gerror.Newf("暂不支持从前台添加变量,请先在数据库表[%v]中配置变量:%v", dao.SysConfig.Table(), k)
|
||||
return
|
||||
}
|
||||
|
||||
// 更新
|
||||
_, err := dao.SysConfig.Ctx(ctx).Where("id", row.Id).Data(g.Map{"value": v, "updated_at": gtime.Now()}).Update()
|
||||
_, err = dao.SysConfig.Ctx(ctx).Where("id", row.Id).Data(g.Map{"value": v, "updated_at": gtime.Now()}).Update()
|
||||
if err != nil {
|
||||
err = gerror.Wrap(err, consts.ErrorORM)
|
||||
return err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return s.syncUpdate(ctx, in)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sSysConfig) getConfigByKey(key string, models []*entity.SysConfig) *entity.SysConfig {
|
||||
@@ -266,3 +294,24 @@ func (s *sSysConfig) getConfigByKey(key string, models []*entity.SysConfig) *ent
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
err = gerror.Newf("syncUpdate %v conifg fail:%+v", in.Group, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user