资产增加标签的功能

This commit is contained in:
dushixiang 2021-01-04 21:29:37 +08:00
parent 5457f8f4dd
commit c362d9b98a
8 changed files with 94 additions and 31 deletions

View File

@ -114,3 +114,11 @@ func AssetTcpingEndpoint(c echo.Context) (err error) {
model.UpdateAssetById(&asset, item.ID) model.UpdateAssetById(&asset, item.ID)
return Success(c, active) return Success(c, active)
} }
func AssetTagsEndpoint(c echo.Context) (err error) {
var items []string
if items, err = model.FindAssetTags(); err != nil {
return err
}
return Success(c, items)
}

View File

@ -57,6 +57,8 @@ func SetupRoutes() *echo.Echo {
assets.GET("/:id", AssetGetEndpoint) assets.GET("/:id", AssetGetEndpoint)
} }
e.GET("/tags", AssetTagsEndpoint)
commands := e.Group("/commands") commands := e.Group("/commands")
{ {
commands.GET("/paging", CommandPagingEndpoint) commands.GET("/paging", CommandPagingEndpoint)
@ -94,6 +96,16 @@ func SetupRoutes() *echo.Echo {
sessions.GET("/:id", SessionGetEndpoint) sessions.GET("/:id", SessionGetEndpoint)
} }
//tags := e.Group("/tags")
//{
// tags.POST("", TagAllEndpoint)
// tags.GET("/paging", TagPagingEndpoint)
// tags.POST("", TagCreateEndpoint)
// tags.PUT("/:id", TagUpdateEndpoint)
// tags.DELETE("/:id", TagDeleteEndpoint)
// tags.GET("/:id", TagGetEndpoint)
//}
e.GET("/properties", PropertyGetEndpoint) e.GET("/properties", PropertyGetEndpoint)
e.PUT("/properties", PropertyUpdateEndpoint) e.PUT("/properties", PropertyUpdateEndpoint)

View File

@ -3,6 +3,7 @@ package model
import ( import (
"next-terminal/pkg/global" "next-terminal/pkg/global"
"next-terminal/pkg/utils" "next-terminal/pkg/utils"
"strings"
) )
type Asset struct { type Asset struct {
@ -20,6 +21,7 @@ type Asset struct {
Description string `json:"description"` Description string `json:"description"`
Active bool `json:"active"` Active bool `json:"active"`
Created utils.JsonTime `json:"created"` Created utils.JsonTime `json:"created"`
Tags string `json:"tags"`
} }
func (r *Asset) TableName() string { func (r *Asset) TableName() string {
@ -84,3 +86,24 @@ func CountAsset() (total int64, err error) {
err = global.DB.Find(&Asset{}).Count(&total).Error err = global.DB.Find(&Asset{}).Count(&total).Error
return return
} }
func FindAssetTags() (o []string, err error) {
var assets []Asset
err = global.DB.Not("tags = ?", "").Find(&assets).Error
if err != nil {
return nil, err
}
o = make([]string, 0)
for i := range assets {
if len(assets[i].Tags) == 0 {
continue
}
split := strings.Split(assets[i].Tags, ",")
o = append(o, split...)
}
return o, nil
}

View File

@ -43,7 +43,6 @@ func CreateNewCredential(o *Credential) (err error) {
} }
func FindCredentialById(id string) (o Credential, err error) { func FindCredentialById(id string) (o Credential, err error) {
err = global.DB.Where("id = ?", id).First(&o).Error err = global.DB.Where("id = ?", id).First(&o).Error
return return
} }

View File

@ -1,23 +0,0 @@
package model
import "next-terminal/pkg/global"
type Tag struct {
Tag string `json:"tag"`
}
func (r *Tag) TableName() string {
return "tags"
}
func FindAllTag() (o []Property) {
if global.DB.Find(&o).Error != nil {
return nil
}
return
}
func CreateNewTag(o *Tag) (err error) {
err = global.DB.Create(o).Error
return
}

View File

@ -70,6 +70,7 @@ class Asset extends Component {
modalTitle: '', modalTitle: '',
modalConfirmLoading: false, modalConfirmLoading: false,
credentials: [], credentials: [],
tags: [],
model: {}, model: {},
selectedRowKeys: [], selectedRowKeys: [],
delBtnLoading: false, delBtnLoading: false,
@ -193,17 +194,31 @@ class Asset extends Component {
await this.showModal('复制资产', result.data); await this.showModal('复制资产', result.data);
} }
async showModal(title, assets = {}) { async showModal(title, asset = {}) {
let result = await request.get('/credentials'); // 并行请求
let getCredentials = request.get('/credentials');
let getTags = request.get('/tags');
let credentials = []; let credentials = [];
if (result.code === 1) { let tags = [];
credentials = result.data;
let r1 = await getCredentials;
let r2 = await getTags;
if (r1['code'] === 1) {
credentials = r1['data'];
} }
if (r2['code'] === 1) {
tags = r2['data'];
}
this.setState({ this.setState({
modalTitle: title, modalTitle: title,
modalVisible: true, modalVisible: true,
credentials: credentials, credentials: credentials,
model: assets tags: tags,
model: asset
}); });
}; };
@ -220,6 +235,8 @@ class Asset extends Component {
modalConfirmLoading: true modalConfirmLoading: true
}); });
formData.tags = formData.tags.join(',');
if (formData.id) { if (formData.id) {
// 向后台提交数据 // 向后台提交数据
const result = await request.put('/assets/' + formData.id, formData); const result = await request.put('/assets/' + formData.id, formData);
@ -501,6 +518,7 @@ class Asset extends Component {
handleCancel={this.handleCancelModal} handleCancel={this.handleCancelModal}
confirmLoading={this.state.modalConfirmLoading} confirmLoading={this.state.modalConfirmLoading}
credentials={this.state.credentials} credentials={this.state.credentials}
tags={this.state.tags}
model={this.state.model} model={this.state.model}
/> />
: null : null

View File

@ -1,5 +1,6 @@
import React, {useEffect, useState} from 'react'; import React, {useEffect, useState} from 'react';
import {Form, Input, InputNumber, Modal, Radio, Select, Tooltip} from "antd/lib/index"; import {Form, Input, InputNumber, Modal, Radio, Select, Tooltip} from "antd/lib/index";
import {isEmpty} from "../../utils/utils";
const {TextArea} = Input; const {TextArea} = Input;
const {Option} = Select; const {Option} = Select;
@ -18,7 +19,7 @@ const protocolMapping = {
'telnet': [{text: '自定义', value: 'custom'}, {text: '授权凭证', value: 'credential'}] 'telnet': [{text: '自定义', value: 'custom'}, {text: '授权凭证', value: 'credential'}]
} }
const AssetModal = function ({title, visible, handleOk, handleCancel, confirmLoading, credentials, model}) { const AssetModal = function ({title, visible, handleOk, handleCancel, confirmLoading, credentials, tags, model}) {
const [form] = Form.useForm(); const [form] = Form.useForm();
@ -42,6 +43,15 @@ const AssetModal = function ({title, visible, handleOk, handleCancel, confirmLoa
} }
} }
let initAssetTags = []
if (!isEmpty(model['tags'])) {
initAssetTags = model['tags'].split(',');
}
let [assetTags, setAssetTags] = useState(initAssetTags);
console.log('初始元素', assetTags)
model['tags'] = undefined;
const formItemLayout = { const formItemLayout = {
labelCol: {span: 6}, labelCol: {span: 6},
wrapperCol: {span: 14}, wrapperCol: {span: 14},
@ -96,6 +106,11 @@ const AssetModal = function ({title, visible, handleOk, handleCancel, confirmLoa
model.accountType = v; model.accountType = v;
} }
const handleTagsChange = v => {
console.log(v)
setAssetTags(v);
}
return ( return (
<Modal <Modal
@ -152,7 +167,6 @@ const AssetModal = function ({title, visible, handleOk, handleCancel, confirmLoa
</Select> </Select>
</Form.Item> </Form.Item>
{ {
accountType === 'credential' ? accountType === 'credential' ?
<Form.Item label="授权凭证" name='credentialId' rules={[{required: true, message: '请选择授权凭证'}]}> <Form.Item label="授权凭证" name='credentialId' rules={[{required: true, message: '请选择授权凭证'}]}>
@ -203,6 +217,14 @@ const AssetModal = function ({title, visible, handleOk, handleCancel, confirmLoa
: null : null
} }
<Form.Item label="标签" name='tags'>
<Select mode="tags" placeholder="请选择标签" defaultValue={assetTags} onChange={handleTagsChange}>
{tags.map(tag => {
return (<Option key={tag}>{tag}</Option>)
})}
</Select>
</Form.Item>
</Form> </Form>
</Modal> </Modal>
) )

View File

@ -143,3 +143,7 @@ export function differTime(start, end) {
} }
return show; return show;
} }
export const isEmpty = (text) =>{
return text === undefined || text == null || text.length === 0;
}