Codex Assistant
22 小时以前 afeeed281e60466b576fbe74d339634cc5d07b82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// 清理无效token的脚本
// 这个脚本需要在小程序开发者工具的控制台中运行
 
console.log('🧹 开始清理无效的JWT token...');
 
// 检查当前存储的token
const currentToken = wx.getStorageSync('token');
console.log('当前token:', currentToken ? `${currentToken.substring(0, 20)}...` : '无');
 
// 检查token格式是否有效
function isValidJWTFormat(token) {
  if (!token || typeof token !== 'string') {
    return false;
  }
  
  // JWT应该有3个部分,用.分隔
  const parts = token.split('.');
  if (parts.length !== 3) {
    console.log('❌ Token格式无效:不是3个部分');
    return false;
  }
  
  // 检查是否包含测试用的无效签名
  if (token.includes('invalid_token')) {
    console.log('❌ 检测到测试用的无效token');
    return false;
  }
  
  try {
    // 尝试解码header和payload
    const header = JSON.parse(atob(parts[0]));
    const payload = JSON.parse(atob(parts[1]));
    
    console.log('Token header:', header);
    console.log('Token payload:', payload);
    
    // 检查是否过期
    const now = Math.floor(Date.now() / 1000);
    if (payload.exp && payload.exp < now) {
      console.log('❌ Token已过期');
      return false;
    }
    
    console.log('✅ Token格式有效');
    return true;
  } catch (e) {
    console.log('❌ Token解码失败:', e.message);
    return false;
  }
}
 
// 清理无效token
if (currentToken) {
  if (!isValidJWTFormat(currentToken)) {
    console.log('🗑️ 清理无效token...');
    
    // 清除存储的认证信息
    wx.removeStorageSync('token');
    wx.removeStorageSync('userInfo');
    wx.removeStorageSync('sessionKey');
    
    // 清除globalData中的认证信息
    const app = getApp();
    if (app) {
      app.globalData.token = null;
      app.globalData.userInfo = null;
      app.globalData.sessionKey = null;
    }
    
    console.log('✅ 无效token已清理');
    console.log('💡 建议重新启动小程序以获取新的有效token');
  } else {
    console.log('✅ 当前token有效,无需清理');
  }
} else {
  console.log('ℹ️ 当前没有存储token');
}
 
console.log('🎉 清理完成!');
 
// 使用说明
console.log('\n📋 使用说明:');
console.log('1. 在小程序开发者工具中打开控制台');
console.log('2. 复制并粘贴这段代码');
console.log('3. 按回车执行');
console.log('4. 如果清理了无效token,请重新启动小程序');