login.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 || APP-PLUS
  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 - 1];
  27. return prePage.route;
  28. }
  29. export function toLogin(push, p1dat13li15asf11aga23ifo) {
  30. store.commit("LOGOUT");
  31. let p1dat13li15asf11aga23ifo2a = prePage();
  32. // #ifdef H5
  33. p1dat13li15asf11aga23ifo2a = location.pathname + location.search;
  34. // #endif
  35. if (!p1dat13li15asf11aga23ifo)
  36. p1dat13li15asf11aga23ifo = '/page/users/login/index'
  37. Cache.set('login_back_url', p1dat13li15asf11aga23ifo2a);
  38. // #ifdef H5
  39. if (isWeixin()) {
  40. let urlData = location.pathname + location.search
  41. if (urlData.indexOf('?') !== -1) {
  42. urlData += '&go_longin=1';
  43. } else {
  44. urlData += '?go_longin=1';
  45. }
  46. if (!Cache.has('snsapiKey')) {
  47. auth.oAuth('snsapi_base', urlData);
  48. } else {
  49. uni.navigateTo({
  50. url: '/pages/users/wechat_login/index',
  51. });
  52. }
  53. } else {
  54. uni.navigateTo({
  55. url: '/pages/users/login/index'
  56. })
  57. }
  58. // #endif
  59. // #ifdef MP
  60. uni.navigateTo({
  61. url: '/pages/users/wechat_login/index'
  62. })
  63. // #endif
  64. }
  65. export function checkLogin() {
  66. let token = Cache.get(LOGIN_STATUS);
  67. let expiresTime = Cache.get(EXPIRES_TIME);
  68. let newTime = Math.round(new Date() / 1000);
  69. if (expiresTime < newTime || !token) {
  70. Cache.clear(LOGIN_STATUS);
  71. Cache.clear(EXPIRES_TIME);
  72. Cache.clear(USER_INFO);
  73. Cache.clear(STATE_R_KEY);
  74. return false;
  75. } else {
  76. store.commit('UPDATE_LOGIN', token);
  77. let userInfo = Cache.get(USER_INFO, true);
  78. if (userInfo) {
  79. store.commit('UPDATE_USERINFO', userInfo);
  80. }
  81. return true;
  82. }
  83. }