修复bug问题

This commit is contained in:
胡圣锋 2025-11-23 18:53:46 +08:00
parent d066882777
commit 13bbd8b9e7

View File

@ -121,11 +121,11 @@
clearable clearable
filterable filterable
multiple multiple
remote
reserve-keyword reserve-keyword
:remote-method="searchUsers"
:loading="userSearchLoading" :loading="userSearchLoading"
style="width: 100%;"> style="width: 100%;"
@focus="handleSelectFocus"
@change="handleUserIdsChange">
<el-option <el-option
v-for="user in userOptions" v-for="user in userOptions"
:key="user.userId" :key="user.userId"
@ -359,6 +359,13 @@ export default {
} }
this.doSearchUsers(keyword); this.doSearchUsers(keyword);
}, },
/** 下拉框获得焦点时,如果没有数据则加载 */
handleSelectFocus() {
// userOptions
if (this.userOptions.length === 0) {
this.searchUsers("");
}
},
/** 执行用户搜索 */ /** 执行用户搜索 */
doSearchUsers(keyword) { doSearchUsers(keyword) {
this.userSearchLoading = true; this.userSearchLoading = true;
@ -366,13 +373,17 @@ export default {
roleId: this.studentRoleId, roleId: this.studentRoleId,
status: '0', status: '0',
pageNum: 1, pageNum: 1,
pageSize: 100 // 100使 pageSize: 200 //
}; };
// //
if (keyword && keyword.trim()) { if (keyword && keyword.trim()) {
searchParams.userName = keyword.trim(); searchParams.userName = keyword.trim();
searchParams.phonenumber = keyword.trim(); searchParams.phonenumber = keyword.trim();
} }
// ID
const selectedUserIds = this.form.userIds || [];
allocatedUserList(searchParams).then(response => { allocatedUserList(searchParams).then(response => {
const rows = response.rows || []; const rows = response.rows || [];
// allocatedUserListuserNamephonenumber // allocatedUserListuserNamephonenumber
@ -385,16 +396,105 @@ export default {
return userName.includes(keywordLower) || nickName.includes(keywordLower); return userName.includes(keywordLower) || nickName.includes(keywordLower);
}); });
} }
this.userOptions = filteredRows.map(item => ({ const searchResults = filteredRows.map(item => ({
userId: item.userId, userId: item.userId,
userName: item.userName, userName: item.userName,
nickName: item.nickName nickName: item.nickName
})); }));
// form.userIds使
//
const existingSelectedUsers = this.userOptions.filter(u =>
selectedUserIds.includes(u.userId)
);
// IDuserOptions
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 => { }).catch(error => {
console.error("搜索学员用户失败:", error); console.error("搜索学员用户失败:", error);
this.userOptions = []; // 使
}).finally(() => { const existingSelectedUsers = this.userOptions.filter(u =>
this.userSearchLoading = false; 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 */ /** 获取学员角色ID */
@ -474,6 +574,10 @@ export default {
/** 新增按钮操作 */ /** 新增按钮操作 */
handleAdd() { handleAdd() {
this.reset(); this.reset();
// userOptions
if (this.userOptions.length === 0) {
this.searchUsers("");
}
this.open = true; this.open = true;
this.title = "添加量表权限"; this.title = "添加量表权限";
}, },
@ -492,63 +596,90 @@ export default {
getPermission(permissionIds[0]).then(response => { getPermission(permissionIds[0]).then(response => {
this.form = response.data; 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 {
// formuserIduserIds
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 // ID
this.form._permissionIds = permissionIds; this.form._permissionIds = permissionIds;
this.form._groupKey = row._groupKey; this.form._groupKey = row._groupKey;
this.open = true; // ID
this.title = "修改量表权限"; let userIdsToLoad = [];
if (row.userIds && row.userIds.length > 0) {
// 使
userIdsToLoad = Array.isArray(row.userIds) ? [...row.userIds] : [row.userIds];
} else if (row.hasAllUsers) {
// ""
userIdsToLoad = [];
} else {
// formuserIduserIds
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) { loadSelectedUsers(userIds) {
if (!userIds || userIds.length === 0) { return new Promise((resolve) => {
return; if (!userIds || userIds.length === 0) {
} resolve();
// ID return;
const promises = userIds.map(userId => { }
return getUser(userId).then(response => { // ID
const userData = response.data || {}; const promises = userIds.map(userId => {
return { return getUser(userId).then(response => {
userId: userData.userId, const userData = response.data || {};
userName: userData.userName, return {
nickName: userData.nickName userId: userData.userId,
}; userName: userData.userName,
}).catch(() => null); nickName: userData.nickName
}); };
Promise.all(promises).then(users => { }).catch(() => null);
const validUsers = users.filter(u => u !== null); });
// Promise.all(promises).then(users => {
validUsers.forEach(user => { const validUsers = users.filter(u => u !== null);
if (!this.userOptions.find(u => u.userId === user.userId)) { //
this.userOptions.push(user); 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() { submitForm() {
this.$refs["form"].validate(valid => { this.$refs["form"].validate(valid => {