turn-response.js 418 B

12345678910111213
  1. 'use strict';
  2. const _ = require('lodash');
  3. module.exports = options => {
  4. return async function turnResponse(ctx, next) {
  5. await next();
  6. const body = ctx.response.body;
  7. if (!body) return;
  8. const { errcode, errmsg, ...others } = body;
  9. const code = errcode === 0 ? 200 : 500;
  10. const msg = errmsg === 'ok' ? '操作成功' : '操作失败';
  11. ctx.response.body = { code, msg, ...others };
  12. };
  13. };