maxkb/ui/src/views/application/index.vue
2025-06-23 17:45:53 +08:00

535 lines
20 KiB
Vue

<template>
<LayoutContainer class="application-manage">
<template #left>
<h4 class="p-16 pb-0">{{ $t('views.application.title') }}</h4>
<folder-tree
:source="FolderSource.TOOL"
:data="folderList"
:currentNodeKey="currentFolder?.id"
@handleNodeClick="folderClickHandel"
@refreshTree="refreshFolder"
class="p-8"
/>
</template>
<ContentContainer :header="currentFolder?.name">
<template #search>
<div class="flex">
<div class="flex-between complex-search">
<el-select
class="complex-search__left"
v-model="search_type"
style="width: 120px"
@change="search_type_change"
>
<el-option :label="$t('common.creator')" value="create_user" />
<el-option :label="$t('common.name')" value="name" />
</el-select>
<el-input
v-if="search_type === 'name'"
v-model="search_form.name"
@change="getList"
:placeholder="$t('common.searchBar.placeholder')"
style="width: 220px"
clearable
/>
<el-select
v-else-if="search_type === 'create_user'"
v-model="search_form.create_user"
@change="getList"
clearable
style="width: 220px"
>
<el-option v-for="u in user_options" :key="u.id" :value="u.id" :label="u.username" />
</el-select>
</div>
<el-dropdown trigger="click">
<el-button
type="primary"
class="ml-8"
v-hasPermission="[
RoleConst.ADMIN,
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
PermissionConst.APPLICATION_EDIT.getWorkspacePermissionWorkspaceManageRole,
PermissionConst.APPLICATION_EDIT.getWorkspacePermission,
]"
>
{{ $t('common.create') }}
<el-icon class="el-icon--right">
<arrow-down />
</el-icon>
</el-button>
<template #dropdown>
<el-dropdown-menu class="create-dropdown">
<el-dropdown-item @click="openCreateDialog('SIMPLE')">
<div class="flex">
<el-avatar shape="square" class="avatar-blue mt-4" :size="36">
<img
src="@/assets/application/icon_simple_application.svg"
style="width: 65%"
alt=""
/>
</el-avatar>
<div class="pre-wrap ml-8">
<div class="lighter">{{ $t('views.application.simple') }}</div>
<el-text type="info" size="small"
>{{ $t('views.application.simplePlaceholder') }}
</el-text>
</div>
</div>
</el-dropdown-item>
<el-dropdown-item @click="openCreateDialog('WORK_FLOW')">
<div class="flex">
<el-avatar shape="square" class="avatar-purple mt-4" :size="36">
<img
src="@/assets/application/icon_workflow_application.svg"
style="width: 65%"
alt=""
/>
</el-avatar>
<div class="pre-wrap ml-8">
<div class="lighter">{{ $t('views.application.workflow') }}</div>
<el-text type="info" size="small"
>{{ $t('views.application.workflowPlaceholder') }}
</el-text>
</div>
</div>
</el-dropdown-item>
<el-dropdown-item>
<el-upload
class="import-button"
ref="elUploadRef"
:file-list="[]"
action="#"
multiple
:auto-upload="false"
:show-file-list="false"
:limit="1"
:on-change="(file: any, fileList: any) => importApplication(file)"
>
<div class="flex align-center">
<el-avatar shape="square" class="mt-4" :size="36" style="background: none">
<img src="@/assets/icon_import.svg" alt="" />
</el-avatar>
<div class="pre-wrap ml-8">
<div class="lighter">{{ $t('common.importCreate') }}</div>
</div>
</div>
</el-upload>
</el-dropdown-item>
<el-dropdown-item @click="openCreateFolder" divided>
<div class="flex align-center">
<AppIcon iconName="app-folder" style="font-size: 32px"></AppIcon>
<div class="pre-wrap ml-4">
<div class="lighter">
{{ $t('components.folder.addFolder') }}
</div>
</div>
</div>
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</template>
<div
v-loading.fullscreen.lock="paginationConfig.current_page === 1 && loading"
style="max-height: calc(100vh - 140px)"
>
<InfiniteScroll
:size="applicationList.length"
:total="paginationConfig.total"
:page_size="paginationConfig.page_size"
v-model:current_page="paginationConfig.current_page"
@load="getList"
:loading="loading"
>
<el-row v-if="applicationList.length > 0" :gutter="15">
<template v-for="(item, index) in applicationList" :key="index">
<el-col
v-if="item.resource_type === 'folder'"
:xs="24"
:sm="12"
:md="12"
:lg="8"
:xl="6"
class="mb-16"
>
<CardBox
:title="item.name"
:description="item.desc || $t('common.noData')"
class="cursor"
@click="clickFolder(item)"
>
<template #icon>
<el-avatar shape="square" :size="32" style="background: none">
<AppIcon iconName="app-folder" style="font-size: 32px"></AppIcon>
</el-avatar>
</template>
<template #subTitle>
<el-text class="color-secondary lighter" size="small">
{{ $t('common.creator') }}: {{ item.nick_name }}
</el-text>
</template>
</CardBox>
</el-col>
<el-col v-else :xs="24" :sm="12" :md="12" :lg="8" :xl="6" class="mb-16">
<CardBox
:title="item.name"
:description="item.desc"
class="cursor"
@click="router.push({ path: `/application/${item.id}/${item.type}/overview` })"
>
<template #icon>
<LogoIcon height="28px" style="width: 28px; height: 28px; display: block" />
</template>
<template #subTitle>
<el-text class="color-secondary" size="small">
<auto-tooltip :content="item.username">
{{ $t('common.creator') }}: {{ item.nick_name }}
</auto-tooltip>
</el-text>
</template>
<template #tag>
<el-tag type="warning" v-if="isWorkFlow(item.type)" style="height: 22px">
{{ $t('views.application.workflow') }}
</el-tag>
<el-tag class="blue-tag" v-else style="height: 22px">
{{ $t('views.application.simple') }}
</el-tag>
</template>
<template #footer>
<div v-if="item.is_publish" class="flex align-center">
<el-icon class="color-success mr-8" style="font-size: 16px">
<SuccessFilled />
</el-icon>
<span class="color-secondary">
{{ $t('views.application.status.published') }}
</span>
<el-divider direction="vertical" />
<el-icon class="mr-8"><Clock /></el-icon>
<span class="color-secondary">{{ dateFormat(item.update_time) }}</span>
</div>
<div v-else class="flex align-center">
<AppIcon iconName="app-disabled" class="color-secondary mr-8"></AppIcon>
<span class="color-secondary">
{{ $t('views.application.status.unpublished') }}
</span>
</div>
</template>
<template #mouseEnter>
<div @click.stop>
<el-dropdown trigger="click">
<el-button text @click.stop>
<el-icon>
<MoreFilled />
</el-icon>
</el-button>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item @click.stop="getAccessToken(item.id)">
<AppIcon iconName="app-create-chat"></AppIcon>
{{ $t('views.application.operation.toChat') }}
</el-dropdown-item>
<el-dropdown-item
@click.stop="settingApplication(item)"
v-if="
hasPermission(
[
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
RoleConst.USER.getWorkspaceRole,
PermissionConst.APPLICATION_EDIT.getWorkspacePermission,
],
'OR',
)
"
>
<el-icon><Setting /></el-icon>
{{ $t('common.setting') }}
</el-dropdown-item>
<el-dropdown-item
divided
@click.stop="exportApplication(item)"
v-if="
hasPermission(
[
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
RoleConst.USER.getWorkspaceRole,
PermissionConst.APPLICATION_EXPORT.getWorkspacePermission,
],
'OR',
)
"
>
<AppIcon iconName="app-export"></AppIcon>
{{ $t('common.export') }}
</el-dropdown-item>
<el-dropdown-item
divided
icon="Delete"
@click.stop="deleteApplication(item)"
v-if="
hasPermission(
[
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
RoleConst.USER.getWorkspaceRole,
PermissionConst.APPLICATION_DELETE.getWorkspacePermission,
],
'OR',
)
"
>{{ $t('common.delete') }}</el-dropdown-item
>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</template>
</CardBox>
</el-col>
</template>
</el-row>
<el-empty :description="$t('common.noData')" v-else />
</InfiniteScroll>
</div>
</ContentContainer>
<CreateApplicationDialog ref="CreateApplicationDialogRef" />
<CopyApplicationDialog ref="CopyApplicationDialogRef" />
<CreateFolderDialog ref="CreateFolderDialogRef" @refresh="refreshFolder" />
</LayoutContainer>
</template>
<script lang="ts" setup>
import { onMounted, ref, reactive, computed } from 'vue'
import CreateApplicationDialog from '@/views/application/component/CreateApplicationDialog.vue'
import CreateFolderDialog from '@/components/folder-tree/CreateFolderDialog.vue'
import CopyApplicationDialog from '@/views/application/component/CopyApplicationDialog.vue'
import ApplicaitonApi from '@/api/application/application'
import { MsgSuccess, MsgConfirm, MsgError } from '@/utils/message'
import useStore from '@/stores'
import { t } from '@/locales'
import { useRouter } from 'vue-router'
import { isWorkFlow } from '@/utils/application'
import { dateFormat } from '@/utils/time'
import { PermissionConst, RoleConst } from '@/utils/permission/data'
import { hasPermission } from '@/utils/permission/index'
import { FolderSource } from '@/enums/common'
const router = useRouter()
const { folder, application, user } = useStore()
const loading = ref(false)
const search_type = ref('name')
const search_form = ref<{
name: string
create_user: string
}>({
name: '',
create_user: '',
})
const user_options = ref<any[]>([])
const paginationConfig = reactive({
current_page: 1,
page_size: 30,
total: 0,
})
const folderList = ref<any[]>([])
const applicationList = ref<any[]>([])
const currentFolder = ref<any>({})
const CopyApplicationDialogRef = ref()
const CreateApplicationDialogRef = ref()
function openCreateDialog(type?: string) {
CreateApplicationDialogRef.value.open(currentFolder.value?.id || 'root', type)
// common
// .asyncGetValid(ValidType.Application, ValidCount.Application, loading)
// .then(async (res: any) => {
// if (res?.data) {
// CreateApplicationDialogRef.value.open()
// } else if (res?.code === 400) {
// MsgConfirm(t('common.tip'), t('views.application.tip.professionalMessage'), {
// cancelButtonText: t('common.confirm'),
// confirmButtonText: t('common.professional'),
// }).then(() => {
// window.open('https://maxkb.cn/pricing.html', '_blank')
// })
// }
// })
}
const search_type_change = () => {
search_form.value = { name: '', create_user: '' }
}
function getList() {
const params = {
folder_id: currentFolder.value?.id || 'root',
}
ApplicaitonApi.getApplication(paginationConfig, params, loading).then((res) => {
paginationConfig.total = res.data.total
applicationList.value = [...applicationList.value, ...res.data.records]
})
}
function clickFolder(item: any) {
currentFolder.value.id = item.id
applicationList.value = []
getList()
}
function getAccessToken(id: string) {
applicationList.value
.filter((app) => app.id === id)[0]
?.work_flow?.nodes?.filter((v: any) => v.id === 'base-node')
.map((v: any) => {
apiInputParams.value = v.properties.api_input_field_list
? v.properties.api_input_field_list.map((v: any) => {
return {
name: v.variable,
value: v.default_value,
}
})
: v.properties.input_field_list
? v.properties.input_field_list
.filter((v: any) => v.assignment_method === 'api_input')
.map((v: any) => {
return {
name: v.variable,
value: v.default_value,
}
})
: []
})
const apiParams = mapToUrlParams(apiInputParams.value)
? '?' + mapToUrlParams(apiInputParams.value)
: ''
application.asyncGetAccessToken(id, loading).then((res: any) => {
window.open(application.location + res?.data?.access_token + apiParams)
})
}
const apiInputParams = ref([])
function copyApplication(row: any) {
application.asyncGetApplicationDetail(row.id, loading).then((res: any) => {
if (res?.data) {
CopyApplicationDialogRef.value.open({ ...res.data, model_id: res.data.model })
}
})
}
const is_show_copy_button = (row: any) => {
return user.userInfo ? user.userInfo.id == row.user_id : false
}
function settingApplication(row: any) {
if (isWorkFlow(row.type)) {
router.push({ path: `/application/${row.id}/workflow` })
} else {
router.push({ path: `/application/${row.id}/${row.type}/setting` })
}
}
function mapToUrlParams(map: any[]) {
const params = new URLSearchParams()
map.forEach((item: any) => {
params.append(encodeURIComponent(item.name), encodeURIComponent(item.value))
})
return params.toString() // 返回 URL 查询字符串
}
function deleteApplication(row: any) {
MsgConfirm(
// @ts-ignore
`${t('views.application.delete.confirmTitle')}${row.name} ?`,
t('views.application.delete.confirmMessage'),
{
confirmButtonText: t('common.confirm'),
cancelButtonText: t('common.cancel'),
confirmButtonClass: 'color-danger',
},
)
.then(() => {
ApplicaitonApi.delApplication(row.id, loading).then(() => {
const index = applicationList.value.findIndex((v) => v.id === row.id)
applicationList.value.splice(index, 1)
MsgSuccess(t('common.deleteSuccess'))
})
})
.catch(() => {})
}
const exportApplication = (application: any) => {
ApplicaitonApi.exportApplication(application.id, application.name, loading).catch((e) => {
if (e.response.status !== 403) {
e.response.data.text().then((res: string) => {
MsgError(`${t('views.application.tip.ExportError')}:${JSON.parse(res).message}`)
})
}
})
}
const elUploadRef = ref()
const importApplication = (file: any) => {
const formData = new FormData()
formData.append('file', file.raw, file.name)
elUploadRef.value.clearFiles()
ApplicaitonApi.importApplication(formData, loading)
.then(async (res: any) => {
if (res?.data) {
applicationList.value = []
getList()
}
})
.catch((e) => {
if (e.code === 400) {
MsgConfirm(t('common.tip'), t('views.application.tip.professionalMessage'), {
cancelButtonText: t('common.confirm'),
confirmButtonText: t('common.professional'),
}).then(() => {
window.open('https://maxkb.cn/pricing.html', '_blank')
})
}
})
}
// 文件夹相关
const CreateFolderDialogRef = ref()
function openCreateFolder() {
CreateFolderDialogRef.value.open(FolderSource.APPLICATION, currentFolder.value.id)
}
function getFolder(bool?: boolean) {
const params = {}
folder.asyncGetFolder(FolderSource.APPLICATION, params, loading).then((res: any) => {
folderList.value = res.data
if (bool) {
// 初始化刷新
currentFolder.value = res.data?.[0] || {}
}
getList()
})
}
function folderClickHandel(row: any) {
currentFolder.value = row
applicationList.value = []
getList()
}
function refreshFolder() {
applicationList.value = []
getFolder()
}
onMounted(() => {
getFolder(true)
})
</script>
<style lang="scss" scoped></style>