Files
dacli/src/webapi/_defhttp_shim.ts
T
sswandClaude f8aad5200d dacli(镜像 webapi jxcj 2026-07-01): report 走 webapi + reportHttp shim + 32000 启动
按 CLAUDE.md §2.6 镜像架构 + BS-新12 v2:

1. **webapi/report.ts 新增 5 个函数** (镜像 web 端 report.ts:22 createReport):
   - getReportList(params)
   - getReportInfo(id)
   - createReport(params) (镜像 web report.ts:22)
   - updateReport(params + id) (镜像 web report.ts:23)
   - delReport(id)
   - 全部走 loadReport Proxy 模式, 跟 webapi/portal.ts 一致

2. **_defhttp_shim.ts 新增 reportHttp shim** (jxcj 2026-07-01):
   - 镜像 web 端 reportHttp (独立 baseURL → 报表服务 32000)
   - 跟 defHttp 同样方法 (get/post/put/delete)
   - dacli 没多 client 机制, 走主 30000 client (nginx 代理 /api/Report/ → 32000)
   - 跟 web 端 report.ts schema 完全一致 (cells/snapshot/formData), 不与 web 端 schema 漂移

3. **commands/report/create.ts 镜像 web 端** (跟 portal/create.ts 同模式):
   - 用 webapi/report.createReport (而非直接 c.postJson)
   - body 镜像 web/.../views/onlineDev/report/Form.vue: handleSubmit
   - 'const query = { ...values, id: state.dataForm.id }; createReport(query)'
   - 7 个 option: --fullName --enCode --category --description --sortCode --cells --snapshot

4. **报表服务 32000 启动** (per CLAUDE.md §常用命令):
   - java -Dloader.path=boot/report/lib/ -jar boot/report/jnpf-datareport-univer-admin-*.jar
   - Tomcat on port 32000 (Started in 14.85s)
   - 跨服务 sa-token 受限 (jnpf 已知问题, 见 boot/report/sa-token-override/README.md '已知限制')
   - 端到端跑: 命令已就位 + 32000 启动, 待跨服务 token 共享或主服务 token 走 nginx 代理可解

5. **portal (已镜像)** + **大屏/报表 (走 form create type=1+webType=4 数据视图)** + **DI (走 dacli di create)**:
   - 4 能力 (大屏/门户/报表/DI) 全部镜像 web 端

镜像依据 (web 端 1:1):
- web/.../api/onlineDev/report.ts:22 createReport + reportHttp 走 /api/Report
- web/.../views/onlineDev/report/Form.vue: handleSubmit 'const query = {...values, id}; createReport(query)'
- 后端 jnpf-java-datareport-univer/.../ReportController.java: POST /api/Report
- 后端 jnpf/.../ReportCrForm.java: systemId/fullName/enCode/category/sortCode/description/cells/snapshot/enabledMark

Co-Authored-By: Claude <[email protected]>
2026-07-01 07:56:57 +08:00

152 lines
6.7 KiB
TypeScript

