user_groups.go 1.2 KB

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