Files
zyt/DEBUG_WEB_TO_MINIPROGRAM.md
T
2026-03-04 15:32:30 +08:00

243 lines
5.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Web端呼叫小程序调试指南
## 当前状态
- ✅ 小程序呼叫Web端:正常
- ❌ Web端呼叫小程序:不通
## 调试步骤
### 步骤1: 检查后端返回的数据
打开浏览器控制台,查看发起通话时的日志:
```
=== 发起一对一通话 ===
后端返回的 patientUserId: patient_1
前端传入的 userId: patient_1
最终使用的 targetUserId: patient_1
完整的 res 对象: { sdkAppId: 1400xxx, userId: 'doctor_1', userSig: 'xxx', patientUserId: 'patient_1' }
```
**检查点:**
1. `res.patientUserId` 是否存在?
2. `res.patientUserId` 的格式是否为 `patient_{id}`
3. 是否与小程序端登录时使用的 userId 一致?
### 步骤2: 检查小程序端的 userId
在小程序端,查看登录时使用的 userId:
```javascript
// 小程序端代码
console.log('小程序登录的 userId:', userId)
// 应该输出: patient_1
```
**检查点:**
1. 小程序端的 userId 格式是否为 `patient_{id}`
2. 是否与Web端发起通话时使用的 userId 完全一致?
### 步骤3: 检查小程序端是否在线
**方法1: 查看小程序端日志**
```javascript
// 小程序端应该有类似的日志
console.log('IM登录成功')
console.log('TUICallKit初始化成功')
```
**方法2: 查看Web端错误码**
- 如果返回 `60011` 错误:说明小程序端不在线
- 如果返回 `60010` 错误:说明小程序端忙线中
- 如果返回 `60008` 错误:说明小程序端拒绝了通话
### 步骤4: 检查后端代码
确认后端是否正确返回 `patientUserId`
```php
// server/app/adminapi/logic/tcm/DiagnosisLogic.php
// getCallSignature 方法应该返回:
return [
'sdkAppId' => (int)$config['sdkAppId'],
'userId' => $doctorUserId, // 医生的userId
'userSig' => $userSig,
'patientUserId' => $patientUserId, // 患者的userId ← 必须有这个
'expireTime' => 86400
];
```
### 步骤5: 测试后端API
使用Postman或浏览器直接测试后端API:
```bash
POST /adminapi/tcm.diagnosis/getCallSignature
Content-Type: application/json
{
"diagnosis_id": 1,
"patient_id": 1
}
```
**预期响应:**
```json
{
"code": 1,
"msg": "success",
"data": {
"sdkAppId": 1400xxx,
"userId": "doctor_1",
"userSig": "xxx",
"patientUserId": "patient_1", 必须有这个字段
"expireTime": 86400
}
}
```
### 步骤6: 检查小程序端初始化代码
小程序端必须正确初始化:
```javascript
// 小程序端 app.js 或页面代码
import { TUICallKit } from '@tencentcloud/call-uikit-wechat'
// 1. 获取签名
const res = await wx.request({
url: 'https://your-api.com/getPatientSignature',
data: { patient_id: 1 }
})
// 2. 初始化
await TUICallKit.init({
SDKAppID: res.data.sdkAppId,
userID: res.data.userId, // 必须是 patient_1 格式
userSig: res.data.userSig
})
// 3. 监听来电
TUICallKit.on('onCallReceived', (event) => {
console.log('收到来电', event)
// 显示来电界面
})
```
## 常见问题
### 问题1: 后端没有返回 patientUserId
**症状:**
```
后端返回的 patientUserId: undefined
前端传入的 userId: patient_1
最终使用的 targetUserId: patient_1
```
**解决方案:**
1. 检查后端代码是否已更新
2. 清除后端缓存:`php think clear`
3. 重启后端服务
### 问题2: userId 格式不一致
**症状:**
- Web端使用: `patient_1`
- 小程序端使用: `1``user_1`
**解决方案:**
统一使用 `patient_{id}` 格式
### 问题3: 小程序端未登录IM
**症状:**
- Web端发起通话后,返回 60011 错误(用户不在线)
- 小程序端没有收到任何通知
**解决方案:**
1. 确保小程序端在启动时调用了 `TUICallKit.init()`
2. 确保小程序端保持在前台运行
3. 检查小程序端的网络连接
### 问题4: SDKAppID 不一致
**症状:**
- Web端和小程序端使用了不同的 SDKAppID
**解决方案:**
确保Web端和小程序端使用相同的 SDKAppID
## 完整的调试日志示例
### 正常情况(应该看到的日志)
**Web端:**
```
签名获取成功: { doctorUserId: 'doctor_1', patientUserId: 'patient_1', sdkAppId: 1400xxx }
TUICallKit init 方法调用完成
初始化完成,显示通话界面
=== 发起一对一通话 ===
后端返回的 patientUserId: patient_1
前端传入的 userId: patient_1
最终使用的 targetUserId: patient_1
通话已发起,目标用户: patient_1
```
**小程序端:**
```
IM登录成功
TUICallKit初始化成功
收到来电: { inviter: 'doctor_1', inviteeList: ['patient_1'], callType: 1 }
```
### 异常情况(问题日志)
**情况1: 后端未返回 patientUserId**
```
后端返回的 patientUserId: undefined
前端传入的 userId: patient_1
最终使用的 targetUserId: patient_1
完整的 res 对象: { sdkAppId: 1400xxx, userId: 'doctor_1', userSig: 'xxx' }
↑ 缺少 patientUserId
```
**情况2: 小程序端不在线**
```
发起通话失败: Error: 对方不在线,无法发起通话
错误码: 60011
```
**情况3: userId 格式不匹配**
```
最终使用的 targetUserId: patient_1
小程序端登录的 userId: 1 ← 格式不一致
```
## 解决方案总结
1. **确保后端返回 patientUserId**
- 修改 `getCallSignature` 方法
- 返回 `patientUserId` 字段
2. **统一 userId 格式**
- Web端:`patient_{id}`
- 小程序端:`patient_{id}`
3. **确保小程序端在线**
- 小程序启动时初始化 TUICallKit
- 保持小程序在前台运行
4. **添加详细日志**
- Web端:查看控制台日志
- 小程序端:查看微信开发者工具控制台
## 下一步
1. 打开浏览器控制台
2. 发起一次通话
3. 查看日志输出
4. 根据日志判断问题所在
5. 按照上述解决方案修复