channel.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "strconv"
  8. "strings"
  9. )
  10. func GetAllChannels(c *gin.Context) {
  11. p, _ := strconv.Atoi(c.Query("p"))
  12. pageSize, _ := strconv.Atoi(c.Query("page_size"))
  13. if p < 0 {
  14. p = 0
  15. }
  16. if pageSize < 0 {
  17. pageSize = common.ItemsPerPage
  18. }
  19. idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  20. channels, err := model.GetAllChannels(p*pageSize, pageSize, false, idSort)
  21. if err != nil {
  22. c.JSON(http.StatusOK, gin.H{
  23. "success": false,
  24. "message": err.Error(),
  25. })
  26. return
  27. }
  28. c.JSON(http.StatusOK, gin.H{
  29. "success": true,
  30. "message": "",
  31. "data": channels,
  32. })
  33. return
  34. }
  35. func FixChannelsAbilities(c *gin.Context) {
  36. count, err := model.FixAbility()
  37. if err != nil {
  38. c.JSON(http.StatusOK, gin.H{
  39. "success": false,
  40. "message": err.Error(),
  41. })
  42. return
  43. }
  44. c.JSON(http.StatusOK, gin.H{
  45. "success": true,
  46. "message": "",
  47. "data": count,
  48. })
  49. }
  50. func SearchChannels(c *gin.Context) {
  51. keyword := c.Query("keyword")
  52. group := c.Query("group")
  53. modelKeyword := c.Query("model")
  54. //idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  55. channels, err := model.SearchChannels(keyword, group, modelKeyword)
  56. if err != nil {
  57. c.JSON(http.StatusOK, gin.H{
  58. "success": false,
  59. "message": err.Error(),
  60. })
  61. return
  62. }
  63. c.JSON(http.StatusOK, gin.H{
  64. "success": true,
  65. "message": "",
  66. "data": channels,
  67. })
  68. return
  69. }
  70. func GetChannel(c *gin.Context) {
  71. id, err := strconv.Atoi(c.Param("id"))
  72. if err != nil {
  73. c.JSON(http.StatusOK, gin.H{
  74. "success": false,
  75. "message": err.Error(),
  76. })
  77. return
  78. }
  79. channel, err := model.GetChannelById(id, false)
  80. if err != nil {
  81. c.JSON(http.StatusOK, gin.H{
  82. "success": false,
  83. "message": err.Error(),
  84. })
  85. return
  86. }
  87. c.JSON(http.StatusOK, gin.H{
  88. "success": true,
  89. "message": "",
  90. "data": channel,
  91. })
  92. return
  93. }
  94. func AddChannel(c *gin.Context) {
  95. channel := model.Channel{}
  96. err := c.ShouldBindJSON(&channel)
  97. if err != nil {
  98. c.JSON(http.StatusOK, gin.H{
  99. "success": false,
  100. "message": err.Error(),
  101. })
  102. return
  103. }
  104. channel.CreatedTime = common.GetTimestamp()
  105. keys := strings.Split(channel.Key, "\n")
  106. channels := make([]model.Channel, 0, len(keys))
  107. for _, key := range keys {
  108. if key == "" {
  109. continue
  110. }
  111. localChannel := channel
  112. localChannel.Key = key
  113. channels = append(channels, localChannel)
  114. }
  115. err = model.BatchInsertChannels(channels)
  116. if err != nil {
  117. c.JSON(http.StatusOK, gin.H{
  118. "success": false,
  119. "message": err.Error(),
  120. })
  121. return
  122. }
  123. c.JSON(http.StatusOK, gin.H{
  124. "success": true,
  125. "message": "",
  126. })
  127. return
  128. }
  129. func DeleteChannel(c *gin.Context) {
  130. id, _ := strconv.Atoi(c.Param("id"))
  131. channel := model.Channel{Id: id}
  132. err := channel.Delete()
  133. if err != nil {
  134. c.JSON(http.StatusOK, gin.H{
  135. "success": false,
  136. "message": err.Error(),
  137. })
  138. return
  139. }
  140. c.JSON(http.StatusOK, gin.H{
  141. "success": true,
  142. "message": "",
  143. })
  144. return
  145. }
  146. func DeleteDisabledChannel(c *gin.Context) {
  147. rows, err := model.DeleteDisabledChannel()
  148. if err != nil {
  149. c.JSON(http.StatusOK, gin.H{
  150. "success": false,
  151. "message": err.Error(),
  152. })
  153. return
  154. }
  155. c.JSON(http.StatusOK, gin.H{
  156. "success": true,
  157. "message": "",
  158. "data": rows,
  159. })
  160. return
  161. }
  162. type ChannelBatch struct {
  163. Ids []int `json:"ids"`
  164. }
  165. func DeleteChannelBatch(c *gin.Context) {
  166. channelBatch := ChannelBatch{}
  167. err := c.ShouldBindJSON(&channelBatch)
  168. if err != nil || len(channelBatch.Ids) == 0 {
  169. c.JSON(http.StatusOK, gin.H{
  170. "success": false,
  171. "message": "参数错误",
  172. })
  173. return
  174. }
  175. err = model.BatchDeleteChannels(channelBatch.Ids)
  176. if err != nil {
  177. c.JSON(http.StatusOK, gin.H{
  178. "success": false,
  179. "message": err.Error(),
  180. })
  181. return
  182. }
  183. c.JSON(http.StatusOK, gin.H{
  184. "success": true,
  185. "message": "",
  186. "data": len(channelBatch.Ids),
  187. })
  188. return
  189. }
  190. func UpdateChannel(c *gin.Context) {
  191. channel := model.Channel{}
  192. err := c.ShouldBindJSON(&channel)
  193. if err != nil {
  194. c.JSON(http.StatusOK, gin.H{
  195. "success": false,
  196. "message": err.Error(),
  197. })
  198. return
  199. }
  200. err = channel.Update()
  201. if err != nil {
  202. c.JSON(http.StatusOK, gin.H{
  203. "success": false,
  204. "message": err.Error(),
  205. })
  206. return
  207. }
  208. c.JSON(http.StatusOK, gin.H{
  209. "success": true,
  210. "message": "",
  211. "data": channel,
  212. })
  213. return
  214. }