user_usable_group.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package setting
  2. import (
  3. "encoding/json"
  4. "one-api/common"
  5. )
  6. var UserUsableGroups = map[string]string{
  7. "default": "默认分组",
  8. "vip": "vip分组",
  9. }
  10. func UserUsableGroups2JSONString() string {
  11. jsonBytes, err := json.Marshal(UserUsableGroups)
  12. if err != nil {
  13. common.SysError("error marshalling user groups: " + err.Error())
  14. }
  15. return string(jsonBytes)
  16. }
  17. func UpdateUserUsableGroupsByJSONString(jsonStr string) error {
  18. UserUsableGroups = make(map[string]string)
  19. return json.Unmarshal([]byte(jsonStr), &UserUsableGroups)
  20. }
  21. func GetUserUsableGroups(userGroup string) map[string]string {
  22. if userGroup == "" {
  23. // 如果userGroup为空,返回UserUsableGroups
  24. return UserUsableGroups
  25. }
  26. // 如果userGroup不在UserUsableGroups中,返回UserUsableGroups + userGroup
  27. if _, ok := UserUsableGroups[userGroup]; !ok {
  28. appendUserUsableGroups := make(map[string]string)
  29. for k, v := range UserUsableGroups {
  30. appendUserUsableGroups[k] = v
  31. }
  32. appendUserUsableGroups[userGroup] = "用户分组"
  33. return appendUserUsableGroups
  34. }
  35. // 如果userGroup在UserUsableGroups中,返回UserUsableGroups
  36. return UserUsableGroups
  37. }
  38. func GroupInUserUsableGroups(groupName string) bool {
  39. _, ok := UserUsableGroups[groupName]
  40. return ok
  41. }