refactor: 组件化

This commit is contained in:
Wenjie Zhang 2025-05-18 17:18:29 +08:00
parent 70fef8df7e
commit 0b24bd36f4
5 changed files with 1192 additions and 1101 deletions

View File

@ -17,29 +17,7 @@
<PlusCircleOutlined />
</a-tooltip>
</div>
<a-dropdown>
<a class="model-select nav-btn" @click.prevent>
<BulbOutlined />
<a-tooltip :title="configStore.config?.model_name" placement="right">
<span class="model-text text"> {{ configStore.config?.model_name }} </span>
</a-tooltip>
<span class="text" style="color: #aaa;">{{ configStore.config?.model_provider }} </span>
</a>
<template #overlay>
<a-menu class="scrollable-menu">
<a-menu-item-group v-for="(item, key) in modelKeys" :key="key" :title="modelNames[item]?.name">
<a-menu-item v-for="(model, idx) in modelNames[item]?.models" :key="`${item}-${idx}`" @click="selectModel(item, model)">
{{ model }}
</a-menu-item>
</a-menu-item-group>
<a-menu-item-group v-if="customModels.length > 0" title="自定义模型">
<a-menu-item v-for="(model, idx) in customModels" :key="`custom-${idx}`" @click="selectModel('custom', model.custom_id)">
custom/{{ model.custom_id }}
</a-menu-item>
</a-menu-item-group>
</a-menu>
</template>
</a-dropdown>
<ModelSelectorComponent @select-model="handleModelSelect" />
</div>
<div class="header__right">
<div class="nav-btn text" @click="opts.showPanel = !opts.showPanel">
@ -189,6 +167,7 @@ import { message } from 'ant-design-vue'
import MessageInputComponent from '@/components/MessageInputComponent.vue'
import MessageComponent from '@/components/MessageComponent.vue'
import RefsSidebar from '@/components/RefsSidebar.vue'
import ModelSelectorComponent from '@/components/ModelSelectorComponent.vue'
import { chatApi } from '@/apis/auth_api'
import { knowledgeBaseApi } from '@/apis/admin_api'
@ -751,22 +730,12 @@ const retryStoppedMessage = (id) => {
}
}
const modelNames = computed(() => configStore.config?.model_names)
const modelStatus = computed(() => configStore.config?.model_provider_status)
const customModels = computed(() => configStore.config?.custom_models || [])
// modelStatus key
const modelKeys = computed(() => {
return Object.keys(modelStatus.value || {}).filter(key => modelStatus.value?.[key])
})
//
const selectModel = (provider, name) => {
//
const handleModelSelect = ({ provider, name }) => {
configStore.setConfigValues({
model_provider: provider,
model_name: name,
})
// message.success(`: ${name} | ${provider}`)
}
//
@ -849,18 +818,7 @@ const findLastIndex = (array, predicate) => {
font-size: 1rem;
}
.model-select {
// color: var(--gray-900);
max-width: 350px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
.model-text {
overflow: hidden;
text-overflow: ellipsis;
}
}
// Model selection is now handled by ModelSelectorComponent
}
.metas {
display: flex;
@ -1165,38 +1123,9 @@ const findLastIndex = (array, predicate) => {
}
}
.scrollable-menu {
max-height: 300px;
overflow-y: auto;
&::-webkit-scrollbar {
width: 6px;
}
&::-webkit-scrollbar-track {
background: transparent;
border-radius: 3px;
}
&::-webkit-scrollbar-thumb {
background: var(--gray-400);
border-radius: 3px;
}
&::-webkit-scrollbar-thumb:hover {
background: var(--gray-500);
}
}
// Scrollable menu styles moved to ModelSelectorComponent
</style>
<style lang="less">
// dropdown
.ant-dropdown-menu {
&.scrollable-menu {
max-height: 300px;
overflow-y: auto;
}
}
</style>
<!-- Global styles for dropdown moved to ModelSelectorComponent -->

View File

