2024-07-15 00:30:33 +08:00
|
|
|
import { Inject } from '@midwayjs/core';
|
2023-01-29 13:44:19 +08:00
|
|
|
import { Context } from '@midwayjs/koa';
|
2024-07-15 00:30:33 +08:00
|
|
|
import { Constants } from './constants.js';
|
2023-01-29 13:44:19 +08:00
|
|
|
|
|
|
|
|
export abstract class BaseController {
|
|
|
|
|
@Inject()
|
|
|
|
|
ctx: Context;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 成功返回
|
|
|
|
|
* @param data 返回数据
|
|
|
|
|
*/
|
2024-08-05 12:49:44 +08:00
|
|
|
ok(data?: any) {
|
2023-01-29 13:44:19 +08:00
|
|
|
const res = {
|
|
|
|
|
...Constants.res.success,
|
|
|
|
|
data: undefined,
|
|
|
|
|
};
|
|
|
|
|
if (data) {
|
|
|
|
|
res.data = data;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 失败返回
|
2023-06-25 15:30:18 +08:00
|
|
|
* @param msg
|
|
|
|
|
* @param code
|
2023-01-29 13:44:19 +08:00
|
|
|
*/
|
2023-06-25 15:30:18 +08:00
|
|
|
fail(msg: string, code: any) {
|
2023-01-29 13:44:19 +08:00
|
|
|
return {
|
|
|
|
|
code: code ? code : Constants.res.error.code,
|
|
|
|
|
msg: msg ? msg : Constants.res.error.code,
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-06-25 15:30:18 +08:00
|
|
|
|
|
|
|
|
getUserId() {
|
|
|
|
|
const userId = this.ctx.user?.id;
|
|
|
|
|
if (userId == null) {
|
|
|
|
|
throw new Error('Token已过期');
|
|
|
|
|
}
|
|
|
|
|
return userId;
|
|
|
|
|
}
|
2023-01-29 13:44:19 +08:00
|
|
|
}
|