pricing.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "one-api/common"
  5. "one-api/model"
  6. )
  7. func GetPricing(c *gin.Context) {
  8. pricing := model.GetPricing()
  9. userId, exists := c.Get("id")
  10. usableGroup := map[string]string{}
  11. groupRatio := map[string]float64{}
  12. for s, f := range common.GroupRatio {
  13. groupRatio[s] = f
  14. }
  15. var group string
  16. if exists {
  17. user, err := model.GetUserById(userId.(int), false)
  18. if err == nil {
  19. group = user.Group
  20. }
  21. }
  22. usableGroup = common.GetUserUsableGroups(group)
  23. // check groupRatio contains usableGroup
  24. for group := range common.GroupRatio {
  25. if _, ok := usableGroup[group]; !ok {
  26. delete(groupRatio, group)
  27. }
  28. }
  29. c.JSON(200, gin.H{
  30. "success": true,
  31. "data": pricing,
  32. "group_ratio": groupRatio,
  33. "usable_group": usableGroup,
  34. })
  35. }
  36. func ResetModelRatio(c *gin.Context) {
  37. defaultStr := common.DefaultModelRatio2JSONString()
  38. err := model.UpdateOption("ModelRatio", defaultStr)
  39. if err != nil {
  40. c.JSON(200, gin.H{
  41. "success": false,
  42. "message": err.Error(),
  43. })
  44. return
  45. }
  46. err = common.UpdateModelRatioByJSONString(defaultStr)
  47. if err != nil {
  48. c.JSON(200, gin.H{
  49. "success": false,
  50. "message": err.Error(),
  51. })
  52. return
  53. }
  54. c.JSON(200, gin.H{
  55. "success": true,
  56. "message": "重置模型倍率成功",
  57. })
  58. }