@ -0,0 +1,686 @@
<template>
<div>
<div class="model-provider-card">
<div class="card-header">
<h3>自定义模型</h3>
</div>
<div class="card-body">
<div class="custom-model" v-for="(item, key) in configStore.config.custom_models" :key="item.custom_id">
<div class="card-models__header">
<div class="name" :title="item.name">{{ item.name }}</div>
<div class="action">
<a-popconfirm
title="确认删除该模型?"
@confirm="handleDeleteCustomModel(item.custom_id)"
okText="确认删除"
cancelText="取消"
ok-type="danger"
:disabled="configStore.config.model_name == item.name"
>
<a-button type="text" :disabled="configStore.config.model_name == item.name" @click.stop><DeleteOutlined /></a-button>
</a-popconfirm>
<a-button type="text" @click.stop="prepareToEditCustomModel(item)"><EditOutlined /></a-button>
</div>
</div>
<div class="api_base">{{ item.api_base }}</div>
</div>
<div class="card-models custom-model" @click="prepareToAddCustomModel">
<div class="card-models__header">
<div class="name"> + 添加模型</div>
</div>
<div class="api_base">添加兼容 OpenAI 的模型</div>
</div>
</div>
</div>
<div class="model-provider-card" v-for="(item, key) in modelKeys" :key="key">
<div class="card-header" @click="toggleExpand(item)">
<div :class="{'model-icon': true, 'available': modelStatus[item]}">
<img :src="modelIcons[item] || modelIcons.default" alt="模型图标">
</div>
<div class="model-title-container">
<h3>{{ modelNames[item].name }}</h3>
</div>
<a-button
type="text"
class="expand-button"
@click.stop="openProviderConfig(item)"
title="配置模型提供商"
>
<SettingOutlined />
</a-button>
<a-button
type="text"
class="expand-button"
@click.stop="toggleExpand(item)"
>
<span class="icon-wrapper" :class="{'rotated': expandedModels[item]}">
<DownCircleOutlined />
</span>
</a-button>
</div>
<div class="card-body-wrapper" :class="{'expanded': expandedModels[item]}">
<div class="card-body" v-if="modelStatus[item]">
<div class="card-models" v-for="(model, idx) in modelNames[item].models" :key="idx">
<div class="model_name">{{ model }}</div>
</div>
</div>
</div>
</div>
<div class="model-provider-card" v-for="(item, key) in notModelKeys" :key="key">
<div class="card-header">
<div class="model-icon">
<img :src="modelIcons[item] || modelIcons.default" alt="模型图标">
</div>
<div class="model-title-container">
<h3 style="font-weight: 400">{{ modelNames[item].name }}</h3>
<a :href="modelNames[item].url" target="_blank" class="model-url">
<InfoCircleOutlined />
</a>
</div>
<a-button
type="text"
class="config-button"
@click.stop="openProviderConfig(item)"
title="配置模型提供商"
>
<SettingOutlined />
</a-button>
<div class="missing-keys">
需配置<span v-for="(key, idx) in modelNames[item].env" :key="idx">{{ key }}</span>
</div>
</div>
</div>
<!-- 添加和编辑自定义模型的弹窗 -->
<a-modal
class="custom-model-modal"
v-model:open="customModel.visible"
:title="customModel.modelTitle"
@ok="handleAddOrEditCustomModel"
@cancel="handleCancelCustomModel"
:okText="'确认'"
:cancelText="'取消'"
:okButtonProps="{disabled: !customModel.name || !customModel.api_base}"
:ok-type="'primary'"
>
<p>添加的模型是兼容 OpenAI 的模型比如 vllmOllama</p>
<a-form :model="customModel" layout="vertical">
<a-form-item label="模型名称" name="name" :rules="[{ required: true, message: '请输入模型名称' }]">
<p class="form-item-description">调用的模型的名称</p>
<a-input v-model:value="customModel.name" :disabled="customModel.edit_type == 'edit'"/>
</a-form-item>
<a-form-item label="API Base" name="api_base" :rules="[{ required: true, message: '请输入API Base' }]">
<p class="form-item-description">比如 <code>http://localhost:11434/v1</code></p>
<a-input v-model:value="customModel.api_base" />
</a-form-item>
<a-form-item label="API KEY" name="api_key">
<a-input-password v-model:value="customModel.api_key" :visibilityToggle="true" autocomplete="new-password"/>
</a-form-item>
</a-form>
</a-modal>
<!-- 模型提供商配置弹窗 -->
<a-modal
class="provider-config-modal"
v-model:open="providerConfig.visible"
:title="`配置${providerConfig.providerName}模型`"
@ok="saveProviderConfig"
@cancel="cancelProviderConfig"
:okText="'保存配置'"
:cancelText="'取消'"
:ok-type="'primary'"
:width="800"
:bodyStyle="{ padding: '16px 24px' }"
>
<div v-if="providerConfig.loading" class="modal-loading-container">
<a-spin :indicator="h(LoadingOutlined, { style: { fontSize: '32px', color: 'var(--main-color)' }})" />
<div class="loading-text">正在获取模型列表...</div>
</div>
<div v-else class="modal-config-content">
<div class="modal-config-header">
<h3>选择 {{ providerConfig.providerName }} 的模型</h3>
<p class="description">勾选您希望在系统中启用的模型请注意列表中可能包含非对话模型请仔细甄别</p>
</div>
<div class="modal-models-section">
<div class="modal-checkbox-list">
<a-checkbox-group v-model:value="providerConfig.selectedModels">
<div v-for="(model, index) in providerConfig.allModels" :key="index" class="modal-checkbox-item">
<a-checkbox :value="model.id">{{ model.id }}</a-checkbox>
</div>
</a-checkbox-group>
</div>
<div v-if="providerConfig.allModels.length === 0" class="modal-no-models">
<a-alert v-if="!modelStatus[providerConfig.provider]" type="warning" message="请在 src/.env 中配置对应的 APIKEY并重新启动服务" />
<div v-else>
<a-alert type="warning" message="该提供商暂未适配获取模型列表的方法,如果需要添加模型,请在 src/static/models.private.yml 中添加。" />
<img src="@/assets/pics/guides/how-to-add-models.png" alt="添加模型指引" style="width: 100%; height: 100%;">
</div>
</div>
</div>
</div>
</a-modal>
</div>
</template>
<script setup>
import { computed, reactive, watch, h } from 'vue'
import { message } from 'ant-design-vue';
import {
DeleteOutlined,
EditOutlined,
InfoCircleOutlined,
SettingOutlined,
DownCircleOutlined,
LoadingOutlined
} from '@ant-design/icons-vue';
import { useConfigStore } from '@/stores/config';
import { modelIcons } from '@/utils/modelIcon';
import { chatApi } from '@/apis/auth_api';
const configStore = useConfigStore();
//
const modelNames = computed(() => configStore.config?.model_names);
const modelStatus = computed(() => configStore.config?.model_provider_status);
//
const customModel = reactive({
modelTitle: '添加自定义模型',
visible: false,
custom_id: '',
name: '',
api_key: '',
api_base: '',
edit_type: 'add',
});
//
const providerConfig = reactive({
visible: false,
provider: '',
providerName: '',
models: [],
allModels: [], //
selectedModels: [], //
loading: false,
});
// modelStatus key
const modelKeys = computed(() => {
return Object.keys(modelStatus.value || {}).filter(key => modelStatus.value[key]);
});
// modelStatus key
const notModelKeys = computed(() => {
return Object.keys(modelStatus.value || {}).filter(key => !modelStatus.value[key]);
});
//
const expandedModels = reactive({});
// modelKeys
watch(modelKeys, (newKeys) => {
newKeys.forEach(key => {
if (expandedModels[key] === undefined) {
expandedModels[key] = true;
}
});
}, { immediate: true });
//
const generateRandomHash = (length) => {
let chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let hash = '';
for (let i = 0; i < length; i++) {
hash += chars.charAt(Math.floor(Math.random() * chars.length));
}
return hash;
};
//
const handleDeleteCustomModel = (customId) => {
const updatedModels = configStore.config.custom_models.filter(item => item.custom_id !== customId);
configStore.setConfigValue('custom_models', updatedModels);
};
//
const prepareToEditCustomModel = (item) => {
customModel.modelTitle = '编辑自定义模型';
customModel.custom_id = item.custom_id;
customModel.visible = true;
customModel.edit_type = 'edit';
customModel.name = item.name;
customModel.api_key = item.api_key;
customModel.api_base = item.api_base;
};
//
const prepareToAddCustomModel = () => {
customModel.modelTitle = '添加自定义模型';
customModel.edit_type = 'add';
customModel.visible = true;
clearCustomModel();
};
//
const clearCustomModel = () => {
customModel.custom_id = '';
customModel.name = '';
customModel.api_key = '';
customModel.api_base = '';
};
// /
const handleCancelCustomModel = () => {
clearCustomModel();
customModel.visible = false;
};
//
const handleAddOrEditCustomModel = async () => {
if (!customModel.name || !customModel.api_base) {
message.error('请填写完整的模型名称和API Base信息。');
return;
}
let custom_models = configStore.config.custom_models || [];
const model_info = {
custom_id: customModel.custom_id || `${customModel.name}-${generateRandomHash(4)}`,
name: customModel.name,
api_key: customModel.api_key,
api_base: customModel.api_base,
};
if (customModel.edit_type === 'add') {
if (custom_models.find(item => item.custom_id === customModel.custom_id)) {
message.error('模型ID已存在');
return;
}
custom_models.push(model_info);
} else {
// custom_id
custom_models = custom_models.map(item => item.custom_id === customModel.custom_id ? model_info : item);
}
customModel.visible = false;
await configStore.setConfigValue('custom_models', custom_models);
message.success(customModel.edit_type === 'add' ? '模型添加成功' : '模型修改成功');
};
//
const toggleExpand = (item) => {
expandedModels[item] = !expandedModels[item];
};
//
const openProviderConfig = (provider) => {
providerConfig.provider = provider;
providerConfig.providerName = modelNames.value[provider].name;
providerConfig.allModels = [];
providerConfig.visible = true;
providerConfig.loading = true;
//
const currentModels = modelNames.value[provider]?.models || [];
providerConfig.selectedModels = [...currentModels];
//
fetchProviderModels(provider);
};
//
const fetchProviderModels = (provider) => {
providerConfig.loading = true;
chatApi.getProviderModels(provider)
.then(data => {
console.log(`${provider} 模型列表:`, data);
// API
let modelsList = [];
// 1: { data: [...] }
if (data.data && Array.isArray(data.data)) {
modelsList = data.data;
}
// 2: { models: [...] } ()
else if (data.models && Array.isArray(data.models)) {
modelsList = data.models.map(model => typeof model === 'string' ? { id: model } : model);
}
// 3: { models: { data: [...] } }
else if (data.models && data.models.data && Array.isArray(data.models.data)) {
modelsList = data.models.data;
}
console.log("处理后的模型列表:", modelsList);
providerConfig.allModels = modelsList;
providerConfig.loading = false;
})
.catch(error => {
console.error(`获取${provider}模型列表失败:`, error);
message.error({ content: `获取${modelNames.value[provider].name}模型列表失败`, duration: 2 });
providerConfig.loading = false;
});
};
//
const saveProviderConfig = async () => {
if (!modelStatus.value[providerConfig.provider]) {
message.error('请在 src/.env 中配置对应的 APIKEY并重新启动服务');
return;
}
message.loading({ content: '保存配置中...', key: 'save-config', duration: 0 });
try {
//
const data = await chatApi.updateProviderModels(providerConfig.provider, providerConfig.selectedModels);
console.log('更新后的模型列表:', data.models);
message.success({ content: '模型配置已保存!', key: 'save-config', duration: 2 });
//
providerConfig.visible = false;
//
configStore.refreshConfig();
} catch (error) {
console.error('保存配置失败:', error);
message.error({ content: '保存配置失败: ' + error.message, key: 'save-config', duration: 2 });
}
};
//
const cancelProviderConfig = () => {
providerConfig.visible = false;
};
</script>
<style lang="less" scoped>
.model-provider-card {
border: 1px solid var(--gray-300);
background-color: white;
border-radius: 8px;
margin-bottom: 12px;
padding: 12px;
.card-header {
display: flex;
align-items: center;
gap: 10px;
cursor: pointer;
.model-title-container {
display: flex;
flex-direction: row;
align-items: center;
gap: 10px;
flex: 1;
}
.model-url {
font-size: 12px;
width: fit-content;
color: var(--gray-500);
}
.model-icon {
width: 28px;
height: 28px;
//
filter: grayscale(100%);
img {
width: 100%;
height: 100%;
border-radius: 4px;
border: 1px solid var(--gray-300);
}
&.available {
filter: grayscale(0%);
}
}
h3 {
margin: 0;
font-size: 0.9rem;
font-weight: bold;
}
a {
text-decoration: none;
color: var(--gray-500);
font-size: 12px;
transition: all 0.1s;
&:hover {
color: var(--gray-900);
}
}
.details, .missing-keys {
margin-left: auto;
}
.success {
width: 0.75rem;
height: 0.75rem;
background-color: rgb(91, 186, 91);
border-radius: 50%;
box-shadow: 0 0 10px 1px rgba(0,128,0, 0.1);
border: 2px solid white;
}
.missing-keys {
margin-top: 4px;
color: var(--gray-600);
font-size: 12px;
& > span {
margin-left: 6px;
user-select: all;
background-color: var(--gray-100);
padding: 2px 6px;
border-radius: 4px;
color: var(--gray-800);
}
}
.expand-button {
margin-left: auto;
height: 32px;
width: 32px;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
cursor: pointer;
color: var(--gray-500);
&:hover {
background-color: var(--gray-100);
}
.icon-wrapper {
display: inline-flex;
transition: transform 0.2s ease;
&.rotated {
transform: rotate(180deg);
}
}
}
}
.card-body-wrapper {
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
&.expanded {
max-height: 700px;
}
}
.card-body {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 12px;
margin-top: 10px;
.card-models {
width: 100%;
border-radius: 8px;
border: 1px solid var(--gray-300);
padding: 8px 10px;
display: flex;
gap: 6px;
justify-content: space-between;
align-items: center;
box-sizing: border-box;
background-color: var(--gray-50);
transition: box-shadow 0.1s;
&:hover {
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.model_name {
font-size: 14px;
color: var(--gray-900);
}
.select-btn {
width: 16px;
height: 16px;
flex: 0 0 16px;
border-radius: 50%;
}
&.custom-model {
display: flex;
flex-direction: column;
align-items: flex-start;
padding-right: 8px;
gap: 10px;
.card-models__header {
width: 100%;
height: 24px;
display: flex;
justify-content: flex-start;
align-items: center;
.name {
color: var(--gray-1000);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
flex: 1;
margin-right: 8px;
position: relative;
&:hover::after {
content: attr(title);
position: absolute;
left: 0;
top: 100%;
background: rgba(0, 0, 0, 0.8);
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
white-space: nowrap;
z-index: 1000;
margin-top: 5px;
}
}
.action {
opacity: 0;
user-select: none;
margin-left: auto;
flex-shrink: 0;
button {
padding: 4px 8px;
}
}
}
.api_base {
font-size: 12px;
color: var(--gray-600);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 100%;
}
&:hover {
.card-models__header {
.action {
opacity: 1;
}
}
}
}
}
}
}
.custom-model-modal {
.ant-form-item {
margin-bottom: 10px;
.form-item-description {
font-size: 12px;
color: var(--gray-600);
margin-bottom: 10px;
}
}
}
.provider-config-modal {
.ant-modal-body {
padding: 16px 0 !important;
.modal-loading-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 200px;
.loading-text {
margin-top: 20px;
color: var(--gray-700);
font-size: 14px;
}
}
.modal-config-content {
max-height: 70vh;
overflow-y: auto;
.modal-config-header {
margin-bottom: 20px;
.description {
font-size: 14px;
color: var(--gray-600);
margin: 0;
}
}
.modal-models-section {
.modal-checkbox-list {
max-height: 50vh;
overflow-y: auto;
.modal-checkbox-item {
margin-bottom: 4px;
padding: 4px 6px;
border-radius: 6px;
background-color: white;
border: 1px solid var(--gray-200);
&:hover {
background-color: var(--gray-50);
}
}
}
}
}
}
}
</style>

