Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x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2x 2x 2x 4x 5x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x | /**
* 用户管理业务逻辑模块
* 提供用户的增删改查、状态管理等功能
*
* @fileOverview 用户管理模块,基于Vue 3 Composition API实现
* @author InspectionFlowSystem Team
* @version 1.0.0
* @since 2025-11-04
*/
import { ref } from 'vue';
import { userAPI, roleAPI } from '../../api/index';
import { ElMessage, ElMessageBox } from 'element-plus';
/**
* 用户数据接口
* @interface User
* @description 定义用户的完整数据结构
* @since 1.0.0
*/
export interface User {
/** 用户ID */
id: number;
/** 用户姓名 */
name: string;
/** 工号 */
job_number: string;
/** 联系电话 */
phone: string;
/** 邮箱 */
email: string;
/** 备注信息 */
remark: string;
/** 角色ID */
role_id: number;
/** 状态 */
status: 'active' | 'inactive';
/** 创建时间 */
created_at?: string;
/** 更新时间 */
updated_at?: string;
}
/**
* 角色数据接口
* @interface Role
* @description 定义角色的完整数据结构
* @since 1.0.0
*/
export interface Role {
/** 角色ID */
id: number;
/** 角色名称 */
name: string;
/** 备注信息 */
remark: string;
/** 权限ID列表 */
permissions: number[];
/** 创建时间 */
created_at?: string;
/** 更新时间 */
updated_at?: string;
}
/**
* 用户表单数据接口
* @interface UserForm
* @description 定义用户表单的完整数据结构
* @since 1.0.0
*/
export interface UserForm {
/** 用户姓名 */
name: string;
/** 工号 */
job_number: string;
/** 联系电话 */
phone: string;
/** 邮箱 */
email: string;
/** 备注信息 */
remark: string;
/** 角色ID */
role_id: number;
/** 密码 */
password?: string;
/** 状态 */
status?: 'active' | 'inactive';
}
/**
* 用户列表响应式引用
* @type {import('vue').Ref<User[]>}
* @description 存储所有用户的列表数据
*/
export const users = ref<User[]>([]);
/**
* 角色列表响应式引用
* @type {import('vue').Ref<Role[]>}
* @description 存储所有角色的列表数据
*/
export const roles = ref<Role[]>([]);
/**
* 对话框显示状态响应式引用
* @type {import('vue').Ref<boolean>}
* @description 控制用户编辑对话框的显示和隐藏
*/
export const dialogVisible = ref(false);
/**
* 表单响应式数据
* @type {import('vue').Ref<UserForm>}
* @description 绑定到用户编辑表单的数据对象
*/
export const form = ref<UserForm>({
name: '',
job_number: '',
phone: '',
email: '',
remark: '',
role_id: 0,
password: '',
status: 'active'
});
/**
* 编辑状态标识
* @type {import('vue').Ref<number|null>}
* @description 存储当前正在编辑的用户ID,为null时表示新建模式
*/
export const editId = ref<number | null>(null);
/**
* 获取用户列表
*
* @async
* @function fetchUsers
* @returns {void}
* @description 从后端 API 获取所有用户列表,更新本地 users 响应式数据
*
* @example
* // 在组件加载时获取用户列表
* onMounted(() => {
* fetchUsers();
* });
*
* @throws {Error} 当API请求失败时会显示错误消息
* @since 1.0.0
*/
export function fetchUsers() {
userAPI.getUsers()
.then(res => {
users.value = res.data.users || res.data;
console.log('获取用户列表成功:', users.value);
})
.catch(error => {
if (error.response?.status === 401 || error.response?.status === 403) {
console.error('获取用户列表失败:', error);
ElMessage.error('获取用户列表失败,未登录或登录超时');
} else {
console.error('获取用户列表失败:', error);
const errorMessage = error.response?.data?.message || '未知错误';
ElMessage.error(`获取用户列表失败: ${errorMessage}`);
}
});
}
/**
* 获取角色列表
*
* @async
* @function fetchRoles
* @returns {void}
* @description 从后端 API 获取所有角色列表,更新本地 roles 响应式数据
*
* @throws {Error} 当API请求失败时会显示错误消息
* @since 1.0.0
*/
export function fetchRoles() {
roleAPI.getRoles()
.then(res => {
roles.value = res.data;
console.log('获取角色列表成功:', roles.value);
})
.catch(error => {
console.error('获取角色列表失败:', error);
const errorMessage = error.response?.data?.message || '未知错误';
ElMessage.error(`获取角色列表失败: ${errorMessage}`);
});
}
/**
* 打开对话框(新建模式)
*
* @function openDialog
* @returns {void}
* @description 打开用户编辑对话框并重置表单为新建模式,清空所有表单数据
*
* @example
* // 点击新增用户按钮
* <el-button @click="openDialog">新增用户</el-button>
*
* @since 1.0.0
*/
export function openDialog() {
dialogVisible.value = true;
editId.value = null;
// 重置表单数据
form.value = {
name: '',
job_number: '',
phone: '',
email: '',
remark: '',
role_id: roles.value.length > 0 ? roles.value[0].id : 0,
password: '',
status: 'active'
};
}
/**
* 关闭对话框
*
* @function closeDialog
* @returns {void}
* @description 关闭用户编辑对话框并清空编辑状态
*
* @example
* // 点击取消按钮
* <el-button @click="closeDialog">取消</el-button>
*
* @since 1.0.0
*/
export function closeDialog() {
dialogVisible.value = false;
editId.value = null;
}
/**
* 表单验证功能
*
* @private
* @function validateForm
* @returns {boolean} 验证是否通过
* @description 检查表单必填字段和数据格式
*
* @throws {ElMessage} 验证失败时会弹出错误消息
* @since 1.0.0
*/
function validateForm(): boolean {
if (!form.value.name.trim()) {
ElMessage.error('请输入用户姓名');
return false;
}
if (!form.value.job_number.trim()) {
ElMessage.error('请输入工号');
return false;
}
if (!form.value.role_id) {
ElMessage.error('请选择角色');
return false;
}
// 如果是新建用户且未设置密码,则默认与工号一致
if (!editId.value && !form.value.password) {
form.value.password = form.value.job_number;
}
return true;
}
/**
* 提交用户信息(创建或更新)
*
* @async
* @function submitUser
* @returns {void}
* @description 验证表单数据并提交到后端,根据editId判断是创建还是更新操作
*
* @throws {Error} 当API请求失败时会显示错误消息
* @since 1.0.0
*
* @see {@link validateForm} - 表单验证函数
* @see {@link fetchUsers} - 刷新用户列表
* @see {@link closeDialog} - 关闭对话框
*/
export function submitUser() {
// 表单验证
if (!validateForm()) {
return;
}
const requestData = {
...form.value
};
const request = editId.value
? userAPI.updateUser(editId.value, requestData)
: userAPI.createUser(requestData);
request
.then(response => {
console.log(editId.value ? '更新用户成功:' : '创建用户成功:', response.data);
fetchUsers(); // 刷新列表
closeDialog(); // 关闭对话框
// 成功提示
ElMessage.success(editId.value ? '用户更新成功!' : '用户创建成功!');
})
.catch(error => {
console.error(editId.value ? '更新用户失败:' : '创建用户失败:', error);
// 错误提示
const errorMsg = error.response?.data?.message || '操作失败,请重试';
ElMessage.error(errorMsg);
});
}
/**
* 编辑用户
*
* @function editUser
* @param {User} item - 要编辑的用户对象
* @returns {void}
* @description 将指定用户的数据填充到编辑表单中,进入编辑模式
*
* @example
* // 点击编辑按钮
* <el-button @click="editUser(user)">编辑</el-button>
*
* @since 1.0.0
*/
export function editUser(item: User) {
dialogVisible.value = true;
editId.value = item.id;
// 填充表单数据
form.value = {
name: item.name || '',
job_number: item.job_number || '',
phone: item.phone || '',
email: item.email || '',
remark: item.remark || '',
role_id: item.role_id || 0,
password: '',
status: item.status || 'active'
};
}
/**
* 切换用户状态(启用/停用)
*
* @async
* @function toggleUserStatus
* @param {User} item - 要切换状态的用户对象
* @returns {void}
* @description 切换用户的启用/停用状态
*
* @throws {Error} 当API请求失败时会显示错误消息
* @since 1.0.0
*
* @see {@link fetchUsers} - 刷新用户列表
*/
export function toggleUserStatus(item: User) {
const newStatus = item.status === 'active' ? 'inactive' : 'active';
const actionText = newStatus === 'active' ? '启用' : '停用';
ElMessageBox.confirm(
`确定要${actionText}用户"${item.name}"吗?`,
`${actionText}确认`,
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}
).then(() => {
// 这里应该调用后端API更新用户状态
// 由于API文档中没有直接更新状态的接口,我们使用PUT更新整个用户信息
const requestData = {
...item,
status: newStatus
};
userAPI.updateUser(item.id, requestData)
.then(() => {
console.log(`${actionText}用户成功:`, item.id);
fetchUsers(); // 刷新列表
ElMessage.success(`${actionText}用户成功!`);
})
.catch(error => {
console.error(`${actionText}用户失败:`, error);
const errorMsg = error.response?.data?.message || '操作失败,请重试';
ElMessage.error(errorMsg);
});
}).catch(() => {
// 用户取消操作,什么都不做
});
}
/**
* 根据角色ID获取角色名称
*
* @function getRoleName
* @param {number} roleId - 角色ID
* @returns {string} 角色名称
* @description 根据角色ID查找对应的角色名称
*
* @since 1.0.0
*/
export function getRoleName(roleId: number): string {
const role = roles.value.find(r => r.id === roleId);
return role ? role.name : '未知角色';
} |