refactor: 完善版 P-2 前端分包与 P-3 揭示 userKey 加固

- admin vite 配置 manualChunks 拆分 naive/echarts/vendor,index chunk 从 1652KB 降到 69KB
- 揭示 userKey 改为 POST 接口(写操作日志、防缓存)+ 前端二次确认,GET 仅返回掩码
- 更新问题清单待完善项状态与项目记忆
This commit is contained in:
Nixevol
2026-07-09 20:21:05 +08:00
parent ad193d1777
commit ec8789ccd9
9 changed files with 79 additions and 17 deletions
+2
View File
@@ -243,6 +243,8 @@
- 天河链对接修复 + 全平台测试(2026-07-09,分支 `dev/qa-fix`,从 `dev/refine` 分出):按 `aidocs/代码审查问题清单.md` 修复 T1-T8 并经**天河链测试环境真实联调**通过。① `TianheProvider`(`apps/api/src/blockchain/tianhe.provider.ts`)重写:新增 `registerUser`(接口1)/`userInfo`(接口8)/`ensureRegistered`(幂等:先查后注册);`attest` 前确保托管钱包已注册(以 `tianheWalletAddress` 为已注册标记)。**关键联调结论**:upload(接口6)必须**手工构造 multipart**——whatwg `FormData`+`Blob` 会被 undici 强加 `filename` 导致天河链返回 500,userDto 分段须「无 filename + Content-Type: application/json」;confirm(接口9)是 **GET 但参数走请求体 JSON**(query 返回「request body missing」),用 `node:http/https` 底层模块发带 body 的 GET,appKey 不进 URL。② 严格校验:`assertOk` 要求 `code===0` 且 upload 必须返回 `transactionHash`,否则抛错走 FAILED(杜绝用本地 dataHash 冒充链上哈希的「假上链」)。③ `BlockchainService`:userId 固定平台级(默认 `artveris-platform`)、userKey 一次性随机生成并**明文持久化**(不再从 appKey 派生);新增 `registerTianheIdentity`/`verifyTianheIdentity`/`revealTianheUserKey`。④ T8 身份生命周期:`settings.service.ts` 把 `tianheUserKey` 从 `SECRET_KEYS`(加密)移到 `MASKED_KEYS`(仅展示掩码)——明文存库使备份跨重装可恢复(不依赖 settingsEncryptionKey);新增 `setRaw`;`SettingsController` 加 `/settings/tianhe/register|verify|identity`;`Settings.vue` 天河链区新增链上身份卡(注册/显示钱包地址/揭示复制 userKey/手动录入经接口8校验 + 强提示)。⑤ **F1 修复**:`seed.ts` 追溯码短码从旧 10 位混合字母改为 12 位纯数字(与 `TRACE_CODE_PATTERN` 一致),否则 C 端公开溯源判 INVALID。⑥ 测试:天河链测试环境注册钱包(地址 `0x4783...f494`)、两码真实上链各得独立 txHash(每物唯一性验证)、存证链接可公开访问;UserID/UserKey 回填 `docs/系统测试信息.md`(已 gitignore,含真实密钥不提交)。后台逐页 + C 端逐场景浏览器点测全通过。验证:4 包构建 + 27 后端单测 + 5 脚本 py_compile 全绿。注意:`docs/系统测试信息.md` 已加入 `.gitignore` 并从版本控制移除。
- 完善版分支 dev/qa-polish(2026-07-09,从 dev/qa-fix 分出):在保守版基础上实现「待完善清单」中收益明确的项。① P-2 admin bundle 拆分:`apps/admin/vite.config.ts` 加 `build.rollupOptions.output.manualChunks`(naive/echarts/vendor 独立 chunk),`index` chunk 从 ~1652KB 降到 ~69KB、Dashboard 从 474KB 降到 10KB,第三方库独立缓存。② P-3 揭示 userKey 加固:`GET /settings/tianhe/identity` 改为只回 `userKeyMasked`(不再 GET 明文);新增 `POST /settings/tianhe/identity/reveal` 回明文——POST 使其被操作日志记录、避免缓存;前端揭示前二次确认(`Settings.vue` `revealUserKey`),实测揭示动作已落操作日志。P-1(天河链 confirm status 偶发 500)、P-4(C 端骨架屏)判定为过度设计,保持现状。三分支:`dev/refine`(基线未动)、`dev/qa-fix`(保守版,严守红线)、`dev/qa-polish`(本分支,含 P-2/P-3 完善)。验证:4 包构建 + 27 单测通过。
## 代码导航地图(2026-07-08 通读沉淀,2026-07-09 校正,供后续开发快速定位)
> 本节是一次性代码通读结论,用于快速定位模块;具体行为以上文时间线与实际代码为准。
+5 -5
View File
@@ -127,9 +127,9 @@
## 待完善清单(dev/qa-polish 分支处理,非红线内缺陷)
> 以下为全平台测试中记录的「可完善但不属功能 bug/断裂」项,按计划在 `dev/qa-polish` 分支按最完善形态实现,供对比选择。
> 以下为全平台测试中记录的「可完善但不属功能 bug/断裂」项,已在 `dev/qa-polish` 分支实现,供对比选择。
- P-1 · 天河链 confirm 的 `status` 语义:测试环境对新交易 status 查询偶发 `500 Index: 3, Size: 1`(天河链侧内部异常)。当前以区块信息判定 confirmed 已足够,但生产环境可加:确认失败到达上限后的告警/看板提示、`status` 字段一旦天河链明确语义后纳入判定。
- P-2 · admin `index` chunk 体积(~469KB gzip,含 naive 全量 + ECharts):可按需引入/`manualChunks` 拆分,提升首屏;当前内部后台可接受。
- P-3 · 天河链身份「注册/重新确认/揭示/手动录入」可加二次确认与操作日志留痕(当前揭示 userKey 已在前端受超管权限保护,但未单独记操作日志)。
- P-4 · C 端节点/图片懒加载与骨架屏体验可进一步打磨(当前功能完整、展示正常)。
- P-2 · admin bundle 拆分(**已在 dev/qa-polish 实现**):`vite.config.ts` 加 `manualChunks`,把 naive-ui / echarts / vendor(vue 全家桶) 拆为独立 chunk。效果:应用 `index` chunk 从 ~1652KB 降到 ~69KB,Dashboard chunk 从 474KB 降到 10KB(echarts 抽出),第三方库独立缓存,首屏与命中率显著改善。
- P-3 · 揭示 userKey 加固(**已在 dev/qa-polish 实现**):`/settings/tianhe/identity` 改为只返回 userKey 掩码(不再 GET 明文);新增 `POST /settings/tianhe/identity/reveal` 返回明文——用 POST 使其被操作日志拦截器记录(审计留痕)、避免缓存;前端揭示前弹二次确认框,实测揭示动作已落 `操作日志`。
- P-1 · 天河链 confirm 的 `status` 语义:测试环境对新交易 status 查询偶发 `500 Index: 3, Size: 1`(天河链侧内部异常)。当前以区块信息判定 confirmed 已足够;`status` 未纳入判定(语义未公开)。**保持现状**——过度处理天河链侧偶发异常属为不可控外部行为过度设计,已有重试/手动重查机制兜底,生产接入后按真实表现再定。
- P-4 · C 端节点/图片懒加载与骨架屏:当前已用 `loading="lazy"` + 收展交互,功能完整、展示正常。**保持现状**——进一步骨架屏收益有限,不做过度打磨。
+2 -1
View File
@@ -137,7 +137,8 @@ export const settingsApi = {
tianheRegister: () => post<{ userId: string; userKey: string; address: string }>('/settings/tianhe/register', {}),
tianheVerify: (data: { userId: string; userKey: string }) =>
post<{ userId: string; address: string }>('/settings/tianhe/verify', data),
tianheIdentity: () => get<{ userId: string; userKey: string; address: string }>('/settings/tianhe/identity'),
tianheIdentity: () => get<{ userId: string; address: string; userKeyMasked: string }>('/settings/tianhe/identity'),
tianheReveal: () => post<{ userId: string; userKey: string; address: string }>('/settings/tianhe/identity/reveal', {}),
};
export const storageApi = {
+2
View File
@@ -554,6 +554,8 @@
"tianheManualEntry": "Manual Entry",
"tianheReveal": "Reveal",
"tianheHide": "Hide",
"tianheRevealConfirmTitle": "Reveal User Key",
"tianheRevealConfirmContent": "The User Key is the sole credential for the platform custodial wallet. Revealing it will be recorded in the operation log. Make sure your environment is secure before continuing.",
"tianheCopy": "Copy",
"tianheCopied": "User Key copied",
"tianheIdentityHint": "The User Key is the only credential to open the platform custodial wallet. TiChain does not store it and it cannot be recovered. Keep the User ID and User Key offline; after reinstalling or migrating, use Manual Entry to continue with the same on-chain identity.",
+2
View File
@@ -554,6 +554,8 @@
"tianheManualEntry": "手动录入",
"tianheReveal": "揭示",
"tianheHide": "隐藏",
"tianheRevealConfirmTitle": "揭示 User Key",
"tianheRevealConfirmContent": "User Key 是平台托管钱包的唯一凭据,揭示操作将被记入操作日志。请确认当前环境安全后再继续。",
"tianheCopy": "复制",
"tianheCopied": "User Key 已复制",
"tianheIdentityHint": "User Key 是打开平台托管钱包的唯一凭据,天河链不留存、无法找回。请务必离线保存 User ID 与 User Key;重装或迁移系统后可通过「手动录入」继续使用同一链上身份。",
+39 -10
View File
@@ -139,11 +139,11 @@
<div class="setting-row">
<n-form-item label="User Key">
<div class="identity-key">
<span class="identity-value mono">{{ identity.userKey ? (revealKey ? identity.userKey : maskKey(identity.userKey)) : '-' }}</span>
<n-button v-if="identity.userKey" size="tiny" quaternary @click="revealKey = !revealKey">
<span class="identity-value mono">{{ identityKeyDisplay }}</span>
<n-button v-if="identity.userKeyMasked" size="tiny" quaternary @click="revealUserKey">
{{ revealKey ? t('pages.settings.tianheHide') : t('pages.settings.tianheReveal') }}
</n-button>
<n-button v-if="identity.userKey" size="tiny" quaternary @click="copyUserKey">{{ t('pages.settings.tianheCopy') }}</n-button>
<n-button v-if="identity.userKeyMasked" size="tiny" quaternary @click="copyUserKey">{{ t('pages.settings.tianheCopy') }}</n-button>
</div>
</n-form-item>
</div>
@@ -259,7 +259,7 @@ const registeringIdentity = ref(false);
const verifyingIdentity = ref(false);
const revealKey = ref(false);
const manualEntryVisible = ref(false);
const identity = reactive({ userId: '', userKey: '', address: '' });
const identity = reactive({ userId: '', userKey: '', userKeyMasked: '', address: '' });
const manualEntry = reactive({ userId: '', userKey: '' });
const TIANHE_TEST_URL = 'https://test.api.tichain.tianhecloud.com';
const TIANHE_PRODUCTION_URL = 'https://api.tichain.tianhecloud.com';
@@ -357,20 +357,42 @@ async function loadIdentity() {
try {
const res = await settingsApi.tianheIdentity();
identity.userId = res.userId || '';
identity.userKey = res.userKey || '';
identity.userKeyMasked = res.userKeyMasked || '';
identity.userKey = '';
identity.address = res.address || '';
revealKey.value = false;
} catch {
// 身份信息读取失败不阻塞设置页
}
}
function maskKey(value: string): string {
if (!value || value.length <= 8) return '******';
return `${value.slice(0, 4)}******${value.slice(-4)}`;
// 揭示明文 userKey:二次确认后经 POST 接口获取(写操作日志、避免缓存)
async function revealUserKey() {
if (revealKey.value) {
revealKey.value = false;
return;
}
dialog.warning({
title: t('pages.settings.tianheRevealConfirmTitle'),
content: t('pages.settings.tianheRevealConfirmContent'),
positiveText: t('common.action.confirm'),
negativeText: t('common.action.cancel'),
onPositiveClick: async () => {
const res = await settingsApi.tianheReveal();
identity.userKey = res.userKey || '';
revealKey.value = true;
},
});
}
async function copyUserKey() {
await navigator.clipboard.writeText(identity.userKey);
let key = identity.userKey;
if (!key) {
const res = await settingsApi.tianheReveal();
key = res.userKey || '';
identity.userKey = key;
}
await navigator.clipboard.writeText(key);
message.success(t('pages.settings.tianheCopied'));
}
@@ -382,6 +404,7 @@ async function registerIdentity() {
const res = await settingsApi.tianheRegister();
identity.userId = res.userId;
identity.userKey = res.userKey;
identity.userKeyMasked = res.userKey ? `${res.userKey.slice(0, 3)}******${res.userKey.slice(-3)}` : '';
identity.address = res.address;
revealKey.value = true;
dialog.warning({
@@ -399,8 +422,10 @@ async function verifyIdentity() {
try {
const res = await settingsApi.tianheVerify({ userId: manualEntry.userId, userKey: manualEntry.userKey });
identity.userId = res.userId;
identity.userKey = manualEntry.userKey;
identity.userKey = '';
identity.userKeyMasked = manualEntry.userKey ? `${manualEntry.userKey.slice(0, 3)}******${manualEntry.userKey.slice(-3)}` : '';
identity.address = res.address;
revealKey.value = false;
manualEntry.userId = '';
manualEntry.userKey = '';
manualEntryVisible.value = false;
@@ -493,6 +518,10 @@ onMounted(async () => {
});
const currentIconUrl = computed(() => resolveAppIcon(form.appIconFiles[0]?.url || form.appIconFiles[0]?.key));
const identityKeyDisplay = computed(() => {
if (!identity.userKeyMasked) return '-';
return revealKey.value && identity.userKey ? identity.userKey : identity.userKeyMasked;
});
</script>
<style scoped>
+12
View File
@@ -60,4 +60,16 @@ export default defineConfig({
'/uploads': { target: 'http://localhost:3000', changeOrigin: true },
},
},
build: {
// 拆分体积较大的第三方库,避免单一 index chunk 过大、拖慢首屏
rollupOptions: {
output: {
manualChunks: {
naive: ['naive-ui'],
echarts: ['echarts'],
vendor: ['vue', 'vue-router', 'pinia', 'vue-i18n', 'axios', 'dayjs'],
},
},
},
},
});
+10 -1
View File
@@ -56,7 +56,16 @@ export class SettingsController {
@ApiBearerAuth()
@Roles(RoleType.SUPER_ADMIN)
@Get('tianhe/identity')
@ApiOperation({ summary: '揭示天河链链上身份明文(userId/userKey/钱包地址),供管理员离线备份' })
@ApiOperation({ summary: '读取天河链链上身份(userId + 钱包地址,userKey 掩码),用于设置页展示' })
async getTianheIdentity() {
const { userId, userKey, address } = await this.blockchain.revealTianheUserKey();
return { userId, address, userKeyMasked: this.settingsService.maskSecretPublic(userKey) };
}
@ApiBearerAuth()
@Roles(RoleType.SUPER_ADMIN)
@Post('tianhe/identity/reveal')
@ApiOperation({ summary: '揭示天河链 userKey 明文(供离线备份);用 POST 以便写入操作日志、避免缓存' })
revealTianheIdentity() {
return this.blockchain.revealTianheUserKey();
}
@@ -127,6 +127,11 @@ export class SettingsService {
return `${value.slice(0, 3)}******${value.slice(-3)}`;
}
// 对外暴露的掩码工具(供控制器展示 userKey 掩码,不返回明文)
maskSecretPublic(value: string): string {
return this.maskSecret(value);
}
isMaskedSecret(value?: string | null): boolean {
return typeof value === 'string' && /^\*+$|^.{1,8}\*{3,}.{1,8}$/.test(value);
}