fcc-core
Version:
Fusion communication center.
55 lines (53 loc) • 2.19 kB
JavaScript
import { XW, xws } from './XW'
import apis from '../api'
import BaseException from '../errors/base-exception'
import statusMsg from '../types/status'
// import { ApiCallback } from '../types/system'
// // console.log(apis)
// class Factory {
// // 添加 api
// static addApi (api: string, callback: ApiCallback) {
// console.log(api)
// }
// }
// 动态添加 api
for (let api in apis) {
Reflect.defineProperty(XW.prototype, api, {
value: async function (...args) {
let xw = xws.get(this)
try {
if (apis[api].type === 'system' || apis[api].type === 'test' || this[apis[api].type]) { // 判断初始化实例的时候有没有初始化该接口
if (xw.agentState === '3' && apis[api].type !== 'system' && apis[api].type !== 'test') { // 判断是否已登录融合通讯中台
throw new BaseException(401, `你当前处于离线或忙碌状态,调用融合通讯中台 api:${api} 失败`)
}
let data = await apis[api](xw, ...args)
return {
status: 200, // 调用接口成功
msg: statusMsg[200],
data
}
} else {
throw new BaseException('404', `调用的融合通讯中台 api:${api} 未开放,请联系管理员!`)
}
} catch (e) {
if (e instanceof BaseException) { // 如果是已知错误,直接返回给用户
// return { ...e, error: e }
e.message = `调用融合通讯中台 ${api} 失败!${e.message}`
console.error(e)
if (e.parameterErrors) {
console.error(`调用融合通讯中台 api:${api} 入参错误详情:`, e.parameterErrors)
}
return e
} else { // 如果是未知错误
if (process.env.NODE_ENV === 'production') { // 生产环境
return new BaseException(500, `${statusMsg[500]} 调用融合通讯中台api:${api} 失败!${JSON.stringify(e)}`) // 告诉用户中台内部发生未知错误
} else { // 开发环境
throw e
}
}
}
},
writable: false
})
}
export { XW }