| 261 | } |
| 262 | |
| 263 | on(name: string, func: ApiFunction) { |
| 264 | const fullName = `${this.name}${name}`; |
| 265 | |
| 266 | if (this.middlewares.length === 0) { |
| 267 | // 没有中间件,直接注册 |
| 268 | this.server.on(fullName, func); |
| 269 | } else { |
| 270 | // 有中间件,需要包装处理函数 |
| 271 | this.server.on(fullName, async (params: any, con: IGetSender) => { |
| 272 | let index = 0; |
| 273 | |
| 274 | const next = async (): Promise<any> => { |
| 275 | if (index < this.middlewares.length) { |
| 276 | const middleware = this.middlewares[index++]; |
| 277 | return await middleware(params, con, next); |
| 278 | } else { |
| 279 | // 所有中间件都执行完毕,执行最终的处理函数 |
| 280 | return await func(params, con); |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | return await next(); |
| 285 | }); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // 转发消息 |