login.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import store from "../store";
  2. import Cache from '../utils/cache';
  3. // #ifdef H5 || APP-PLUS
  4. import { isWeixin } from "../utils";
  5. import auth from './wechat';
  6. // #endif
  7. import { LOGIN_STATUS, USER_INFO, EXPIRES_TIME, STATE_R_KEY} from './../config/cache';
  8. function prePage(){
  9. let pages = getCurrentPages();
  10. let prePage = pages[pages.length - 2];
  11. // #ifdef H5
  12. return prePage;
  13. // #endif
  14. return prePage.$vm;
  15. }
  16. export function toLogin(push, pathLogin) {
  17. store.commit("LOGOUT");
  18. let path = prePage();
  19. if(path){
  20. path = path.router;
  21. if(path == undefined){
  22. path = location.pathname;
  23. }
  24. }
  25. // #ifdef H5
  26. else{
  27. path = location.pathname;
  28. }
  29. // #endif
  30. if(!pathLogin)
  31. pathLogin = '/page/users/login/index'
  32. Cache.set('login_back_url',path);
  33. // #ifdef H5 || APP-PLUS
  34. if (path !== pathLogin) {
  35. push ? uni.navigateTo({
  36. url:'/pages/users/login/index'
  37. }) : uni.reLaunch({
  38. url: '/pages/users/login/index'
  39. });
  40. }
  41. // #endif
  42. // #ifdef MP
  43. // #endif
  44. }
  45. export function checkLogin()
  46. {
  47. let token = Cache.get(LOGIN_STATUS);
  48. let expiresTime = Cache.get(EXPIRES_TIME);
  49. let newTime = Math.round(new Date() / 1000);
  50. if (expiresTime < newTime || !token){
  51. Cache.clear(LOGIN_STATUS);
  52. Cache.clear(EXPIRES_TIME);
  53. Cache.clear(USER_INFO);
  54. Cache.clear(STATE_R_KEY);
  55. return false;
  56. }else{
  57. store.commit('UPDATE_LOGIN',token);
  58. let userInfo = Cache.get(USER_INFO,true);
  59. if(userInfo){
  60. store.commit('UPDATE_USERINFO',userInfo);
  61. }
  62. return true;
  63. }
  64. }