在首页点击"绑定车辆"按钮时,调用接口 /system/vehicle/user/bound/undefined,导致 userId 为 undefined,无法正确获取用户绑定的车辆信息。
在 pages/index.vue 中:
缺少 currentUser 计算属性
javascript computed: { ...mapState({ userName: state => state.user.name // ❌ 缺少 currentUser 映射 }) }
错误使用 currentUser.userId
javascript loadRunningTasks() { const queryParams = { assigneeId: this.currentUser.userId, // ❌ currentUser 未定义 } }
loadUserProfile() - 调用 getUserProfile API(不需要 userId 参数)✅ 正常loadRunningTasks() - 使用 this.currentUser.userId ❌ 报错文件路径:app/pages/index.vue
修改前:javascript computed: { ...mapState({ userName: state => state.user.name }), // ... 其他计算属性 }
修改后:javascript computed: { ...mapState({ userName: state => state.user.name, currentUser: state => state.user // ✅ 添加 currentUser 映射 }), // ... 其他计算属性 }
修改前:javascript loadUserProfile() { getUserProfile().then(response => { const userInfo = response.data || response if (userInfo.boundVehicle) { this.boundVehicle = userInfo.boundVehicle.vehicleNumber this.boundVehicleId = userInfo.boundVehicle.vehicleId } }).catch(error => { console.error('获取用户信息失败:', error) }) }
修改后:
```javascript
loadUserProfile() {
const userId = this.currentUser.userId
if (!userId) {
console.error('用户未登录,无法获取绑定车辆信息')
return
}
getUserProfile().then(response => {
const userInfo = response.data || response
if (userInfo.boundVehicle) {
this.boundVehicle = userInfo.boundVehicle.vehicleNumber
this.boundVehicleId = userInfo.boundVehicle.vehicleId
}
}).catch(error => {
console.error('获取用户信息失败:', error)
})
}
```
改进点:
- ✅ 添加用户登录状态检查
- ✅ 提前返回,避免无效的 API 调用
修改前:javascript loadRunningTasks() { this.loading = true const queryParams = { pageNum: 1, pageSize: 100, assigneeId: this.currentUser.userId, // ❌ currentUser 未定义 taskStatuses: 'PENDING,DEPARTING,ARRIVED,RETURNING,IN_PROGRESS' } // ... }
修改后:
```javascript
loadRunningTasks() {
const userId = this.currentUser.userId
if (!userId) {
console.error('用户未登录,无法加载任务列表')
return
}
this.loading = true
const queryParams = {
pageNum: 1,
pageSize: 100,
assigneeId: userId, // ✅ 使用已验证的 userId
taskStatuses: 'PENDING,DEPARTING,ARRIVED,RETURNING,IN_PROGRESS'
}
// ...
}
```
改进点:
- ✅ 提取 userId 到独立变量
- ✅ 添加用户登录状态检查
- ✅ 提前返回,避免无效的 API 调用
- ✅ 代码更清晰易读
根据项目中的 Vuex 配置,state.user 包含以下信息:
{
userId: 1, // 用户ID
name: '张三', // 用户名
avatar: '', // 头像
deptId: 101, // 部门ID
roles: [], // 角色列表
permissions: [] // 权限列表
}
computed: {
...mapState({
currentUser: state => state.user
})
}
// 使用时:
this.currentUser.userId
this.currentUser.name
this.currentUser.deptId
computed: {
...mapState({
userId: state => state.user.userId,
userName: state => state.user.name,
userDeptId: state => state.user.deptId
})
}
// 使用时:
this.userId
this.userName
this.userDeptId
推荐方式1,因为:
- ✅ 代码更简洁
- ✅ 易于扩展(需要新属性时不用修改 computed)
- ✅ 与其他页面保持一致(如 bind-vehicle.vue)
bind-vehicle.vue 中使用了相同的模式:
computed: {
...mapState({
currentUser: state => state.user
})
},
methods: {
loadCurrentBoundVehicle() {
const userId = this.currentUser.userId
getUserBoundVehicle(userId).then(response => {
// ...
})
}
}
这是本次修复的参考标准。
修复前: GET /system/vehicle/user/bound/undefined ❌ 错误
修复后: GET /system/vehicle/user/bound/1 ✅ 正确
在所有页面中,需要使用用户信息时,统一采用以下方式:
export default {
computed: {
...mapState({
currentUser: state => state.user
})
},
methods: {
someMethod() {
const userId = this.currentUser.userId
if (!userId) {
console.error('用户未登录')
return
}
// 继续业务逻辑
}
}
}
在所有需要用户信息的方法中,添加登录状态检查:
const userId = this.currentUser.userId
if (!userId) {
console.error('用户未登录,无法执行操作')
return
}
可以添加 ESLint 规则检测未定义的变量:
{
"rules": {
"no-undef": "error",
"no-unused-vars": "warn"
}
}
app/pages/index.vue - 首页app/pages/bind-vehicle.vue - 绑定车辆页面(正确实现示例)app/pages/mine/index.vue - 个人中心(正确实现示例)app/api/vehicle.js - 车辆相关 API本次修复通过以下步骤解决了 userId 为 undefined 的问题:
computed 中添加 currentUser 映射loadUserProfile() 中添加用户登录检查loadRunningTasks() 中提取并验证 userIdthis.currentUser.userId 获取用户ID核心改进:
- 代码更健壮,增加了错误检查
- 遵循了项目中其他页面的最佳实践
- 避免了无效的 API 调用
- 提供了更好的错误提示
| 版本 | 日期 | 修改内容 | 修改人 |
|---|
| 1.0 | 2025-10-15 | 修复 userId 为 undefined 的问题 | - |