group.go 1004 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "one-api/model"
  6. "one-api/setting"
  7. )
  8. func GetGroups(c *gin.Context) {
  9. groupNames := make([]string, 0)
  10. for groupName, _ := range setting.GetGroupRatioCopy() {
  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]map[string]interface{})
  21. userGroup := ""
  22. userId := c.GetInt("id")
  23. userGroup, _ = model.GetUserGroup(userId, false)
  24. for groupName, ratio := range setting.GetGroupRatioCopy() {
  25. // UserUsableGroups contains the groups that the user can use
  26. userUsableGroups := setting.GetUserUsableGroups(userGroup)
  27. if desc, ok := userUsableGroups[groupName]; ok {
  28. usableGroups[groupName] = map[string]interface{}{
  29. "ratio": ratio,
  30. "desc": desc,
  31. }
  32. }
  33. }
  34. c.JSON(http.StatusOK, gin.H{
  35. "success": true,
  36. "message": "",
  37. "data": usableGroups,
  38. })
  39. }