group.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package controller
  2. import (
  3. "net/http"
  4. "one-api/model"
  5. "one-api/setting"
  6. "github.com/gin-gonic/gin"
  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. if setting.GroupInUserUsableGroups("auto") {
  35. usableGroups["auto"] = map[string]interface{}{
  36. "ratio": "自动",
  37. "desc": setting.GetUsableGroupDescription("auto"),
  38. }
  39. }
  40. c.JSON(http.StatusOK, gin.H{
  41. "success": true,
  42. "message": "",
  43. "data": usableGroups,
  44. })
  45. }