clearLogin.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * 清除所有登录状态的工具方法
  3. * 用于调试和解决登录状态混乱的问题
  4. */
  5. var Parse = getApp().Parse;
  6. /**
  7. * 强制清除所有登录状态
  8. */
  9. async function clearAllLoginState() {
  10. console.log('===========================================');
  11. console.log('======= 强制清除所有登录状态 =======');
  12. console.log('===========================================');
  13. try {
  14. // 1. 获取当前用户信息(用于日志)
  15. const currentUser = Parse.User.current();
  16. if (currentUser) {
  17. console.log('当前用户 ID:', currentUser.id);
  18. console.log('当前手机号:', currentUser.get('mobile') || '无');
  19. }
  20. // 2. 登出 Parse 用户(即使 token 无效也要尝试)
  21. if (currentUser) {
  22. try {
  23. await Parse.User.logOut();
  24. console.log('✅ 已登出 Parse 用户');
  25. } catch (logoutError) {
  26. // 如果登出失败(比如 token 无效),直接清除本地缓存
  27. console.log('⚠️ Parse 登出失败(可能 token 已失效),直接清除本地状态');
  28. console.log(' 错误信息:', logoutError.message);
  29. }
  30. }
  31. // 3. 清除所有本地存储(无论 Parse 登出是否成功)
  32. wx.removeStorageSync('userLogin');
  33. wx.removeStorageSync('sessionToken');
  34. wx.removeStorageSync('isGuestMode');
  35. wx.removeStorageSync('userInfo');
  36. console.log('✅ 已清除本地存储');
  37. // 4. 验证清除结果
  38. const afterUser = Parse.User.current();
  39. console.log('清除后的用户:', afterUser ? '还有用户!' : '无');
  40. console.log('清除后的 userLogin:', wx.getStorageSync('userLogin') || '无');
  41. console.log('===========================================');
  42. console.log('✅ 登录状态已完全清除');
  43. console.log('===========================================');
  44. return true;
  45. } catch (error) {
  46. console.error('❌ 清除登录状态失败:', error);
  47. // 即使出错,也尝试清除本地存储
  48. try {
  49. wx.removeStorageSync('userLogin');
  50. wx.removeStorageSync('sessionToken');
  51. wx.removeStorageSync('isGuestMode');
  52. wx.removeStorageSync('userInfo');
  53. console.log('✅ 已强制清除本地存储');
  54. } catch (e) {
  55. console.error('❌ 清除本地存储也失败:', e);
  56. }
  57. return false;
  58. }
  59. }
  60. module.exports = {
  61. clearAllLoginState
  62. };