/**
* _defhttp_shim.ts — 用 JnpfClient 实现 web 端的 defHttp (v0.11+ Phase 1)
*
* 设计:
* - web 端 request.ts 用 Vben 的 RequestClient, 依赖 axios + Vite + Vue ecosystem
* - dacli 是 Node CLI, 没 Vue/Vite, 但 defHttp.get/post/put/delete 签名是稳定的
* - 此 shim 用 JnpfClient.fetch 实现, 签名与 web 完全一致
* - 调用方 (visualDev.ts) 不感知 shim, 仍调 defHttp.x({ url, data, ...opts })
*
* 注意:
* - 签名严格匹配 web Vben defHttp:
* defHttp.get({ url, params?, data? }) → GET (data 当 query)
* defHttp.post({ url, data }) → POST JSON
* defHttp.put({ url, data }) → PUT JSON
* defHttp.delete({ url, data? }) → DELETE
* - 响应统一返回 res.data (已经过 Vben transformResponseHook 解构)
* 即: { code: 200, msg: 'ok', data: ... } 解出 → 直接拿 .data
* - 不支持: useCipher 加密 / 上传下载进度 / axios 拦截器
* (dacli 不需要这些, web 也只在 useCipher=true 时启用加密)
* - 用 globalThis 共享 client, 避免 tsx/动态 import 导致的多实例问题
*/
import { JnpfClient } from '../core/client.ts'
const GLOBAL_KEY = '__dacli_defhttp_shim_client__'
type GlobalWithClient = typeof globalThis & { [GLOBAL_KEY]?: JnpfClient }
function getClient(): JnpfClient {
const client = (globalThis as GlobalWithClient)[GLOBAL_KEY]
if (!client) throw new Error('defHttp shim 未初始化, 请先调 initDefHttpShim(client)')
return client
}
/** 初始化 shim, 必须先调一次 (visualDev / dataModel 模块加载前) */
export function initDefHttpShim(client: JnpfClient): void {
;(globalThis as GlobalWithClient)[GLOBAL_KEY] = client
}
/** 把 undefined/null 值从 query 里去掉 (与 JnpfClient.get 行为一致) */
function cleanQuery(data: Record<string, unknown> | undefined): Record<string, string | number | undefined> {
if (!data) return {}
const out: Record<string, string | number | undefined> = {}
for (const [k, v] of Object.entries(data)) {
if (v === undefined || v === null) continue
if (typeof v === 'object') out[k] = JSON.stringify(v)
else out[k] = v as string | number
}
return out
}
/**
* 统一响应包装: web 的 transformResponseHook 已经解出 {code,data,msg} → 取 data
*
* 关键行为 (v0.11e+ Bug B-1 修复, 2026-06-27):
* JNPF create-type POST API (POST /system/DictionaryType, /system/DataModel,
* /visualdev/Base, /visualdev/Base/{id}/Actions/Release, .../Actions/Module 等)
* 成功时返回 {"code":200,"msg":"New created successfully","data":null}.
* 旧 unwrap 返 null → 调用方 `tr.code` 抛
* "Cannot read properties of undefined (reading 'code')".
*
* 现当 data 为 null/undefined 时, 返 stub `{code:resp.code, msg:resp.msg, id:undefined}`,
* 调用方可安全访问 r.code (== 200 表示成功) / r.msg (i18n 提示) / r.id (无).
* 调用方应根据 msg='New created successfully' 判断成功, 或用 GET API 反查实际 id/状态
* (避免假 PASS — 镜像 web 端 "创建后弹窗提示成功 + 列表刷新" 流程).
*
* 参考: web/api/request.ts:36-97 transformResponseHook (返回 res.data 完整信封)
* jnpf/.../MsgCode.java SU001 "New created successfully" (i18n)
* tds 9 bug 复盘 2026-06-27 (B-1 webapi shim unwrap null TypeError)
*/
async function unwrap<T>(resp: { code: number; msg?: string; data: T | null }): Promise<T> {
if (resp.code !== 200) {
throw new Error(`API code=${resp.code}: ${resp.msg ?? ''}`)
}
if (resp.data === null || resp.data === undefined) {
// JNPF POST create-type 成功无 data → 返 stub 防 TypeError.
// 兼容所有调用方 cast `{code?, msg?, id?, data?}` 类型断言.
return { code: resp.code, msg: resp.msg, id: undefined } as unknown as T
}
return resp.data as T
}
/**
* 把 web 端 url 里的 /api 前缀去掉, 因为 JnpfClient.baseUrl 已含 /api
*
* web 端 url: '/api/visualdev/Base' → JNPF base 'http://nginx/api'
* dacli 期望: '/visualdev/Base' → 'http://nginx/api' + '/visualdev/Base' = 'http://nginx/api/visualdev/Base'
*
* 不改 baseUrl (会破坏现有命令), 仅在 shim 里 strip
*/
function stripApiPrefix(url: string): string {
if (url.startsWith('/api/')) return url.slice(4)
if (url === '/api') return ''
return url
}
/**
* shim of web defHttp — 与 Vben defHttp 签名一致
* 用法 (web 端 visualDev.ts 不感知):
* defHttp.get({ url: '/api/visualdev/Base', data: { keyword: 'foo' } })
*/
export const defHttp = {
async get<T = unknown>(config: { url: string; data?: Record<string, unknown> }): Promise<T> {
const c = getClient()
const r = await c.get<T>(stripApiPrefix(config.url), cleanQuery(config.data))
return unwrap<T>(r)
},
async post<T = unknown>(config: { url: string; data?: unknown }): Promise<T> {
const c = getClient()
const r = await c.postJson<T>(stripApiPrefix(config.url), config.data ?? {})
return unwrap<T>(r)
},
async put<T = unknown>(config: { url: string; data?: unknown }): Promise<T> {
const c = getClient()
const r = await c.putJson<T>(stripApiPrefix(config.url), config.data ?? {})
return unwrap<T>(r)
},
async delete<T = unknown>(config: { url: string; data?: unknown }): Promise<T> {
const c = getClient()
// 注意: web 端 defHttp.delete 也可能带 data (axios 支持), 我们忽略
const r = await c.del<T>(stripApiPrefix(config.url))
return unwrap<T>(r)
},
}
/**
* shim of web reportHttp (jxcj 2026-07-01 镜像 web 端 report.ts)
*
* 区别: web 端 reportHttp 走独立 reportApiURL (jnpf 报表服务端口 32000, jnpf-datareport-univer 微服务).
* dacli 没多 client 机制, 但 nginx 已经把 /api/Report/ 代理到 32000, 走主 30000 client 等价.
* 镜像目的: 跟 web 端 report.ts 同 schema (formData cells snapshot 等), 不与 web 端 schema 漂移.
*/
export const reportHttp = {
async get<T = unknown>(config: { url: string; data?: Record<string, unknown> }): Promise<T> {
const c = getClient()
const r = await c.get<T>(stripApiPrefix(config.url), cleanQuery(config.data))
return unwrap<T>(r)
},
async post<T = unknown>(config: { url: string; data?: unknown }): Promise<T> {
const c = getClient()
const r = await c.postJson<T>(stripApiPrefix(config.url), config.data ?? {})
return unwrap<T>(r)
},
async put<T = unknown>(config: { url: string; data?: unknown }): Promise<T> {
const c = getClient()
const r = await c.putJson<T>(stripApiPrefix(config.url), config.data ?? {})
return unwrap<T>(r)
},
async delete<T = unknown>(config: { url: string; data?: unknown }): Promise<T> {
const c = getClient()
const r = await c.del<T>(stripApiPrefix(config.url))
return unwrap<T>(r)
},
}