import { Context, Next } from 'koa'; import CODE from '@utils/code'; interface InterfaceSession { user?: string } const noAuths = ['/api/login', '/api/register']; export default () => { return async (ctx: Context, next: Next): Promise => { if (noAuths.includes(ctx.url)) { await next(); return; } if (ctx.session) { const session: InterfaceSession = ctx.session.toJSON(); if (!session.user) { ctx.session.user = null; ctx.body = ctx.$response(null, '未登录', false, CODE.NOT_LOGIN); return; } await next(); } else { ctx.status = 500; ctx.throw(500, 'session获取失败'); } }; };