checkLogin.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * @Description: 检查用户是否已经登陆
  3. * @Autor: kindring
  4. * @Date: 2021-12-22 16:24:37
  5. * @LastEditors: kindring
  6. * @LastEditTime: 2022-01-26 14:07:50
  7. * @LastDescript:
  8. */
  9. const codeMap = require('../maps/rcodeMap');
  10. const progress = require('../maps/progress');
  11. /**
  12. * 检查是否登录
  13. * @param accountType 账户类型
  14. * @param {'json'|'view'} type 返回的数据类型
  15. * @returns
  16. */
  17. function checkLogin(accountType,type = 'json') {
  18. return function(req, res, next) {
  19. let field,redirectPath = progress.userLoginUrl;
  20. switch (accountType) {
  21. case 2:
  22. field = progress.adminSessionField;
  23. redirectPath = progress.adminLoginUrl;
  24. break;
  25. case 1:
  26. default:
  27. field = progress.userSessionField;
  28. }
  29. if (!req.session[field]) {
  30. // 类型
  31. let resAction;
  32. switch (type) {
  33. case 'view':
  34. resAction = {
  35. action: 'redirect',
  36. params: [302, redirectPath]
  37. }
  38. break;
  39. case 'json':
  40. default:
  41. resAction = {
  42. action: 'json',
  43. params: [{
  44. rcode: codeMap.notLogin,
  45. msg: 'not login'
  46. }]
  47. }
  48. break;
  49. }
  50. return res[resAction.action](...resAction.params)
  51. }
  52. next();
  53. }
  54. }
  55. module.exports = checkLogin;