116 lines
2.5 KiB
Go
116 lines
2.5 KiB
Go
package configloaders
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
|
"github.com/TeaOSLab/EdgeUser/internal/rpc"
|
|
"github.com/iwind/TeaGo/logs"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
var sharedUIConfig *systemconfigs.UserUIConfig = nil
|
|
|
|
func init() {
|
|
// 更新任务
|
|
// TODO 改成实时更新
|
|
var ticker = time.NewTicker(1 * time.Minute)
|
|
go func() {
|
|
for range ticker.C {
|
|
err := reloadUIConfig()
|
|
if err != nil {
|
|
logs.Println("[CONFIG_LOADERS]load ui config failed: " + err.Error())
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func LoadUIConfig() (*systemconfigs.UserUIConfig, error) {
|
|
locker.Lock()
|
|
defer locker.Unlock()
|
|
|
|
config, err := loadUIConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
v := reflect.Indirect(reflect.ValueOf(config)).Interface().(systemconfigs.UserUIConfig)
|
|
return &v, nil
|
|
}
|
|
|
|
func loadUIConfig() (*systemconfigs.UserUIConfig, error) {
|
|
if sharedUIConfig != nil {
|
|
return sharedUIConfig, nil
|
|
}
|
|
var rpcClient, err = rpc.SharedRPC()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, err := rpcClient.SysSettingRPC().ReadSysSetting(rpcClient.Context(0), &pb.ReadSysSettingRequest{
|
|
Code: systemconfigs.SettingCodeUserUIConfig,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(resp.ValueJSON) == 0 {
|
|
sharedUIConfig = systemconfigs.NewUserUIConfig()
|
|
return sharedUIConfig, nil
|
|
}
|
|
|
|
var config = systemconfigs.NewUserUIConfig()
|
|
err = json.Unmarshal(resp.ValueJSON, config)
|
|
if err != nil {
|
|
logs.Println("[UI_MANAGER]" + err.Error())
|
|
sharedUIConfig = systemconfigs.NewUserUIConfig()
|
|
return sharedUIConfig, nil
|
|
}
|
|
sharedUIConfig = config
|
|
|
|
// 时区
|
|
updateTimeZone(config)
|
|
|
|
return sharedUIConfig, nil
|
|
}
|
|
|
|
func reloadUIConfig() error {
|
|
var rpcClient, err = rpc.SharedRPC()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := rpcClient.SysSettingRPC().ReadSysSetting(rpcClient.Context(0), &pb.ReadSysSettingRequest{
|
|
Code: systemconfigs.SettingCodeUserUIConfig,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(resp.ValueJSON) == 0 {
|
|
return nil
|
|
}
|
|
|
|
var config = systemconfigs.NewUserUIConfig()
|
|
err = json.Unmarshal(resp.ValueJSON, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var oldConfig = sharedUIConfig
|
|
sharedUIConfig = config
|
|
|
|
// 时区
|
|
if oldConfig == nil || oldConfig.TimeZone != config.TimeZone {
|
|
updateTimeZone(config)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// 修改时区
|
|
func updateTimeZone(config *systemconfigs.UserUIConfig) {
|
|
if len(config.TimeZone) > 0 {
|
|
location, err := time.LoadLocation(config.TimeZone)
|
|
if err == nil && time.Local != location {
|
|
time.Local = location
|
|
}
|
|
}
|
|
}
|