Compare commits

..
Author SHA1 Message Date
bot_dev1 4a12aa9d4b fix(#189): 驾驶舱主题 token 卸载时清理,修复全局 CSS 变量污染
问题:cockpit/index.vue 加载模板时把 theme token 通过
setProperty 注入 :root,但 onUnmounted 仅 clearInterval 未清理,
导致进过驾驶舱/切换模板后全局 CSS 变量被残留值污染,其他页面颜色异常。

修复:
1. 注入前记录被改动的变量名及旧值(injectedTokens)。
2. onUnmounted 时逐个恢复旧值(有旧值则 setProperty 恢复,
   原本无则 removeProperty 移除),杜绝全局污染。

验收:进驾驶舱再切到其他页面,全局 CSS 变量无残留值,颜色正常。
(本环境无 pnpm,构建验证转 bot_qa)
2026-08-06 16:12:57 +08:00
5 changed files with 109 additions and 97 deletions
@@ -2,15 +2,16 @@
/**
* 模型管理(issue #183 [E2] 前端接入注册表 API)
* - 数据源:服务端注册表 API(list/promote/rollback 全走服务端,多用户共享);
* - 降级:API 不可达时回退 localStorage(iaop.admin.models.v1),UI 标注「离线演示模式」;
* - 降级:API 不可达时回退 localStorage(iaop.admin.models.v1,旧版 key),UI 标注「离线演示模式」;
* - 迁移:检测 localStorage 有数据且服务端在线时提示导入。
* - 表格:antd a-table(响应式 :data-source 直绑,规避 vxe-grid proxy 异步时序问题)。
*/
import { computed, onMounted, ref } from 'vue';
import { Page, VbenButton } from '@vben/common-ui';
import { message } from 'antdv-next';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
fetchRegistryModels,
promoteModel,
@@ -44,25 +45,39 @@ function saveLocal(list: LocalModel[]) {
}
}
const online = ref(false);
const online = ref(false); // 注册表服务在线?
const loading = ref(true);
const models = ref<RegistryModel[]>([]);
const migrateVisible = ref(false);
const columns = [
{ title: '模型', dataIndex: 'name', key: 'name', width: 200 },
{ title: '版本', dataIndex: 'version', key: 'version', width: 120 },
{ title: '阶段', dataIndex: 'stage', key: 'stage', width: 110 },
{ title: '说明', dataIndex: 'description', key: 'description' },
{ title: '操作', key: 'action', width: 180 },
];
const gridOptions: VxeTableGridOptions = {
rowConfig: { keyField: 'name' },
height: 'auto',
stripe: true,
columns: [
{ title: '模型', field: 'name', minWidth: 180 },
{ title: '版本', field: 'version', width: 120 },
{ title: '阶段', field: 'stage', width: 110, slots: { default: 'stage' } },
{ title: '说明', field: 'description', minWidth: 220 },
{ title: '操作', field: 'action', width: 170, slots: { default: 'action' } },
],
proxy: {
query: async ({ page }) => ({
items: models.value.slice((page.currentPage - 1) * page.pageSize, page.currentPage * page.pageSize),
total: models.value.length,
}),
},
};
const tagColor = (stage: string) =>
stage === 'prod' ? 'green' : stage === 'staging' ? 'orange' : 'blue';
const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });
async function refresh() {
gridApi.query();
}
async function loadFromServer() {
const resp = await fetchRegistryModels();
models.value = resp.models || [];
models.value = resp.models;
}
async function init() {
@@ -70,7 +85,7 @@ async function init() {
try {
await loadFromServer();
online.value = true;
// 迁移提示:本地有服务端没有的模型
// 迁移提示:本地有数据且服务端在线(且本地有服务端没有的模型)
const local = loadLocal();
const serverNames = new Set(models.value.map((m) => `${m.name}@${m.version}`));
const localExtra = local.filter((m) => !serverNames.has(`${m.name}@${m.version}`));
@@ -86,6 +101,7 @@ async function init() {
}));
} finally {
loading.value = false;
refresh();
}
}
@@ -94,14 +110,17 @@ async function promote(m: RegistryModel) {
try {
await promoteModel(m.name, m.version);
await loadFromServer();
refresh();
return;
} catch (e) {
message.error(`提升失败:${(e as Error).message}`);
return;
}
}
// 离线降级
m.stage = m.stage === 'staging' ? 'prod' : m.stage === 'dev' ? 'staging' : m.stage;
saveLocal(models.value.map((x) => ({ name: x.name, version: x.version, stage: x.stage, desc: x.description })));
refresh();
}
async function rollback(m: RegistryModel) {
@@ -109,6 +128,7 @@ async function rollback(m: RegistryModel) {
try {
await rollbackModel(m.name, 'prod', m.version);
await loadFromServer();
refresh();
return;
} catch (e) {
message.error(`回滚失败:${(e as Error).message}`);
@@ -117,6 +137,7 @@ async function rollback(m: RegistryModel) {
}
m.stage = 'staging';
saveLocal(models.value.map((x) => ({ name: x.name, version: x.version, stage: x.stage, desc: x.description })));
refresh();
}
async function migrateLocal() {
@@ -132,6 +153,7 @@ async function migrateLocal() {
}
}
await loadFromServer();
refresh();
migrateVisible.value = false;
if (fail === 0) {
localStorage.removeItem(MODELS_KEY);
@@ -141,6 +163,9 @@ async function migrateLocal() {
}
}
const tagColor = (stage: string) =>
stage === 'prod' ? 'green' : stage === 'staging' ? 'orange' : 'blue';
onMounted(init);
</script>
@@ -150,17 +175,15 @@ onMounted(init);
<a-tag :color="online ? 'green' : 'orange'">{{ online ? '● 服务端在线' : '● 离线演示模式' }}</a-tag>
</template>
<a-spin :spinning="loading">
<a-table :data-source="models" :columns="columns" row-key="name" size="small" :pagination="{ pageSize: 10 }">
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'stage'">
<a-tag :color="tagColor(record.stage)">{{ record.stage }}</a-tag>
</template>
<template v-else-if="column.key === 'action'">
<VbenButton size="small" :disabled="record.stage === 'prod'" @click="promote(record)">提升</VbenButton>
<VbenButton size="small" variant="outline" :disabled="record.stage !== 'prod'" @click="rollback(record)">回滚</VbenButton>
</template>
<Grid>
<template #stage="{ row }">
<a-tag :color="tagColor(row.stage)">{{ row.stage }}</a-tag>
</template>
</a-table>
<template #action="{ row }">
<VbenButton size="small" :disabled="row.stage === 'prod'" @click="promote(row)">提升</VbenButton>
<VbenButton size="small" variant="outline" :disabled="row.stage !== 'prod'" @click="rollback(row)">回滚</VbenButton>
</template>
</Grid>
</a-spin>
<a-modal v-model:open="migrateVisible" title="本地模型导入注册表"
:on-ok="migrateLocal" ok-text="导入服务端" cancel-text="暂不">
@@ -29,6 +29,9 @@ const loading = ref(true);
const errorMsg = ref('');
let timer = 0;
// issue #189:记录注入 :root 的主题变量及其旧值,卸载时恢复,
// 避免进过驾驶舱后全局 CSS 变量被残留值污染其他页面。
const injectedTokens: Array<{ key: string; prev: string }> = [];
onMounted(async () => {
try {
@@ -37,9 +40,12 @@ onMounted(async () => {
widgets.value = (plan.widgets as unknown as PlanWidget[]) || [];
// 主题 token 注入 CSS 变量(切模板 = 换一套变量)
const root = document.documentElement;
Object.entries((plan.themeTokens as Record<string, string>) || {}).forEach(([k, v]) =>
root.style.setProperty(k, v),
);
Object.entries((plan.themeTokens as Record<string, string>) || {}).forEach(([k, v]) => {
// 先记下旧值,卸载时恢复(getComputedStyle 取级联值;为空表示原本无)
const prev = root.style.getPropertyValue(k);
injectedTokens.push({ key: k, prev });
root.style.setProperty(k, v);
});
timer = window.setInterval(refreshHealth, 2000);
await refreshHealth();
} catch {
@@ -49,7 +55,19 @@ onMounted(async () => {
}
});
onUnmounted(() => window.clearInterval(timer));
onUnmounted(() => {
window.clearInterval(timer);
// issue #189:清理本次注入到 :root 的主题变量,恢复旧值,杜绝全局污染
const root = document.documentElement;
for (const { key, prev } of injectedTokens) {
if (prev) {
root.style.setProperty(key, prev);
} else {
root.style.removeProperty(key);
}
}
injectedTokens.length = 0;
});
const inferOnline = ref(false);
async function refreshHealth() {
@@ -4,7 +4,6 @@ import { ref } from 'vue';
import { Page, VbenButton } from '@vben/common-ui';
import { BxsFile } from '@vben/icons';
import type { UploadChangeParam } from 'antdv-next';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
@@ -34,19 +33,14 @@ const gridOptions: VxeTableGridOptions<Record<string, string>> = {
const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });
function onFile(info: UploadChangeParam) {
// a-upload 的 @change 载荷是 UploadChangeParam { file, fileList },
// 在 :before-upload="() => false"(禁止自动上传)时文件对象在 file.originFileObj 上,
// 不是 DOM Event,不能用 e.target.files(会抛 TypeError 导致无响应)。
const { file } = info;
// 仅在选中文件后处理一次(removed 状态时跳过,避免清空预览)
if (file.status === 'removed') return;
const raw = file.originFileObj ?? file;
if (!raw) return;
function onFile(e: Event) {
const el = e.target as HTMLInputElement;
const file = el.files?.[0];
if (!file) return;
fileName.value = file.name;
const reader = new FileReader();
reader.onload = () => parseCsv(String(reader.result || ''));
reader.readAsText(raw as Blob);
reader.readAsText(file);
}
function parseCsv(text: string) {
@@ -1,9 +1,8 @@
<script setup lang="ts">
import { computed, ref } from 'vue';
import { ref } from 'vue';
import { Page, VbenButton } from '@vben/common-ui';
import { MaterialSymbolsEdit } from '@vben/icons';
import { message } from 'antdv-next';
const components = ref([
{ type: 'process_view', title: '四状态工艺流程', enabled: true },
@@ -25,25 +24,7 @@ const move = (i: number, dir: number) => {
components.value[j] = t;
};
const layoutJson = computed(() =>
JSON.stringify({ widgets: components.value.filter((c) => c.enabled) }, null, 2),
);
// 发布布局:将布局 JSON 暂存到 localStorage(与版本发布页联动——
// version.vue 发布时一并打包该布局),注册表服务落地后可改为注册到服务端。
const LAYOUT_STORAGE_KEY = 'iaop.studio.layout.v1';
const lastPublishedAt = ref('');
async function publishLayout() {
const payload = layoutJson.value;
try {
localStorage.setItem(LAYOUT_STORAGE_KEY, payload);
lastPublishedAt.value = new Date().toISOString().slice(0, 19).replace('T', ' ');
message.success('布局已发布(暂存本地,将在「版本发布」时一并打包)');
} catch {
message.error('布局发布失败:本地存储不可用');
}
}
const layoutJson = () => JSON.stringify({ widgets: components.value.filter((c) => c.enabled) }, null, 2);
</script>
<template>
@@ -60,9 +41,8 @@ async function publishLayout() {
</a-list>
</a-card>
<a-card title="导出布局 JSON(由驾驶舱渲染)" :bordered="false" class="col">
<pre class="json-body">{{ layoutJson }}</pre>
<a-tag v-if="lastPublishedAt" color="green">最近发布:{{ lastPublishedAt }}</a-tag>
<VbenButton variant="solid" @click="publishLayout"><MaterialSymbolsEdit class="size-4" />发布布局(版本发布见「版本发布」页)</VbenButton>
<pre class="json-body">{{ layoutJson() }}</pre>
<VbenButton variant="solid"><MaterialSymbolsEdit class="size-4" />发布布局(版本发布见「版本发布」页)</VbenButton>
</a-card>
</div>
</Page>
@@ -2,15 +2,16 @@
/**
* 模板版本发布(issue #183 [E2]):发布 = 注册新版本到服务端注册表;
* 注册表离线时降级本地记录并标注。
* 表格:antd a-table(响应式直绑,规避 vxe-grid proxy 时序)。
*/
import { onMounted, ref } from 'vue';
import { ref } from 'vue';
import { Page, VbenButton } from '@vben/common-ui';
import { MaterialSymbolsAdd } from '@vben/icons';
import { message } from 'antdv-next';
import { fetchRegistryModels, registerModel } from '#/api/iaop/registry';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { registerModel } from '#/api/iaop/registry';
interface VersionRow {
version: string;
@@ -27,41 +28,38 @@ const rows = ref<VersionRow[]>([
{ version: 'v0.8.0', stage: 'dev', time: '2026-08-03 09:00', author: 'bot_dev1', note: '布局资产初版' },
]);
const columns = [
{ title: '版本', dataIndex: 'version', key: 'version', width: 120 },
{ title: '阶段', dataIndex: 'stage', key: 'stage', width: 110 },
{ title: '时间', dataIndex: 'time', key: 'time', width: 180 },
{ title: '作者', dataIndex: 'author', key: 'author', width: 120 },
{ title: '说明', dataIndex: 'note', key: 'note' },
];
const gridOptions: VxeTableGridOptions = {
rowConfig: { keyField: 'version' },
height: 'auto',
stripe: true,
columns: [
{ title: '版本', field: 'version', width: 110 },
{ title: '阶段', field: 'stage', width: 100, slots: { default: 'stage' } },
{ title: '时间', field: 'time', width: 170 },
{ title: '作者', field: 'author', width: 110 },
{ title: '说明', field: 'note', minWidth: 200 },
],
proxy: {
query: async ({ page }) => ({
items: rows.value.slice((page.currentPage - 1) * page.pageSize, page.currentPage * page.pageSize),
total: rows.value.length,
}),
},
};
const tagColor = (stage: string) =>
stage === 'prod' ? 'green' : stage === 'staging' ? 'orange' : 'blue';
onMounted(async () => {
// 初始探测服务端在线状态(注册表可达 → 在线)
try {
await fetchRegistryModels();
online.value = true;
} catch {
online.value = false;
}
});
const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });
async function publish() {
const version = `v${Date.now() % 100000}`;
const name = 'iaop-template';
// 一并打包「驾驶舱布局编排」页发布的布局(issue #187:发布布局与版本发布联动)
const layoutJson = localStorage.getItem('iaop.studio.layout.v1') || '';
const description = layoutJson ? '手动发布(前端,含布局)' : '手动发布(前端)';
try {
// 发布 = 注册新版本到服务端注册表(staging);description 含布局标记(issue #187 联动)
await registerModel({ name, version, stage: 'staging', description });
// 发布 = 注册新版本到服务端注册表(staging)
await registerModel({ name, version, stage: 'staging', description: '手动发布(前端)' });
online.value = true;
rows.value.unshift({
version, stage: 'staging',
time: new Date().toISOString().slice(0, 16).replace('T', ' '),
author: 'admin', note: layoutJson ? '已发布到服务端注册表(含布局)' : '已发布到服务端注册表',
author: 'admin', note: '已发布到服务端注册表',
});
message.success(`已注册 ${name}@${version}(staging)`);
} catch {
@@ -73,6 +71,7 @@ async function publish() {
});
message.warning('注册表服务不可达:本次发布仅本地记录');
}
gridApi.query();
}
</script>
@@ -81,12 +80,10 @@ async function publish() {
<template #extra>
<VbenButton variant="solid" @click="publish"><MaterialSymbolsAdd class="size-4" />发布新版本</VbenButton>
</template>
<a-table :data-source="rows" :columns="columns" row-key="version" size="small" :pagination="false">
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'stage'">
<a-tag :color="tagColor(record.stage)">{{ record.stage }}</a-tag>
</template>
<Grid>
<template #stage="{ row }">
<a-tag :color="row.stage === 'prod' ? 'green' : row.stage === 'staging' ? 'orange' : 'blue'">{{ row.stage }}</a-tag>
</template>
</a-table>
</Grid>
</Page>
</template>