web_login.js 940 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. /**
  3. * [前端用户登录检测]
  4. * @author szjcomo
  5. * @createTime 2020-08-03
  6. * @return {[type]} [description]
  7. */
  8. module.exports = options => {
  9. /**
  10. * [notFoundHandler 中间件实现]
  11. * @author szjcomo
  12. * @createTime 2020-08-03
  13. * @param {[type]} ctx [description]
  14. * @param {Function} next [description]
  15. * @return {[type]} [description]
  16. */
  17. return async function webLogin(ctx, next) {
  18. const token = ctx.request.header.weblogintoken;
  19. if (token) {
  20. try {
  21. const user = ctx.app.jwt.verify(token, options.secret);
  22. await next();
  23. } catch (err) {
  24. return ctx.appJson(ctx.app.szjcomo.appResult('登录时间过期,请重新登录', null, true, 10001));
  25. }
  26. } else {
  27. return ctx.appJson(ctx.app.szjcomo.appResult('未登录,请先登录后再进行数据操作', null, true, 10001));
  28. }
  29. };
  30. };