| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- /**
- * 清除所有登录状态的工具方法
- * 用于调试和解决登录状态混乱的问题
- */
- var Parse = getApp().Parse;
- /**
- * 强制清除所有登录状态
- */
- async function clearAllLoginState() {
- console.log('===========================================');
- console.log('======= 强制清除所有登录状态 =======');
- console.log('===========================================');
-
- try {
- // 1. 获取当前用户信息(用于日志)
- const currentUser = Parse.User.current();
- if (currentUser) {
- console.log('当前用户 ID:', currentUser.id);
- console.log('当前手机号:', currentUser.get('mobile') || '无');
- }
-
- // 2. 登出 Parse 用户(即使 token 无效也要尝试)
- if (currentUser) {
- try {
- await Parse.User.logOut();
- console.log('✅ 已登出 Parse 用户');
- } catch (logoutError) {
- // 如果登出失败(比如 token 无效),直接清除本地缓存
- console.log('⚠️ Parse 登出失败(可能 token 已失效),直接清除本地状态');
- console.log(' 错误信息:', logoutError.message);
- }
- }
-
- // 3. 清除所有本地存储(无论 Parse 登出是否成功)
- wx.removeStorageSync('userLogin');
- wx.removeStorageSync('sessionToken');
- wx.removeStorageSync('isGuestMode');
- wx.removeStorageSync('userInfo');
- console.log('✅ 已清除本地存储');
-
- // 4. 验证清除结果
- const afterUser = Parse.User.current();
- console.log('清除后的用户:', afterUser ? '还有用户!' : '无');
- console.log('清除后的 userLogin:', wx.getStorageSync('userLogin') || '无');
-
- console.log('===========================================');
- console.log('✅ 登录状态已完全清除');
- console.log('===========================================');
-
- return true;
- } catch (error) {
- console.error('❌ 清除登录状态失败:', error);
-
- // 即使出错,也尝试清除本地存储
- try {
- wx.removeStorageSync('userLogin');
- wx.removeStorageSync('sessionToken');
- wx.removeStorageSync('isGuestMode');
- wx.removeStorageSync('userInfo');
- console.log('✅ 已强制清除本地存储');
- } catch (e) {
- console.error('❌ 清除本地存储也失败:', e);
- }
-
- return false;
- }
- }
- module.exports = {
- clearAllLoginState
- };
|