pricing.go 1.3 KB

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