修复bug问题
This commit is contained in:
parent
d066882777
commit
13bbd8b9e7
|
|
@ -121,11 +121,11 @@
|
|||
clearable
|
||||
filterable
|
||||
multiple
|
||||
remote
|
||||
reserve-keyword
|
||||
:remote-method="searchUsers"
|
||||
:loading="userSearchLoading"
|
||||
style="width: 100%;">
|
||||
style="width: 100%;"
|
||||
@focus="handleSelectFocus"
|
||||
@change="handleUserIdsChange">
|
||||
<el-option
|
||||
v-for="user in userOptions"
|
||||
:key="user.userId"
|
||||
|
|
@ -359,6 +359,13 @@ export default {
|
|||
}
|
||||
this.doSearchUsers(keyword);
|
||||
},
|
||||
/** 下拉框获得焦点时,如果没有数据则加载 */
|
||||
handleSelectFocus() {
|
||||
// 如果 userOptions 为空,加载初始数据
|
||||
if (this.userOptions.length === 0) {
|
||||
this.searchUsers("");
|
||||
}
|
||||
},
|
||||
/** 执行用户搜索 */
|
||||
doSearchUsers(keyword) {
|
||||
this.userSearchLoading = true;
|
||||
|
|
@ -366,13 +373,17 @@ export default {
|
|||
roleId: this.studentRoleId,
|
||||
status: '0',
|
||||
pageNum: 1,
|
||||
pageSize: 100 // 每次搜索返回100条,足够选择使用
|
||||
pageSize: 200 // 增加页面大小,确保有足够的数据
|
||||
};
|
||||
// 如果有关键词,添加搜索条件
|
||||
if (keyword && keyword.trim()) {
|
||||
searchParams.userName = keyword.trim();
|
||||
searchParams.phonenumber = keyword.trim();
|
||||
}
|
||||
|
||||
// 获取已选中的用户ID列表
|
||||
const selectedUserIds = this.form.userIds || [];
|
||||
|
||||
allocatedUserList(searchParams).then(response => {
|
||||
const rows = response.rows || [];
|
||||
// 如果有关键词,进一步过滤(因为allocatedUserList可能只支持userName和phonenumber搜索)
|
||||
|
|
@ -385,16 +396,105 @@ export default {
|
|||
return userName.includes(keywordLower) || nickName.includes(keywordLower);
|
||||
});
|
||||
}
|
||||
this.userOptions = filteredRows.map(item => ({
|
||||
const searchResults = filteredRows.map(item => ({
|
||||
userId: item.userId,
|
||||
userName: item.userName,
|
||||
nickName: item.nickName
|
||||
}));
|
||||
|
||||
// 保留已选中的用户(form.userIds中的用户),即使它们不在搜索结果中
|
||||
// 这样可以确保编辑时已选中的用户不会丢失
|
||||
const existingSelectedUsers = this.userOptions.filter(u =>
|
||||
selectedUserIds.includes(u.userId)
|
||||
);
|
||||
|
||||
// 检查是否有已选中的用户ID不在当前userOptions中,需要主动加载
|
||||
const missingUserIds = selectedUserIds.filter(userId =>
|
||||
!existingSelectedUsers.find(u => u.userId === userId) &&
|
||||
!searchResults.find(u => u.userId === userId)
|
||||
);
|
||||
|
||||
// 如果有缺失的用户,需要加载它们
|
||||
if (missingUserIds.length > 0) {
|
||||
const loadPromises = missingUserIds.map(userId => {
|
||||
return getUser(userId).then(response => {
|
||||
const userData = response.data || {};
|
||||
return {
|
||||
userId: userData.userId,
|
||||
userName: userData.userName,
|
||||
nickName: userData.nickName
|
||||
};
|
||||
}).catch(() => null);
|
||||
});
|
||||
|
||||
Promise.all(loadPromises).then(loadedUsers => {
|
||||
const validLoadedUsers = loadedUsers.filter(u => u !== null);
|
||||
|
||||
// 合并搜索结果、已存在的选中用户和刚加载的用户(去重)
|
||||
const mergedOptions = [...searchResults];
|
||||
[...existingSelectedUsers, ...validLoadedUsers].forEach(selectedUser => {
|
||||
if (!mergedOptions.find(u => u.userId === selectedUser.userId)) {
|
||||
mergedOptions.push(selectedUser);
|
||||
}
|
||||
});
|
||||
|
||||
this.userOptions = mergedOptions;
|
||||
this.userSearchLoading = false;
|
||||
}).catch(() => {
|
||||
// 即使加载失败,也要合并已有的数据
|
||||
const mergedOptions = [...searchResults];
|
||||
existingSelectedUsers.forEach(selectedUser => {
|
||||
if (!mergedOptions.find(u => u.userId === selectedUser.userId)) {
|
||||
mergedOptions.push(selectedUser);
|
||||
}
|
||||
});
|
||||
this.userOptions = mergedOptions;
|
||||
this.userSearchLoading = false;
|
||||
});
|
||||
} else {
|
||||
// 没有缺失的用户,直接合并
|
||||
const mergedOptions = [...searchResults];
|
||||
existingSelectedUsers.forEach(selectedUser => {
|
||||
if (!mergedOptions.find(u => u.userId === selectedUser.userId)) {
|
||||
mergedOptions.push(selectedUser);
|
||||
}
|
||||
});
|
||||
this.userOptions = mergedOptions;
|
||||
this.userSearchLoading = false;
|
||||
}
|
||||
}).catch(error => {
|
||||
console.error("搜索学员用户失败:", error);
|
||||
this.userOptions = [];
|
||||
}).finally(() => {
|
||||
this.userSearchLoading = false;
|
||||
// 即使搜索失败,也要保留已选中的用户
|
||||
const existingSelectedUsers = this.userOptions.filter(u =>
|
||||
selectedUserIds.includes(u.userId)
|
||||
);
|
||||
// 如果已选用户不在userOptions中,尝试加载
|
||||
const missingUserIds = selectedUserIds.filter(userId =>
|
||||
!existingSelectedUsers.find(u => u.userId === userId)
|
||||
);
|
||||
if (missingUserIds.length > 0) {
|
||||
const loadPromises = missingUserIds.map(userId => {
|
||||
return getUser(userId).then(response => {
|
||||
const userData = response.data || {};
|
||||
return {
|
||||
userId: userData.userId,
|
||||
userName: userData.userName,
|
||||
nickName: userData.nickName
|
||||
};
|
||||
}).catch(() => null);
|
||||
});
|
||||
Promise.all(loadPromises).then(loadedUsers => {
|
||||
const validLoadedUsers = loadedUsers.filter(u => u !== null);
|
||||
this.userOptions = [...existingSelectedUsers, ...validLoadedUsers];
|
||||
this.userSearchLoading = false;
|
||||
}).catch(() => {
|
||||
this.userOptions = existingSelectedUsers;
|
||||
this.userSearchLoading = false;
|
||||
});
|
||||
} else {
|
||||
this.userOptions = existingSelectedUsers;
|
||||
this.userSearchLoading = false;
|
||||
}
|
||||
});
|
||||
},
|
||||
/** 获取学员角色ID */
|
||||
|
|
@ -474,6 +574,10 @@ export default {
|
|||
/** 新增按钮操作 */
|
||||
handleAdd() {
|
||||
this.reset();
|
||||
// 确保 userOptions 有初始数据
|
||||
if (this.userOptions.length === 0) {
|
||||
this.searchUsers("");
|
||||
}
|
||||
this.open = true;
|
||||
this.title = "添加量表权限";
|
||||
},
|
||||
|
|
@ -492,63 +596,90 @@ export default {
|
|||
getPermission(permissionIds[0]).then(response => {
|
||||
this.form = response.data;
|
||||
|
||||
// 如果有汇总的用户信息,使用汇总的信息
|
||||
// 注意:如果同时有"所有用户"和具体用户权限,优先使用具体用户(因为"所有用户"权限是多余的)
|
||||
if (row.userIds && row.userIds.length > 0) {
|
||||
// 有具体用户,使用具体用户列表
|
||||
this.form.userIds = row.userIds;
|
||||
// 加载这些用户到选项列表中
|
||||
this.loadSelectedUsers(row.userIds);
|
||||
} else if (row.hasAllUsers) {
|
||||
// 只有"所有用户"权限,没有具体用户
|
||||
this.form.userIds = [];
|
||||
} else {
|
||||
// 兼容处理:如果form中有userId,转换为userIds数组
|
||||
if (this.form.userId && !this.form.userIds) {
|
||||
this.form.userIds = [this.form.userId];
|
||||
} else if (!this.form.userIds) {
|
||||
this.form.userIds = [];
|
||||
}
|
||||
// 如果编辑时选择了用户,需要加载这些用户到选项列表中
|
||||
if (this.form.userIds && this.form.userIds.length > 0) {
|
||||
this.loadSelectedUsers(this.form.userIds);
|
||||
}
|
||||
}
|
||||
|
||||
// 存储所有相关的权限ID,用于提交时删除
|
||||
this.form._permissionIds = permissionIds;
|
||||
this.form._groupKey = row._groupKey;
|
||||
|
||||
this.open = true;
|
||||
this.title = "修改量表权限";
|
||||
// 确定要加载的用户ID列表
|
||||
let userIdsToLoad = [];
|
||||
if (row.userIds && row.userIds.length > 0) {
|
||||
// 有具体用户,使用具体用户列表(确保是数组)
|
||||
userIdsToLoad = Array.isArray(row.userIds) ? [...row.userIds] : [row.userIds];
|
||||
} else if (row.hasAllUsers) {
|
||||
// 只有"所有用户"权限,没有具体用户
|
||||
userIdsToLoad = [];
|
||||
} else {
|
||||
// 兼容处理:如果form中有userId,转换为userIds数组
|
||||
if (this.form.userId) {
|
||||
userIdsToLoad = [this.form.userId];
|
||||
} else {
|
||||
userIdsToLoad = [];
|
||||
}
|
||||
}
|
||||
|
||||
// 使用 Vue.set 确保响应式
|
||||
this.$set(this.form, 'userIds', userIdsToLoad);
|
||||
|
||||
// 如果有用户需要加载,先加载用户信息到选项列表
|
||||
if (userIdsToLoad.length > 0) {
|
||||
this.loadSelectedUsers(userIdsToLoad).then(() => {
|
||||
// 确保 userOptions 已更新后再打开对话框
|
||||
this.$nextTick(() => {
|
||||
this.open = true;
|
||||
this.title = "修改量表权限";
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// 没有用户,直接打开对话框
|
||||
this.open = true;
|
||||
this.title = "修改量表权限";
|
||||
}
|
||||
});
|
||||
},
|
||||
/** 加载已选中的用户信息到选项列表 */
|
||||
loadSelectedUsers(userIds) {
|
||||
if (!userIds || userIds.length === 0) {
|
||||
return;
|
||||
}
|
||||
// 为每个用户ID获取用户信息
|
||||
const promises = userIds.map(userId => {
|
||||
return getUser(userId).then(response => {
|
||||
const userData = response.data || {};
|
||||
return {
|
||||
userId: userData.userId,
|
||||
userName: userData.userName,
|
||||
nickName: userData.nickName
|
||||
};
|
||||
}).catch(() => null);
|
||||
});
|
||||
Promise.all(promises).then(users => {
|
||||
const validUsers = users.filter(u => u !== null);
|
||||
// 将已选用户添加到选项列表(避免重复)
|
||||
validUsers.forEach(user => {
|
||||
if (!this.userOptions.find(u => u.userId === user.userId)) {
|
||||
this.userOptions.push(user);
|
||||
}
|
||||
return new Promise((resolve) => {
|
||||
if (!userIds || userIds.length === 0) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
// 为每个用户ID获取用户信息
|
||||
const promises = userIds.map(userId => {
|
||||
return getUser(userId).then(response => {
|
||||
const userData = response.data || {};
|
||||
return {
|
||||
userId: userData.userId,
|
||||
userName: userData.userName,
|
||||
nickName: userData.nickName
|
||||
};
|
||||
}).catch(() => null);
|
||||
});
|
||||
Promise.all(promises).then(users => {
|
||||
const validUsers = users.filter(u => u !== null);
|
||||
// 将已选用户添加到选项列表(避免重复)
|
||||
validUsers.forEach(user => {
|
||||
if (!this.userOptions.find(u => u.userId === user.userId)) {
|
||||
this.userOptions.push(user);
|
||||
}
|
||||
});
|
||||
// 确保 userOptions 是响应式的
|
||||
this.$forceUpdate();
|
||||
resolve();
|
||||
}).catch(() => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
},
|
||||
/** 用户选择变化事件 */
|
||||
handleUserIdsChange(value) {
|
||||
// 确保 form.userIds 是数组
|
||||
if (!Array.isArray(value)) {
|
||||
this.form.userIds = value ? [value] : [];
|
||||
} else {
|
||||
this.form.userIds = value;
|
||||
}
|
||||
console.log("用户选择变化:", this.form.userIds);
|
||||
},
|
||||
/** 提交按钮 */
|
||||
submitForm() {
|
||||
this.$refs["form"].validate(valid => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user