Files
ZYZ/web-server/app/middleware/gmTokenParser.ts
2026-03-13 01:38:40 +00:00

53 lines
1.8 KiB
TypeScript

import { GMUserModel } from '@db/GMUser';
import { GMGroupModel } from '@db/GMGroup'
import { GMRecordModel } from '@db/GMRecord'
import { GM_API_TYPE, STATUS } from '@consts';
import { gameData } from '@pubUtils/data';
import { Context } from 'egg';
module.exports = () => {
return async function tokenParser(ctx: Context, next: () => Promise<any>) {
if (!ctx.request.headers || !ctx.request.headers.token) {
console.error('token not found');
ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS);
return;
}
const token = typeof ctx.request.headers.token === 'string' ? ctx.request.headers.token : ctx.request.headers.token[0];
const user = await GMUserModel.getGmAccountByToken(token);
if (!user) {
console.error('token invalid');
ctx.body = ctx.service.utils.resResult(STATUS.TOKEN_ERR);
return;
}
const url = ctx.request.url;
let dicApi = gameData.apiByUrl.get(url);
if(!dicApi) {
ctx.body = ctx.service.utils.resResult(STATUS.GM_MISS_API);
return;
}
if(user.groupId == undefined) {
ctx.body = ctx.service.utils.resResult(STATUS.GM_NO_AUTHORITY_GET);
return
}
let group = await GMGroupModel.getGroupById(user.groupId);
if(!group || !user.isEnable) {
ctx.body = ctx.service.utils.resResult(STATUS.GM_NO_AUTHORITY_GET);
return
}
if(!group.apis.includes(0) && !group.apis.includes(dicApi.id)) {
ctx.body = ctx.service.utils.resResult(STATUS.GM_NO_AUTHORITY_GET);
return
}
ctx.user = user;
await next();
if(dicApi.type != GM_API_TYPE.find) {
GMRecordModel.createRecord(user?user.uid:0, ctx.app.config.realEnv, ctx.request.url, JSON.stringify(ctx.request.body||{}), JSON.stringify(ctx.body||{}));
}
};
};