login.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2024 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. if(Cache.get('WECHAT_APPID')){
  52. uni.navigateTo({
  53. url: '/pages/users/wechat_login/index',
  54. });
  55. }else{
  56. return uni.navigateTo({
  57. url:'/pages/users/login/index'
  58. })
  59. }
  60. } else {
  61. if (path !== pathLogin) {
  62. push ? uni.navigateTo({
  63. url: '/pages/users/login/index'
  64. }) : uni.reLaunch({
  65. url: '/pages/users/login/index'
  66. });
  67. }
  68. }
  69. // #endif
  70. // #ifdef MP
  71. let url
  72. uni.navigateTo({
  73. url: '/pages/users/wechat_login/index'
  74. })
  75. // #endif
  76. // #ifdef APP-PLUS
  77. uni.navigateTo({
  78. url: '/pages/users/login/index',
  79. })
  80. // #endif
  81. }
  82. export function checkLogin() {
  83. let token = Cache.get(LOGIN_STATUS);
  84. let expiresTime = Cache.get(EXPIRES_TIME) || 0;
  85. let newTime = Math.round(new Date() / 1000);
  86. if (expiresTime < newTime || !token) {
  87. Cache.clear(LOGIN_STATUS);
  88. Cache.clear(EXPIRES_TIME);
  89. Cache.clear(USER_INFO);
  90. Cache.clear(STATE_R_KEY);
  91. return false;
  92. } else {
  93. store.commit('UPDATE_LOGIN', token);
  94. let userInfo = Cache.get(USER_INFO, true);
  95. if (userInfo) {
  96. store.commit('UPDATE_USERINFO', userInfo);
  97. }
  98. return true;
  99. }
  100. }