channel.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "strconv"
  9. "strings"
  10. "github.com/gin-gonic/gin"
  11. )
  12. type OpenAIModel struct {
  13. ID string `json:"id"`
  14. Object string `json:"object"`
  15. Created int64 `json:"created"`
  16. OwnedBy string `json:"owned_by"`
  17. Permission []struct {
  18. ID string `json:"id"`
  19. Object string `json:"object"`
  20. Created int64 `json:"created"`
  21. AllowCreateEngine bool `json:"allow_create_engine"`
  22. AllowSampling bool `json:"allow_sampling"`
  23. AllowLogprobs bool `json:"allow_logprobs"`
  24. AllowSearchIndices bool `json:"allow_search_indices"`
  25. AllowView bool `json:"allow_view"`
  26. AllowFineTuning bool `json:"allow_fine_tuning"`
  27. Organization string `json:"organization"`
  28. Group string `json:"group"`
  29. IsBlocking bool `json:"is_blocking"`
  30. } `json:"permission"`
  31. Root string `json:"root"`
  32. Parent string `json:"parent"`
  33. }
  34. type OpenAIModelsResponse struct {
  35. Data []OpenAIModel `json:"data"`
  36. Success bool `json:"success"`
  37. }
  38. func parseStatusFilter(statusParam string) int {
  39. switch strings.ToLower(statusParam) {
  40. case "enabled", "1":
  41. return common.ChannelStatusEnabled
  42. case "disabled", "0":
  43. return 0
  44. default:
  45. return -1
  46. }
  47. }
  48. func GetAllChannels(c *gin.Context) {
  49. p, _ := strconv.Atoi(c.Query("p"))
  50. pageSize, _ := strconv.Atoi(c.Query("page_size"))
  51. if p < 1 {
  52. p = 1
  53. }
  54. if pageSize < 1 {
  55. pageSize = common.ItemsPerPage
  56. }
  57. channelData := make([]*model.Channel, 0)
  58. idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  59. enableTagMode, _ := strconv.ParseBool(c.Query("tag_mode"))
  60. statusParam := c.Query("status")
  61. // statusFilter: -1 all, 1 enabled, 0 disabled (include auto & manual)
  62. statusFilter := parseStatusFilter(statusParam)
  63. // type filter
  64. typeStr := c.Query("type")
  65. typeFilter := -1
  66. if typeStr != "" {
  67. if t, err := strconv.Atoi(typeStr); err == nil {
  68. typeFilter = t
  69. }
  70. }
  71. var total int64
  72. if enableTagMode {
  73. tags, err := model.GetPaginatedTags((p-1)*pageSize, pageSize)
  74. if err != nil {
  75. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  76. return
  77. }
  78. for _, tag := range tags {
  79. if tag == nil || *tag == "" {
  80. continue
  81. }
  82. tagChannels, err := model.GetChannelsByTag(*tag, idSort)
  83. if err != nil {
  84. continue
  85. }
  86. filtered := make([]*model.Channel, 0)
  87. for _, ch := range tagChannels {
  88. if statusFilter == common.ChannelStatusEnabled && ch.Status != common.ChannelStatusEnabled {
  89. continue
  90. }
  91. if statusFilter == 0 && ch.Status == common.ChannelStatusEnabled {
  92. continue
  93. }
  94. if typeFilter >= 0 && ch.Type != typeFilter {
  95. continue
  96. }
  97. filtered = append(filtered, ch)
  98. }
  99. channelData = append(channelData, filtered...)
  100. }
  101. total, _ = model.CountAllTags()
  102. } else {
  103. baseQuery := model.DB.Model(&model.Channel{})
  104. if typeFilter >= 0 {
  105. baseQuery = baseQuery.Where("type = ?", typeFilter)
  106. }
  107. if statusFilter == common.ChannelStatusEnabled {
  108. baseQuery = baseQuery.Where("status = ?", common.ChannelStatusEnabled)
  109. } else if statusFilter == 0 {
  110. baseQuery = baseQuery.Where("status != ?", common.ChannelStatusEnabled)
  111. }
  112. baseQuery.Count(&total)
  113. order := "priority desc"
  114. if idSort {
  115. order = "id desc"
  116. }
  117. err := baseQuery.Order(order).Limit(pageSize).Offset((p-1)*pageSize).Omit("key").Find(&channelData).Error
  118. if err != nil {
  119. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  120. return
  121. }
  122. }
  123. countQuery := model.DB.Model(&model.Channel{})
  124. if statusFilter == common.ChannelStatusEnabled {
  125. countQuery = countQuery.Where("status = ?", common.ChannelStatusEnabled)
  126. } else if statusFilter == 0 {
  127. countQuery = countQuery.Where("status != ?", common.ChannelStatusEnabled)
  128. }
  129. var results []struct {
  130. Type int64
  131. Count int64
  132. }
  133. _ = countQuery.Select("type, count(*) as count").Group("type").Find(&results).Error
  134. typeCounts := make(map[int64]int64)
  135. for _, r := range results {
  136. typeCounts[r.Type] = r.Count
  137. }
  138. c.JSON(http.StatusOK, gin.H{
  139. "success": true,
  140. "message": "",
  141. "data": gin.H{
  142. "items": channelData,
  143. "total": total,
  144. "page": p,
  145. "page_size": pageSize,
  146. "type_counts": typeCounts,
  147. },
  148. })
  149. return
  150. }
  151. func FetchUpstreamModels(c *gin.Context) {
  152. id, err := strconv.Atoi(c.Param("id"))
  153. if err != nil {
  154. c.JSON(http.StatusOK, gin.H{
  155. "success": false,
  156. "message": err.Error(),
  157. })
  158. return
  159. }
  160. channel, err := model.GetChannelById(id, true)
  161. if err != nil {
  162. c.JSON(http.StatusOK, gin.H{
  163. "success": false,
  164. "message": err.Error(),
  165. })
  166. return
  167. }
  168. baseURL := common.ChannelBaseURLs[channel.Type]
  169. if channel.GetBaseURL() != "" {
  170. baseURL = channel.GetBaseURL()
  171. }
  172. url := fmt.Sprintf("%s/v1/models", baseURL)
  173. switch channel.Type {
  174. case common.ChannelTypeGemini:
  175. url = fmt.Sprintf("%s/v1beta/openai/models", baseURL)
  176. case common.ChannelTypeAli:
  177. url = fmt.Sprintf("%s/compatible-mode/v1/models", baseURL)
  178. }
  179. body, err := GetResponseBody("GET", url, channel, GetAuthHeader(channel.Key))
  180. if err != nil {
  181. c.JSON(http.StatusOK, gin.H{
  182. "success": false,
  183. "message": err.Error(),
  184. })
  185. return
  186. }
  187. var result OpenAIModelsResponse
  188. if err = json.Unmarshal(body, &result); err != nil {
  189. c.JSON(http.StatusOK, gin.H{
  190. "success": false,
  191. "message": fmt.Sprintf("解析响应失败: %s", err.Error()),
  192. })
  193. return
  194. }
  195. var ids []string
  196. for _, model := range result.Data {
  197. id := model.ID
  198. if channel.Type == common.ChannelTypeGemini {
  199. id = strings.TrimPrefix(id, "models/")
  200. }
  201. ids = append(ids, id)
  202. }
  203. c.JSON(http.StatusOK, gin.H{
  204. "success": true,
  205. "message": "",
  206. "data": ids,
  207. })
  208. }
  209. func FixChannelsAbilities(c *gin.Context) {
  210. count, err := model.FixAbility()
  211. if err != nil {
  212. c.JSON(http.StatusOK, gin.H{
  213. "success": false,
  214. "message": err.Error(),
  215. })
  216. return
  217. }
  218. c.JSON(http.StatusOK, gin.H{
  219. "success": true,
  220. "message": "",
  221. "data": count,
  222. })
  223. }
  224. func SearchChannels(c *gin.Context) {
  225. keyword := c.Query("keyword")
  226. group := c.Query("group")
  227. modelKeyword := c.Query("model")
  228. statusParam := c.Query("status")
  229. statusFilter := parseStatusFilter(statusParam)
  230. idSort, _ := strconv.ParseBool(c.Query("id_sort"))
  231. enableTagMode, _ := strconv.ParseBool(c.Query("tag_mode"))
  232. channelData := make([]*model.Channel, 0)
  233. if enableTagMode {
  234. tags, err := model.SearchTags(keyword, group, modelKeyword, idSort)
  235. if err != nil {
  236. c.JSON(http.StatusOK, gin.H{
  237. "success": false,
  238. "message": err.Error(),
  239. })
  240. return
  241. }
  242. for _, tag := range tags {
  243. if tag != nil && *tag != "" {
  244. tagChannel, err := model.GetChannelsByTag(*tag, idSort)
  245. if err == nil {
  246. channelData = append(channelData, tagChannel...)
  247. }
  248. }
  249. }
  250. } else {
  251. channels, err := model.SearchChannels(keyword, group, modelKeyword, idSort)
  252. if err != nil {
  253. c.JSON(http.StatusOK, gin.H{
  254. "success": false,
  255. "message": err.Error(),
  256. })
  257. return
  258. }
  259. channelData = channels
  260. }
  261. if statusFilter == common.ChannelStatusEnabled || statusFilter == 0 {
  262. filtered := make([]*model.Channel, 0, len(channelData))
  263. for _, ch := range channelData {
  264. if statusFilter == common.ChannelStatusEnabled && ch.Status != common.ChannelStatusEnabled {
  265. continue
  266. }
  267. if statusFilter == 0 && ch.Status == common.ChannelStatusEnabled {
  268. continue
  269. }
  270. filtered = append(filtered, ch)
  271. }
  272. channelData = filtered
  273. }
  274. // calculate type counts for search results
  275. typeCounts := make(map[int64]int64)
  276. for _, channel := range channelData {
  277. typeCounts[int64(channel.Type)]++
  278. }
  279. typeParam := c.Query("type")
  280. typeFilter := -1
  281. if typeParam != "" {
  282. if tp, err := strconv.Atoi(typeParam); err == nil {
  283. typeFilter = tp
  284. }
  285. }
  286. if typeFilter >= 0 {
  287. filtered := make([]*model.Channel, 0, len(channelData))
  288. for _, ch := range channelData {
  289. if ch.Type == typeFilter {
  290. filtered = append(filtered, ch)
  291. }
  292. }
  293. channelData = filtered
  294. }
  295. page, _ := strconv.Atoi(c.DefaultQuery("p", "1"))
  296. pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
  297. if page < 1 {
  298. page = 1
  299. }
  300. if pageSize <= 0 {
  301. pageSize = 20
  302. }
  303. total := len(channelData)
  304. startIdx := (page - 1) * pageSize
  305. if startIdx > total {
  306. startIdx = total
  307. }
  308. endIdx := startIdx + pageSize
  309. if endIdx > total {
  310. endIdx = total
  311. }
  312. pagedData := channelData[startIdx:endIdx]
  313. c.JSON(http.StatusOK, gin.H{
  314. "success": true,
  315. "message": "",
  316. "data": gin.H{
  317. "items": pagedData,
  318. "total": total,
  319. "type_counts": typeCounts,
  320. },
  321. })
  322. return
  323. }
  324. func GetChannel(c *gin.Context) {
  325. id, err := strconv.Atoi(c.Param("id"))
  326. if err != nil {
  327. c.JSON(http.StatusOK, gin.H{
  328. "success": false,
  329. "message": err.Error(),
  330. })
  331. return
  332. }
  333. channel, err := model.GetChannelById(id, false)
  334. if err != nil {
  335. c.JSON(http.StatusOK, gin.H{
  336. "success": false,
  337. "message": err.Error(),
  338. })
  339. return
  340. }
  341. c.JSON(http.StatusOK, gin.H{
  342. "success": true,
  343. "message": "",
  344. "data": channel,
  345. })
  346. return
  347. }
  348. func AddChannel(c *gin.Context) {
  349. channel := model.Channel{}
  350. err := c.ShouldBindJSON(&channel)
  351. if err != nil {
  352. c.JSON(http.StatusOK, gin.H{
  353. "success": false,
  354. "message": err.Error(),
  355. })
  356. return
  357. }
  358. channel.CreatedTime = common.GetTimestamp()
  359. keys := strings.Split(channel.Key, "\n")
  360. if channel.Type == common.ChannelTypeVertexAi {
  361. if channel.Other == "" {
  362. c.JSON(http.StatusOK, gin.H{
  363. "success": false,
  364. "message": "部署地区不能为空",
  365. })
  366. return
  367. } else {
  368. if common.IsJsonStr(channel.Other) {
  369. // must have default
  370. regionMap := common.StrToMap(channel.Other)
  371. if regionMap["default"] == nil {
  372. c.JSON(http.StatusOK, gin.H{
  373. "success": false,
  374. "message": "部署地区必须包含default字段",
  375. })
  376. return
  377. }
  378. }
  379. }
  380. keys = []string{channel.Key}
  381. }
  382. channels := make([]model.Channel, 0, len(keys))
  383. for _, key := range keys {
  384. if key == "" {
  385. continue
  386. }
  387. localChannel := channel
  388. localChannel.Key = key
  389. // Validate the length of the model name
  390. models := strings.Split(localChannel.Models, ",")
  391. for _, model := range models {
  392. if len(model) > 255 {
  393. c.JSON(http.StatusOK, gin.H{
  394. "success": false,
  395. "message": fmt.Sprintf("模型名称过长: %s", model),
  396. })
  397. return
  398. }
  399. }
  400. channels = append(channels, localChannel)
  401. }
  402. err = model.BatchInsertChannels(channels)
  403. if err != nil {
  404. c.JSON(http.StatusOK, gin.H{
  405. "success": false,
  406. "message": err.Error(),
  407. })
  408. return
  409. }
  410. c.JSON(http.StatusOK, gin.H{
  411. "success": true,
  412. "message": "",
  413. })
  414. return
  415. }
  416. func DeleteChannel(c *gin.Context) {
  417. id, _ := strconv.Atoi(c.Param("id"))
  418. channel := model.Channel{Id: id}
  419. err := channel.Delete()
  420. if err != nil {
  421. c.JSON(http.StatusOK, gin.H{
  422. "success": false,
  423. "message": err.Error(),
  424. })
  425. return
  426. }
  427. c.JSON(http.StatusOK, gin.H{
  428. "success": true,
  429. "message": "",
  430. })
  431. return
  432. }
  433. func DeleteDisabledChannel(c *gin.Context) {
  434. rows, err := model.DeleteDisabledChannel()
  435. if err != nil {
  436. c.JSON(http.StatusOK, gin.H{
  437. "success": false,
  438. "message": err.Error(),
  439. })
  440. return
  441. }
  442. c.JSON(http.StatusOK, gin.H{
  443. "success": true,
  444. "message": "",
  445. "data": rows,
  446. })
  447. return
  448. }
  449. type ChannelTag struct {
  450. Tag string `json:"tag"`
  451. NewTag *string `json:"new_tag"`
  452. Priority *int64 `json:"priority"`
  453. Weight *uint `json:"weight"`
  454. ModelMapping *string `json:"model_mapping"`
  455. Models *string `json:"models"`
  456. Groups *string `json:"groups"`
  457. }
  458. func DisableTagChannels(c *gin.Context) {
  459. channelTag := ChannelTag{}
  460. err := c.ShouldBindJSON(&channelTag)
  461. if err != nil || channelTag.Tag == "" {
  462. c.JSON(http.StatusOK, gin.H{
  463. "success": false,
  464. "message": "参数错误",
  465. })
  466. return
  467. }
  468. err = model.DisableChannelByTag(channelTag.Tag)
  469. if err != nil {
  470. c.JSON(http.StatusOK, gin.H{
  471. "success": false,
  472. "message": err.Error(),
  473. })
  474. return
  475. }
  476. c.JSON(http.StatusOK, gin.H{
  477. "success": true,
  478. "message": "",
  479. })
  480. return
  481. }
  482. func EnableTagChannels(c *gin.Context) {
  483. channelTag := ChannelTag{}
  484. err := c.ShouldBindJSON(&channelTag)
  485. if err != nil || channelTag.Tag == "" {
  486. c.JSON(http.StatusOK, gin.H{
  487. "success": false,
  488. "message": "参数错误",
  489. })
  490. return
  491. }
  492. err = model.EnableChannelByTag(channelTag.Tag)
  493. if err != nil {
  494. c.JSON(http.StatusOK, gin.H{
  495. "success": false,
  496. "message": err.Error(),
  497. })
  498. return
  499. }
  500. c.JSON(http.StatusOK, gin.H{
  501. "success": true,
  502. "message": "",
  503. })
  504. return
  505. }
  506. func EditTagChannels(c *gin.Context) {
  507. channelTag := ChannelTag{}
  508. err := c.ShouldBindJSON(&channelTag)
  509. if err != nil {
  510. c.JSON(http.StatusOK, gin.H{
  511. "success": false,
  512. "message": "参数错误",
  513. })
  514. return
  515. }
  516. if channelTag.Tag == "" {
  517. c.JSON(http.StatusOK, gin.H{
  518. "success": false,
  519. "message": "tag不能为空",
  520. })
  521. return
  522. }
  523. err = model.EditChannelByTag(channelTag.Tag, channelTag.NewTag, channelTag.ModelMapping, channelTag.Models, channelTag.Groups, channelTag.Priority, channelTag.Weight)
  524. if err != nil {
  525. c.JSON(http.StatusOK, gin.H{
  526. "success": false,
  527. "message": err.Error(),
  528. })
  529. return
  530. }
  531. c.JSON(http.StatusOK, gin.H{
  532. "success": true,
  533. "message": "",
  534. })
  535. return
  536. }
  537. type ChannelBatch struct {
  538. Ids []int `json:"ids"`
  539. Tag *string `json:"tag"`
  540. }
  541. func DeleteChannelBatch(c *gin.Context) {
  542. channelBatch := ChannelBatch{}
  543. err := c.ShouldBindJSON(&channelBatch)
  544. if err != nil || len(channelBatch.Ids) == 0 {
  545. c.JSON(http.StatusOK, gin.H{
  546. "success": false,
  547. "message": "参数错误",
  548. })
  549. return
  550. }
  551. err = model.BatchDeleteChannels(channelBatch.Ids)
  552. if err != nil {
  553. c.JSON(http.StatusOK, gin.H{
  554. "success": false,
  555. "message": err.Error(),
  556. })
  557. return
  558. }
  559. c.JSON(http.StatusOK, gin.H{
  560. "success": true,
  561. "message": "",
  562. "data": len(channelBatch.Ids),
  563. })
  564. return
  565. }
  566. func UpdateChannel(c *gin.Context) {
  567. channel := model.Channel{}
  568. err := c.ShouldBindJSON(&channel)
  569. if err != nil {
  570. c.JSON(http.StatusOK, gin.H{
  571. "success": false,
  572. "message": err.Error(),
  573. })
  574. return
  575. }
  576. if channel.Type == common.ChannelTypeVertexAi {
  577. if channel.Other == "" {
  578. c.JSON(http.StatusOK, gin.H{
  579. "success": false,
  580. "message": "部署地区不能为空",
  581. })
  582. return
  583. } else {
  584. if common.IsJsonStr(channel.Other) {
  585. // must have default
  586. regionMap := common.StrToMap(channel.Other)
  587. if regionMap["default"] == nil {
  588. c.JSON(http.StatusOK, gin.H{
  589. "success": false,
  590. "message": "部署地区必须包含default字段",
  591. })
  592. return
  593. }
  594. }
  595. }
  596. }
  597. err = channel.Update()
  598. if err != nil {
  599. c.JSON(http.StatusOK, gin.H{
  600. "success": false,
  601. "message": err.Error(),
  602. })
  603. return
  604. }
  605. channel.Key = ""
  606. c.JSON(http.StatusOK, gin.H{
  607. "success": true,
  608. "message": "",
  609. "data": channel,
  610. })
  611. return
  612. }
  613. func FetchModels(c *gin.Context) {
  614. var req struct {
  615. BaseURL string `json:"base_url"`
  616. Type int `json:"type"`
  617. Key string `json:"key"`
  618. }
  619. if err := c.ShouldBindJSON(&req); err != nil {
  620. c.JSON(http.StatusBadRequest, gin.H{
  621. "success": false,
  622. "message": "Invalid request",
  623. })
  624. return
  625. }
  626. baseURL := req.BaseURL
  627. if baseURL == "" {
  628. baseURL = common.ChannelBaseURLs[req.Type]
  629. }
  630. client := &http.Client{}
  631. url := fmt.Sprintf("%s/v1/models", baseURL)
  632. request, err := http.NewRequest("GET", url, nil)
  633. if err != nil {
  634. c.JSON(http.StatusInternalServerError, gin.H{
  635. "success": false,
  636. "message": err.Error(),
  637. })
  638. return
  639. }
  640. // remove line breaks and extra spaces.
  641. key := strings.TrimSpace(req.Key)
  642. // If the key contains a line break, only take the first part.
  643. key = strings.Split(key, "\n")[0]
  644. request.Header.Set("Authorization", "Bearer "+key)
  645. response, err := client.Do(request)
  646. if err != nil {
  647. c.JSON(http.StatusInternalServerError, gin.H{
  648. "success": false,
  649. "message": err.Error(),
  650. })
  651. return
  652. }
  653. //check status code
  654. if response.StatusCode != http.StatusOK {
  655. c.JSON(http.StatusInternalServerError, gin.H{
  656. "success": false,
  657. "message": "Failed to fetch models",
  658. })
  659. return
  660. }
  661. defer response.Body.Close()
  662. var result struct {
  663. Data []struct {
  664. ID string `json:"id"`
  665. } `json:"data"`
  666. }
  667. if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
  668. c.JSON(http.StatusInternalServerError, gin.H{
  669. "success": false,
  670. "message": err.Error(),
  671. })
  672. return
  673. }
  674. var models []string
  675. for _, model := range result.Data {
  676. models = append(models, model.ID)
  677. }
  678. c.JSON(http.StatusOK, gin.H{
  679. "success": true,
  680. "data": models,
  681. })
  682. }
  683. func BatchSetChannelTag(c *gin.Context) {
  684. channelBatch := ChannelBatch{}
  685. err := c.ShouldBindJSON(&channelBatch)
  686. if err != nil || len(channelBatch.Ids) == 0 {
  687. c.JSON(http.StatusOK, gin.H{
  688. "success": false,
  689. "message": "参数错误",
  690. })
  691. return
  692. }
  693. err = model.BatchSetChannelTag(channelBatch.Ids, channelBatch.Tag)
  694. if err != nil {
  695. c.JSON(http.StatusOK, gin.H{
  696. "success": false,
  697. "message": err.Error(),
  698. })
  699. return
  700. }
  701. c.JSON(http.StatusOK, gin.H{
  702. "success": true,
  703. "message": "",
  704. "data": len(channelBatch.Ids),
  705. })
  706. return
  707. }
  708. func GetTagModels(c *gin.Context) {
  709. tag := c.Query("tag")
  710. if tag == "" {
  711. c.JSON(http.StatusBadRequest, gin.H{
  712. "success": false,
  713. "message": "tag不能为空",
  714. })
  715. return
  716. }
  717. channels, err := model.GetChannelsByTag(tag, false) // Assuming false for idSort is fine here
  718. if err != nil {
  719. c.JSON(http.StatusInternalServerError, gin.H{
  720. "success": false,
  721. "message": err.Error(),
  722. })
  723. return
  724. }
  725. var longestModels string
  726. maxLength := 0
  727. // Find the longest models string among all channels with the given tag
  728. for _, channel := range channels {
  729. if channel.Models != "" {
  730. currentModels := strings.Split(channel.Models, ",")
  731. if len(currentModels) > maxLength {
  732. maxLength = len(currentModels)
  733. longestModels = channel.Models
  734. }
  735. }
  736. }
  737. c.JSON(http.StatusOK, gin.H{
  738. "success": true,
  739. "message": "",
  740. "data": longestModels,
  741. })
  742. return
  743. }