From c362d9b98a4d94f4fa7dcf8787b7963021f8a3cc Mon Sep 17 00:00:00 2001 From: dushixiang Date: Mon, 4 Jan 2021 21:29:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E4=BA=A7=E5=A2=9E=E5=8A=A0=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/api/asset.go | 8 ++++++++ pkg/api/routes.go | 12 +++++++++++ pkg/model/asset.go | 23 +++++++++++++++++++++ pkg/model/credential.go | 1 - pkg/model/tag.go | 23 --------------------- web/src/components/asset/Asset.js | 28 +++++++++++++++++++++----- web/src/components/asset/AssetModal.js | 26 ++++++++++++++++++++++-- web/src/utils/utils.js | 4 ++++ 8 files changed, 94 insertions(+), 31 deletions(-) delete mode 100644 pkg/model/tag.go diff --git a/pkg/api/asset.go b/pkg/api/asset.go index 188640c..2108aa7 100644 --- a/pkg/api/asset.go +++ b/pkg/api/asset.go @@ -114,3 +114,11 @@ func AssetTcpingEndpoint(c echo.Context) (err error) { model.UpdateAssetById(&asset, item.ID) 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) +} diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 057f53c..a221d34 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -57,6 +57,8 @@ func SetupRoutes() *echo.Echo { assets.GET("/:id", AssetGetEndpoint) } + e.GET("/tags", AssetTagsEndpoint) + commands := e.Group("/commands") { commands.GET("/paging", CommandPagingEndpoint) @@ -94,6 +96,16 @@ func SetupRoutes() *echo.Echo { 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.PUT("/properties", PropertyUpdateEndpoint) diff --git a/pkg/model/asset.go b/pkg/model/asset.go index c601bd2..de39fe8 100644 --- a/pkg/model/asset.go +++ b/pkg/model/asset.go @@ -3,6 +3,7 @@ package model import ( "next-terminal/pkg/global" "next-terminal/pkg/utils" + "strings" ) type Asset struct { @@ -20,6 +21,7 @@ type Asset struct { Description string `json:"description"` Active bool `json:"active"` Created utils.JsonTime `json:"created"` + Tags string `json:"tags"` } func (r *Asset) TableName() string { @@ -84,3 +86,24 @@ func CountAsset() (total int64, err error) { err = global.DB.Find(&Asset{}).Count(&total).Error 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 +} diff --git a/pkg/model/credential.go b/pkg/model/credential.go index d47ab2e..d2409f2 100644 --- a/pkg/model/credential.go +++ b/pkg/model/credential.go @@ -43,7 +43,6 @@ func CreateNewCredential(o *Credential) (err error) { } func FindCredentialById(id string) (o Credential, err error) { - err = global.DB.Where("id = ?", id).First(&o).Error return } diff --git a/pkg/model/tag.go b/pkg/model/tag.go deleted file mode 100644 index d79c073..0000000 --- a/pkg/model/tag.go +++ /dev/null @@ -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 -} diff --git a/web/src/components/asset/Asset.js b/web/src/components/asset/Asset.js index e7280ab..29c4962 100644 --- a/web/src/components/asset/Asset.js +++ b/web/src/components/asset/Asset.js @@ -70,6 +70,7 @@ class Asset extends Component { modalTitle: '', modalConfirmLoading: false, credentials: [], + tags: [], model: {}, selectedRowKeys: [], delBtnLoading: false, @@ -193,17 +194,31 @@ class Asset extends Component { await this.showModal('复制资产', result.data); } - async showModal(title, assets = {}) { - let result = await request.get('/credentials'); + async showModal(title, asset = {}) { + // 并行请求 + let getCredentials = request.get('/credentials'); + let getTags = request.get('/tags'); + let credentials = []; - if (result.code === 1) { - credentials = result.data; + let tags = []; + + let r1 = await getCredentials; + let r2 = await getTags; + + if (r1['code'] === 1) { + credentials = r1['data']; } + + if (r2['code'] === 1) { + tags = r2['data']; + } + this.setState({ modalTitle: title, modalVisible: true, credentials: credentials, - model: assets + tags: tags, + model: asset }); }; @@ -220,6 +235,8 @@ class Asset extends Component { modalConfirmLoading: true }); + formData.tags = formData.tags.join(','); + if (formData.id) { // 向后台提交数据 const result = await request.put('/assets/' + formData.id, formData); @@ -501,6 +518,7 @@ class Asset extends Component { handleCancel={this.handleCancelModal} confirmLoading={this.state.modalConfirmLoading} credentials={this.state.credentials} + tags={this.state.tags} model={this.state.model} /> : null diff --git a/web/src/components/asset/AssetModal.js b/web/src/components/asset/AssetModal.js index a41c779..8d844b1 100644 --- a/web/src/components/asset/AssetModal.js +++ b/web/src/components/asset/AssetModal.js @@ -1,5 +1,6 @@ import React, {useEffect, useState} from 'react'; import {Form, Input, InputNumber, Modal, Radio, Select, Tooltip} from "antd/lib/index"; +import {isEmpty} from "../../utils/utils"; const {TextArea} = Input; const {Option} = Select; @@ -18,7 +19,7 @@ const protocolMapping = { '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(); @@ -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 = { labelCol: {span: 6}, wrapperCol: {span: 14}, @@ -96,6 +106,11 @@ const AssetModal = function ({title, visible, handleOk, handleCancel, confirmLoa model.accountType = v; } + const handleTagsChange = v => { + console.log(v) + setAssetTags(v); + } + return ( - { accountType === 'credential' ? @@ -203,6 +217,14 @@ const AssetModal = function ({title, visible, handleOk, handleCancel, confirmLoa : null } + + + + ) diff --git a/web/src/utils/utils.js b/web/src/utils/utils.js index 001c53c..fef15c2 100644 --- a/web/src/utils/utils.js +++ b/web/src/utils/utils.js @@ -143,3 +143,7 @@ export function differTime(start, end) { } return show; } + +export const isEmpty = (text) =>{ + return text === undefined || text == null || text.length === 0; +}