group.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package controller
  2. import (
  3. "net/http"
  4. "one-api/model"
  5. "one-api/setting"
  6. "one-api/setting/ratio_setting"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func GetGroups(c *gin.Context) {
  10. groupNames := make([]string, 0)
  11. for groupName := range ratio_setting.GetGroupRatioCopy() {
  12. groupNames = append(groupNames, groupName)
  13. }
  14. c.JSON(http.StatusOK, gin.H{
  15. "success": true,
  16. "message": "",
  17. "data": groupNames,
  18. })
  19. }
  20. func GetUserGroups(c *gin.Context) {
  21. usableGroups := make(map[string]map[string]interface{})
  22. userGroup := ""
  23. userId := c.GetInt("id")
  24. userGroup, _ = model.GetUserGroup(userId, false)
  25. for groupName, ratio := range ratio_setting.GetGroupRatioCopy() {
  26. // UserUsableGroups contains the groups that the user can use
  27. userUsableGroups := setting.GetUserUsableGroups(userGroup)
  28. if desc, ok := userUsableGroups[groupName]; ok {
  29. usableGroups[groupName] = map[string]interface{}{
  30. "ratio": ratio,
  31. "desc": desc,
  32. }
  33. }
  34. }
  35. if setting.GroupInUserUsableGroups("auto") {
  36. usableGroups["auto"] = map[string]interface{}{
  37. "ratio": "自动",
  38. "desc": setting.GetUsableGroupDescription("auto"),
  39. }
  40. }
  41. c.JSON(http.StatusOK, gin.H{
  42. "success": true,
  43. "message": "",
  44. "data": usableGroups,
  45. })
  46. }