feat: 添加租户工具配置系统
All checks were successful
continuous-integration/drone/push Build is passing

- 新增 platform_tool_configs 表和 ToolConfig Model
- 新增工具配置 CRUD API (/api/tool-configs)
- 租户详情页添加工具配置管理 Tab
- 修复查看 Token 显示问题,添加专用获取接口
This commit is contained in:
2026-01-27 11:30:02 +08:00
parent 8e675c207d
commit 7134947c0c
7 changed files with 901 additions and 55 deletions

View File

@@ -232,9 +232,14 @@ function handleCopyUrl() {
}
async function handleViewToken(row) {
// 这里需要后端返回真实 token暂时用 placeholder
// 实际生产中可能需要单独 API 获取
showToken(row.access_token === '******' ? '需要调用API获取' : row.access_token, row.app_code)
try {
const res = await api.get(`/api/tenant-apps/${row.id}/token`)
currentToken.value = res.data.access_token
currentAppUrl.value = res.data.base_url || ''
tokenDialogVisible.value = true
} catch (e) {
// 错误已在拦截器处理
}
}
onMounted(() => {

View File

@@ -1,15 +1,22 @@
<script setup>
import { ref, onMounted } from 'vue'
import { ref, reactive, onMounted, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '@/api'
import { useAuthStore } from '@/stores/auth'
const authStore = useAuthStore()
const route = useRoute()
const router = useRouter()
const tenantId = route.params.id
const loading = ref(false)
const tenant = ref(null)
const activeTab = ref('basic')
// ========================================
// 基本信息
// ========================================
async function fetchDetail() {
loading.value = true
try {
@@ -32,6 +39,199 @@ function getStatusText(status) {
return map[status] || status
}
// ========================================
// 工具配置
// ========================================
const configLoading = ref(false)
const configList = ref([])
const configTypes = ref([])
const configKeyOptions = ref({})
const selectedTool = ref('__shared__') // 当前选择的工具
// 工具列表(租户级共享 + 各个工具)
const toolTabs = computed(() => {
const tabs = [{ code: '__shared__', name: '租户通用配置' }]
// 从配置列表中提取已有工具
const tools = new Set()
configList.value.forEach(c => {
if (c.tool_code && c.tool_code !== '__shared__') {
tools.add(c.tool_code)
}
})
// 预定义的工具列表
const predefinedTools = [
{ code: 'customer-profile', name: '客户画像' },
{ code: 'high-eq', name: '高情商回复' },
{ code: 'brainstorm', name: '头脑风暴' },
{ code: 'consultation-plan', name: '面诊方案' },
{ code: 'medical-compliance', name: '医疗合规' }
]
predefinedTools.forEach(t => {
tabs.push(t)
tools.delete(t.code)
})
// 添加其他已有配置的工具
tools.forEach(code => {
tabs.push({ code, name: code })
})
return tabs
})
// 当前工具的配置列表
const currentToolConfigs = computed(() => {
if (selectedTool.value === '__shared__') {
return configList.value.filter(c => !c.tool_code)
}
return configList.value.filter(c => c.tool_code === selectedTool.value)
})
// 按类型分组的配置
const groupedConfigs = computed(() => {
const groups = {}
currentToolConfigs.value.forEach(c => {
const type = c.config_type || 'params'
if (!groups[type]) {
groups[type] = []
}
groups[type].push(c)
})
return groups
})
// 获取类型名称
function getTypeName(type) {
const names = {
datasource: '数据源配置',
jssdk: 'JS-SDK 配置',
webhook: 'Webhook 配置',
params: '工具参数'
}
return names[type] || type
}
async function fetchToolConfigs() {
configLoading.value = true
try {
const res = await api.get('/api/tool-configs', {
params: { tenant_id: tenantId, size: 100 }
})
configList.value = res.data.items || []
} catch (e) {
console.error('获取配置失败:', e)
} finally {
configLoading.value = false
}
}
async function fetchConfigSchema() {
try {
const [typesRes, keysRes] = await Promise.all([
api.get('/api/tool-configs/schema/types'),
api.get('/api/tool-configs/schema/keys')
])
configTypes.value = typesRes.data.types || []
configKeyOptions.value = keysRes.data || {}
} catch (e) {
console.error('获取配置元数据失败:', e)
}
}
// 配置编辑对话框
const configDialogVisible = ref(false)
const configDialogTitle = ref('')
const editingConfigId = ref(null)
const configFormRef = ref(null)
const configForm = reactive({
config_type: 'params',
config_key: '',
config_value: '',
is_encrypted: 0,
description: ''
})
const configRules = {
config_type: [{ required: true, message: '请选择配置类型', trigger: 'change' }],
config_key: [{ required: true, message: '请输入配置键名', trigger: 'blur' }]
}
function handleAddConfig() {
editingConfigId.value = null
configDialogTitle.value = '添加配置'
Object.assign(configForm, {
config_type: 'params',
config_key: '',
config_value: '',
is_encrypted: 0,
description: ''
})
configDialogVisible.value = true
}
function handleEditConfig(config) {
editingConfigId.value = config.id
configDialogTitle.value = '编辑配置'
Object.assign(configForm, {
config_type: config.config_type,
config_key: config.config_key,
config_value: config.config_value || '',
is_encrypted: config.is_encrypted,
description: config.description || ''
})
configDialogVisible.value = true
}
async function handleConfigSubmit() {
await configFormRef.value.validate()
try {
if (editingConfigId.value) {
await api.put(`/api/tool-configs/${editingConfigId.value}`, {
config_value: configForm.config_value,
is_encrypted: configForm.is_encrypted,
description: configForm.description
})
ElMessage.success('更新成功')
} else {
await api.post('/api/tool-configs', {
tenant_id: tenantId,
tool_code: selectedTool.value === '__shared__' ? null : selectedTool.value,
config_type: configForm.config_type,
config_key: configForm.config_key,
config_value: configForm.config_value,
is_encrypted: configForm.is_encrypted,
description: configForm.description
})
ElMessage.success('添加成功')
}
configDialogVisible.value = false
fetchToolConfigs()
} catch (e) {
// 错误已在拦截器处理
}
}
async function handleDeleteConfig(config) {
await ElMessageBox.confirm(`确定删除配置「${config.config_key}」吗?`, '提示', {
type: 'warning'
})
try {
await api.delete(`/api/tool-configs/${config.id}`)
ElMessage.success('删除成功')
fetchToolConfigs()
} catch (e) {
// 错误已在拦截器处理
}
}
// 切换 Tab 时加载配置
watch(activeTab, (newVal) => {
if (newVal === 'config' && configList.value.length === 0) {
fetchToolConfigs()
fetchConfigSchema()
}
})
onMounted(() => {
fetchDetail()
})
@@ -44,62 +244,266 @@ onMounted(() => {
<el-button link @click="router.back()">
<el-icon><ArrowLeft /></el-icon>
</el-button>
租户详情
租户详情 <span v-if="tenant" style="color: #909399; margin-left: 8px">{{ tenant.name }}</span>
</div>
</div>
<template v-if="tenant">
<!-- 基本信息 -->
<el-descriptions title="基本信息" :column="2" border style="margin-bottom: 20px">
<el-descriptions-item label="租户ID">{{ tenant.id }}</el-descriptions-item>
<el-descriptions-item label="租户代码">{{ tenant.code }}</el-descriptions-item>
<el-descriptions-item label="租户名称">{{ tenant.name }}</el-descriptions-item>
<el-descriptions-item label="状态">
<el-tag :type="getStatusType(tenant.status)" size="small">
{{ getStatusText(tenant.status) }}
</el-tag>
</el-descriptions-item>
<el-descriptions-item label="过期时间">{{ tenant.expired_at || '-' }}</el-descriptions-item>
<el-descriptions-item label="创建时间">{{ tenant.created_at }}</el-descriptions-item>
<el-descriptions-item label="联系人">{{ tenant.contact_info?.contact || '-' }}</el-descriptions-item>
<el-descriptions-item label="联系电话">{{ tenant.contact_info?.phone || '-' }}</el-descriptions-item>
</el-descriptions>
<!-- 用量统计 -->
<el-descriptions title="用量统计" :column="3" border style="margin-bottom: 20px">
<el-descriptions-item label="AI 调用总次数">
{{ tenant.usage_summary?.total_calls?.toLocaleString() || 0 }}
</el-descriptions-item>
<el-descriptions-item label="Token 消耗">
{{ tenant.usage_summary?.total_tokens?.toLocaleString() || 0 }}
</el-descriptions-item>
<el-descriptions-item label="累计费用">
¥{{ tenant.usage_summary?.total_cost?.toFixed(2) || '0.00' }}
</el-descriptions-item>
</el-descriptions>
<!-- 订阅信息 -->
<div style="margin-bottom: 20px">
<h4 style="margin-bottom: 12px">应用订阅</h4>
<el-table :data="tenant.subscriptions" style="width: 100%">
<el-table-column prop="app_code" label="应用" width="150" />
<el-table-column prop="start_date" label="开始日期" width="120" />
<el-table-column prop="end_date" label="结束日期" width="120" />
<el-table-column prop="quota" label="配额">
<template #default="{ row }">
{{ row.quota ? JSON.stringify(row.quota) : '-' }}
</template>
</el-table-column>
<el-table-column prop="status" label="状态" width="100">
<template #default="{ row }">
<el-tag :type="row.status === 'active' ? 'success' : 'danger'" size="small">
{{ row.status === 'active' ? '有效' : '已过期' }}
<el-tabs v-model="activeTab">
<!-- 基本信息 Tab -->
<el-tab-pane label="基本信息" name="basic">
<el-descriptions :column="2" border style="margin-bottom: 20px">
<el-descriptions-item label="租户ID">{{ tenant.id }}</el-descriptions-item>
<el-descriptions-item label="租户代码">{{ tenant.code }}</el-descriptions-item>
<el-descriptions-item label="租户名称">{{ tenant.name }}</el-descriptions-item>
<el-descriptions-item label="状态">
<el-tag :type="getStatusType(tenant.status)" size="small">
{{ getStatusText(tenant.status) }}
</el-tag>
</template>
</el-table-column>
</el-table>
<el-empty v-if="!tenant.subscriptions?.length" description="暂无订阅" />
</div>
</el-descriptions-item>
<el-descriptions-item label="过期时间">{{ tenant.expired_at || '-' }}</el-descriptions-item>
<el-descriptions-item label="创建时间">{{ tenant.created_at }}</el-descriptions-item>
<el-descriptions-item label="联系人">{{ tenant.contact_info?.contact || '-' }}</el-descriptions-item>
<el-descriptions-item label="联系电话">{{ tenant.contact_info?.phone || '-' }}</el-descriptions-item>
</el-descriptions>
<!-- 用量统计 -->
<el-descriptions title="用量统计" :column="3" border style="margin-bottom: 20px">
<el-descriptions-item label="AI 调用总次数">
{{ tenant.usage_summary?.total_calls?.toLocaleString() || 0 }}
</el-descriptions-item>
<el-descriptions-item label="Token 消耗">
{{ tenant.usage_summary?.total_tokens?.toLocaleString() || 0 }}
</el-descriptions-item>
<el-descriptions-item label="累计费用">
¥{{ tenant.usage_summary?.total_cost?.toFixed(2) || '0.00' }}
</el-descriptions-item>
</el-descriptions>
<!-- 订阅信息 -->
<div style="margin-bottom: 20px">
<h4 style="margin-bottom: 12px">应用订阅</h4>
<el-table :data="tenant.subscriptions" style="width: 100%">
<el-table-column prop="app_code" label="应用" width="150" />
<el-table-column prop="start_date" label="开始日期" width="120" />
<el-table-column prop="end_date" label="结束日期" width="120" />
<el-table-column prop="quota" label="配额">
<template #default="{ row }">
{{ row.quota ? JSON.stringify(row.quota) : '-' }}
</template>
</el-table-column>
<el-table-column prop="status" label="状态" width="100">
<template #default="{ row }">
<el-tag :type="row.status === 'active' ? 'success' : 'danger'" size="small">
{{ row.status === 'active' ? '有效' : '已过期' }}
</el-tag>
</template>
</el-table-column>
</el-table>
<el-empty v-if="!tenant.subscriptions?.length" description="暂无订阅" />
</div>
</el-tab-pane>
<!-- 工具配置 Tab -->
<el-tab-pane label="工具配置" name="config">
<div class="config-container" v-loading="configLoading">
<!-- 工具选择 Tabs -->
<div class="tool-tabs">
<el-radio-group v-model="selectedTool" size="small">
<el-radio-button
v-for="tool in toolTabs"
:key="tool.code"
:value="tool.code"
>
{{ tool.name }}
</el-radio-button>
</el-radio-group>
<el-button
v-if="authStore.isOperator"
type="primary"
size="small"
@click="handleAddConfig"
style="margin-left: 16px"
>
<el-icon><Plus /></el-icon>
添加配置
</el-button>
</div>
<!-- 配置列表按类型分组 -->
<div class="config-groups">
<template v-if="Object.keys(groupedConfigs).length > 0">
<div
v-for="(configs, type) in groupedConfigs"
:key="type"
class="config-group"
>
<div class="group-header">
<span class="group-title">{{ getTypeName(type) }}</span>
</div>
<el-table :data="configs" size="small">
<el-table-column prop="config_key" label="配置键" width="200" />
<el-table-column prop="config_value" label="配置值">
<template #default="{ row }">
<span v-if="row.is_encrypted" class="encrypted-value">******</span>
<span v-else>{{ row.config_value || '-' }}</span>
</template>
</el-table-column>
<el-table-column prop="description" label="说明" width="200" />
<el-table-column label="操作" width="150" fixed="right">
<template #default="{ row }">
<el-button
v-if="authStore.isOperator"
type="primary"
link
size="small"
@click="handleEditConfig(row)"
>编辑</el-button>
<el-button
v-if="authStore.isOperator"
type="danger"
link
size="small"
@click="handleDeleteConfig(row)"
>删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<el-empty
v-else
:description="`暂无${selectedTool === '__shared__' ? '租户通用' : '该工具'}配置`"
>
<el-button v-if="authStore.isOperator" type="primary" @click="handleAddConfig">
添加配置
</el-button>
</el-empty>
</div>
</div>
</el-tab-pane>
</el-tabs>
</template>
<!-- 配置编辑对话框 -->
<el-dialog v-model="configDialogVisible" :title="configDialogTitle" width="550px">
<el-form ref="configFormRef" :model="configForm" :rules="configRules" label-width="100px">
<el-form-item label="配置类型" prop="config_type">
<el-select
v-model="configForm.config_type"
:disabled="!!editingConfigId"
style="width: 100%"
>
<el-option
v-for="t in configTypes"
:key="t.code"
:label="t.name"
:value="t.code"
>
<span>{{ t.name }}</span>
<span style="color: #909399; font-size: 12px; margin-left: 8px">{{ t.description }}</span>
</el-option>
</el-select>
</el-form-item>
<el-form-item label="配置键" prop="config_key">
<el-autocomplete
v-model="configForm.config_key"
:fetch-suggestions="(query, cb) => {
const keys = configKeyOptions[configForm.config_type] || []
const results = query
? keys.filter(k => k.key.includes(query) || k.name.includes(query))
: keys
cb(results.map(k => ({ value: k.key, name: k.name })))
}"
:disabled="!!editingConfigId"
placeholder="输入或选择配置键"
style="width: 100%"
>
<template #default="{ item }">
<span>{{ item.value }}</span>
<span style="color: #909399; font-size: 12px; margin-left: 8px">{{ item.name }}</span>
</template>
</el-autocomplete>
</el-form-item>
<el-form-item label="配置值">
<el-input
v-model="configForm.config_value"
:type="configForm.is_encrypted ? 'password' : 'text'"
:show-password="configForm.is_encrypted"
placeholder="请输入配置值"
/>
</el-form-item>
<el-form-item label="加密存储">
<el-switch
v-model="configForm.is_encrypted"
:active-value="1"
:inactive-value="0"
/>
<span style="color: #909399; font-size: 12px; margin-left: 8px">
敏感信息(如密码、密钥)建议加密存储
</span>
</el-form-item>
<el-form-item label="说明">
<el-input
v-model="configForm.description"
type="textarea"
:rows="2"
placeholder="可选描述此配置的用途"
/>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="configDialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleConfigSubmit">确定</el-button>
</template>
</el-dialog>
</div>
</template>
<style scoped>
.config-container {
padding: 0 4px;
}
.tool-tabs {
display: flex;
align-items: center;
margin-bottom: 20px;
flex-wrap: wrap;
gap: 8px;
}
.config-groups {
min-height: 200px;
}
.config-group {
margin-bottom: 24px;
}
.group-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
padding-bottom: 8px;
border-bottom: 1px solid #ebeef5;
}
.group-title {
font-weight: 600;
color: #303133;
font-size: 14px;
}
.encrypted-value {
color: #909399;
font-family: monospace;
}
</style>