0d1ba65592
- Replaced references to common.GroupRatio and common.UserUsableGroups with corresponding functions from the new setting package across multiple controllers and services. - Introduced new setting functions for managing group ratios and user usable groups, enhancing code organization and maintainability. - Updated related functions to ensure consistent behavior with the new setting package integration.
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package setting
|
|
|
|
import (
|
|
"encoding/json"
|
|
"one-api/common"
|
|
)
|
|
|
|
var UserUsableGroups = map[string]string{
|
|
"default": "默认分组",
|
|
"vip": "vip分组",
|
|
}
|
|
|
|
func UserUsableGroups2JSONString() string {
|
|
jsonBytes, err := json.Marshal(UserUsableGroups)
|
|
if err != nil {
|
|
common.SysError("error marshalling user groups: " + err.Error())
|
|
}
|
|
return string(jsonBytes)
|
|
}
|
|
|
|
func UpdateUserUsableGroupsByJSONString(jsonStr string) error {
|
|
UserUsableGroups = make(map[string]string)
|
|
return json.Unmarshal([]byte(jsonStr), &UserUsableGroups)
|
|
}
|
|
|
|
func GetUserUsableGroups(userGroup string) map[string]string {
|
|
if userGroup == "" {
|
|
// 如果userGroup为空,返回UserUsableGroups
|
|
return UserUsableGroups
|
|
}
|
|
// 如果userGroup不在UserUsableGroups中,返回UserUsableGroups + userGroup
|
|
if _, ok := UserUsableGroups[userGroup]; !ok {
|
|
appendUserUsableGroups := make(map[string]string)
|
|
for k, v := range UserUsableGroups {
|
|
appendUserUsableGroups[k] = v
|
|
}
|
|
appendUserUsableGroups[userGroup] = "用户分组"
|
|
return appendUserUsableGroups
|
|
}
|
|
// 如果userGroup在UserUsableGroups中,返回UserUsableGroups
|
|
return UserUsableGroups
|
|
}
|
|
|
|
func GroupInUserUsableGroups(groupName string) bool {
|
|
_, ok := UserUsableGroups[groupName]
|
|
return ok
|
|
}
|