pricing.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package controller
  2. import (
  3. "one-api/model"
  4. "one-api/setting"
  5. "one-api/setting/ratio_setting"
  6. "github.com/gin-gonic/gin"
  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 ratio_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. for g := range groupRatio {
  22. ratio, ok := ratio_setting.GetGroupGroupRatio(group, g)
  23. if ok {
  24. groupRatio[g] = ratio
  25. }
  26. }
  27. }
  28. }
  29. usableGroup = setting.GetUserUsableGroups(group)
  30. // check groupRatio contains usableGroup
  31. for group := range ratio_setting.GetGroupRatioCopy() {
  32. if _, ok := usableGroup[group]; !ok {
  33. delete(groupRatio, group)
  34. }
  35. }
  36. c.JSON(200, gin.H{
  37. "success": true,
  38. "data": pricing,
  39. "group_ratio": groupRatio,
  40. "usable_group": usableGroup,
  41. })
  42. }
  43. func ResetModelRatio(c *gin.Context) {
  44. defaultStr := ratio_setting.DefaultModelRatio2JSONString()
  45. err := model.UpdateOption("ModelRatio", defaultStr)
  46. if err != nil {
  47. c.JSON(200, gin.H{
  48. "success": false,
  49. "message": err.Error(),
  50. })
  51. return
  52. }
  53. err = ratio_setting.UpdateModelRatioByJSONString(defaultStr)
  54. if err != nil {
  55. c.JSON(200, gin.H{
  56. "success": false,
  57. "message": err.Error(),
  58. })
  59. return
  60. }
  61. c.JSON(200, gin.H{
  62. "success": true,
  63. "message": "重置模型倍率成功",
  64. })
  65. }