你是谁

你是谁
用户410367055
2025年12月19日
|
浏览 70

我是一个专注于技术、编程与 AI 领域的智能问答助手,旨在提供精准、简洁的解决方案,涵盖编程、软件开发、前后端、部署运维、代码报错及技术栈相关问题。如需继续排查,请点击下方【🔍追问按钮】提交补充信息。

我要提问
复制内容
分享给好友
AI编程问答网 免责声明:
以上内容除特别注明外均来源于网友提问,AI编程问答网回答,权益归原著者所有;
 
上一篇:import _ from "lodash" // satisfies instanceof,val is Type export type req_t = (fun: string, ...argArray: any[]) => Promise<any> export type res_t<T extends object> = { [K in keyof T]: T[K] extends (fun: string, ...argArray: any[]) => Promise<infer R> ? R : never }[keyof T] export function callObjProxy<T>(req: req_t): T { function proxy2(prevPath: string[]): any { return new Proxy(() => { }, { get(target, propKey) { if (typeof propKey !== 'string') { return undefined } const next = [...prevPath, propKey] return proxy2(next); }, apply(target, thisArg, argArray) { const fun = prevPath.join( '.') return req(fun, ...argArray); } }); } return proxy2([]) as T; } export interface callObjSend_t { catch(str: string): void } export async function callObj<T extends Record<string, any>>(binObj: T, ...args: any[]) { function sendErrmsg(str: string) { return ["catch", str] } try { const [funpath, ...funParams] = args || [] console.log("callObj", ...args) if (typeof funpath !== "string") { return sendErrmsg("callObj funpath不是string") } // 1. 解析路径 const p = funpath.replace(/[\[\]]/g, '.').split('.').filter(Boolean); if (p.length === 0) { return sendErrmsg("callObj 路径不存在") } // 2. 获取方法 const fun = _.get(binObj, p); if (typeof fun !== "function") { return sendErrmsg(`callObj 指定路径不是函数: ${funpath}`) } // 3. 获取方法的直接父对象(this 上下文) const parentPath = p.slice(0, -1); const context = parentPath.length > 0 ? _.get(binObj, parentPath) : binObj; if (context == null) { return sendErrmsg(`callObj 方法上下文不存在: ${funpath}`) } // 4. 正确执行 // console.log("callObj", { funpath, funParams }) const data = await fun.apply(context, funParams); // const data = await fun(...funParams); //Cannot read properties of undefined (reading 'broModules') return data } catch (err) { // console.log("callObj error", { funpath, funParams, err }) if (isError(err)) { return sendErrmsg(err.message) } else { return sendErrmsg("callObj未知错误".concat(String(err))) } } }