admin_backend/global/config.go
2026-04-16 10:20:16 +08:00

125 lines
2.7 KiB
Go

package global_config
import (
"backend/common"
"backend/util"
"encoding/json"
)
var GlobalConfig map[string]interface{}
func InitConfig() {
GlobalConfig = make(map[string]interface{})
// 这里可以加载配置文件或从环境变量中读取配置
// 例如:
GlobalConfig["app_name"] = "MyApp"
GlobalConfig["debug"] = true
GlobalConfig["version"] = "1.0.0"
type Config struct {
ID int `json:"id" db:"id"`
Key string `json:"key" db:"config_key"`
Value string `json:"value" db:"config_value"`
Remark string `json:"remark" db:"remark"`
}
List := []*Config{}
db := util.MPool.GetGameDB()
defer db.Close()
if db == nil {
return
}
err := db.Select(&List, "SELECT `id`, `config_key`, `config_value`, `remark` FROM config")
if err != nil {
return
}
for _, config := range List {
configMap := make(map[string]interface{})
json.Unmarshal([]byte(config.Value), &configMap)
GlobalConfig[config.Key] = configMap
}
}
func GetConfig(key string) interface{} {
if value, ok := GlobalConfig[key]; ok {
return value
}
return nil
}
func SetConfig(key string, value interface{}) {
if GlobalConfig == nil {
GlobalConfig = make(map[string]interface{})
}
GlobalConfig[key] = value
}
func GetTranlaterConfig(user string) *common.TranlaterUser {
config := GetConfig("translater.config")
if config == nil {
return nil
}
configMap, ok := config.(map[string]interface{})
if !ok {
return nil
}
userList, ok := configMap["user"].([]interface{})
if !ok {
return nil
}
for _, item := range userList {
itemMap, ok := item.(map[string]interface{})
if !ok {
continue
}
if itemMap["account"] == user {
return &common.TranlaterUser{
Account: itemMap["account"].(string),
Colunm: itemMap["column"].(string),
Keys: itemMap["keys"].(string),
KeysType: itemMap["keysType"].(string),
}
}
}
return nil
}
func GetLanguageLimitConfig() []*common.LanguageLimit {
config := GetConfig("language.len_limit")
if config == nil {
return nil
}
list, ok := config.(map[string]interface{})["list"].([]interface{})
if !ok {
return nil
}
result := make([]*common.LanguageLimit, 0)
for _, item := range list {
itemMap, ok := item.(map[string]interface{})
if !ok {
continue
}
result = append(result, &common.LanguageLimit{
Key: itemMap["key"].(string),
Zh_CN: int(itemMap["zh_CN"].(float64)),
Latin: int(itemMap["latin"].(float64)),
})
}
return result
}
func GetTestChargeUidList() []int {
config := GetConfig("test.account")
if config == nil {
return nil
}
uidList, ok := config.(map[string]interface{})["charge.uid"].([]interface{})
if !ok {
return nil
}
result := make([]int, 0)
for _, uid := range uidList {
result = append(result, int(uid.(float64)))
}
return result
}