login.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import store from "../store";
  2. import Cache from '../utils/cache';
  3. import { Debounce } from '@/utils/validate.js'
  4. // #ifdef H5
  5. import { isWeixin } from "../utils";
  6. import auth from './wechat';
  7. // #endif
  8. import { LOGIN_STATUS, USER_INFO, EXPIRES_TIME, STATE_R_KEY, BACK_URL} from './../config/cache';
  9. function prePage(){
  10. let pages = getCurrentPages();
  11. let prePage = pages[pages.length - 1];
  12. return prePage.route;
  13. }
  14. export const toLogin = Debounce(_toLogin,800)
  15. export function _toLogin(push, pathLogin) {
  16. console.log(111)
  17. store.commit("LOGOUT");
  18. let path = prePage();
  19. let login_back_url = Cache.get(BACK_URL);
  20. // #ifdef H5
  21. // path = location.href;
  22. path = location.pathname + location.search;
  23. // #endif
  24. console.log(222)
  25. if(!pathLogin){
  26. pathLogin = '/page/users/login/index'
  27. Cache.set('login_back_url',path);
  28. }
  29. // #ifdef H5
  30. if (isWeixin()) {
  31. let urlData = location.pathname + location.search
  32. if (!Cache.has('snsapiKey')) {
  33. auth.oAuth('snsapi_base', urlData);
  34. } else {
  35. if (['/pages/user/index'].indexOf(login_back_url) === -1) {
  36. uni.navigateTo({
  37. // url: '/pages/users/wechat_login/index'
  38. url: '/pages/users/login/index'
  39. })
  40. }
  41. }
  42. } else {
  43. if (['/pages/user/index'].indexOf(login_back_url) === -1) {
  44. uni.navigateTo({
  45. url: '/pages/users/login/index'
  46. })
  47. }
  48. }
  49. // #endif
  50. console.log(333,login_back_url)
  51. if (['pages/user/index','/pages/user/index'].indexOf(login_back_url) === -1) {
  52. // #ifdef APP-PLUS || APP ||MP
  53. console.log(444)
  54. uni.navigateTo({
  55. // url: '/pages/users/wechat_login/index'
  56. // url: '/pages/users/login/index'
  57. })
  58. // #endif
  59. }
  60. }
  61. export function checkLogin()
  62. {
  63. let token = Cache.get(LOGIN_STATUS);
  64. let expiresTime = Cache.get(EXPIRES_TIME);
  65. let newTime = Math.round(new Date() / 1000);
  66. if (expiresTime < newTime || !token){
  67. Cache.clear(LOGIN_STATUS);
  68. Cache.clear(EXPIRES_TIME);
  69. Cache.clear(USER_INFO);
  70. Cache.clear(STATE_R_KEY);
  71. return false;
  72. }else{
  73. store.commit('UPDATE_LOGIN',token);
  74. let userInfo = Cache.get(USER_INFO,true);
  75. if(userInfo){
  76. store.commit('UPDATE_USERINFO',userInfo);
  77. }
  78. return true;
  79. }
  80. }