login.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. import {
  13. Debounce
  14. } from '@/utils/validate.js'
  15. // #ifdef H5 || APP-PLUS
  16. import {
  17. isWeixin
  18. } from "../utils";
  19. import auth from './wechat';
  20. // #endif
  21. import {
  22. LOGIN_STATUS,
  23. USER_INFO,
  24. EXPIRES_TIME,
  25. STATE_R_KEY
  26. } from './../config/cache';
  27. import Routine from '@/libs/routine';
  28. function prePage() {
  29. let pages = getCurrentPages();
  30. let prePage = pages[pages.length - 1];
  31. // #ifndef APP-PLUS
  32. return prePage.route;
  33. // #endif
  34. // #ifdef APP-PLUS
  35. return prePage.$page.fullPath;
  36. // #endif
  37. }
  38. export const toLogin = Debounce(_toLogin, 800)
  39. function _toLogin(push, pathLogin) {
  40. // #ifdef H5
  41. if (isWeixin()) {
  42. if (!uni.getStorageSync('authIng')) {
  43. store.commit("LOGOUT");
  44. }
  45. } else {
  46. store.commit("LOGOUT");
  47. }
  48. // #endif
  49. // #ifndef H5
  50. store.commit("LOGOUT");
  51. // #endif
  52. let path = prePage();
  53. // #ifdef H5
  54. path = location.pathname + location.search;
  55. // #endif
  56. if (!pathLogin)
  57. pathLogin = '/page/users/login/index'
  58. Cache.set('login_back_url', path);
  59. let wechatStatus = uni.getStorageSync('wechatStatus');
  60. const BASIC_CONFIG = Cache.get('BASIC_CONFIG')
  61. // #ifdef H5
  62. if (isWeixin() && wechatStatus) {
  63. let urlData = location.pathname + location.search
  64. if (urlData.indexOf('?') !== -1) {
  65. urlData += '&go_longin=1';
  66. } else {
  67. urlData += '?go_longin=1';
  68. }
  69. if (!Cache.has('snsapiKey')) {
  70. auth.oAuth('snsapi_base', urlData);
  71. } else {
  72. uni.navigateTo({
  73. url: '/pages/users/wechat_login/index',
  74. });
  75. }
  76. } else {
  77. uni.navigateTo({
  78. url: '/pages/users/login/index'
  79. })
  80. }
  81. // #endif
  82. // #ifdef MP
  83. let url
  84. console.log(BASIC_CONFIG,'BASIC_CONFIG')
  85. if (!BASIC_CONFIG.wechat_auth_switch) {
  86. // url = '/pages/users/binding_phone/index?pageType=0'
  87. url = '/pages/users/wechat_login/index'
  88. } else {
  89. url = '/pages/users/wechat_login/index'
  90. }
  91. uni.navigateTo({
  92. url
  93. })
  94. // Routine.getCode()
  95. // .then(code => {
  96. // console.log(code)
  97. // Routine.silenceAuth(code).then(res => {
  98. // console.log(res)
  99. // })
  100. // })
  101. // .catch(err => {
  102. // uni.hideLoading();
  103. // });
  104. // #endif
  105. // #ifdef APP-PLUS
  106. uni.navigateTo({
  107. url: '/pages/users/login/index'
  108. })
  109. // #endif
  110. }
  111. export function checkLogin() {
  112. let token = Cache.get(LOGIN_STATUS);
  113. let expiresTime = Cache.get(EXPIRES_TIME);
  114. let newTime = Math.round(new Date() / 1000);
  115. if (expiresTime < newTime || !token) {
  116. uni.setStorageSync('authIng', false)
  117. Cache.clear(LOGIN_STATUS);
  118. Cache.clear(EXPIRES_TIME);
  119. Cache.clear(USER_INFO);
  120. Cache.clear(STATE_R_KEY);
  121. return false;
  122. } else {
  123. store.commit('UPDATE_LOGIN', token);
  124. let userInfo = Cache.get(USER_INFO, true);
  125. if (userInfo) {
  126. store.commit('UPDATE_USERINFO', userInfo);
  127. }
  128. return true;
  129. }
  130. }