channel.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. //idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  54. channels, err := model.SearchChannels(keyword, group)
  55. if err != nil {
  56. c.JSON(http.StatusOK, gin.H{
  57. "success": false,
  58. "message": err.Error(),
  59. })
  60. return
  61. }
  62. c.JSON(http.StatusOK, gin.H{
  63. "success": true,
  64. "message": "",
  65. "data": channels,
  66. })
  67. return
  68. }
  69. func GetChannel(c *gin.Context) {
  70. id, err := strconv.Atoi(c.Param("id"))
  71. if err != nil {
  72. c.JSON(http.StatusOK, gin.H{
  73. "success": false,
  74. "message": err.Error(),
  75. })
  76. return
  77. }
  78. channel, err := model.GetChannelById(id, false)
  79. if err != nil {
  80. c.JSON(http.StatusOK, gin.H{
  81. "success": false,
  82. "message": err.Error(),
  83. })
  84. return
  85. }
  86. c.JSON(http.StatusOK, gin.H{
  87. "success": true,
  88. "message": "",
  89. "data": channel,
  90. })
  91. return
  92. }
  93. func AddChannel(c *gin.Context) {
  94. channel := model.Channel{}
  95. err := c.ShouldBindJSON(&channel)
  96. if err != nil {
  97. c.JSON(http.StatusOK, gin.H{
  98. "success": false,
  99. "message": err.Error(),
  100. })
  101. return
  102. }
  103. channel.CreatedTime = common.GetTimestamp()
  104. keys := strings.Split(channel.Key, "\n")
  105. channels := make([]model.Channel, 0, len(keys))
  106. for _, key := range keys {
  107. if key == "" {
  108. continue
  109. }
  110. localChannel := channel
  111. localChannel.Key = key
  112. channels = append(channels, localChannel)
  113. }
  114. err = model.BatchInsertChannels(channels)
  115. if err != nil {
  116. c.JSON(http.StatusOK, gin.H{
  117. "success": false,
  118. "message": err.Error(),
  119. })
  120. return
  121. }
  122. c.JSON(http.StatusOK, gin.H{
  123. "success": true,
  124. "message": "",
  125. })
  126. return
  127. }
  128. func DeleteChannel(c *gin.Context) {
  129. id, _ := strconv.Atoi(c.Param("id"))
  130. channel := model.Channel{Id: id}
  131. err := channel.Delete()
  132. if err != nil {
  133. c.JSON(http.StatusOK, gin.H{
  134. "success": false,
  135. "message": err.Error(),
  136. })
  137. return
  138. }
  139. c.JSON(http.StatusOK, gin.H{
  140. "success": true,
  141. "message": "",
  142. })
  143. return
  144. }
  145. func DeleteDisabledChannel(c *gin.Context) {
  146. rows, err := model.DeleteDisabledChannel()
  147. if err != nil {
  148. c.JSON(http.StatusOK, gin.H{
  149. "success": false,
  150. "message": err.Error(),
  151. })
  152. return
  153. }
  154. c.JSON(http.StatusOK, gin.H{
  155. "success": true,
  156. "message": "",
  157. "data": rows,
  158. })
  159. return
  160. }
  161. type ChannelBatch struct {
  162. Ids []int `json:"ids"`
  163. }
  164. func DeleteChannelBatch(c *gin.Context) {
  165. channelBatch := ChannelBatch{}
  166. err := c.ShouldBindJSON(&channelBatch)
  167. if err != nil || len(channelBatch.Ids) == 0 {
  168. c.JSON(http.StatusOK, gin.H{
  169. "success": false,
  170. "message": "参数错误",
  171. })
  172. return
  173. }
  174. err = model.BatchDeleteChannels(channelBatch.Ids)
  175. if err != nil {
  176. c.JSON(http.StatusOK, gin.H{
  177. "success": false,
  178. "message": err.Error(),
  179. })
  180. return
  181. }
  182. c.JSON(http.StatusOK, gin.H{
  183. "success": true,
  184. "message": "",
  185. "data": len(channelBatch.Ids),
  186. })
  187. return
  188. }
  189. func UpdateChannel(c *gin.Context) {
  190. channel := model.Channel{}
  191. err := c.ShouldBindJSON(&channel)
  192. if err != nil {
  193. c.JSON(http.StatusOK, gin.H{
  194. "success": false,
  195. "message": err.Error(),
  196. })
  197. return
  198. }
  199. err = channel.Update()
  200. if err != nil {
  201. c.JSON(http.StatusOK, gin.H{
  202. "success": false,
  203. "message": err.Error(),
  204. })
  205. return
  206. }
  207. c.JSON(http.StatusOK, gin.H{
  208. "success": true,
  209. "message": "",
  210. "data": channel,
  211. })
  212. return
  213. }