2
0

group.go 917 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. )
  8. func GetGroups(c *gin.Context) {
  9. groupNames := make([]string, 0)
  10. for groupName, _ := range common.GroupRatio {
  11. groupNames = append(groupNames, groupName)
  12. }
  13. c.JSON(http.StatusOK, gin.H{
  14. "success": true,
  15. "message": "",
  16. "data": groupNames,
  17. })
  18. }
  19. func GetUserGroups(c *gin.Context) {
  20. usableGroups := make(map[string]string)
  21. userGroup := ""
  22. userId := c.GetInt("id")
  23. userGroup, _ = model.CacheGetUserGroup(userId)
  24. for groupName, _ := range common.GroupRatio {
  25. // UserUsableGroups contains the groups that the user can use
  26. userUsableGroups := common.GetUserUsableGroups(userGroup)
  27. if _, ok := userUsableGroups[groupName]; ok {
  28. usableGroups[groupName] = userUsableGroups[groupName]
  29. }
  30. }
  31. c.JSON(http.StatusOK, gin.H{
  32. "success": true,
  33. "message": "",
  34. "data": usableGroups,
  35. })
  36. }