pricing.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 := common.GroupRatio
  12. var group string
  13. if exists {
  14. user, err := model.GetChannelById(userId.(int), false)
  15. if err != nil {
  16. c.JSON(200, gin.H{
  17. "success": false,
  18. "message": err.Error(),
  19. })
  20. return
  21. }
  22. group = user.Group
  23. }
  24. usableGroup = common.GetUserUsableGroups(group)
  25. // check groupRatio contains usableGroup
  26. for group := range common.GroupRatio {
  27. if _, ok := usableGroup[group]; !ok {
  28. delete(groupRatio, group)
  29. }
  30. }
  31. c.JSON(200, gin.H{
  32. "success": true,
  33. "data": pricing,
  34. "group_ratio": groupRatio,
  35. "usable_group": usableGroup,
  36. })
  37. }
  38. func ResetModelRatio(c *gin.Context) {
  39. defaultStr := common.DefaultModelRatio2JSONString()
  40. err := model.UpdateOption("ModelRatio", defaultStr)
  41. if err != nil {
  42. c.JSON(200, gin.H{
  43. "success": false,
  44. "message": err.Error(),
  45. })
  46. return
  47. }
  48. err = common.UpdateModelRatioByJSONString(defaultStr)
  49. if err != nil {
  50. c.JSON(200, gin.H{
  51. "success": false,
  52. "message": err.Error(),
  53. })
  54. return
  55. }
  56. c.JSON(200, gin.H{
  57. "success": true,
  58. "message": "重置模型倍率成功",
  59. })
  60. }