View File

@ -0,0 +1,117 @@
<template>
<a-dropdown>
<a class="model-select nav-btn" @click.prevent>
<BulbOutlined />
<a-tooltip :title="configStore.config?.model_name" placement="right">
<span class="model-text text"> {{ configStore.config?.model_name }} </span>
</a-tooltip>
<span class="text" style="color: #aaa;">{{ configStore.config?.model_provider }} </span>
</a>
<template #overlay>
<a-menu class="scrollable-menu">
<a-menu-item-group v-for="(item, key) in modelKeys" :key="key" :title="modelNames[item]?.name">
<a-menu-item v-for="(model, idx) in modelNames[item]?.models" :key="`${item}-${idx}`" @click="handleSelectModel(item, model)">
{{ model }}
</a-menu-item>
</a-menu-item-group>
<a-menu-item-group v-if="customModels.length > 0" title="自定义模型">
<a-menu-item v-for="(model, idx) in customModels" :key="`custom-${idx}`" @click="handleSelectModel('custom', model.custom_id)">
custom/{{ model.custom_id }}
</a-menu-item>
</a-menu-item-group>
</a-menu>
</template>
</a-dropdown>
</template>
<script setup>
import { computed } from 'vue'
import { BulbOutlined } from '@ant-design/icons-vue'
import { useConfigStore } from '@/stores/config'
const configStore = useConfigStore()
const emit = defineEmits(['select-model'])
// configStore
const modelNames = computed(() => configStore.config?.model_names)
const modelStatus = computed(() => configStore.config?.model_provider_status)
const customModels = computed(() => configStore.config?.custom_models || [])
// modelStatus key
const modelKeys = computed(() => {
return Object.keys(modelStatus.value || {}).filter(key => modelStatus.value?.[key])
})
//
const handleSelectModel = (provider, name) => {
emit('select-model', { provider, name })
}
</script>
<style lang="less" scoped>
.model-select {
max-width: 350px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
.model-text {
overflow: hidden;
text-overflow: ellipsis;
}
}
.nav-btn {
height: 2.5rem;
display: flex;
justify-content: center;
align-items: center;
border-radius: 8px;
color: var(--gray-900);
cursor: pointer;
width: auto;
transition: background-color 0.3s;
padding: 0.5rem 0.75rem;
.text {
margin-left: 10px;
}
&:hover {
background-color: var(--main-light-3);
}
}
.scrollable-menu {
max-height: 300px;
overflow-y: auto;
&::-webkit-scrollbar {
width: 6px;
}
&::-webkit-scrollbar-track {
background: transparent;
border-radius: 3px;
}
&::-webkit-scrollbar-thumb {
background: var(--gray-400);
border-radius: 3px;
}
&::-webkit-scrollbar-thumb:hover {
background: var(--gray-500);
}
}
</style>
<style lang="less">
// dropdown
.ant-dropdown-menu {
&.scrollable-menu {
max-height: 300px;
overflow-y: auto;
}
}
</style>

