修复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
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 || [];
// allocatedUserListuserNamephonenumber
@ -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)
);
// 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 => {
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 {
// 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
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 {
// 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) {
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 => {