login.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 (isWeixin()) {
  35. auth.oAuth();
  36. } else {
  37. if (path !== pathLogin) {
  38. push ? uni.navigateTo({
  39. url:'/pages/users/login/index'
  40. }) : uni.reLaunch({
  41. url: '/pages/users/login/index'
  42. });
  43. }
  44. }
  45. // #endif
  46. // #ifdef MP
  47. // #endif
  48. }
  49. export function checkLogin()
  50. {
  51. let token = Cache.get(LOGIN_STATUS);
  52. let expiresTime = Cache.get(EXPIRES_TIME);
  53. let newTime = Math.round(new Date() / 1000);
  54. if (expiresTime < newTime || !token){
  55. Cache.clear(LOGIN_STATUS);
  56. Cache.clear(EXPIRES_TIME);
  57. Cache.clear(USER_INFO);
  58. Cache.clear(STATE_R_KEY);
  59. return false;
  60. }else{
  61. store.commit('UPDATE_LOGIN',token);
  62. let userInfo = Cache.get(USER_INFO,true);
  63. if(userInfo){
  64. store.commit('UPDATE_USERINFO',userInfo);
  65. }
  66. return true;
  67. }
  68. }