View File

@ -0,0 +1,372 @@
<template>
<div class="user-management">
<h3>用户管理</h3>
<p>用户管理请谨慎操作一旦删除用户该用户将无法登录</p>
<a-button type="primary" @click="showAddUserModal">
<template #icon><PlusOutlined /></template>
添加用户
</a-button>
<a-spin :spinning="userManagement.loading">
<div v-if="userManagement.error" class="error-message">
{{ userManagement.error }}
</div>
<a-table
:dataSource="userManagement.users"
:columns="userColumns"
rowKey="id"
:pagination="{ pageSize: 10 }"
>
<!-- 角色列自定义渲染 -->
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'role'">
<a-tag :color="getRoleColor(record.role)">{{ getRoleLabel(record.role) }}</a-tag>
</template>
<!-- 操作列 -->
<template v-if="column.key === 'action'">
<div class="table-actions">
<a-button type="link" @click="showEditUserModal(record)">
<EditOutlined />
</a-button>
<a-button
type="link"
danger
@click="confirmDeleteUser(record)"
:disabled="record.id === userStore.userId || (record.role === 'superadmin' && userStore.userRole !== 'superadmin')"
>
<DeleteOutlined />
</a-button>
</div>
</template>
</template>
</a-table>
</a-spin>
<!-- 用户表单模态框 -->
<a-modal
v-model:visible="userManagement.modalVisible"
:title="userManagement.modalTitle"
@ok="handleUserFormSubmit"
:confirmLoading="userManagement.loading"
@cancel="userManagement.modalVisible = false"
:maskClosable="false"
>
<a-form layout="vertical">
<a-form-item label="用户名" required>
<a-input v-model:value="userManagement.form.username" placeholder="请输入用户名" />
</a-form-item>
<template v-if="userManagement.editMode">
<div class="password-toggle">
<a-checkbox v-model:checked="userManagement.displayPasswordFields">
修改密码
</a-checkbox>
</div>
</template>
<template v-if="!userManagement.editMode || userManagement.displayPasswordFields">
<a-form-item label="密码" required>
<a-input-password v-model:value="userManagement.form.password" placeholder="请输入密码" />
</a-form-item>
<a-form-item label="确认密码" required>
<a-input-password v-model:value="userManagement.form.confirmPassword" placeholder="请再次输入密码" />
</a-form-item>
</template>
<a-form-item label="角色">
<a-select v-model:value="userManagement.form.role">
<a-select-option value="user">普通用户</a-select-option>
<a-select-option value="admin" v-if="userStore.isSuperAdmin">管理员</a-select-option>
<a-select-option value="superadmin" v-if="userStore.isSuperAdmin">超级管理员</a-select-option>
</a-select>
</a-form-item>
</a-form>
</a-modal>
</div>
</template>
<script setup>
import { reactive, onMounted, watch } from 'vue';
import { notification } from 'ant-design-vue';
import { useUserStore } from '@/stores/user';
import {
DeleteOutlined,
EditOutlined,
PlusOutlined
} from '@ant-design/icons-vue';
const userStore = useUserStore();
//
const userManagement = reactive({
loading: false,
users: [],
error: null,
modalVisible: false,
modalTitle: '添加用户',
editMode: false,
editUserId: null,
form: {
username: '',
password: '',
confirmPassword: '',
role: 'user' //
},
displayPasswordFields: true, //
});
//
watch(() => userManagement.displayPasswordFields, (newVal) => {
//
if (!newVal) {
userManagement.form.password = '';
userManagement.form.confirmPassword = '';
}
});
//
const fetchUsers = async () => {
try {
userManagement.loading = true;
const users = await userStore.getUsers();
userManagement.users = users;
userManagement.error = null;
} catch (error) {
console.error('获取用户列表失败:', error);
userManagement.error = '获取用户列表失败';
} finally {
userManagement.loading = false;
}
};
//
const showAddUserModal = () => {
userManagement.modalTitle = '添加用户';
userManagement.editMode = false;
userManagement.editUserId = null;
userManagement.form = {
username: '',
password: '',
confirmPassword: '',
role: 'user' //
};
userManagement.displayPasswordFields = true;
userManagement.modalVisible = true;
};
//
const showEditUserModal = (user) => {
userManagement.modalTitle = '编辑用户';
userManagement.editMode = true;
userManagement.editUserId = user.id;
userManagement.form = {
username: user.username,
password: '',
confirmPassword: '',
role: user.role
};
userManagement.displayPasswordFields = true; //
userManagement.modalVisible = true;
};
//
const handleUserFormSubmit = async () => {
try {
//
if (!userManagement.form.username) {
notification.error({ message: '用户名不能为空' });
return;
}
if (userManagement.displayPasswordFields) {
if (!userManagement.form.password) {
notification.error({ message: '密码不能为空' });
return;
}
if (userManagement.form.password !== userManagement.form.confirmPassword) {
notification.error({ message: '两次输入的密码不一致' });
return;
}
}
userManagement.loading = true;
//
if (userManagement.editMode) {
//
const updateData = {
username: userManagement.form.username,
role: userManagement.form.role
};
//
if (userManagement.displayPasswordFields && userManagement.form.password) {
updateData.password = userManagement.form.password;
}
await userStore.updateUser(userManagement.editUserId, updateData);
notification.success({ message: '用户更新成功' });
} else {
await userStore.createUser({
username: userManagement.form.username,
password: userManagement.form.password,
role: userManagement.form.role
});
notification.success({ message: '用户创建成功' });
}
//
await fetchUsers();
userManagement.modalVisible = false;
} catch (error) {
console.error('用户操作失败:', error);
notification.error({
message: '操作失败',
description: error.message || '请稍后重试'
});
} finally {
userManagement.loading = false;
}
};
// 使
const togglePasswordFields = () => {
userManagement.displayPasswordFields = !userManagement.displayPasswordFields;
if (!userManagement.displayPasswordFields) {
userManagement.form.password = '';
userManagement.form.confirmPassword = '';
}
};
//
const confirmDeleteUser = (user) => {
//
if (user.id === userStore.userId) {
notification.error({ message: '不能删除自己的账户' });
return;
}
//
const { modal } = notification;
modal.confirm({
title: '确认删除用户',
content: `确定要删除用户 "${user.username}" 吗?此操作不可撤销。`,
okText: '删除',
okType: 'danger',
cancelText: '取消',
async onOk() {
try {
userManagement.loading = true;
await userStore.deleteUser(user.id);
notification.success({ message: '用户删除成功' });
//
await fetchUsers();
} catch (error) {
console.error('删除用户失败:', error);
notification.error({
message: '删除失败',
description: error.message || '请稍后重试'
});
} finally {
userManagement.loading = false;
}
}
});
};
//
const userColumns = [
{
title: 'ID',
dataIndex: 'id',
key: 'id',
width: 80
},
{
title: '用户名',
dataIndex: 'username',
key: 'username'
},
{
title: '角色',
dataIndex: 'role',
key: 'role',
width: 120
},
{
title: '创建时间',
dataIndex: 'created_at',
key: 'created_at',
width: 180
},
{
title: '最后登录',
dataIndex: 'last_login',
key: 'last_login',
width: 180
},
{
title: '操作',
key: 'action',
width: 120
}
];
//
const getRoleLabel = (role) => {
switch (role) {
case 'superadmin': return '超级管理员';
case 'admin': return '管理员';
case 'user': return '普通用户';
default: return role;
}
};
//
const getRoleColor = (role) => {
switch (role) {
case 'superadmin': return 'red';
case 'admin': return 'blue';
case 'user': return 'green';
default: return 'default';
}
};
//
onMounted(() => {
fetchUsers();
});
</script>
<style lang="less" scoped>
.user-management {
margin-top: 20px;
h3 {
margin-bottom: 10px;
}
p {
margin-bottom: 20px;
}
.error-message {
color: red;
margin: 10px 0;
}
.table-actions {
button {
padding: 0 4px;
}
}
.password-toggle {
margin-bottom: 16px;
}
}
</style>

File diff suppressed because it is too large Load Diff