login.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import store from "../store";
  11. import Cache from '../utils/cache';
  12. // #ifdef H5
  13. import {
  14. isWeixin
  15. } from "../utils";
  16. import auth from './wechat';
  17. // #endif
  18. import {
  19. LOGIN_STATUS,
  20. USER_INFO,
  21. EXPIRES_TIME,
  22. STATE_R_KEY
  23. } from './../config/cache';
  24. function prePage() {
  25. let pages = getCurrentPages();
  26. let prePage = pages[pages.length - 2];
  27. // #ifdef H5
  28. return prePage;
  29. // #endif
  30. //return prePage.$vm;
  31. }
  32. export function toLogin(push, pathLogin) {
  33. store.commit("LOGOUT");
  34. let path = prePage();
  35. if (path) {
  36. path = path.router;
  37. if (path == undefined) {
  38. path = location.pathname + location.search;
  39. }
  40. }
  41. // #ifdef H5
  42. else {
  43. path = location.pathname + location.search;
  44. }
  45. // #endif
  46. if (!pathLogin)
  47. pathLogin = '/page/users/login/index'
  48. Cache.set('login_back_url', path);
  49. // #ifdef H5
  50. if (isWeixin()) {
  51. auth.oAuth();
  52. } else {
  53. if (path !== pathLogin) {
  54. push ? uni.navigateTo({
  55. url: '/pages/users/login/index'
  56. }) : uni.reLaunch({
  57. url: '/pages/users/login/index'
  58. });
  59. }
  60. }
  61. // #endif
  62. // #ifdef APP-PLUS
  63. uni.navigateTo({
  64. url: '/pages/users/login/index',
  65. })
  66. // #endif
  67. }
  68. export function checkLogin() {
  69. let token = Cache.get(LOGIN_STATUS);
  70. let expiresTime = Cache.get(EXPIRES_TIME) || 0;
  71. let newTime = Math.round(new Date() / 1000);
  72. if (expiresTime < newTime || !token) {
  73. Cache.clear(LOGIN_STATUS);
  74. Cache.clear(EXPIRES_TIME);
  75. Cache.clear(USER_INFO);
  76. Cache.clear(STATE_R_KEY);
  77. return false;
  78. } else {
  79. store.commit('UPDATE_LOGIN', token);
  80. let userInfo = Cache.get(USER_INFO, true);
  81. if (userInfo) {
  82. store.commit('UPDATE_USERINFO', userInfo);
  83. }
  84. return true;
  85. }
  86. }