Files
waf-platform/EdgeAdmin/internal/configloaders/user_price_config_plus.go
2026-02-04 20:27:13 +08:00

80 lines
2.1 KiB
Go

//go:build plus
package configloaders
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/iwind/TeaGo/logs"
"reflect"
)
var sharedUserPriceConfig *userconfigs.UserPriceConfig = nil
func LoadUserPriceConfig() (*userconfigs.UserPriceConfig, error) {
locker.Lock()
defer locker.Unlock()
config, err := loadUserPriceConfig()
if err != nil {
return nil, err
}
var v = reflect.Indirect(reflect.ValueOf(config)).Interface().(userconfigs.UserPriceConfig)
return &v, nil
}
func ResetUserPriceConfig(priceConfig *userconfigs.UserPriceConfig) {
sharedUserPriceConfig = priceConfig
}
func UpdateUserPriceConfig(priceConfig *userconfigs.UserPriceConfig) error {
priceConfigJSON, err := json.Marshal(priceConfig)
if err != nil {
return err
}
rpcClient, err := rpc.SharedRPC()
if err != nil {
return err
}
_, err = rpcClient.SysSettingRPC().UpdateSysSetting(rpcClient.Context(0), &pb.UpdateSysSettingRequest{
Code: systemconfigs.SettingCodeUserPriceConfig,
ValueJSON: priceConfigJSON,
})
sharedUserPriceConfig = priceConfig
return err
}
func loadUserPriceConfig() (*userconfigs.UserPriceConfig, error) {
if sharedUserPriceConfig != nil {
return sharedUserPriceConfig, nil
}
var rpcClient, err = rpc.SharedRPC()
if err != nil {
return nil, err
}
resp, err := rpcClient.SysSettingRPC().ReadSysSetting(rpcClient.Context(0), &pb.ReadSysSettingRequest{
Code: systemconfigs.SettingCodeUserPriceConfig,
})
if err != nil {
return nil, err
}
if len(resp.ValueJSON) == 0 {
sharedUserPriceConfig = userconfigs.DefaultUserPriceConfig()
return sharedUserPriceConfig, nil
}
var config = userconfigs.DefaultUserPriceConfig()
err = json.Unmarshal(resp.ValueJSON, config)
if err != nil {
logs.Println("loadUserPriceConfig: " + err.Error())
sharedUserPriceConfig = userconfigs.DefaultUserPriceConfig()
return sharedUserPriceConfig, nil
}
sharedUserPriceConfig = config
return sharedUserPriceConfig, nil
}