checkUserRK.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. const _ = require('lodash');
  3. const whiteList = [
  4. '/util/crk', // 生成rk
  5. '/admin/login', // 管理登陆
  6. '/user/wxLogin', // 用户微信登陆
  7. '/user/login', // 用户密码登录
  8. '/user', // 创建用户
  9. '/pay/order', // 支付回调
  10. ];
  11. module.exports = options => {
  12. return async function checkuserrk(ctx, next) {
  13. const request = _.get(ctx, 'request');
  14. const method = _.get(request, 'method');
  15. const uri = _.get(request, 'url');
  16. if (process.env.NODE_ENV === 'development') await next();
  17. // get方法放过
  18. else if (method === 'GET') await next();
  19. else {
  20. // 白名单中的路由放过: 查看是否以白名单中每一项为结尾,如果是的话,那就说明这个路由不需要检查requestKey
  21. const inWhiteList = whiteList.find(f => _.endsWith(uri, f));
  22. if (inWhiteList) await next();
  23. // 管理员的post放过
  24. else if (ctx.admin) await next();
  25. // 检查rk
  26. else {
  27. await ctx.service.util.rk.urk();
  28. await next();
  29. }
  30. }
  31. };
  32. };