From b9007ced9008aca9fd6ef58193cae0cdaeb8c240 Mon Sep 17 00:00:00 2001 From: daggeryu <997411652@qq.com> Date: Thu, 5 Dec 2024 23:21:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=20=E5=85=B3=E9=94=AE=E8=AF=8D=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=8A=A0=E6=A0=87=E7=AD=BE=E8=81=9A=E5=90=88=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=A4=A7=E4=BA=8E1=E4=B8=AA=E7=A9=BA=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E6=B8=A0=E9=81=93=E6=97=A0=E6=B3=95=E5=B1=95=E5=BC=80?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/components/ChannelsTable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/src/components/ChannelsTable.js b/web/src/components/ChannelsTable.js index 4445ccb4..0139030f 100644 --- a/web/src/components/ChannelsTable.js +++ b/web/src/components/ChannelsTable.js @@ -484,7 +484,7 @@ const ChannelsTable = () => { if (!enableTagMode) { channelDates.push(channels[i]); } else { - let tag = channels[i].tag; + let tag = channels[i].tag?channels[i].tag:""; // find from channelTags let tagIndex = channelTags[tag]; let tagChannelDates = undefined; From aa82adc5a92493bcc64cb844e0522d824d28e96e Mon Sep 17 00:00:00 2001 From: CalciumIon <1808837298@qq.com> Date: Fri, 6 Dec 2024 22:03:50 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B9=E6=B8=A0?= =?UTF-8?q?=E9=81=93=E6=90=9C=E7=B4=A2=E4=B8=8B=E6=A0=87=E7=AD=BE=E8=81=9A?= =?UTF-8?q?=E5=90=88=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/channel.go | 38 ++++++++++++++++----- model/channel.go | 52 +++++++++++++++++++++++++++++ web/src/components/ChannelsTable.js | 6 ++-- 3 files changed, 85 insertions(+), 11 deletions(-) diff --git a/controller/channel.go b/controller/channel.go index 40b9cde4..83755333 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -168,18 +168,40 @@ func SearchChannels(c *gin.Context) { group := c.Query("group") modelKeyword := c.Query("model") idSort, _ := strconv.ParseBool(c.Query("id_sort")) - channels, err := model.SearchChannels(keyword, group, modelKeyword, idSort) - if err != nil { - c.JSON(http.StatusOK, gin.H{ - "success": false, - "message": err.Error(), - }) - return + enableTagMode, _ := strconv.ParseBool(c.Query("tag_mode")) + channelData := make([]*model.Channel, 0) + if enableTagMode { + tags, err := model.SearchTags(keyword, group, modelKeyword, idSort) + if err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } + for _, tag := range tags { + if tag != nil && *tag != "" { + tagChannel, err := model.GetChannelsByTag(*tag) + if err == nil { + channelData = append(channelData, tagChannel...) + } + } + } + } else { + channels, err := model.SearchChannels(keyword, group, modelKeyword, idSort) + if err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } + channelData = channels } c.JSON(http.StatusOK, gin.H{ "success": true, "message": "", - "data": channels, + "data": channelData, }) return } diff --git a/model/channel.go b/model/channel.go index 9d59579f..48408f1d 100644 --- a/model/channel.go +++ b/model/channel.go @@ -410,3 +410,55 @@ func GetPaginatedTags(offset int, limit int) ([]*string, error) { err := DB.Model(&Channel{}).Select("DISTINCT tag").Where("tag != ''").Offset(offset).Limit(limit).Find(&tags).Error return tags, err } + +func SearchTags(keyword string, group string, model string, idSort bool) ([]*string, error) { + var tags []*string + keyCol := "`key`" + groupCol := "`group`" + modelsCol := "`models`" + + // 如果是 PostgreSQL,使用双引号 + if common.UsingPostgreSQL { + keyCol = `"key"` + groupCol = `"group"` + modelsCol = `"models"` + } + + order := "priority desc" + if idSort { + order = "id desc" + } + + // 构造基础查询 + baseQuery := DB.Model(&Channel{}).Omit(keyCol) + + // 构造WHERE子句 + var whereClause string + var args []interface{} + if group != "" && group != "null" { + var groupCondition string + if common.UsingMySQL { + groupCondition = `CONCAT(',', ` + groupCol + `, ',') LIKE ?` + } else { + // sqlite, PostgreSQL + groupCondition = `(',' || ` + groupCol + ` || ',') LIKE ?` + } + whereClause = "(id = ? OR name LIKE ? OR " + keyCol + " = ?) AND " + modelsCol + ` LIKE ? AND ` + groupCondition + args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+model+"%", "%,"+group+",%") + } else { + whereClause = "(id = ? OR name LIKE ? OR " + keyCol + " = ?) AND " + modelsCol + " LIKE ?" + args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+model+"%") + } + + err := baseQuery.Where(whereClause, args...). + Select("DISTINCT tag"). + Where("tag != ''"). + Order(order). + Find(&tags).Error + + if err != nil { + return nil, err + } + + return tags, nil +} diff --git a/web/src/components/ChannelsTable.js b/web/src/components/ChannelsTable.js index 0139030f..61dd45a2 100644 --- a/web/src/components/ChannelsTable.js +++ b/web/src/components/ChannelsTable.js @@ -768,7 +768,7 @@ const ChannelsTable = () => { } }; - const searchChannels = async (searchKeyword, searchGroup, searchModel) => { + const searchChannels = async (searchKeyword, searchGroup, searchModel, enableTagMode) => { if (searchKeyword === '' && searchGroup === '' && searchModel === '') { await loadChannels(0, pageSize, idSort, enableTagMode); setActivePage(1); @@ -982,7 +982,7 @@ const ChannelsTable = () => { />
{ - searchChannels(searchKeyword, searchGroup, searchModel); + searchChannels(searchKeyword, searchGroup, searchModel, enableTagMode); }} labelPosition="left" > @@ -1015,7 +1015,7 @@ const ChannelsTable = () => { initValue={null} onChange={(v) => { setSearchGroup(v); - searchChannels(searchKeyword, v, searchModel); + searchChannels(searchKeyword, v, searchModel, enableTagMode); }} />