feat: 增加主机信息查询接口

This commit is contained in:
ssongliu 2022-08-29 18:44:35 +08:00 committed by ssongliu
parent 907d8614a8
commit c5b2eddd3f
9 changed files with 48 additions and 16 deletions

View File

@ -27,13 +27,13 @@ func (b *BaseApi) Create(c *gin.Context) {
}
func (b *BaseApi) PageHosts(c *gin.Context) {
var req dto.PageInfo
var req dto.SearchWithPage
if err := c.ShouldBindJSON(&req); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
return
}
total, list, err := hostService.Page(req)
total, list, err := hostService.Search(req)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return

View File

@ -14,6 +14,11 @@ type HostCreate struct {
Description string `json:"description"`
}
type SearchWithPage struct {
PageInfo
Info string `json:"info"`
}
type HostInfo struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"createdAt"`

View File

@ -3,6 +3,7 @@ package repo
import (
"github.com/1Panel-dev/1Panel/app/model"
"github.com/1Panel-dev/1Panel/global"
"gorm.io/gorm"
)
type HostRepo struct{}
@ -10,6 +11,7 @@ type HostRepo struct{}
type IHostRepo interface {
Get(opts ...DBOption) (model.Host, error)
Page(limit, offset int, opts ...DBOption) (int64, []model.Host, error)
WithByInfo(info string) DBOption
Create(host *model.Host) error
Update(id uint, vars map[string]interface{}) error
Delete(opts ...DBOption) error
@ -41,6 +43,13 @@ func (u *HostRepo) Page(page, size int, opts ...DBOption) (int64, []model.Host,
return count, hosts, err
}
func (c *HostRepo) WithByInfo(info string) DBOption {
return func(g *gorm.DB) *gorm.DB {
infoStr := "%" + info + "%"
return g.Where("name LIKE ? OR addr LIKE ?", infoStr, infoStr)
}
}
func (u *HostRepo) Create(host *model.Host) error {
return global.DB.Create(host).Error
}

View File

@ -12,7 +12,7 @@ type HostService struct{}
type IHostService interface {
GetConnInfo(id uint) (*model.Host, error)
Page(search dto.PageInfo) (int64, interface{}, error)
Search(search dto.SearchWithPage) (int64, interface{}, error)
Create(hostDto dto.HostCreate) (*dto.HostInfo, error)
Update(id uint, upMap map[string]interface{}) error
BatchDelete(ids []uint) error
@ -30,8 +30,8 @@ func (u *HostService) GetConnInfo(id uint) (*model.Host, error) {
return &host, err
}
func (u *HostService) Page(search dto.PageInfo) (int64, interface{}, error) {
total, hosts, err := hostRepo.Page(search.Page, search.PageSize)
func (u *HostService) Search(search dto.SearchWithPage) (int64, interface{}, error) {
total, hosts, err := hostRepo.Page(search.Page, search.PageSize, hostRepo.WithByInfo(search.Info))
var dtoHosts []dto.HostInfo
for _, host := range hosts {
var item dto.HostInfo

View File

@ -1,4 +1,4 @@
import { CommonModel } from '.';
import { CommonModel, ReqPage } from '.';
export namespace Host {
export interface Host extends CommonModel {
@ -21,4 +21,7 @@ export namespace Host {
description: string;
}
export interface ReqSearchWithPage extends ReqPage {
info?: string;
}
}

View File

@ -1,8 +1,8 @@
import http from '@/api';
import { ResPage, ReqPage } from '../interface';
import { ResPage } from '../interface';
import { Host } from '../interface/host';
export const getHostList = (params: ReqPage) => {
export const getHostList = (params: Host.ReqSearchWithPage) => {
return http.post<ResPage<Host.Host>>(`/hosts/search`, params);
};

View File

@ -2,6 +2,7 @@ export default {
commons: {
button: {
create: 'Create',
add: 'Add',
delete: 'Delete',
edit: 'Edit',
confirm: 'Confirm',

View File

@ -2,6 +2,7 @@ export default {
commons: {
button: {
create: '创建',
add: '添加',
delete: '删除',
edit: '编辑',
confirm: '确认',

View File

@ -37,7 +37,12 @@
</div>
<el-drawer :size="320" v-model="hostDrawer" :title="$t('terminal.hostHistory')" direction="rtl">
<el-button @click="onAddHost">{{ $t('terminal.addHost') }}</el-button>
<el-input size="small" clearable style="margin-top: 5px" v-model="searcConfig.info">
<template #prepend>
<el-button icon="plus" @click="onAddHost">{{ $t('commons.button.add') }}</el-button>
</template>
<template #append><el-button icon="search" @click="loadHost" /></template>
</el-input>
<div v-infinite-scroll="nextPage" style="overflow: auto">
<el-card
@click="onConnLocal()"
@ -89,16 +94,16 @@
<el-dialog v-model="connVisiable" :title="$t('terminal.addHost')" width="30%">
<el-form ref="hostInfoRef" label-width="80px" :model="hostInfo" :rules="rules">
<el-form-item :label="$t('commons.table.name')" prop="name">
<el-input v-model="hostInfo.name" style="width: 80%" />
<el-input clearable v-model="hostInfo.name" style="width: 80%" />
</el-form-item>
<el-form-item label="IP" prop="addr">
<el-input v-model="hostInfo.addr" style="width: 80%" />
<el-input clearable v-model="hostInfo.addr" style="width: 80%" />
</el-form-item>
<el-form-item :label="$t('terminal.port')" prop="port">
<el-input v-model.number="hostInfo.port" style="width: 80%" />
<el-input clearable v-model.number="hostInfo.port" style="width: 80%" />
</el-form-item>
<el-form-item :label="$t('terminal.user')" prop="user">
<el-input v-model="hostInfo.user" style="width: 80%" />
<el-input clearable v-model="hostInfo.user" style="width: 80%" />
</el-form-item>
<el-form-item :label="$t('terminal.authMode')" prop="authMode">
<el-radio-group v-model="hostInfo.authMode">
@ -107,10 +112,10 @@
</el-radio-group>
</el-form-item>
<el-form-item :label="$t('terminal.password')" v-if="hostInfo.authMode === 'password'" prop="password">
<el-input show-password type="password" v-model="hostInfo.password" style="width: 80%" />
<el-input clearable show-password type="password" v-model="hostInfo.password" style="width: 80%" />
</el-form-item>
<el-form-item :label="$t('terminal.key')" v-if="hostInfo.authMode === 'key'" prop="privateKey">
<el-input type="textarea" v-model="hostInfo.privateKey" style="width: 80%" />
<el-input clearable type="textarea" v-model="hostInfo.privateKey" style="width: 80%" />
</el-form-item>
</el-form>
<template #footer>
@ -148,6 +153,12 @@ let tabIndex = 0;
const data = ref();
const hostDrawer = ref(false);
let searcConfig = reactive<Host.ReqSearchWithPage>({
info: '',
page: 1,
pageSize: 8,
});
const paginationConfig = reactive({
currentPage: 1,
pageSize: 8,
@ -212,7 +223,9 @@ const handleTabsEdit = (targetName: string, action: 'remove' | 'add') => {
};
const loadHost = async () => {
const res = await getHostList({ page: paginationConfig.currentPage, pageSize: paginationConfig.pageSize });
searcConfig.page = paginationConfig.currentPage;
searcConfig.pageSize = paginationConfig.pageSize;
const res = await getHostList(searcConfig);
data.value = res.data.items;
paginationConfig.total = res.data.total;
};