- defineModel 对象默认值改为工厂函数,消除 art-form / art-search-bar 连锁泛型推断错误 - art-table 改用 element-plus 导出的 TableInstance 替代 InstanceType<typeof ElTable> - STATUS_TAG 收紧为 el-tag 合法字面量联合类型 - SidebarSubmenu 外部链接菜单 index 不再传 undefined
235 lines
9.9 KiB
Vue
235 lines
9.9 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* 按插件授权面板(GitLab project members / RuoYi 分配用户式)
|
||
* - 左:插件列表(搜索、已授权人数角标、选中高亮)
|
||
* - 右:该插件已授权账号表(行内开关、多选批量撤销)+「添加授权」对话框(账号多选 + 默认权限)
|
||
*/
|
||
import { computed, ref } from 'vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import { grantPlugin, revokePluginGrants } from '@/api/business'
|
||
import GrantAccountsDialog from './grant-accounts-dialog.vue'
|
||
|
||
interface Props {
|
||
matrix: any
|
||
}
|
||
const props = defineProps<Props>()
|
||
const emit = defineEmits<{ (e: 'changed'): void }>()
|
||
|
||
// ---------- 左侧插件列表 ----------
|
||
const pluginKw = ref('')
|
||
const selectedPlugin = ref('')
|
||
|
||
const pluginList = computed(() => {
|
||
const kw = pluginKw.value.trim().toLowerCase()
|
||
return (props.matrix.plugins || []).filter((p: any) => {
|
||
return !kw || String(p.id).toLowerCase().includes(kw) || String(p.name || '').toLowerCase().includes(kw)
|
||
})
|
||
})
|
||
|
||
function grantsOf(pluginId: string): any[] {
|
||
return (props.matrix.grants || []).filter((g: any) => g.plugin_id === pluginId)
|
||
}
|
||
|
||
const selectedPluginInfo = computed(() =>
|
||
(props.matrix.plugins || []).find((p: any) => p.id === selectedPlugin.value)
|
||
)
|
||
|
||
const grantedRows = computed(() => {
|
||
const accMap = new Map<string, any>()
|
||
for (const a of props.matrix.accounts || []) accMap.set(a.account, a)
|
||
return grantsOf(selectedPlugin.value).map((g: any) => ({
|
||
...g,
|
||
nickname: accMap.get(g.account)?.nickname || '',
|
||
dept: accMap.get(g.account)?.dept || '',
|
||
status: accMap.get(g.account)?.status
|
||
}))
|
||
})
|
||
|
||
const STATUS_TAG: Record<string, 'success' | 'primary' | 'warning' | 'danger' | 'info'> = {
|
||
published: 'success', testing: 'primary', disabled: 'warning', revoked: 'danger'
|
||
}
|
||
const STATUS_LABEL: Record<string, string> = {
|
||
published: '已发布', testing: '测试中', disabled: '已停用', revoked: '已吊销'
|
||
}
|
||
const statusTag = (s: string) => STATUS_TAG[s] || 'info'
|
||
const statusLabel = (s: string) => STATUS_LABEL[s] || s
|
||
|
||
// ---------- 表格多选批量撤销 ----------
|
||
const tableRef = ref()
|
||
const selectedAccounts = computed(() => (tableRef.value?.getSelectionRows() || []).map((r: any) => r.account))
|
||
|
||
async function togglePerm(row: any, field: 'can_download' | 'can_run', val: boolean) {
|
||
const next = { can_download: row.can_download, can_run: row.can_run, [field]: val }
|
||
const granted = next.can_download || next.can_run
|
||
try {
|
||
await grantPlugin({ account: row.account, plugin_id: selectedPlugin.value, granted, ...next })
|
||
row.can_download = next.can_download
|
||
row.can_run = next.can_run
|
||
emit('changed')
|
||
} catch (e) {
|
||
ElMessage.error('保存失败,已还原')
|
||
emit('changed')
|
||
}
|
||
}
|
||
|
||
async function revokeOne(row: any) {
|
||
await ElMessageBox.confirm(`撤销账号「${row.account}」对插件「${selectedPlugin.value}」的授权?`, '撤销确认', {
|
||
type: 'warning', confirmButtonText: '撤销', cancelButtonText: '取消'
|
||
})
|
||
await grantPlugin({ account: row.account, plugin_id: selectedPlugin.value, granted: false })
|
||
ElMessage.success('已撤销')
|
||
emit('changed')
|
||
}
|
||
|
||
async function batchRevoke() {
|
||
if (!selectedAccounts.value.length) {
|
||
ElMessage.info('请先勾选要撤销的账号')
|
||
return
|
||
}
|
||
await ElMessageBox.confirm(
|
||
`将撤销 ${selectedAccounts.value.length} 个账号对插件「${selectedPlugin.value}」的授权,确认?`,
|
||
'批量撤销',
|
||
{ type: 'warning', confirmButtonText: '撤销', cancelButtonText: '取消' }
|
||
)
|
||
await revokePluginGrants({ plugin_id: selectedPlugin.value, accounts: selectedAccounts.value })
|
||
ElMessage.success('批量撤销完成')
|
||
emit('changed')
|
||
}
|
||
|
||
// ---------- 添加授权对话框 ----------
|
||
const addVisible = ref(false)
|
||
|
||
function openAdd() {
|
||
if (!selectedPlugin.value) return
|
||
addVisible.value = true
|
||
}
|
||
|
||
function handleGranted() {
|
||
addVisible.value = false
|
||
emit('changed')
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="grid grid-cols-[300px_1fr] gap-4 min-h-[560px] max-md:grid-cols-1">
|
||
<!-- 左:插件列表 -->
|
||
<el-card shadow="never" class="rounded-xl !p-0 overflow-hidden">
|
||
<div class="flex items-center justify-between px-3.5 py-3 border-b border-g-300/50">
|
||
<div class="flex items-center gap-2">
|
||
<div class="size-7 rounded-lg grid place-items-center bg-[#13deb91a] text-[#13deb9]">
|
||
<ArtSvgIcon icon="ri:box-3-line" class="text-sm" />
|
||
</div>
|
||
<span class="text-sm font-semibold">插件</span>
|
||
<el-tag size="small" round effect="plain">{{ pluginList.length }}</el-tag>
|
||
</div>
|
||
</div>
|
||
<div class="px-3 py-2.5 border-b border-g-300/50">
|
||
<el-input v-model="pluginKw" placeholder="搜索插件" clearable size="small" />
|
||
</div>
|
||
<el-scrollbar height="460px">
|
||
<div class="p-2">
|
||
<div
|
||
v-for="p in pluginList"
|
||
:key="p.id"
|
||
class="flex items-center gap-2.5 px-2.5 py-2 mb-1 rounded-lg cursor-pointer transition-colors border-l-2"
|
||
:class="p.id === selectedPlugin
|
||
? 'bg-[var(--el-color-primary-light-9)] border-[var(--el-color-primary)]'
|
||
: 'border-transparent hover:bg-[var(--el-fill-color-light)]'"
|
||
@click="selectedPlugin = p.id"
|
||
>
|
||
<div
|
||
class="size-8 rounded-lg grid place-items-center shrink-0"
|
||
:class="p.id === selectedPlugin ? 'bg-[var(--el-color-primary)] text-white' : 'bg-[var(--el-color-primary-light-9)] text-[var(--el-color-primary)]'"
|
||
>
|
||
<ArtSvgIcon icon="ri:box-3-line" class="text-sm" />
|
||
</div>
|
||
<div class="flex-1 min-w-0">
|
||
<div class="text-sm font-medium leading-tight truncate">{{ p.name }}</div>
|
||
<div class="text-[11px] text-gray-400 truncate">v{{ p.latest_version }} · {{ p.id }}</div>
|
||
</div>
|
||
<el-tag v-if="grantsOf(p.id).length" size="small" type="primary" effect="light" round>
|
||
{{ grantsOf(p.id).length }}
|
||
</el-tag>
|
||
</div>
|
||
<el-empty v-if="!pluginList.length" description="无插件" :image-size="60" />
|
||
</div>
|
||
</el-scrollbar>
|
||
</el-card>
|
||
|
||
<!-- 右:插件授权账号表 -->
|
||
<el-card shadow="never" class="rounded-xl">
|
||
<template #header>
|
||
<div class="flex items-center justify-between flex-wrap gap-2">
|
||
<div class="flex items-center gap-2.5">
|
||
<div class="size-9 rounded-lg grid place-items-center bg-[#13deb91a] text-[#13deb9]">
|
||
<ArtSvgIcon icon="ri:box-3-line" class="text-base" />
|
||
</div>
|
||
<div>
|
||
<div class="text-sm font-semibold leading-tight flex items-center gap-2">
|
||
{{ selectedPluginInfo?.name || selectedPlugin }}
|
||
<el-tag v-if="selectedPluginInfo" size="small" :type="statusTag(selectedPluginInfo.status)">
|
||
{{ statusLabel(selectedPluginInfo.status) }}
|
||
</el-tag>
|
||
</div>
|
||
<div class="text-xs text-gray-400">
|
||
{{ selectedPlugin }} · v{{ selectedPluginInfo?.latest_version || '-' }} · 已授权 {{ grantedRows.length }} 个账号
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="flex gap-2">
|
||
<el-button size="small" type="primary" @click="openAdd">
|
||
<ArtSvgIcon icon="ri:user-add-line" class="mr-1" />添加授权
|
||
</el-button>
|
||
<el-button size="small" type="danger" plain :disabled="!selectedAccounts.length" @click="batchRevoke">
|
||
<ArtSvgIcon icon="ri:delete-bin-line" class="mr-1" />批量撤销{{ selectedAccounts.length ? `(${selectedAccounts.length})` : '' }}
|
||
</el-button>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<el-table ref="tableRef" :data="grantedRows" border stripe size="small" max-height="480">
|
||
<el-table-column type="selection" width="42" />
|
||
<el-table-column label="账号" min-width="150">
|
||
<template #default="{ row }">
|
||
<div class="flex items-center gap-2.5">
|
||
<div class="size-7 rounded-lg grid place-items-center bg-[var(--el-color-primary-light-9)] text-[var(--el-color-primary)] text-xs font-bold shrink-0">
|
||
{{ (row.nickname || row.account || '?').slice(0, 1).toUpperCase() }}
|
||
</div>
|
||
<div>
|
||
<div class="text-sm leading-tight">{{ row.nickname || row.account }}</div>
|
||
<div class="text-xs text-gray-400">{{ row.account }}</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column prop="dept" label="部门" width="90" />
|
||
<el-table-column label="允许下载" width="95" align="center">
|
||
<template #default="{ row }">
|
||
<el-switch :model-value="row.can_download" @change="(v: any) => togglePerm(row, 'can_download', !!v)" />
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="允许运行" width="95" align="center">
|
||
<template #default="{ row }">
|
||
<el-switch :model-value="row.can_run" @change="(v: any) => togglePerm(row, 'can_run', !!v)" />
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column prop="updated_at" label="更新时间" width="160" />
|
||
<el-table-column label="操作" width="80" fixed="right">
|
||
<template #default="{ row }">
|
||
<el-button link type="danger" size="small" @click="revokeOne(row)">撤销</el-button>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
<el-empty v-if="!grantedRows.length" description="该插件还没有授权账号,点击右上角「添加授权」" :image-size="80" />
|
||
</el-card>
|
||
|
||
<GrantAccountsDialog
|
||
v-model:visible="addVisible"
|
||
:plugin-id="selectedPlugin"
|
||
:accounts="matrix.accounts || []"
|
||
:granted-accounts="grantedRows.map((r: any) => r.account)"
|
||
@success="handleGranted"
|
||
/>
|
||
</div>
|
||
</template>
|