合并远程更改:更新权限管理功能,支持多用户和远程搜索
This commit is contained in:
commit
343cb8c76d
|
|
@ -6,7 +6,7 @@ spring:
|
||||||
druid:
|
druid:
|
||||||
# 主库数据源
|
# 主库数据源
|
||||||
master:
|
master:
|
||||||
url: jdbc:mysql://1.15.149.240:3306/ry_xinli?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
url: jdbc:mysql://127.0.0.1:3306/ry_xinli?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||||
username: ry_xinli
|
username: ry_xinli
|
||||||
password: ZLZBcfGtsWJe5r4z
|
password: ZLZBcfGtsWJe5r4z
|
||||||
# 从库数据源
|
# 从库数据源
|
||||||
|
|
|
||||||
|
|
@ -463,6 +463,17 @@ public class SysUserServiceImpl implements ISysUserService
|
||||||
userRoleMapper.deleteUserRoleByUserId(userId);
|
userRoleMapper.deleteUserRoleByUserId(userId);
|
||||||
// 删除用户与岗位表
|
// 删除用户与岗位表
|
||||||
userPostMapper.deleteUserPostByUserId(userId);
|
userPostMapper.deleteUserPostByUserId(userId);
|
||||||
|
// 删除用户的量表权限(支持多用户授权后,一个用户可能有多条权限记录)
|
||||||
|
try {
|
||||||
|
com.ddnai.system.service.psychology.IPsyScalePermissionService permissionService =
|
||||||
|
SpringUtils.getBean(com.ddnai.system.service.psychology.IPsyScalePermissionService.class);
|
||||||
|
if (permissionService != null) {
|
||||||
|
permissionService.deletePermissionByUserId(userId);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("删除用户量表权限失败,userId: {}", userId, e);
|
||||||
|
// 不中断删除流程,继续删除用户
|
||||||
|
}
|
||||||
return userMapper.deleteUserById(userId);
|
return userMapper.deleteUserById(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -485,6 +496,24 @@ public class SysUserServiceImpl implements ISysUserService
|
||||||
userRoleMapper.deleteUserRole(userIds);
|
userRoleMapper.deleteUserRole(userIds);
|
||||||
// 删除用户与岗位关联
|
// 删除用户与岗位关联
|
||||||
userPostMapper.deleteUserPost(userIds);
|
userPostMapper.deleteUserPost(userIds);
|
||||||
|
// 删除用户的量表权限(支持多用户授权后,一个用户可能有多条权限记录)
|
||||||
|
try {
|
||||||
|
com.ddnai.system.service.psychology.IPsyScalePermissionService permissionService =
|
||||||
|
SpringUtils.getBean(com.ddnai.system.service.psychology.IPsyScalePermissionService.class);
|
||||||
|
if (permissionService != null) {
|
||||||
|
for (Long userId : userIds) {
|
||||||
|
try {
|
||||||
|
permissionService.deletePermissionByUserId(userId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("删除用户量表权限失败,userId: {}", userId, e);
|
||||||
|
// 继续处理其他用户
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("获取权限服务失败,跳过删除用户量表权限", e);
|
||||||
|
// 不中断删除流程,继续删除用户
|
||||||
|
}
|
||||||
return userMapper.deleteUserByIds(userIds);
|
return userMapper.deleteUserByIds(userIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,38 @@ public class PsyScalePermissionServiceImpl implements IPsyScalePermissionService
|
||||||
{
|
{
|
||||||
permission.setStatus("0");
|
permission.setStatus("0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查是否已存在相同的权限记录(避免重复)
|
||||||
|
PsyScalePermission query = new PsyScalePermission();
|
||||||
|
query.setScaleId(permission.getScaleId());
|
||||||
|
query.setUserId(permission.getUserId());
|
||||||
|
query.setRoleId(permission.getRoleId());
|
||||||
|
query.setDeptId(permission.getDeptId());
|
||||||
|
query.setClassName(permission.getClassName());
|
||||||
|
query.setStatus("0"); // 只检查有效状态的权限
|
||||||
|
|
||||||
|
List<PsyScalePermission> existing = permissionMapper.selectPermissionList(query);
|
||||||
|
if (existing != null && !existing.isEmpty())
|
||||||
|
{
|
||||||
|
// 检查是否有完全相同的权限记录(除了时间范围)
|
||||||
|
boolean hasDuplicate = existing.stream().anyMatch(p -> {
|
||||||
|
boolean sameUser = (p.getUserId() == null && permission.getUserId() == null)
|
||||||
|
|| (p.getUserId() != null && p.getUserId().equals(permission.getUserId()));
|
||||||
|
boolean sameRole = (p.getRoleId() == null && permission.getRoleId() == null)
|
||||||
|
|| (p.getRoleId() != null && p.getRoleId().equals(permission.getRoleId()));
|
||||||
|
boolean sameDept = (p.getDeptId() == null && permission.getDeptId() == null)
|
||||||
|
|| (p.getDeptId() != null && p.getDeptId().equals(permission.getDeptId()));
|
||||||
|
boolean sameClass = (p.getClassName() == null && permission.getClassName() == null)
|
||||||
|
|| (p.getClassName() != null && p.getClassName().equals(permission.getClassName()));
|
||||||
|
return sameUser && sameRole && sameDept && sameClass;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (hasDuplicate)
|
||||||
|
{
|
||||||
|
throw new RuntimeException("该权限已存在,请勿重复添加");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return permissionMapper.insertPermission(permission);
|
return permissionMapper.insertPermission(permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,9 +45,27 @@
|
||||||
|
|
||||||
<el-table v-loading="loading" :data="permissionList" @selection-change="handleSelectionChange">
|
<el-table v-loading="loading" :data="permissionList" @selection-change="handleSelectionChange">
|
||||||
<el-table-column type="selection" width="55" align="center" />
|
<el-table-column type="selection" width="55" align="center" />
|
||||||
<el-table-column label="权限ID" align="center" prop="permissionId" width="100" />
|
|
||||||
<el-table-column label="量表名称" align="center" prop="scaleName" width="200" />
|
<el-table-column label="量表名称" align="center" prop="scaleName" width="200" />
|
||||||
<el-table-column label="用户名称" align="center" prop="userName" width="150" />
|
<el-table-column label="用户名称" align="center" prop="userNames" min-width="250">
|
||||||
|
<template slot-scope="scope">
|
||||||
|
<span v-if="scope.row.hasAllUsers">
|
||||||
|
<el-tag type="info" size="small">所有用户</el-tag>
|
||||||
|
</span>
|
||||||
|
<span v-else-if="scope.row.userNames && scope.row.userNames.length > 0">
|
||||||
|
<el-tag
|
||||||
|
v-for="(userName, index) in (scope.row.userNames.length > 5 ? scope.row.userNames.slice(0, 5) : scope.row.userNames)"
|
||||||
|
:key="index"
|
||||||
|
size="small"
|
||||||
|
style="margin-right: 5px; margin-bottom: 3px;">
|
||||||
|
{{ userName }}
|
||||||
|
</el-tag>
|
||||||
|
<span v-if="scope.row.userCount > 5" style="color: #909399; font-size: 12px;">
|
||||||
|
等{{ scope.row.userCount }}人
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
<span v-else style="color: #909399;">-</span>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
<el-table-column label="部门名称" align="center" prop="deptName" width="150" />
|
<el-table-column label="部门名称" align="center" prop="deptName" width="150" />
|
||||||
<el-table-column label="角色名称" align="center" prop="roleName" width="150" />
|
<el-table-column label="角色名称" align="center" prop="roleName" width="150" />
|
||||||
<el-table-column label="班级名称" align="center" prop="className" width="150" />
|
<el-table-column label="班级名称" align="center" prop="className" width="150" />
|
||||||
|
|
@ -96,15 +114,28 @@
|
||||||
</el-option>
|
</el-option>
|
||||||
</el-select>
|
</el-select>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="用户" prop="userId">
|
<el-form-item label="用户" prop="userIds">
|
||||||
<el-select v-model="form.userId" placeholder="请选择用户(留空表示所有用户)" clearable filterable style="width: 100%;">
|
<el-select
|
||||||
|
v-model="form.userIds"
|
||||||
|
placeholder="请选择用户(可多选,留空表示所有用户)"
|
||||||
|
clearable
|
||||||
|
filterable
|
||||||
|
multiple
|
||||||
|
reserve-keyword
|
||||||
|
:loading="userSearchLoading"
|
||||||
|
style="width: 100%;"
|
||||||
|
@focus="handleSelectFocus"
|
||||||
|
@change="handleUserIdsChange">
|
||||||
<el-option
|
<el-option
|
||||||
v-for="user in userList"
|
v-for="user in userOptions"
|
||||||
:key="user.userId"
|
:key="user.userId"
|
||||||
:label="user.nickName ? `${user.nickName}(${user.userName})` : user.userName"
|
:label="user.nickName ? `${user.nickName}(${user.userName})` : user.userName"
|
||||||
:value="Number(user.userId)">
|
:value="user.userId">
|
||||||
</el-option>
|
</el-option>
|
||||||
</el-select>
|
</el-select>
|
||||||
|
<div style="margin-top: 5px; color: #909399; font-size: 12px;">
|
||||||
|
提示:可输入姓名或账号搜索,支持多选。留空表示所有用户。
|
||||||
|
</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="开始时间" prop="startTime">
|
<el-form-item label="开始时间" prop="startTime">
|
||||||
<el-date-picker
|
<el-date-picker
|
||||||
|
|
@ -145,7 +176,9 @@
|
||||||
<script>
|
<script>
|
||||||
import { listPermission, getPermission, delPermission, addPermission, updatePermission } from "@/api/psychology/permission";
|
import { listPermission, getPermission, delPermission, addPermission, updatePermission } from "@/api/psychology/permission";
|
||||||
import { listScale } from "@/api/psychology/scale";
|
import { listScale } from "@/api/psychology/scale";
|
||||||
import { listUser } from "@/api/system/user";
|
import { allocatedUserList } from "@/api/system/role";
|
||||||
|
import { listRole } from "@/api/system/role";
|
||||||
|
import { getUser } from "@/api/system/user";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "PsyScalePermission",
|
name: "PsyScalePermission",
|
||||||
|
|
@ -167,8 +200,12 @@ export default {
|
||||||
permissionList: [],
|
permissionList: [],
|
||||||
// 量表列表
|
// 量表列表
|
||||||
scaleList: [],
|
scaleList: [],
|
||||||
// 用户列表
|
// 用户列表(用于下拉框显示)
|
||||||
userList: [],
|
userOptions: [],
|
||||||
|
// 用户搜索加载状态
|
||||||
|
userSearchLoading: false,
|
||||||
|
// 学员角色ID(缓存)
|
||||||
|
studentRoleId: null,
|
||||||
// 弹出层标题
|
// 弹出层标题
|
||||||
title: "",
|
title: "",
|
||||||
// 是否显示弹出层
|
// 是否显示弹出层
|
||||||
|
|
@ -203,10 +240,86 @@ export default {
|
||||||
/** 查询权限列表 */
|
/** 查询权限列表 */
|
||||||
getList() {
|
getList() {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
listPermission(this.queryParams).then(response => {
|
// 由于需要在前端汇总,需要获取所有数据(不分页或获取足够多的数据)
|
||||||
this.permissionList = response.rows;
|
// 先获取所有数据,然后在前端进行汇总和分页
|
||||||
this.total = response.total;
|
const queryParams = {
|
||||||
|
...this.queryParams,
|
||||||
|
pageNum: 1,
|
||||||
|
pageSize: 10000 // 获取足够多的数据,确保能汇总所有记录
|
||||||
|
};
|
||||||
|
listPermission(queryParams).then(response => {
|
||||||
|
const rawList = response.rows || [];
|
||||||
|
// 对数据进行汇总:按量表ID + 开始时间 + 结束时间 + 状态分组
|
||||||
|
const groupedList = this.groupPermissions(rawList);
|
||||||
|
|
||||||
|
// 前端分页处理
|
||||||
|
const pageNum = this.queryParams.pageNum || 1;
|
||||||
|
const pageSize = this.queryParams.pageSize || 10;
|
||||||
|
const start = (pageNum - 1) * pageSize;
|
||||||
|
const end = start + pageSize;
|
||||||
|
this.permissionList = groupedList.slice(start, end);
|
||||||
|
this.total = groupedList.length; // 汇总后的总数量
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("查询权限列表失败:", error);
|
||||||
|
this.$modal.msgError("查询权限列表失败");
|
||||||
|
this.loading = false;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
/** 对权限列表进行分组汇总 */
|
||||||
|
groupPermissions(rawList) {
|
||||||
|
// 使用Map进行分组,key为 scaleId_startTime_endTime_status
|
||||||
|
const groupMap = new Map();
|
||||||
|
|
||||||
|
rawList.forEach(item => {
|
||||||
|
// 生成分组key:scaleId + startTime + endTime + status(状态也要考虑,因为有效和无效的权限应该分开显示)
|
||||||
|
const startTimeStr = item.startTime || '';
|
||||||
|
const endTimeStr = item.endTime || '';
|
||||||
|
const status = item.status || '0';
|
||||||
|
const key = `${item.scaleId}_${startTimeStr}_${endTimeStr}_${status}`;
|
||||||
|
|
||||||
|
if (!groupMap.has(key)) {
|
||||||
|
// 创建汇总记录
|
||||||
|
const groupedItem = {
|
||||||
|
...item,
|
||||||
|
permissionIds: [], // 存储所有相关的权限ID
|
||||||
|
userIds: [], // 存储所有用户ID
|
||||||
|
userNames: [], // 存储所有用户名称(用于显示)
|
||||||
|
userCount: 0, // 用户总数(包括"所有用户"的情况)
|
||||||
|
hasAllUsers: false, // 是否包含"所有用户"权限
|
||||||
|
_groupKey: key // 分组key,用于编辑和删除
|
||||||
|
};
|
||||||
|
groupMap.set(key, groupedItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
const groupedItem = groupMap.get(key);
|
||||||
|
// 添加权限ID
|
||||||
|
if (item.permissionId) {
|
||||||
|
groupedItem.permissionIds.push(item.permissionId);
|
||||||
|
}
|
||||||
|
// 添加用户信息
|
||||||
|
if (item.userId && item.userName) {
|
||||||
|
// 具体用户权限
|
||||||
|
if (!groupedItem.userIds.includes(item.userId)) {
|
||||||
|
groupedItem.userIds.push(item.userId);
|
||||||
|
groupedItem.userNames.push(item.userName);
|
||||||
|
groupedItem.userCount++;
|
||||||
|
}
|
||||||
|
} else if (!item.userId && !item.roleId && !item.deptId) {
|
||||||
|
// "所有用户"权限(user_id, role_id, dept_id 都为空)
|
||||||
|
groupedItem.hasAllUsers = true;
|
||||||
|
// 注意:如果同时有"所有用户"和具体用户权限,userCount仍然显示具体用户数量
|
||||||
|
// 但hasAllUsers标志会显示"所有用户"标签
|
||||||
|
}
|
||||||
|
// 注意:角色权限和部门权限不在此处汇总,它们会单独显示
|
||||||
|
});
|
||||||
|
|
||||||
|
// 转换为数组并排序
|
||||||
|
return Array.from(groupMap.values()).sort((a, b) => {
|
||||||
|
// 按创建时间倒序
|
||||||
|
const timeA = a.createTime || '';
|
||||||
|
const timeB = b.createTime || '';
|
||||||
|
return timeB.localeCompare(timeA);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
/** 加载量表列表(显示所有量表,不包含问卷) */
|
/** 加载量表列表(显示所有量表,不包含问卷) */
|
||||||
|
|
@ -218,13 +331,197 @@ export default {
|
||||||
.filter(scale => !scale.sourceType || scale.sourceType === 'scale');
|
.filter(scale => !scale.sourceType || scale.sourceType === 'scale');
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
/** 加载用户列表 */
|
/** 加载用户列表(只加载学员角色的用户)- 改为远程搜索模式,不预加载 */
|
||||||
loadUsers() {
|
loadUsers() {
|
||||||
listUser({ status: '0', pageNum: 1, pageSize: 1000 }).then(response => {
|
// 预加载学员角色ID,用于后续搜索
|
||||||
this.userList = (response.rows || []).map(user => ({
|
this.getStudentRoleId().then(studentRoleId => {
|
||||||
...user,
|
this.studentRoleId = studentRoleId;
|
||||||
userId: Number(user.userId) // 确保userId是数字类型
|
if (!studentRoleId) {
|
||||||
|
console.warn("未找到学员角色,用户搜索功能可能不可用");
|
||||||
|
}
|
||||||
|
// 初始加载少量用户,供用户选择
|
||||||
|
this.searchUsers("");
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("获取学员角色ID失败:", error);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
/** 搜索用户(远程搜索) */
|
||||||
|
searchUsers(keyword) {
|
||||||
|
if (!this.studentRoleId) {
|
||||||
|
// 如果还没有获取到学员角色ID,先获取
|
||||||
|
this.getStudentRoleId().then(studentRoleId => {
|
||||||
|
this.studentRoleId = studentRoleId;
|
||||||
|
if (studentRoleId) {
|
||||||
|
this.doSearchUsers(keyword);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.doSearchUsers(keyword);
|
||||||
|
},
|
||||||
|
/** 下拉框获得焦点时,如果没有数据则加载 */
|
||||||
|
handleSelectFocus() {
|
||||||
|
// 如果 userOptions 为空,加载初始数据
|
||||||
|
if (this.userOptions.length === 0) {
|
||||||
|
this.searchUsers("");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
/** 执行用户搜索 */
|
||||||
|
doSearchUsers(keyword) {
|
||||||
|
this.userSearchLoading = true;
|
||||||
|
const searchParams = {
|
||||||
|
roleId: this.studentRoleId,
|
||||||
|
status: '0',
|
||||||
|
pageNum: 1,
|
||||||
|
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搜索)
|
||||||
|
let filteredRows = rows;
|
||||||
|
if (keyword && keyword.trim()) {
|
||||||
|
const keywordLower = keyword.trim().toLowerCase();
|
||||||
|
filteredRows = rows.filter(item => {
|
||||||
|
const userName = (item.userName || '').toLowerCase();
|
||||||
|
const nickName = (item.nickName || '').toLowerCase();
|
||||||
|
return userName.includes(keywordLower) || nickName.includes(keywordLower);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
// 即使搜索失败,也要保留已选中的用户
|
||||||
|
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 */
|
||||||
|
getStudentRoleId() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// 先尝试从角色列表中找到学员角色
|
||||||
|
listRole({}).then(response => {
|
||||||
|
const roles = response.rows || [];
|
||||||
|
// 查找学员角色:roleId=101 或 roleKey='student' 或 roleName包含'学员'
|
||||||
|
const studentRole = roles.find(role => {
|
||||||
|
return (role.roleId === 101) ||
|
||||||
|
(role.roleKey && role.roleKey.toLowerCase() === 'student') ||
|
||||||
|
(role.roleName && role.roleName.includes('学员'));
|
||||||
|
});
|
||||||
|
if (studentRole && studentRole.roleId) {
|
||||||
|
resolve(studentRole.roleId);
|
||||||
|
} else {
|
||||||
|
// 如果找不到,尝试使用默认值101
|
||||||
|
console.warn("未找到学员角色,使用默认值101");
|
||||||
|
resolve(101);
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("获取角色列表失败:", error);
|
||||||
|
// 如果获取角色列表失败,尝试使用默认值101
|
||||||
|
resolve(101);
|
||||||
|
});
|
||||||
|
>>>>>>> 13bbd8b9e742b8b2a020195d81f66187af7d4066
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
// 取消按钮
|
// 取消按钮
|
||||||
|
|
@ -237,14 +534,17 @@ export default {
|
||||||
this.form = {
|
this.form = {
|
||||||
permissionId: undefined,
|
permissionId: undefined,
|
||||||
scaleId: undefined,
|
scaleId: undefined,
|
||||||
userId: undefined,
|
userId: undefined, // 保留用于编辑时兼容
|
||||||
|
userIds: [], // 多选用户ID数组
|
||||||
deptId: undefined,
|
deptId: undefined,
|
||||||
roleId: undefined,
|
roleId: undefined,
|
||||||
className: undefined,
|
className: undefined,
|
||||||
startTime: undefined,
|
startTime: undefined,
|
||||||
endTime: undefined,
|
endTime: undefined,
|
||||||
status: "0",
|
status: "0",
|
||||||
remark: undefined
|
remark: undefined,
|
||||||
|
_permissionIds: undefined, // 存储所有相关的权限ID(用于编辑时删除)
|
||||||
|
_groupKey: undefined // 分组key
|
||||||
};
|
};
|
||||||
this.resetForm("form");
|
this.resetForm("form");
|
||||||
},
|
},
|
||||||
|
|
@ -260,63 +560,253 @@ export default {
|
||||||
},
|
},
|
||||||
// 多选框选中数据
|
// 多选框选中数据
|
||||||
handleSelectionChange(selection) {
|
handleSelectionChange(selection) {
|
||||||
this.ids = selection.map(item => item.permissionId);
|
// 汇总后的记录包含多个权限ID,需要展开
|
||||||
|
this.ids = [];
|
||||||
|
selection.forEach(item => {
|
||||||
|
if (item.permissionIds && item.permissionIds.length > 0) {
|
||||||
|
this.ids.push(...item.permissionIds);
|
||||||
|
} else if (item.permissionId) {
|
||||||
|
this.ids.push(item.permissionId);
|
||||||
|
}
|
||||||
|
});
|
||||||
this.single = selection.length != 1;
|
this.single = selection.length != 1;
|
||||||
this.multiple = !selection.length;
|
this.multiple = !selection.length;
|
||||||
},
|
},
|
||||||
/** 新增按钮操作 */
|
/** 新增按钮操作 */
|
||||||
handleAdd() {
|
handleAdd() {
|
||||||
this.reset();
|
this.reset();
|
||||||
|
// 确保 userOptions 有初始数据
|
||||||
|
if (this.userOptions.length === 0) {
|
||||||
|
this.searchUsers("");
|
||||||
|
}
|
||||||
this.open = true;
|
this.open = true;
|
||||||
this.title = "添加量表权限";
|
this.title = "添加量表权限";
|
||||||
},
|
},
|
||||||
/** 修改按钮操作 */
|
/** 修改按钮操作 */
|
||||||
handleUpdate(row) {
|
handleUpdate(row) {
|
||||||
this.reset();
|
this.reset();
|
||||||
const permissionId = row.permissionId || this.ids[0];
|
// 汇总后的记录包含多个权限ID,需要加载所有相关的权限记录
|
||||||
getPermission(permissionId).then(response => {
|
const permissionIds = row.permissionIds || (row.permissionId ? [row.permissionId] : []);
|
||||||
const data = response.data || {};
|
|
||||||
// 确保userId类型正确(转换为数字或undefined)
|
if (permissionIds.length === 0) {
|
||||||
this.form = {
|
this.$modal.msgError("无法获取权限信息");
|
||||||
...data,
|
return;
|
||||||
userId: data.userId ? Number(data.userId) : undefined,
|
}
|
||||||
scaleId: data.scaleId ? Number(data.scaleId) : undefined
|
|
||||||
};
|
// 加载第一条权限记录作为基础信息
|
||||||
|
getPermission(permissionIds[0]).then(response => {
|
||||||
|
this.form = response.data;
|
||||||
|
|
||||||
|
// 存储所有相关的权限ID,用于提交时删除
|
||||||
|
this.form._permissionIds = permissionIds;
|
||||||
|
this.form._groupKey = row._groupKey;
|
||||||
|
|
||||||
|
// 确定要加载的用户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.open = true;
|
||||||
this.title = "修改量表权限";
|
this.title = "修改量表权限";
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// 没有用户,直接打开对话框
|
||||||
|
this.open = true;
|
||||||
|
this.title = "修改量表权限";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
/** 加载已选中的用户信息到选项列表 */
|
||||||
|
loadSelectedUsers(userIds) {
|
||||||
|
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() {
|
submitForm() {
|
||||||
this.$refs["form"].validate(valid => {
|
this.$refs["form"].validate(valid => {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
// 准备提交数据,确保类型正确
|
// 处理多选用户:如果选择了多个用户,需要为每个用户创建一条权限记录
|
||||||
const submitData = {
|
const userIds = this.form.userIds || [];
|
||||||
...this.form,
|
|
||||||
userId: this.form.userId ? Number(this.form.userId) : null,
|
|
||||||
scaleId: this.form.scaleId ? Number(this.form.scaleId) : undefined
|
|
||||||
};
|
|
||||||
|
|
||||||
if (this.form.permissionId != undefined) {
|
// 编辑模式:先删除所有相关的权限记录,然后根据新的用户列表创建新记录
|
||||||
updatePermission(submitData).then(response => {
|
if (this.form._permissionIds && this.form._permissionIds.length > 0) {
|
||||||
this.$modal.msgSuccess("修改成功");
|
// 删除所有相关的权限记录
|
||||||
this.open = false;
|
delPermission(this.form._permissionIds).then(() => {
|
||||||
this.getList();
|
// 然后根据新的用户列表创建权限记录
|
||||||
|
this.createPermissionsForUsers(userIds);
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("删除原权限失败:", error);
|
||||||
|
this.$modal.msgError(error.msg || "修改失败:删除原权限失败");
|
||||||
|
});
|
||||||
|
} else if (this.form.permissionId != undefined) {
|
||||||
|
// 兼容旧数据:只有单个权限ID
|
||||||
|
delPermission(this.form.permissionId).then(() => {
|
||||||
|
this.createPermissionsForUsers(userIds);
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("删除原权限失败:", error);
|
||||||
|
this.$modal.msgError(error.msg || "修改失败:删除原权限失败");
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
addPermission(submitData).then(response => {
|
// 新增模式:为每个用户创建权限记录
|
||||||
this.$modal.msgSuccess("新增成功");
|
this.createPermissionsForUsers(userIds);
|
||||||
this.open = false;
|
|
||||||
this.getList();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
/** 为多个用户创建权限记录 */
|
||||||
|
createPermissionsForUsers(userIds) {
|
||||||
|
if (userIds.length === 0) {
|
||||||
|
// 没有选择用户,表示所有用户,创建一条userId为null的记录
|
||||||
|
const addForm = { ...this.form };
|
||||||
|
delete addForm.userIds;
|
||||||
|
delete addForm.permissionId;
|
||||||
|
delete addForm._permissionIds; // 删除临时字段
|
||||||
|
delete addForm._groupKey; // 删除临时字段
|
||||||
|
addForm.userId = null;
|
||||||
|
addPermission(addForm).then(response => {
|
||||||
|
this.$modal.msgSuccess("新增成功");
|
||||||
|
this.open = false;
|
||||||
|
this.getList();
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("新增权限失败:", error);
|
||||||
|
const errorMsg = error.msg || error.message || "新增失败";
|
||||||
|
this.$modal.msgError(errorMsg);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// 为每个用户创建权限记录
|
||||||
|
const promises = userIds.map(userId => {
|
||||||
|
const addForm = { ...this.form };
|
||||||
|
delete addForm.userIds;
|
||||||
|
delete addForm.permissionId;
|
||||||
|
delete addForm._permissionIds; // 删除临时字段
|
||||||
|
delete addForm._groupKey; // 删除临时字段
|
||||||
|
addForm.userId = userId;
|
||||||
|
return addPermission(addForm).catch(error => {
|
||||||
|
// 如果是重复权限错误,返回null而不是reject,这样其他用户仍能成功
|
||||||
|
if (error.msg && error.msg.includes("已存在")) {
|
||||||
|
console.warn(`用户 ${userId} 的权限已存在,跳过`);
|
||||||
|
return { skipped: true, userId };
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Promise.allSettled(promises).then(results => {
|
||||||
|
const successCount = results.filter(r => r.status === 'fulfilled' && !r.value?.skipped).length;
|
||||||
|
const skippedCount = results.filter(r => r.status === 'fulfilled' && r.value?.skipped).length;
|
||||||
|
const failedCount = results.filter(r => r.status === 'rejected').length;
|
||||||
|
|
||||||
|
let message = "";
|
||||||
|
if (successCount > 0) {
|
||||||
|
message = `成功为 ${successCount} 个用户分配权限`;
|
||||||
|
}
|
||||||
|
if (skippedCount > 0) {
|
||||||
|
message += (message ? "," : "") + `${skippedCount} 个用户权限已存在(已跳过)`;
|
||||||
|
}
|
||||||
|
if (failedCount > 0) {
|
||||||
|
message += (message ? "," : "") + `${failedCount} 个用户权限分配失败`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (successCount > 0 || skippedCount > 0) {
|
||||||
|
this.$modal.msgSuccess(message || "操作完成");
|
||||||
|
this.open = false;
|
||||||
|
this.getList();
|
||||||
|
} else {
|
||||||
|
this.$modal.msgError(message || "权限分配失败");
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("批量新增权限失败:", error);
|
||||||
|
this.$modal.msgError(error.msg || error.message || "批量新增权限失败");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
/** 删除按钮操作 */
|
/** 删除按钮操作 */
|
||||||
handleDelete(row) {
|
handleDelete(row) {
|
||||||
const permissionIds = row.permissionId || this.ids;
|
// 汇总后的记录包含多个权限ID
|
||||||
this.$modal.confirm('是否确认删除权限编号为"' + permissionIds + '"的数据项?').then(() => {
|
const permissionIds = row.permissionIds || (row.permissionId ? [row.permissionId] : []);
|
||||||
|
|
||||||
|
if (permissionIds.length === 0) {
|
||||||
|
this.$modal.msgError("无法获取权限信息");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const scaleName = row.scaleName || '未知量表';
|
||||||
|
// 计算实际要删除的记录数
|
||||||
|
const recordCount = permissionIds.length;
|
||||||
|
// 计算用户数(如果是"所有用户",显示"所有用户";否则显示具体用户数)
|
||||||
|
let userDesc = '';
|
||||||
|
if (row.hasAllUsers) {
|
||||||
|
userDesc = '所有用户';
|
||||||
|
} else if (row.userCount > 0) {
|
||||||
|
userDesc = `${row.userCount}个用户`;
|
||||||
|
} else {
|
||||||
|
userDesc = `${recordCount}条记录`;
|
||||||
|
}
|
||||||
|
const confirmMsg = recordCount > 1
|
||||||
|
? `是否确认删除量表"${scaleName}"的${userDesc}(共${recordCount}条权限记录)?`
|
||||||
|
: `是否确认删除权限编号为"${permissionIds[0]}"的数据项?`;
|
||||||
|
|
||||||
|
this.$modal.confirm(confirmMsg).then(() => {
|
||||||
return delPermission(permissionIds);
|
return delPermission(permissionIds);
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
this.getList();
|
this.getList();
|
||||||
|
|
|
||||||
|
|
@ -8,65 +8,25 @@
|
||||||
|
|
||||||
<el-form :model="form" label-width="120px">
|
<el-form :model="form" label-width="120px">
|
||||||
<el-form-item label="选择用户">
|
<el-form-item label="选择用户">
|
||||||
<div style="display: flex; align-items: center; gap: 10px; flex-wrap: wrap;">
|
<el-select
|
||||||
<!-- 方式一:下拉框选择 -->
|
v-model="userId"
|
||||||
<el-select
|
filterable
|
||||||
v-model="userId"
|
remote
|
||||||
filterable
|
reserve-keyword
|
||||||
remote
|
placeholder="输入姓名/账号搜索学员"
|
||||||
reserve-keyword
|
:remote-method="searchUsers"
|
||||||
placeholder="下拉框选择用户"
|
:loading="userSearchLoading"
|
||||||
:remote-method="searchUsers"
|
style="width: 320px;"
|
||||||
:loading="userSearchLoading"
|
@change="handleUserChange">
|
||||||
style="width: 280px;"
|
<el-option
|
||||||
@focus="handleSelectFocus"
|
v-for="user in userOptions"
|
||||||
@change="handleUserChange">
|
:key="user.userId"
|
||||||
<el-option
|
:label="user.nickName ? `${user.nickName}(${user.userName})` : user.userName"
|
||||||
v-for="user in userOptions"
|
:value="user.userId">
|
||||||
:key="user.userId"
|
</el-option>
|
||||||
:label="user.nickName ? `${user.nickName}(${user.userName})` : user.userName"
|
</el-select>
|
||||||
:value="user.userId">
|
<el-button type="text" @click="reloadCurrentUser" style="margin-left: 10px;">刷新当前</el-button>
|
||||||
</el-option>
|
|
||||||
</el-select>
|
|
||||||
<!-- 方式二:搜索框搜索 -->
|
|
||||||
<el-input
|
|
||||||
v-model="searchKeyword"
|
|
||||||
placeholder="输入姓名/账号/手机号搜索用户"
|
|
||||||
clearable
|
|
||||||
style="width: 280px;"
|
|
||||||
@keyup.enter.native="handleSearch"
|
|
||||||
@clear="handleSearchClear">
|
|
||||||
<el-button slot="append" icon="el-icon-search" @click="handleSearch" :loading="searchLoading"></el-button>
|
|
||||||
</el-input>
|
|
||||||
<el-button type="text" @click="reloadCurrentUser">刷新当前</el-button>
|
|
||||||
</div>
|
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
|
||||||
<!-- 搜索结果列表 -->
|
|
||||||
<el-form-item v-if="searchResults.length > 0" label="搜索结果">
|
|
||||||
<el-table
|
|
||||||
:data="searchResults"
|
|
||||||
border
|
|
||||||
max-height="300"
|
|
||||||
highlight-current-row
|
|
||||||
@row-click="handleSelectSearchUser"
|
|
||||||
style="width: 100%;">
|
|
||||||
<el-table-column type="index" label="序号" width="60" align="center" />
|
|
||||||
<el-table-column prop="userName" label="账号" width="150" />
|
|
||||||
<el-table-column prop="nickName" label="姓名" width="150" />
|
|
||||||
<el-table-column prop="phonenumber" label="手机号" width="130" />
|
|
||||||
<el-table-column prop="email" label="邮箱" min-width="180" :show-overflow-tooltip="true" />
|
|
||||||
<el-table-column label="操作" width="100" align="center">
|
|
||||||
<template slot-scope="scope">
|
|
||||||
<el-button type="text" size="small" @click="handleSelectSearchUser(scope.row)">选择</el-button>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
</el-table>
|
|
||||||
<div style="margin-top: 10px; color: #909399; font-size: 12px;">
|
|
||||||
提示:点击表格行或"选择"按钮即可选择该用户
|
|
||||||
</div>
|
|
||||||
</el-form-item>
|
|
||||||
|
|
||||||
<el-form-item label="当前用户">
|
<el-form-item label="当前用户">
|
||||||
<el-input v-model="userName" disabled style="width: 300px;" />
|
<el-input v-model="userName" disabled style="width: 300px;" />
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
|
@ -99,7 +59,9 @@
|
||||||
<script>
|
<script>
|
||||||
import { assignUserScales, getUserScaleIds } from "@/api/psychology/permission";
|
import { assignUserScales, getUserScaleIds } from "@/api/psychology/permission";
|
||||||
import { listScale } from "@/api/psychology/scale";
|
import { listScale } from "@/api/psychology/scale";
|
||||||
import { getUser, listUser } from "@/api/system/user";
|
import { getUser } from "@/api/system/user";
|
||||||
|
import { allocatedUserList } from "@/api/system/role";
|
||||||
|
import { listRole } from "@/api/system/role";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "UserScalePermission",
|
name: "UserScalePermission",
|
||||||
|
|
@ -111,11 +73,7 @@ export default {
|
||||||
scaleList: [],
|
scaleList: [],
|
||||||
selectedScaleIds: [],
|
selectedScaleIds: [],
|
||||||
userOptions: [],
|
userOptions: [],
|
||||||
userSearchLoading: false,
|
userSearchLoading: false
|
||||||
// 搜索相关
|
|
||||||
searchKeyword: "",
|
|
||||||
searchResults: [],
|
|
||||||
searchLoading: false
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
|
|
@ -173,44 +131,82 @@ export default {
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
/** 搜索可切换的用户 */
|
/** 搜索可切换的用户(只搜索学员角色的用户) */
|
||||||
searchUsers(keyword) {
|
searchUsers(keyword) {
|
||||||
this.userSearchLoading = true;
|
this.userSearchLoading = true;
|
||||||
const queryParams = {
|
// 先获取学员角色ID
|
||||||
pageNum: 1,
|
this.getStudentRoleId().then(studentRoleId => {
|
||||||
pageSize: 10000, // 增加页面大小以获取更多用户
|
if (!studentRoleId) {
|
||||||
status: '0'
|
this.$modal.msgError("未找到学员角色,无法搜索用户");
|
||||||
};
|
this.userOptions = [];
|
||||||
|
this.userSearchLoading = false;
|
||||||
// 如果有搜索关键词,添加搜索条件
|
return;
|
||||||
if (keyword && keyword.trim()) {
|
}
|
||||||
queryParams.userName = keyword.trim();
|
// 使用allocatedUserList查询已分配学员角色的用户
|
||||||
queryParams.nickName = keyword.trim();
|
allocatedUserList({
|
||||||
}
|
roleId: studentRoleId,
|
||||||
|
status: '0',
|
||||||
listUser(queryParams).then(response => {
|
pageNum: 1,
|
||||||
const rows = response.rows || [];
|
pageSize: 1000, // 设置足够大的值以获取所有匹配的学员用户
|
||||||
// 合并新搜索到的用户,避免重复
|
userName: keyword || undefined,
|
||||||
const newUsers = rows.map(item => ({
|
phonenumber: keyword || undefined
|
||||||
userId: item.userId,
|
}).then(response => {
|
||||||
userName: item.userName,
|
const rows = response.rows || [];
|
||||||
nickName: item.nickName
|
// 如果有关键词,进一步过滤(因为allocatedUserList可能只支持userName和phonenumber搜索)
|
||||||
}));
|
let filteredRows = rows;
|
||||||
|
if (keyword) {
|
||||||
// 合并用户列表,去重
|
const keywordLower = keyword.toLowerCase();
|
||||||
const existingUserIds = new Set(this.userOptions.map(u => u.userId));
|
filteredRows = rows.filter(item => {
|
||||||
newUsers.forEach(user => {
|
const userName = (item.userName || '').toLowerCase();
|
||||||
if (!existingUserIds.has(user.userId)) {
|
const nickName = (item.nickName || '').toLowerCase();
|
||||||
this.userOptions.push(user);
|
return userName.includes(keywordLower) || nickName.includes(keywordLower);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
this.userOptions = filteredRows.map(item => ({
|
||||||
|
userId: item.userId,
|
||||||
|
userName: item.userName,
|
||||||
|
nickName: item.nickName
|
||||||
|
}));
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("搜索学员用户失败:", error);
|
||||||
|
this.$modal.msgError("搜索学员用户失败");
|
||||||
|
this.userOptions = [];
|
||||||
|
}).finally(() => {
|
||||||
|
this.userSearchLoading = false;
|
||||||
});
|
});
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.error('搜索用户失败:', error);
|
console.error("获取学员角色ID失败:", error);
|
||||||
this.$modal.msgError("搜索用户失败");
|
this.$modal.msgError("获取学员角色ID失败");
|
||||||
}).finally(() => {
|
this.userOptions = [];
|
||||||
this.userSearchLoading = false;
|
this.userSearchLoading = false;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
/** 获取学员角色ID */
|
||||||
|
getStudentRoleId() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// 先尝试从角色列表中找到学员角色
|
||||||
|
listRole({}).then(response => {
|
||||||
|
const roles = response.rows || [];
|
||||||
|
// 查找学员角色:roleId=101 或 roleKey='student' 或 roleName包含'学员'
|
||||||
|
const studentRole = roles.find(role => {
|
||||||
|
return (role.roleId === 101) ||
|
||||||
|
(role.roleKey && role.roleKey.toLowerCase() === 'student') ||
|
||||||
|
(role.roleName && role.roleName.includes('学员'));
|
||||||
|
});
|
||||||
|
if (studentRole && studentRole.roleId) {
|
||||||
|
resolve(studentRole.roleId);
|
||||||
|
} else {
|
||||||
|
// 如果找不到,尝试使用默认值101
|
||||||
|
console.warn("未找到学员角色,使用默认值101");
|
||||||
|
resolve(101);
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
console.error("获取角色列表失败:", error);
|
||||||
|
// 如果获取角色列表失败,尝试使用默认值101
|
||||||
|
resolve(101);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
/** 切换用户 */
|
/** 切换用户 */
|
||||||
handleUserChange(value) {
|
handleUserChange(value) {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
|
|
@ -264,90 +260,6 @@ export default {
|
||||||
/** 选择变化 */
|
/** 选择变化 */
|
||||||
handleChange(value, direction, movedKeys) {
|
handleChange(value, direction, movedKeys) {
|
||||||
// 可以在这里添加额外的逻辑
|
// 可以在这里添加额外的逻辑
|
||||||
},
|
|
||||||
/** 下拉框获得焦点时,如果没有用户列表,则加载所有用户 */
|
|
||||||
handleSelectFocus() {
|
|
||||||
if (this.userOptions.length === 0) {
|
|
||||||
this.loadAllUsers();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
/** 加载所有用户 */
|
|
||||||
loadAllUsers() {
|
|
||||||
this.userSearchLoading = true;
|
|
||||||
listUser({
|
|
||||||
pageNum: 1,
|
|
||||||
pageSize: 1000, // 加载更多用户
|
|
||||||
status: '0'
|
|
||||||
}).then(response => {
|
|
||||||
const rows = response.rows || [];
|
|
||||||
this.userOptions = rows.map(item => ({
|
|
||||||
userId: item.userId,
|
|
||||||
userName: item.userName,
|
|
||||||
nickName: item.nickName
|
|
||||||
}));
|
|
||||||
this.$modal.msgSuccess(`已加载 ${this.userOptions.length} 个用户`);
|
|
||||||
}).catch(error => {
|
|
||||||
console.error('加载用户列表失败:', error);
|
|
||||||
this.$modal.msgError("加载用户列表失败");
|
|
||||||
}).finally(() => {
|
|
||||||
this.userSearchLoading = false;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
/** 搜索用户 */
|
|
||||||
handleSearch() {
|
|
||||||
if (!this.searchKeyword || !this.searchKeyword.trim()) {
|
|
||||||
this.$modal.msgWarning("请输入搜索关键词");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.searchLoading = true;
|
|
||||||
this.searchResults = [];
|
|
||||||
const keyword = this.searchKeyword.trim();
|
|
||||||
|
|
||||||
// 支持按姓名、账号、手机号搜索
|
|
||||||
listUser({
|
|
||||||
pageNum: 1,
|
|
||||||
pageSize: 100,
|
|
||||||
status: '0',
|
|
||||||
userName: keyword,
|
|
||||||
nickName: keyword,
|
|
||||||
phonenumber: keyword
|
|
||||||
}).then(response => {
|
|
||||||
const rows = response.rows || [];
|
|
||||||
this.searchResults = rows.map(item => ({
|
|
||||||
userId: item.userId,
|
|
||||||
userName: item.userName,
|
|
||||||
nickName: item.nickName,
|
|
||||||
phonenumber: item.phonenumber || '-',
|
|
||||||
email: item.email || '-'
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (this.searchResults.length === 0) {
|
|
||||||
this.$modal.msgWarning("未找到匹配的用户");
|
|
||||||
} else {
|
|
||||||
this.$modal.msgSuccess(`找到 ${this.searchResults.length} 个用户`);
|
|
||||||
}
|
|
||||||
}).catch(error => {
|
|
||||||
console.error('搜索用户失败:', error);
|
|
||||||
this.$modal.msgError("搜索用户失败");
|
|
||||||
}).finally(() => {
|
|
||||||
this.searchLoading = false;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
/** 清空搜索结果 */
|
|
||||||
handleSearchClear() {
|
|
||||||
this.searchResults = [];
|
|
||||||
this.searchKeyword = "";
|
|
||||||
},
|
|
||||||
/** 选择搜索结果中的用户 */
|
|
||||||
handleSelectSearchUser(row) {
|
|
||||||
if (!row || !row.userId) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.userId = row.userId;
|
|
||||||
this.initializeUserData(row.userId);
|
|
||||||
this.searchResults = []; // 选择后清空搜索结果
|
|
||||||
this.$modal.msgSuccess(`已选择用户:${row.nickName || row.userName}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -431,6 +431,9 @@ export default {
|
||||||
// 加载已有选项
|
// 加载已有选项
|
||||||
listOption(row.itemId).then(response => {
|
listOption(row.itemId).then(response => {
|
||||||
this.optionList = response.data || [];
|
this.optionList = response.data || [];
|
||||||
|
}).catch(error => {
|
||||||
|
console.error('加载选项列表失败:', error);
|
||||||
|
this.$modal.msgError('加载选项列表失败,请稍后重试');
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
/** 新增选项 */
|
/** 新增选项 */
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user