channel.go 18 KB

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