diff --git a/.workbuddy/memory/2026-08-14.md b/.workbuddy/memory/2026-08-14.md index 36247c8..61450a5 100644 --- a/.workbuddy/memory/2026-08-14.md +++ b/.workbuddy/memory/2026-08-14.md @@ -370,3 +370,245 @@ quoteData = append(quoteData, pcrValues...) - [ ] 2400 样本全量性能验证(预计 ~16 分钟) - [x] 部署文档:docs/sswflow/nax/NAX_2P_VM.md 已重写为 V1.5+ connector 模式端到端版(commit 0e366b1, 380 行/23.7KB),含拓扑/协议/部署资产/部署流程/验收/已知边界/部署坑 - [ ] connector 实体初始数据(bank_credit_features / bureau_credit_scores 等数据集元数据)应登记到 tds_dataset 表(当前 connector 拉到的 localDataDir 仍是占位符) + +## AGENTS.md 增补:阿里云个人版公共共享镜像仓(2026-08-14 12:00) + +按用户指令,在 `/Users/xs/Desktop/ssw/AGENTS.md`「关键约定(必读)」开篇(鉴权小节之前)新增 **Docker 镜像源(阿里云个人版公共仓)** 子节: + +- 地址:`crpi-yscq976uwza88qap.cn-shenzhen.personal.cr.aliyuncs.com/ssw188/tmp:` +- 公开仓库(无需登录即可 `docker pull`),通过 tag 区分镜像内容(如 `tmp:alpine-3.20`) +- 苏苏本机已 `docker login` 可推,其他人需自己注册阿里云个人版账号 +- 典型场景:Docker Hub 国内网络不可达时作镜像分发通道 / 多机器同步本地构建镜像(`cc:latest`、nax、scard 等) +- 与 `docker registry-mirror` 配置无关(本仓库走完整地址直连) + +## SwiftBar sandbox-services 一键启停 demo + e2e + dag + 切显示器(v3 → v4 加 dag,2026-08-14) + +**插件**:`~/Documents/SwiftBar/sandbox-services.5s.sh`(5s 刷新,菜单栏**唯一**图标 🚀)。 +- **菜单结构**(3 个分组,N/3): + - sandbox: `🚀 sandbox N/3` + `demo 18850 ⏹/✅` + `e2e 7788 ⏹/✅` + `dag 7789 ⏹/✅` + 一个按钮(`▶ 全部启动` ↔ `⏹ 全部停止`,启停三个一起) + - 显示器: `🖥 🏠/📡 当前: (<档>)` + 一个按钮(`切到远程档 (1440x900)` ↔ `切回日常档 ()`) +- **菜单项点击 → MenuAction 分支 + 第一参数**:8s debounce 防双击 + - `toggle_sandbox` → 看 demo 端口决定启停 → nohup & disown 拉起或 `xargs kill -TERM` → `open -g swiftbar://refreshallplugins` 立刻刷图标 + - `toggle_display` → displayplacer 切档(自动判断当前档位 + 保存日常档到 `$HOME/Library/Application Support/display-toggle/config.json`) +- **存活判定 = 端口**:`/usr/sbin/lsof -nP -iTCP: -sTCP:LISTEN -t >/dev/null 2>&1`,**完全不用 pidfile**;start 函数本身幂等(活就 `return 0`,不活才真启)。 +- **demo**:`$PY $REPO/demo/server.py --host 0.0.0.0 --port 18850`(PY = `/Users/xs/.workbuddy/binaries/python/envs/nax-psi-demo/bin/python`)。 +- **e2e**:cd 到 `$REPO/docs/e2e` 起 `bash start.sh`(7788,start.sh 自带 BE/FE 补位)。 +- **stop**:对端口上所有 pid `xargs kill -TERM`(不等 SIGKILL 兜底,简化逻辑)。 +- **显示器切换**:displayplacer `id: mode:`,日常档从 config.json 读,远程档从 `displayplacer list | grep 'res:1440x900' | head -1` 取。 + +**单一图标实现** (从 v2 → v3): +- 切显示器逻辑**完全 inline** 进 sandbox-services.5s.sh(约 50 行额外代码,文件总 143 行),删除原 `~/Documents/SwiftBar/切显示器.sh`。 +- 旧 `切显示器.sh` 移到 `~/Documents/scripts/切显示器.sh` 作为 reference(保留不是必须)。 +- 关键发现:**`true` 不生效**(SwiftBar 仍 enabled: yes)、`.bak` 后缀不生效(SwiftBar 仍扫)→ **必须 mv 到 SwiftBar 扫描目录外** 才能 100% 不被加载。 + +**踩坑(已记 `~/.workbuddy/skills/macos-swiftbar/` skill,精简实战要点版)**: +- macOS BSD 无 `/usr/bin/setsid`,detach 子进程 = `nohup & disown + 三流重定向`。 +- 苏苏真实脚本目录 = `~/Documents/SwiftBar/`(不是 `~/Library/Application Support/SwiftBar/`,后者是 SwiftBar 默认但被改成 Documents)。 +- `` metadata + 文件后缀改名 都**不**让 SwiftBar 跳过加载,要禁用插件**必须 mv 出扫描目录**。 +- Ice 折叠:加新 SwiftBar 插件(每个有 `` 占独立 NSStatusItem)若让菜单栏拥挤,Ice 会自动折叠其他图标。诊断报告 `~/Library/Application Support/SwiftBar/Diagnostics/latest-system-report.txt` 看 NSStatusItem VisibleCC Item-1 = 0 提示被隐藏。**单一图标方案彻底规避这个问题**。 +- ⚠️ 测试 toggle_display 会真切屏幕:sandbox 里 displayplacer 是宿主机进程,实测切到了 1440x900 又切回 3440x1440。SwiftBar 真实菜单点也是真的切屏,操作前确认。 + +## SwiftBar 点击无反应修复(v4,2026-08-14 14:05) +- **根因**:菜单项参数语法错误。写成 `bash=$0 toggle_sandbox terminal=false` 时,SwiftBar 把 `toggle_sandbox` 解析为另一个无值属性,脚本 `$1` 为空,`case` 匹配不到任何分支 → 点击无反应。 +- **修复**:全部改为 `bash=$0 param1=toggle_sandbox terminal=false`(显示器同理 `param1=toggle_display`)。SwiftBar 的附加参数必须用 `param1=` / `param2=` 传递。 +- 手动执行 `/Users/xs/Documents/SwiftBar/sandbox-services.5s.sh toggle_sandbox` 验证 `$1` 能进分支;刷新后菜单项输出已带 `param1=`。 +- 同步更新 `~/.workbuddy/skills/macos-swiftbar/SKILL.md` §2 / §9,把错误示例改对。 + +## SwiftBar 改为各自启停(v5,2026-08-14 14:12) +- 用户要求不要"全部启动",三个服务各自独立启停。 +- 把 `toggle_sandbox` 拆成 `toggle_demo` / `toggle_e2e` / `toggle_dag` 三个 case 分支,各自独立 debounce 文件(`/tmp/demo-toggle-last` 等)。 +- 菜单渲染改为每行一个按钮:运行中显示 `✅ | color=green`(点击停止),未运行显示 `▶ 启动`(点击启动)。 +- 显示器切换保持独立 `toggle_display` 不变。 +- 刷新 SwiftBar 后生效。 + +## e2e / dag-panel 启动脚本改用 Safari(2026-08-14 14:15) +- 用户要求默认用 Safari 打开,不要用 Chrome。 +- 修改 `docs/e2e/start.sh` 和 `docs/e2e/dag-panel/dag-panel.sh` 中的 `open -a "Google Chrome"` → `open -a "Safari"`。 +- Playwright 测试框架本身仍用 Chrome(channel:chrome),不动(测试驱动与手动打开控制台不是一回事)。 + +## SwiftBar 增加 SSW 开发服务器启停(2026-08-14 14:40) +- `sandbox-services.5s.sh` 新增 SSW 开发服务器分组: + - ⚡ 极速版 = web(5173) + boot(30000),`param1=toggle_ssw_lite` 启停 + - 📦 完整版 = jweb(3000) + boot(30000),`param1=toggle_ssw_full` 启停 +- boot 启动命令: `cd boot && java -DDB_HOST=127.0.0.1 -DDB_PORT=15432 -DREDIS_HOST=127.0.0.1 -DREDIS_PORT=16379 -Dserver.port=30000 -jar ssw-admin/target/ssw-admin-7.0.0-RELEASE.jar` +- 状态显示: 双端口都在线→✅绿色,部分在线→⚡橙色,全离线→▶启动。 + +### open_ssw_* 改用后台 wait_and_open(2026-08-14 15:00) +- **问题**: boot 端口绑了 LISTEN 但 Spring 还在初始化(FISCO/redis/db 握手),Safari 立刻打开 → 「打开页面失败」。 +- **修法**: `wait_and_open $port $url $timeout` 函数 → 等端口 LISTEN + curl 探活(2xx/3xx)再 open Safari。 +- 整个轮询放进后台子 shell `(... ) >/dev/null 2>&1 &`,SwiftBar 菜单脚本立即返回不卡顿。 +- 同样给 demo/e2e/dag 加上,点 → 启动 → 自动开 Safari。 +- 超时兜底: 即使没就绪也 open 一次,让 Safari 显示真实错误而不是空白。 + +--- + +## nax 框架 + 两容器调试(13:00-15:10) + +### 用户决策 +- "看看 refs/secretflow 怎么做的" → secretflow SS-GLM 默认 `sr_sig = 0.5 + 0.5·x/√(1+x²)`,全程无 clip、无比较 +- "调试阶段可以不用那么复杂,直接在 vm 里跑 nax 框架" → 简化部署:docker compose 两个容器(alice + bob),Ray head/worker + 自定义资源 alice_node/bob_node +- "通了后再部署到 tds/沙盒内" +- "你启动两个容器先跑吧,轻量一点" → docker compose 轻量方案 + +### nax 框架改动(V1.5.1) +- `nax/device/vault.py` 加 `sigmoid_sr(x, iters=6)`:sr_sig = 0.5 + 0.5·x·rsqrt(1+x²),复用 rsqrt +- `nax/ml/vertical.py` VerticalLogisticRegression 加 `sigmoid_method='poly'|'sr'` 参数 + clip_w_inf 已有 +- `nax/crypto/share.py` mul_triple / matmul_triple / trunc_pair 都已加 fxp 缩放(让 a/b 缩到 fxp 尺度,防 matmul 链式增长) + +### demo/nax-2p-docker/(新增) +- 两容器:alice(Ray head + driver)+ bob(Ray worker) +- 基于 ssw-nax-runtime:latest 覆盖 nax 源码(确保含 sigmoid_sr / sigmoid_method) +- driver.py 走 nax framework RayBackend:VFrame.psi_align → to_secret → VerticalLogisticRegression(sr) +- 物理隔离证据:alice_node pinned 192.168.147.2, bob_node pinned 192.168.147.3 + +### 跑通状态 +- PSI: n_common=2400 ✅ +- 训练 30 epochs 50s ✅(SR 全程无 polynomial 越界) +- z-score 归一化生效:max|z|=16.20(之前 2.68e8) +- **AUC=0.4989(vs baseline 0.78)**——训练没收敛 +- 根因:w 解密后最大 9 万,clip 后被缩成 1.0,精度丢光,迭代无效 +- fxp 增长虽改 trunc_pair 让 max|z| 从 6 万→16(改善 4000 倍),但 w 累积增长仍在 +- **下一步**:dry_run 模式(明文 numpy)跑同 VLR,对比 plaintext 期望 vs 密文实际,看是否 nax framework 的 VLR 实现本身有 bug,或只是 lr/epochs 超参 + +### 已踩坑 +- ssw-nax-runtime ENTRYPOINT 是 nax_adapter.py(等 stdin)→ docker-compose.yml 必须 `entrypoint: ["bash", "-c"]` 覆盖 +- build context 不能用绝对路径 → docker-compose.yml 配 `context: 仓库根, dockerfile: 相对路径` +- fixtures 软链不被 build context 解析 → cp -RL 转真目录 +- SiteRef 没有 .site("alice").count(),用 `intersection_size(idx_alice)` 或 VFrame.count() +- nax 框架 matmul 公开+密文 vs 全密文路径不同;driver 用全密文 box.share(secret) + + +## SwiftBar jweb 端口修复(2026-08-14 15:20) +- **现象**: 点 📦 完整版启动后 Safari 打 localhost:3000 空白;lsof 看 3000 没监听,5173/5174/5175 倒是有 jweb vite。 +- **根因**: `jweb/internal/vite-config/src/config/application.ts:111-112` 的 `mergeConfig(common, applicationConfig, vite)` 把 `jnpf-web-apps-main/vite.config.mts`(只声明 `server.proxy`)**浅合并覆盖了** applicationConfig 的 `server.port`/`server.open`/`server.warmup` → vite 默认 5173,端口冲突后顺延 5174/5175。 +- **修法**: `start_jweb()` 加 `VITE_PORT=3000 VITE_OPEN_BROWSER=false` 环境变量(不侵入 jweb vendor)。验证 `pnpm vite --mode development --port 3000` 绑到 3000 ✅。 +- 顺手清掉之前残留的 jweb vite 进程(5173/5174/5175)。 + +### jweb 3000 端口第二次踩坑(2026-08-14 15:30) +- **第二次失败**: `VITE_PORT=3000 pnpm dev` 还是 5173。Debug vite 日志显示 `port: 5173, host: undefined`——applicationConfig 完全没生效。 +- **真正根因**: `jweb/apps/jnpf-web-apps-main/` 根目录**没有 index.html**(只有 `dist/index.html` 产物)。`@vben/vite-config` 的 `defineConfig(userConfigPromise?, type='auto')` 在 auto 模式下用 `existsSync('index.html')` 判断走 application 还是 library → 没 index.html → library 路径 → applicationConfig(server.port=3000) **完全被 bypass**,回到 vite 默认 5173。 +- **修法**: 不用 env 也不用 applicationConfig,直接 vite CLI `--port 3000 --host 0.0.0.0`(vite 把端口写到 server.port 优先级最高)。验证 `pnpm vite --mode development --port 3000 --host 0.0.0.0` 绑 3000 ✅。 +- **新坑**: `--host true` 报 `getaddrinfo ENOTFOUND true` —— vite 把字符串 "true" 当主机名解析。必须 `--host 0.0.0.0` 或 `--host` (无值,vite 文档默认)。 +- 顺手踩:zsh `nohup & disown` 在 subshell `(...)` 里 disown 编号会报 `disown:4: no current job`,直接去掉 `disown`(nohup 已脱离 tty)。 + +### 重大 bug 发现(V1.5.1 必修) +**`Vault.add_public` 实现错误**:只在第一方 share 加 `enc(pub)`,其他方 share 不变。 +- 预期:reveal(x) = x +- 实际:reveal(x + pub) = x + pub +- 影响范围:`v.add(secret, 0.5)`、`v.sub(secret, y)`(sub 也调 add)……所有涉及"密文 + 公开数"的操作 +- 修复方向(待实施):所有方 share 都加 `enc(pub)`,reveal 时总和 = x + n*enc(pub);但 n*enc(pub) 仍在 64-bit 范围,decode 时需 `sum mod 2^fxp / 2^fxp` 取真实值(数学上等价,因 enc(pub) 是 2^fxp 倍数,n*enc(pub) 加到 64-bit 整数上后,>> fxp 还原真实值 x + n*pub,这又错了……) +- **正确修复**:`v.add(secret, pub)` 应该让**所有方**都加 `enc(pub)/n`(公开常数),reveal 时 sum = x + enc(pub),>> fxp = x + pub,仍然错! +- **真正正确**:让 first share 加 `enc(pub)`,其他 share 加 `enc(0) - enc(pub)/2 + ...`。实际上 nax framework 设计应该是 first share + enc(pub),其他 share **不修改**——这意味着 reveal 出的真值 = x + pub。这是设计 bug。 +- **修复方案**:v.add(secret, pub) 应该改为让**所有方**都加 enc(pub),reveal 时需要 `reveal(secret, to)` 知道刚才是 `add_public`,自动减回 n*pub;或者把 add 的实现改成"恒等映射":第一方 share + enc(pub),其他方 share 不变 → 直接 reveal 得 x + pub,然后调用方必须**先 reveal 后**才能用——这违反 vault 设计。 +- **最简 fix**:sigmoid 不用 add(half, half_inner),改为 mul 让 half 不被加进去。例如 `0.5·x·rsqrt(1+x²) + 0.5` 写成 `0.5·(1 + x·rsqrt(1+x²))`,1 是公开数,`1 + x·rsqrt(...)` 用 `add_public(1.0, x·rsqrt(...))` —— 还是有 add_public bug。 +- **真正 fix**:改 add_public 让所有方都加 enc(pub),但 reveal 时减去 n*pub(因为 n*enc(pub) 编码到 uint64 加 x 后再 >> fxp 还是 x + n*pub,不是 x……) + + +### jweb 真根因:缺 index.html 导致 application/library 都坏(2026-08-14 15:55) +- **终极根因**: `jweb/apps/jnpf-web-apps-main/` 没有 `index.html` 也没 `src/main.ts`/`src/index.ts` → `@vben/vite-config` 的 `defineConfig` 在 auto 模式用 `existsSync('index.html')` 走 library 路径 → applicationConfig(server.port/open/warmup/SPA fallback) 完全不生效 → vite 默认 5173 + 根路径 404。 +- **试过方案**: + 1. ❌ `VITE_PORT=3000` env: vite 读到 VITE_PORT=3000,但 applicationConfig 没被调用,server.port 仍是 5173。 + 2. ❌ `defineConfig(..., 'application')` 强制类型: vite 报 `ENOENT index.html` → 启动时 Pre-transform error。 + 3. ✅ **创建 jnpf-web-apps-main/index.html 引用 /src/bootstrap.ts**: vite auto-detect application → applicationConfig 全生效 → `--port 3000 --host 0.0.0.0` 真正绑 3000 + SPA fallback 工作。 +- **关键**: jweb vite 启动后 esbuild 处理 bootstrap.ts 的预优化可能 EPIPE(子进程挂掉),需 `sleep 20+` 等依赖优化完成再访问 `/login` 等 SPA 路由。 +- **修法文件**: 新增 `jweb/apps/jnpf-web-apps-main/index.html`(git 未跟踪,需 `.gitignore` 决定是否入库;本地临时修复)。 + + +### 密文 VLR 跑通真相(2026-08-14 16:10) + +**最终战绩**: +- 路径:两容器物理隔离(alice 192.168.147.2 / bob 192.168.147.3),Ray head/worker + alice_node/bob_node 资源 +- PSI: 2400(nax framework ECDH-PSI) +- 训练:20 epochs / 17.1s / peak max|z|=2.51 +- **AUC = 0.7798, KS = 0.4379**(与 plaintext baseline 0.7785 / 0.45 几乎一致) + +**踩坑——两小时 debug 走过的弯路**: + +1. **SR sigmoid 走错分支**(a=denom=1+x² > 3 时 nax framework rsqrt 符号翻转 + 震荡发散到 4.5e172)→ 修复 vault.sigmoid_sr 加 min(denom, 3) 保护,但仍有 ±1.9 偏置 +2. **delta_w norm=1.59** 看似正常但 w 不断累积 → 实际是 matmul 链式 fxp 增长 +3. **plaintext AUC=0.78, 密文 AUC=0.5** → 一度怀疑 nax 框架算法/sigmoid/IRLS 实现 + +**真正根因(最后一公里)**:driver.py line 245 写错 `y_true = alice_df["default_flag"].values[: len(p)]`——这是 raw bank CSV 前 2400 行,而 p 来自 psi_align 后的对齐行序。**y 与 p 行序错乱 → AUC 跌到 0.5**。 + +**修复**:用 `aligned.parts["alice"].fetch()["default_flag"].values` 拿 aligned 后的 y,再 `nax.init(..., allow_plain_fetch=True)`。 + +**关键洞察**:所有 rsqrt / polynomial 越界 / fxp 增长都被这次误诊掩盖了 debug 视线——y 与 p 错乱已经是 AUC 跌到 0.5 的充分原因,不需要其他 bug。 + +**PlainVault vs Vault 真实密文 二分对照**: +- PlainVault + psi_align + to_secret + secret_column: AUC=0.7799 ✓ +- 真实密文 Ray backend: AUC=0.7798(修复 y_true 后)✓ +- 两者 w/p 标量统计几乎一致;差异在 fxp 量化噪声(AUC rank-based,对整体偏置不敏感) + +**调试教训(私有沉淀)**: +- AUC 跌到 0.5 但 p 统计量正常 → 优先怀疑 y 与 p 行序错乱(最常见、最快定位) +- 怀疑 nax 框架算法/sigmoid bug 之前,必须先验证 y 行序 +- 真实密文走 PSI → 必须 reveal aligned 的 y,不能用 raw 表 +- `allow_plain_fetch=True` 调试期间打开,生产环境关掉 + +**demo 路径**:`/Users/xs/Desktop/ssw/demo/nax-2p-docker/` +- driver.py + docker-compose.yml + plaintext_baseline.py + plaintext_fxp_psi_baseline.py +- ssw-nax-2p:dev-local 镜像(基于 ssw-nax-runtime + 覆盖最新 nax 源码 + 装 sklearn) +- 物理隔离 alice/bob 容器,Ray head/worker + alice_node/bob_node 资源 + +### jweb 缺文件从官方源补齐(2026-08-14 16:05) +- **diff 源**:`/Users/xs/Desktop/workspace/jnpf/jnpf-web-monorepo-framework/apps/jnpf-web-apps-main/`(官方 7.0 仓库) +- **ssw jweb 缺**: src/locales/index.ts + src/app.vue + src/preferences.ts + index.html +- **拷贝**(从官方 jnpf-web-monorepo-framework 直接 cp,保留 ssw 改造的 bootstrap.ts 入口): + - `src/locales/index.ts` (128 行,导出 $t + setupI18n) + - `src/app.vue` + - `src/preferences.ts` + - `index.html`(指向 /src/bootstrap.ts,因为 ssw 把 main.ts 逻辑合并进 bootstrap.ts 了) +- **验证**: pnpm vite --port 3000 --host 0.0.0.0 → vite ready 0 ERROR → `/` 200 HTML,`/login` 200 HTML +- **不要 cp main.ts**: ssw jweb 是把 initApplication+bootstrap 合并到 bootstrap.ts(加了 ssw 专属代码 registerSswComp / gridLayout / disAutoConnect 等),main.ts 的 import 路径对不上 + +### jweb 页面打开后一直在首页转圈(2026-08-14 17:45) +# 无头浏览器探测 (`/tmp/jweb-probe2.js`,playwright 1.59.1 + 缓存 chromium-1234): +# - #app div 存在但 0 children,0 innerHTML +# - #__app-loading__ loader 一直显示(loaderHidden:false) +# - 无 console error,无失败请求 → bootstrap() 没跑起来 +# **根因**: ssw 删了官方 src/main.ts(把 initApplication+bootstrap 逻辑合并进了 src/bootstrap.ts), +# 但 index.html 指向 /src/bootstrap.ts,而 bootstrap.ts 末尾只有 `export { bootstrap }`, +# **没有顶层自动调用代码** → bootstrap 永远不被触发,SPA 永远不挂载。 +# **修法**: 在 bootstrap.ts 末尾添加 initApplication() 启动代码(复刻官方 main.ts 语义): +# 1) import initPreferences(从 @vben/preferences) + unmountGlobalLoading(从 @vben/utils) + overridesPreferences(从 ./preferences) +# 2) 顶层 const _appEnCode = getJnpfAppEnCode(); +# 3) async function initApplication() { ... await initPreferences({...}); await bootstrap(namespace); unmountGlobalLoading(); } +# 4) initApplication();(顶层调用) +# **验证**: playwright 探测 → appChildren=1, appInnerLen=4527, loader 已消失, +# bodyText="账号登录 扫码登录 登 录", URL 自动跳到 /login。 +# **警告**: 还剩 1 个 Vue warn "Plugin has already been applied to target app" —— 是某个 antd 组件重复 use, +# 不影响功能但需后续清理(通常是 registerGlobComp + registerSswComp 都注册了某个组件)。 + +### jweb proxy 不生效,前端 POST login 返回 404(2026-08-14 18:00) +# playwright 探测 `/login` 页面: GET /api/oauth/getLoginConfig 200 OK,POST /api/oauth/Login **404**。 +# **boot 后端实测**: POST http://localhost:30000/api/oauth/Login code=200 OK(API本身没问题)。 +# **真正根因**: jweb vite proxy 配置拿不到 VITE_PROXY: +# 1. ssw jweb 缺 `.env.development`(官方仓库里有) → VITE_PROXY 未定义 → proxy 列表 = [] +# 2. 即使 .env.development 存在,`internal/vite-config/src/utils/env.ts` 的 `getConfFiles()` 通过 `process.env.npm_lifecycle_script` 解析 `--mode` 字符串, +# **pnpm dev 包装后 npm_lifecycle_script 不带 --mode**(因为 --mode 由 dev 脚本里的 pnpm vite --mode development 拼), +# → 默认 mode='production' → 只找 .env / .env.production → 完全忽略 .env.development +# 3. 即使都解决,`loadAndConvertEnv` 默认 match='VITE_' 应该 OK,但用户 vite.config.mts 顶层 await 时 cwd 是 apps/jnpf-web-apps-main, +# `process.cwd()` 正确,但 confFiles 默认从 npm_lifecycle_script 拿,production mode 跳过 .env.development。 +# **修法**(在 vite.config.mts 顶层): +# ```ts +# const { createProxy } = await loadAndConvertEnv( +# 'VITE_', +# ['.env', '.env.local', '.env.development', '.env.development.local'], +# ); +# ``` +# 显式传 confFiles 绕过 npm_lifecycle_script 解析,同时复制官方 .env.development 到 ssw jweb 根。 +# **验证**: createProxy = {"/dev": {target:"http://localhost:30000", ...}, "/reportDev": {...}}, +# curl /dev/api/oauth/getLoginConfig via vite proxy → 200。 +# **次要发现**: playwright + curl 同时打 vite 会让 esbuild 子进程 EPIPE 崩(vite 进程死亡), +# 必须先 curl 后 playwright,或干脆只用 Safari 验证。 + +### jweb 登录提示"账户密码错误",web 同样账号成功(2026-08-14 18:10) +# 三边 curl 实测都返回 200 + 真实 token: jweb(3000) + web(5173) + boot(30000) 都成功。 +# → 不是后端问题,jweb 前端**自己算出来的加密密文**跟 boot 期望的不一样。 +# **根因**: ssw jweb 缺官方 `.env`(基础配置),其中 `VITE_CIPHER_KEY=EY8WePvjM5GGwQzn` 缺失。 +# - jweb 用 `import.meta.env.VITE_CIPHER_KEY` 走 `AesEncryption` 类加密密码,key undefined → 加密出错的密文 +# - web 把 CIPHER_KEY = 'EY8WePvjM5GGwQzn' **硬编码**在 src/utils/cipher.ts,绕过了 env +# - 所以 web 正常,jweb 失败 +# **修法**: cp 官方 `.env` 到 jweb 根(`VITE_CIPHER_KEY` 在官方 .env 而非 .env.development)。 +# 顺带验证: 官方 jnpf-web-monorepo-framework/apps/jnpf-web-apps-main/.env 含 VITE_CIPHER_KEY、 +# VITE_APP_NAMESPACE、VITE_APP_STORE_SECURE_KEY、VITE_A_MAP_* 等平台关键配置。 diff --git a/jweb/apps/jnpf-web-apps-main/.env b/jweb/apps/jnpf-web-apps-main/.env new file mode 100644 index 0000000..52972d7 --- /dev/null +++ b/jweb/apps/jnpf-web-apps-main/.env @@ -0,0 +1,16 @@ +# 应用标题 +VITE_APP_TITLE=JNPF快速开发平台 + +# 应用命名空间,用于缓存、store等功能的前缀,确保隔离 +VITE_APP_NAMESPACE=jnpf-web + +# 对store进行加密的密钥,在将store持久化到localStorage时会使用该密钥进行加密 +VITE_APP_STORE_SECURE_KEY=jnpf + +# 密钥 +VITE_CIPHER_KEY=EY8WePvjM5GGwQzn + +# 高德地图相关key +VITE_A_MAP_JS_KEY= +VITE_A_MAP_WEB_KEY= +VITE_A_MAP_SECURITY_JS_CODE= diff --git a/jweb/apps/jnpf-web-apps-main/.env.development b/jweb/apps/jnpf-web-apps-main/.env.development new file mode 100644 index 0000000..1f66ec7 --- /dev/null +++ b/jweb/apps/jnpf-web-apps-main/.env.development @@ -0,0 +1,38 @@ +# 端口号 +VITE_PORT=3000 + +VITE_BASE=/ + +# 本地开发代理,可以解决跨域及多地址代理 +# 如果接口地址匹配到,则会转发到http://localhost:30000,防止本地出现跨域问题 +# 可以有多个,注意多个不能换行,否则代理将会失效 +VITE_PROXY=[["/dev","http://localhost:30000"], ["/reportDev","http://localhost:32000"]] + +# 接口地址 +VITE_GLOB_API_URL=/dev + +# 报表接口地址 +VITE_GLOB_REPORT_API_URL=/reportDev + +# WebSocket基础地址 +VITE_GLOB_WEBSOCKET_URL=ws://localhost:30000 + +# 是否打开 devtools,true 为打开,false 为关闭 +VITE_DEVTOOLS=false + +# 是否注入全局loading +VITE_INJECT_APP_LOADING=true + +# 是否开启 Vite 启动预热(默认开启)。预热会牺牲少量启动时间,换取「打开页面 / 路由跳转」秒开。 +# false 完全关闭(启动最快 ~3s,但每个页面首次进入要现编译,打开/跳转会变慢) +VITE_WARMUP=true + +# 预热哪些 view 页面(逗号分隔的 glob)。Vite 7 的预热本就是「后台非阻塞」, +# 不会拖慢启动,所以这里默认预热全部页面,打开/跳转都秒开。 +# 若想减少后台预热开销,可收窄,例如只预热常用模块: +# VITE_WARMUP_VIEWS=./src/views/workFlow/**/*.vue,./src/views/onlineDev/**/*.vue +# 完全关闭预热(启动最快,但页面首次进入要现编译):VITE_WARMUP=false +VITE_WARMUP_VIEWS=./src/views/**/*.vue + +# 容器内无浏览器,关闭启动自动打开(避免 xdg-open ENOENT 报错) +VITE_OPEN_BROWSER=false diff --git a/jweb/apps/jnpf-web-apps-main/index.html b/jweb/apps/jnpf-web-apps-main/index.html new file mode 100644 index 0000000..89fc88f --- /dev/null +++ b/jweb/apps/jnpf-web-apps-main/index.html @@ -0,0 +1,12 @@ + + + + + + JNPF 快速开发平台 + + +
+ + + diff --git a/jweb/apps/jnpf-web-apps-main/src/app.vue b/jweb/apps/jnpf-web-apps-main/src/app.vue new file mode 100644 index 0000000..6fb52e5 --- /dev/null +++ b/jweb/apps/jnpf-web-apps-main/src/app.vue @@ -0,0 +1,39 @@ + + + diff --git a/jweb/apps/jnpf-web-apps-main/src/bootstrap.ts b/jweb/apps/jnpf-web-apps-main/src/bootstrap.ts index 2407159..6254f7d 100644 --- a/jweb/apps/jnpf-web-apps-main/src/bootstrap.ts +++ b/jweb/apps/jnpf-web-apps-main/src/bootstrap.ts @@ -10,8 +10,9 @@ import { mitt } from '@jnpf/utils'; import { registerAccessDirective } from '@vben/access'; import { registerLoadingDirective } from '@vben/common-ui'; -import { preferences } from '@vben/preferences'; +import { initPreferences, preferences } from '@vben/preferences'; import { initStores, useUserStore } from '@vben/stores'; +import { unmountGlobalLoading } from '@vben/utils'; import '@vben/styles'; import '@vben/styles/antd'; @@ -25,6 +26,7 @@ import { useBaseStore } from '#/store'; import { getJnpfAppEnCode } from '#/utils/jnpf'; import { registerSswComp } from '#/views/ssw/registerSswComp'; +import { overridesPreferences } from './preferences'; import { initComponentAdapter } from './adapter/component'; import App from './app.vue'; @@ -104,4 +106,26 @@ async function bootstrap(namespace: string) { app.mount('#app'); } +// ─── ssw: 取代官方 src/main.ts 启动入口 ──────────────────────────────────────── +// 原官方 main.ts 被 ssw 删除(逻辑已并入本文件 bootstrap()),但 index.html 仍指向本文件, +// 所以要在文件顶层自动触发启动。复刻官方 initApplication() 语义。 +const _appEnCode = getJnpfAppEnCode(); + +async function initApplication() { + const env = import.meta.env.PROD ? 'prod' : 'dev'; + const appVersion = import.meta.env.VITE_APP_VERSION; + const namespace = `${import.meta.env.VITE_APP_NAMESPACE}-${appVersion}-${env}${_appEnCode ? `_${_appEnCode}` : ''}`; + + if (!_appEnCode) { + if (!overridesPreferences.app) overridesPreferences.app = {}; + overridesPreferences.app.layout = 'mixed-nav'; + } + + await initPreferences({ namespace, overrides: overridesPreferences }); + await bootstrap(namespace); + unmountGlobalLoading(); +} + +initApplication(); + export { bootstrap }; diff --git a/jweb/apps/jnpf-web-apps-main/src/locales/index.ts b/jweb/apps/jnpf-web-apps-main/src/locales/index.ts new file mode 100644 index 0000000..1c44218 --- /dev/null +++ b/jweb/apps/jnpf-web-apps-main/src/locales/index.ts @@ -0,0 +1,128 @@ +import type { Locale } from 'ant-design-vue/es/locale'; + +import type { App } from 'vue'; + +import type { LocaleSetupOptions, SupportedLanguagesType } from '@vben/locales'; + +import { ref } from 'vue'; + +import { $t, setupI18n as coreSetup, i18n, loadLocalesMap } from '@vben/locales'; +import { preferences } from '@vben/preferences'; + +import antdEnLocale from 'ant-design-vue/es/locale/en_US'; +import antdDefaultLocale from 'ant-design-vue/es/locale/zh_CN'; +import antdTwLocale from 'ant-design-vue/es/locale/zh_TW'; +import dayjs from 'dayjs'; + +import { getLangJson } from '#/api/system/baseLang'; + +const antdLocale = ref(antdDefaultLocale); + +const modules = import.meta.glob('./langs/*.json'); + +const localesMap = loadLocalesMap(modules); + +export function flattenObject(obj, prefix = '') { + return Object.keys(obj).reduce((acc, key) => { + const fullPath = prefix ? `${prefix}.${key}` : key; // 构建新的路径 + if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) { + // 如果当前属性是对象,且不是数组,继续递归 + Object.assign(acc, flattenObject(obj[key], fullPath)); + } else { + // 否则,直接设置路径和值 + acc[fullPath] = obj[key]; + } + return acc; + }, {}); +} + +/** + * 加载应用特有的语言包 + * 这里也可以改造为从服务端获取翻译数据 + * @param lang + */ +async function loadMessages(lang: SupportedLanguagesType) { + const [appLocaleMessages] = await Promise.all([localesMap[lang]?.(), loadThirdPartyMessage(lang)]); + return flattenObject(appLocaleMessages?.default); +} + +/** + * 加载第三方组件库的语言包 + * @param lang + */ +async function loadThirdPartyMessage(lang: SupportedLanguagesType) { + await Promise.all([loadAntdLocale(lang), loadDayjsLocale(lang)]); +} + +/** + * 加载dayjs的语言包 + * @param lang + */ +async function loadDayjsLocale(lang: SupportedLanguagesType) { + let locale; + switch (lang) { + case 'en-US': { + locale = await import('dayjs/locale/en'); + break; + } + case 'zh-CN': { + locale = await import('dayjs/locale/zh-cn'); + break; + } + case 'zh-TW': { + locale = await import('dayjs/locale/zh-tw'); + break; + } + // 默认使用中文 + default: { + locale = await import('dayjs/locale/zh-cn'); + } + } + if (locale) { + dayjs.locale(locale); + } else { + console.error(`Failed to load dayjs locale for ${lang}`); + } +} + +/** + * 加载antd的语言包 + * @param lang + */ +async function loadAntdLocale(lang: SupportedLanguagesType) { + switch (lang) { + case 'en-US': { + antdLocale.value = antdEnLocale; + break; + } + case 'zh-CN': { + antdLocale.value = antdDefaultLocale; + break; + } + case 'zh-TW': { + antdLocale.value = antdTwLocale; + break; + } + default: { + antdLocale.value = antdDefaultLocale; + break; + } + } +} +async function initLocale(locale: SupportedLanguagesType) { + const res = await getLangJson(); + if (!res || !res.data) return; + const message = JSON.parse(res.data) || {}; + i18n.global.mergeLocaleMessage(locale, message); +} + +async function setupI18n(app: App, options: LocaleSetupOptions = {}) { + await coreSetup(app, { + defaultLocale: preferences.app.locale, + loadMessages, + missingWarn: !import.meta.env.PROD, + ...options, + }); +} + +export { $t, antdLocale, initLocale, setupI18n }; diff --git a/jweb/apps/jnpf-web-apps-main/src/preferences.ts b/jweb/apps/jnpf-web-apps-main/src/preferences.ts new file mode 100644 index 0000000..95a64b2 --- /dev/null +++ b/jweb/apps/jnpf-web-apps-main/src/preferences.ts @@ -0,0 +1,13 @@ +import { defineOverridesPreferences } from '@vben/preferences'; + +/** + * @description 项目配置文件 + * 只需要覆盖项目中的一部分配置,不需要的配置不用覆盖,会自动使用默认配置 + * !!! 更改配置后请清空缓存,否则可能不生效 + */ +export const overridesPreferences = defineOverridesPreferences({ + // overrides + app: { + name: import.meta.env.VITE_APP_TITLE, + }, +}); diff --git a/jweb/apps/jnpf-web-apps-main/vite.config.mts b/jweb/apps/jnpf-web-apps-main/vite.config.mts index 5bbed4d..70eca03 100644 --- a/jweb/apps/jnpf-web-apps-main/vite.config.mts +++ b/jweb/apps/jnpf-web-apps-main/vite.config.mts @@ -3,7 +3,13 @@ import fs from 'node:fs'; import { defineConfig, loadAndConvertEnv } from '@vben/vite-config'; -const { createProxy } = await loadAndConvertEnv(); +// ⚠️ 必须显式传 match='VITE_' + confFiles:internal/vite-config 的 getConfFiles() 通过 npm_lifecycle_script 解析 --mode, +// 但 pnpm dev 包装后 npm_lifecycle_script 不带 --mode 字符串,导致 mode 默认为 production, +// 进而只找 .env / .env.production —— .env.development 完全被忽略 → VITE_PROXY 读不到 → createProxy = [] +const { createProxy } = await loadAndConvertEnv( + 'VITE_', + ['.env', '.env.local', '.env.development', '.env.development.local'], +); function pathResolve(dir: string) { // eslint-disable-next-line n/prefer-global/process @@ -43,6 +49,9 @@ export default defineConfig(async () => { return { application: {}, vite: { + // ⚠️ 注意:必须把 server.proxy 放在 vite 顶层 config(server),不能放在 vite.server 子字段—— + // vite-config 的 application.ts 也有 server 字段,会通过 mergeConfig 浅合并覆盖用户 vite.server + // 验证: 单独写 vite.server.proxy → vite debug 日志显示 server.proxy: [] server: { proxy: createProxy, }, @@ -91,4 +100,4 @@ export default defineConfig(async () => { ], }, }; -}); +}); \ No newline at end of file