'use strict'; const moment = require('moment'); const _ = require('lodash'); const operaMethod = require('../utils/routerword.js'); module.exports = (options, app) => { return async (ctx, next) => { try { const { authorization } = ctx.request.header; if (authorization) { let user = decodeURI(authorization); // console.log(user); if (user) { user = JSON.parse(user); const { path: route, method, query, body, ip } = ctx.request; if (method !== 'GET') { const { id, name } = user; const params = {}; if (query) params.query = query; if (body) params.body = body; if (method === 'DELETE') { const pl = route.split('/'); const last = _.last(pl); if (last && last.length > 20) { // >20确保是ObjectId,ObjectId是24位 params.id = last; } } const obj = { route, method, params, userid: id, name, ip }; const opera = operaMethod(route, method); if (opera) obj.opera = opera; await ctx.service.logs.create(obj); } } } } catch (error) { console.error(`${moment().format('YYYY-MM-DD HH:mm:ss')}日志生成发生错误!`); console.error(error); } finally { await next(); } }; };