Просмотр исходного кода

feat: Add channel search by model field (close #72)

1808837298@qq.com 2 лет назад
Родитель
Сommit
413d4f0a66
3 измененных файлов с 48 добавлено и 17 удалено
  1. 2 1
      controller/channel.go
  2. 26 8
      model/channel.go
  3. 20 8
      web/src/components/ChannelsTable.js

+ 2 - 1
controller/channel.go

@@ -54,8 +54,9 @@ func FixChannelsAbilities(c *gin.Context) {
 func SearchChannels(c *gin.Context) {
 	keyword := c.Query("keyword")
 	group := c.Query("group")
+	modelKeyword := c.Query("model")
 	//idSort, _ := strconv.ParseBool(c.Query("id_sort"))
-	channels, err := model.SearchChannels(keyword, group)
+	channels, err := model.SearchChannels(keyword, group, modelKeyword)
 	if err != nil {
 		c.JSON(http.StatusOK, gin.H{
 			"success": false,

+ 26 - 8
model/channel.go

@@ -43,21 +43,39 @@ func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Chan
 	return channels, err
 }
 
-func SearchChannels(keyword string, group string) (channels []*Channel, err error) {
+func SearchChannels(keyword string, group string, model string) ([]*Channel, error) {
+	var channels []*Channel
 	keyCol := "`key`"
+	groupCol := "`group`"
+	modelsCol := "`models`"
+
+	// 如果是 PostgreSQL,使用双引号
 	if common.UsingPostgreSQL {
 		keyCol = `"key"`
+		groupCol = `"group"`
+		modelsCol = `"models"`
 	}
+
+	// 构造基础查询
+	baseQuery := DB.Model(&Channel{}).Omit(keyCol)
+
+	// 构造WHERE子句
+	var whereClause string
+	var args []interface{}
 	if group != "" {
-		groupCol := "`group`"
-		if common.UsingPostgreSQL {
-			groupCol = `"group"`
-		}
-		err = DB.Omit("key").Where("(id = ? or name LIKE ? or "+keyCol+" = ?) and "+groupCol+" LIKE ?", common.String2Int(keyword), keyword+"%", keyword, "%"+group+"%").Find(&channels).Error
+		whereClause = "(id = ? OR name LIKE ? OR " + keyCol + " = ?) AND " + groupCol + " LIKE ? AND " + modelsCol + " LIKE ?"
+		args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+group+"%", "%"+model+"%")
 	} else {
-		err = DB.Omit("key").Where("id = ? or name LIKE ? or "+keyCol+" = ?", common.String2Int(keyword), keyword+"%", keyword).Find(&channels).Error
+		whereClause = "(id = ? OR name LIKE ? OR " + keyCol + " = ?) AND " + modelsCol + " LIKE ?"
+		args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+model+"%")
 	}
-	return channels, err
+
+	// 执行查询
+	err := baseQuery.Where(whereClause, args...).Find(&channels).Error
+	if err != nil {
+		return nil, err
+	}
+	return channels, nil
 }
 
 func GetChannelById(id int, selectAll bool) (*Channel, error) {

+ 20 - 8
web/src/components/ChannelsTable.js

@@ -257,6 +257,7 @@ const ChannelsTable = () => {
     const [idSort, setIdSort] = useState(false);
     const [searchKeyword, setSearchKeyword] = useState('');
     const [searchGroup, setSearchGroup] = useState('');
+    const [searchModel, setSearchModel] = useState('');
     const [searching, setSearching] = useState(false);
     const [updatingBalance, setUpdatingBalance] = useState(false);
     const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE);
@@ -440,15 +441,15 @@ const ChannelsTable = () => {
         }
     };
 
-    const searchChannels = async (searchKeyword, searchGroup) => {
-        if (searchKeyword === '' && searchGroup === '') {
+    const searchChannels = async (searchKeyword, searchGroup, searchModel) => {
+        if (searchKeyword === '' && searchGroup === '' && searchModel === '') {
             // if keyword is blank, load files instead.
             await loadChannels(0, pageSize, idSort);
             setActivePage(1);
             return;
         }
         setSearching(true);
-        const res = await API.get(`/api/channel/search?keyword=${searchKeyword}&group=${searchGroup}`);
+        const res = await API.get(`/api/channel/search?keyword=${searchKeyword}&group=${searchGroup}&model=${searchModel}`);
         const {success, message, data} = res.data;
         if (success) {
             setChannels(data);
@@ -625,13 +626,12 @@ const ChannelsTable = () => {
     return (
         <>
             <EditChannel refresh={refresh} visible={showEdit} handleClose={closeEdit} editingChannel={editingChannel}/>
-            <Form onSubmit={() => {searchChannels(searchKeyword, searchGroup)}} labelPosition='left'>
-
+            <Form onSubmit={() => {searchChannels(searchKeyword, searchGroup, searchModel)}} labelPosition='left'>
                 <div style={{display: 'flex'}}>
                     <Space>
                         <Form.Input
-                            field='search'
-                            label='关键词'
+                            field='search_keyword'
+                            label='搜索渠道关键词'
                             placeholder='ID,名称和密钥 ...'
                             value={searchKeyword}
                             loading={searching}
@@ -639,10 +639,22 @@ const ChannelsTable = () => {
                                 setSearchKeyword(v.trim())
                             }}
                         />
+                        <Form.Input
+                          field='search_model'
+                          label='模型'
+                          placeholder='模型关键字'
+                          value={searchModel}
+                          loading={searching}
+                          onChange={(v)=>{
+                              setSearchModel(v.trim())
+                          }}
+                        />
                         <Form.Select field="group" label='分组' optionList={groupOptions} onChange={(v) => {
                             setSearchGroup(v)
-                            searchChannels(searchKeyword, v)
+                            searchChannels(searchKeyword, v, searchModel)
                         }}/>
+                        <Button label='查询' type="primary" htmlType="submit" className="btn-margin-right"
+                                style={{marginRight: 8}}>查询</Button>
                     </Space>
                 </div>
             </Form>