login.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2025 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 { Debounce } from '@/utils/validate.js'
  13. // #ifdef H5 || APP-PLUS
  14. import { isWeixin } from "../utils";
  15. import auth from './wechat';
  16. // #endif
  17. import { LOGIN_STATUS, USER_INFO, EXPIRES_TIME, STATE_R_KEY, BACK_URL, GLOBAL_DATA} from './../config/cache';
  18. import {globalConfigApi} from "../api/public";
  19. import util from "../utils/util";
  20. function prePage(){
  21. let pages = getCurrentPages();
  22. let prePage = pages[pages.length - 1];
  23. return prePage.$page.fullPath;
  24. }
  25. export const toLogin = Debounce(_toLogin,800)
  26. export function _toLogin(push, pathLogin) {
  27. // 公众号登录方式(单选),1微信授权,2手机号登录/
  28. let publicLoginType = Cache.get('publicLoginType');
  29. let path = prePage();
  30. let login_back_url = Cache.get(BACK_URL);
  31. // #ifdef H5
  32. path = location.href;
  33. path = location.pathname + location.search;
  34. // #endif
  35. if(!pathLogin){
  36. pathLogin = '/page/users/login/index'
  37. Cache.set(BACK_URL,path);
  38. }
  39. // #ifdef H5
  40. if (isWeixin() && publicLoginType ==1) {
  41. let urlData = location.pathname + location.search
  42. if (urlData.indexOf('?') !== -1) {
  43. urlData += '&go_longin=1';
  44. } else {
  45. urlData += '?go_longin=1';
  46. }
  47. if (!Cache.has('snsapiKey')) {
  48. auth.oAuth('snsapi_base', urlData);
  49. } else {
  50. uni.navigateTo({
  51. url: '/pages/users/wechat_login/index'
  52. })
  53. }
  54. } else {
  55. uni.navigateTo({
  56. url: '/pages/users/login/index'
  57. })
  58. }
  59. // #endif
  60. if (['pages/user/index','/pages/user/index','/pages/order_addcart/order_addcart'].indexOf(login_back_url) == -1) {
  61. // #ifdef MP
  62. uni.navigateTo({
  63. url: '/pages/users/wechat_login/index'
  64. })
  65. // #endif
  66. // #ifdef APP-PLUS
  67. uni.showModal({
  68. title: '登录提示',
  69. content: '登录以后可体验商城完整功能',
  70. cancelColor: '#000000',
  71. showCancel: false, // 是否显示取消按钮,默认为 true
  72. confirmColor: '#526BB1',
  73. success: function (res) {
  74. if (res.confirm) {
  75. uni.navigateTo({
  76. url: '/pages/users/login/index'
  77. })
  78. } else if (res.cancel) {
  79. uni.navigateTo({
  80. url: '/pages/index/index'
  81. })
  82. }
  83. }
  84. });
  85. // #endif
  86. }
  87. }
  88. export function checkLogin()
  89. {
  90. let token = Cache.get(LOGIN_STATUS);
  91. let expiresTime = Cache.get(EXPIRES_TIME);
  92. let newTime = Math.round(new Date() / 1000);
  93. if (expiresTime < newTime || !token){
  94. Cache.clear(LOGIN_STATUS);
  95. Cache.clear(EXPIRES_TIME);
  96. Cache.clear(USER_INFO);
  97. Cache.clear(STATE_R_KEY);
  98. return false;
  99. }else{
  100. store.commit('UPDATE_LOGIN',token);
  101. let userInfo = Cache.get(USER_INFO,true);
  102. if(userInfo){
  103. store.commit('UPDATE_USERINFO',userInfo);
  104. }
  105. return true;
  106. }
  107. }