🀄 完善标签查询功能

This commit is contained in:
dushixiang 2021-01-06 20:29:48 +08:00
parent fbac4c7c2b
commit 1a3a4025d5
4 changed files with 82 additions and 10 deletions

View File

@ -29,8 +29,9 @@ func AssetPagingEndpoint(c echo.Context) error {
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize")) pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
name := c.QueryParam("name") name := c.QueryParam("name")
protocol := c.QueryParam("protocol") protocol := c.QueryParam("protocol")
tags := c.QueryParam("tags")
items, total, _ := model.FindPageAsset(pageIndex, pageSize, name, protocol) items, total, _ := model.FindPageAsset(pageIndex, pageSize, name, protocol, tags)
return Success(c, H{ return Success(c, H{
"total": total, "total": total,

View File

@ -43,7 +43,7 @@ func FindAssetByConditions(protocol string) (o []Asset, err error) {
return return
} }
func FindPageAsset(pageIndex, pageSize int, name, protocol string) (o []Asset, total int64, err error) { func FindPageAsset(pageIndex, pageSize int, name, protocol, tags string) (o []Asset, total int64, err error) {
db := global.DB db := global.DB
if len(name) > 0 { if len(name) > 0 {
db = db.Where("name like ?", "%"+name+"%") db = db.Where("name like ?", "%"+name+"%")
@ -53,6 +53,13 @@ func FindPageAsset(pageIndex, pageSize int, name, protocol string) (o []Asset, t
db = db.Where("protocol = ?", protocol) db = db.Where("protocol = ?", protocol)
} }
if len(tags) > 0 {
tagArr := strings.Split(tags, ",")
for i := range tagArr {
db = db.Where("find_in_set(?, tags)", tagArr[i])
}
}
err = db.Order("created desc").Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error err = db.Order("created desc").Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error
if o == nil { if o == nil {
@ -105,5 +112,5 @@ func FindAssetTags() (o []string, err error) {
o = append(o, split...) o = append(o, split...)
} }
return o, nil return utils.Distinct(o), nil
} }

View File

@ -119,3 +119,16 @@ func IsDir(path string) bool {
func IsFile(path string) bool { func IsFile(path string) bool {
return !IsDir(path) return !IsDir(path)
} }
// 去除重复元素
func Distinct(list []string) []string {
result := make([]string, 0, len(list))
temp := map[string]struct{}{}
for _, item := range list {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}

View File

@ -21,6 +21,7 @@ import qs from "qs";
import AssetModal from "./AssetModal"; import AssetModal from "./AssetModal";
import request from "../../common/request"; import request from "../../common/request";
import {message} from "antd/es"; import {message} from "antd/es";
import {isEmpty, itemRender} from "../../utils/utils";
import { import {
@ -34,7 +35,6 @@ import {
SyncOutlined, SyncOutlined,
UndoOutlined UndoOutlined
} from '@ant-design/icons'; } from '@ant-design/icons';
import {itemRender} from "../../utils/utils";
import {PROTOCOL_COLORS} from "../../common/constants"; import {PROTOCOL_COLORS} from "../../common/constants";
import Logout from "../user/Logout"; import Logout from "../user/Logout";
@ -77,7 +77,14 @@ class Asset extends Component {
}; };
async componentDidMount() { async componentDidMount() {
await this.loadTableData(); this.loadTableData();
let result = await request.get('/tags');
if (result['code'] === 1) {
this.setState({
tags: result['data']
})
}
} }
async delete(id) { async delete(id) {
@ -151,6 +158,21 @@ class Asset extends Component {
this.loadTableData(query); this.loadTableData(query);
}; };
handleTagsChange = tags => {
console.log(tags)
// this.setState({
// tags: tags
// })
let query = {
...this.state.queryParams,
'pageIndex': 1,
'pageSize': this.state.queryParams.pageSize,
'tags': tags.join(','),
}
this.loadTableData(query);
}
handleSearchByProtocol = protocol => { handleSearchByProtocol = protocol => {
let query = { let query = {
...this.state.queryParams, ...this.state.queryParams,
@ -335,13 +357,13 @@ class Asset extends Component {
); );
} }
}, { }, {
title: 'IP', title: '网络',
dataIndex: 'ip', dataIndex: 'ip',
key: 'ip', key: 'ip',
}, { render: (text, record) => {
title: '端口',
dataIndex: 'port', return record['ip'] + ':' + record['port'];
key: 'port', }
}, { }, {
title: '连接协议', title: '连接协议',
dataIndex: 'protocol', dataIndex: 'protocol',
@ -361,6 +383,23 @@ class Asset extends Component {
return (<Badge status="error" text="不可用"/>); return (<Badge status="error" text="不可用"/>);
} }
} }
}, {
title: '标签',
dataIndex: 'tags',
key: 'tags',
render: tags => {
if (!isEmpty(tags)) {
let tagDocuments = []
let tagArr = tags.split(',');
for (let i = 0; i < tagArr.length; i++) {
if (tags[i] === '-') {
continue;
}
tagDocuments.push(<Tag>{tagArr[i]}</Tag>)
}
return tagDocuments;
}
}
}, { }, {
title: '创建日期', title: '创建日期',
dataIndex: 'created', dataIndex: 'created',
@ -427,6 +466,18 @@ class Asset extends Component {
onSearch={this.handleSearchByName} onSearch={this.handleSearchByName}
/> />
<Select mode="multiple"
allowClear
placeholder="请选择标签" onChange={this.handleTagsChange}
style={{minWidth: 150}}>
{this.state.tags.map(tag => {
if (tag === '-') {
return undefined;
}
return (<Select.Option key={tag}>{tag}</Select.Option>)
})}
</Select>
<Select onChange={this.handleSearchByProtocol} <Select onChange={this.handleSearchByProtocol}
value={this.state.queryParams.protocol ? this.state.queryParams.protocol : ''} value={this.state.queryParams.protocol ? this.state.queryParams.protocol : ''}
style={{width: 100}}> style={{width: 100}}>