hejianhao
2026-02-24 b28f7ed61cb6fc7a9b9712299164629e052a40da
fix: 修复WebSocket认证和请求头中默认UID的处理

修复WebSocket连接时认证消息中硬编码的appUserId,改为使用从存储获取的UID或默认值'-1',保持与普通HTTP请求头中UID处理逻辑的一致性,避免因UID获取失败导致认证异常。
2个文件已修改
12 ■■■■■ 已修改文件
src/utils/request.ts 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/utils/websocket.ts 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/utils/request.ts
@@ -150,7 +150,7 @@
  header['XP-Nonce'] = getStamp();
  // 获取uid
  const uid = await getStorage(_UID) || di;
  const uid = await getStorage(_UID) || '-1';
  header['XP-Uid'] = uid.toString();
  // 获取设备id
src/utils/websocket.ts
@@ -1,5 +1,5 @@
import Taro from '@tarojs/taro';
import { getOrCreateLocalId, getStorage } from './tools';
import { getStorage } from './tools';
import { _UID } from '../constants/api';
type MessageCallback = (data: any) => void;
@@ -39,10 +39,10 @@
    this.url = url;
    console.log(`正在连接 WebSocket: ${url}`);
    const uid = await getStorage(_UID) || await getOrCreateLocalId();
    const uid = await getStorage(_UID) || '-1';
    const task = Taro.connectSocket({
      header: {
        'XP-Uid': uid.toString(),
        'XP-Uid': 1 || uid.toString(),
      },
      url: this.url,
      success: () => { },
@@ -87,8 +87,8 @@
        this.isConnected = true;
        this.reconnectAttempts = 0;
        this.startHeartbeat();
        const uid = await getStorage(_UID) || await getOrCreateLocalId();
        this.send({ type: 'auth', appUserId: uid, timestamp: Date.now() });
        const uid = await getStorage(_UID) || '-1';
        this.send({ type: 'auth', appUserId: '12345678', timestamp: Date.now() });
        this.notifyListeners('open', { status: 'connected' });
      });