login.js 3.2 KB

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