Files
dacli/src/webapi/auth.ts
T
ssw 7b95ae6d10 feat(dacli): v3.0+ webapi 镜像化 (app 命令族, regression audit P0)
新增 webapi 镜像模块 + app 命令 webapi 化 (详见 docs/cmd-review/regression-2026-06-29.md):

**新增 webapi 模块** (2 文件):
- src/webapi/auth.ts: 镜像 web/api/core/auth.ts (loginApi + logoutApi + saasLoginApi + getTenantMobileCode + applySaasApi)
  - 6 个便捷包装函数 + AuthApi namespace 类型 + loadWebModule 真 import
- src/webapi/system.ts: 镜像 web/api/system/system.ts (15 函数)
  - getSystemList / getAuthSystemList / create / update / getInfo / delSystem
  - copySystem / exportSystem / getAppAuth / saveAppAuth / changeAppAuth / getDevUser
  - getSystemSelector / getSystemlist / setPin

**重构 app 命令** (3 文件):
- app/list.ts: webapi/system.getSystemList (替代直接 c.get)
- app/info.ts: webapi/system.getInfo (替代直接 c.get)
- app/create.ts: webapi/system.getSystemList + create + saveAppAuth (step 1 + 4)
  - step 2/3 保留 c.postJson (ssw-common 特有, web 无等价)
  - step 5 保留 JnpfClient.login (有 encryptPassword 加密逻辑)
- 修复历史 TS 错误: installAiHelpHook.source 无 webapi 字段, 用 web 字段
- 修复历史 TS 错误: ClientOptions 无 checkSystem 字段, 用 any cast 透传

**应用隔离价值**:
- 之前缺失能力恢复: copySystem / exportSystem / saveAppAuth / changeAppAuth / setPin
- 与 web 端 schema 漂移风险消除 (复用 web 端类型 + query 参数处理 + 错误响应)

测试: TS 编译干净 + 3 项验证 PASS
- dacli app list 走 webapi 返回 11 个应用 ✓
- dacli app info 走 webapi 返回完整字段 ✓
- dacli apis list --tag=system 走 OR 匹配返回 364 API ✓

留给下个 turn: storage/org/report _validate.ts 补 formValidate + 校验项 (task 25)
2026-06-29 16:50:37 +08:00

120 lines
3.7 KiB
TypeScript

/**
* webapi/auth.ts — 直接 import web 端 auth.ts (v3.0+ regression audit P0)
*
* web/apps/jnpf-web-apps-main/src/api/core/auth.ts 6 函数:
* loginApi, saasLoginApi, getTenantMobileCode, applySaasApi, logoutApi
*
* v3.0+ (2026-06-29) 新增原因:
* - cmd-review/app.md 已识别: app/login.ts 当前 0 webapi 引用, 直接 c.postJson
* - 应 webapi 镜像, 复用 web 端 loginApi 完整参数链 (account/password/code/origin/timestamp/jnpf_ticket/grant_type)
* - v0.11e+ Phase E: 登录后 base_system 存在性检测逻辑保留 (dacli 特有, web 端无)
*
* 设计 (与其他 webapi 模块一致):
* - loadWebModule 真 import web 源码 (不复刻)
* - defHttp shim 用 JnpfClient 实现 (Node CLI 环境)
* - initWebapi(client) 一次性初始化 + 缓存
*/
import { JnpfClient } from '../core/client.ts'
import { loadWebModule } from './_loader.ts'
const AUTH_SRC = '/home/ubuntu/ccWorkDir/web/apps/jnpf-web-apps-main/src/api/core/auth.ts'
let cached: any = null
export async function initWebapi(client: JnpfClient): Promise<void> {
if (cached) return
const { initDefHttpShim } = await import('./_defhttp_shim.ts')
initDefHttpShim(client)
cached = await loadWebModule(AUTH_SRC)
}
export function isWebapiLoaded(): boolean {
return cached !== null
}
/**
* AuthApi namespace — 镜像 web AuthApi.LoginParams
*
* 注意: web 端 grant_type 是必填, 我们保留这个约束.
*/
export namespace AuthApi {
/** 登录接口参数 — 镜像 web AuthApi.LoginParams */
export interface LoginParams {
account: string
password: string
code?: string
origin?: string
timestamp?: number | string
jnpf_ticket?: string
grant_type: string
}
/** saas 多租户登录参数 */
export interface SaasLoginParams {
accessToken: string
[key: string]: unknown
}
/** 登录响应 — web 端 defHttp 已 transformResponseHook 解构 {code, msg, data}, 直接拿 .data */
export interface LoginResponse {
/** token 字符串 */
token: string
[key: string]: unknown
}
}
/**
* Proxy 包装的 webapi 模块
*
* 调用前必须先 `await initWebapi(client)`
* 用法: `const res = await loadAuth.loginApi({ account, password, grant_type: 'password' })`
*/
export const loadAuth: any = new Proxy({} as any, {
get(_target, prop) {
if (!cached) {
throw new Error(`auth 模块未初始化, 请先 await initWebapi(client)`)
}
return cached[prop]
},
})
/**
* 便捷包装: loginApi + 解构响应
*
* 与 web 端 loginApi 行为完全一致 (grant_type 必填, headers Content-Type: FORM_URLENCODED 由 defHttp shim 处理)
*/
export async function loginApi(params: AuthApi.LoginParams): Promise<AuthApi.LoginResponse> {
return loadAuth.loginApi(params)
}
/**
* 便捷包装: logoutApi (v3.0+ 新增命令 `dacli app logout` 基础)
*
* web 端 logoutApi 仅 POST /api/oauth/Logout, withCredentials: true
* dacli 用 cookie 存 token (JnpfClient 默认行为), shim 已透传 withCredentials
*/
export async function logoutApi(): Promise<unknown> {
return loadAuth.logoutApi()
}
/**
* 便捷包装: saas 多租户登录
*/
export async function saasLoginApi(params: AuthApi.SaasLoginParams): Promise<AuthApi.LoginResponse> {
const { accessToken, ...rest } = params
return loadAuth.saasLoginApi({ accessToken, ...rest })
}
/**
* 便捷包装: 获取企业租户验证码
*/
export async function getTenantMobileCode(data: Record<string, unknown>): Promise<unknown> {
return loadAuth.getTenantMobileCode(data)
}
/**
* 便捷包装: 企业申请接口
*/
export async function applySaasApi(data: Record<string, unknown>): Promise<unknown> {
return loadAuth.applySaasApi(data)
}