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 | 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 5x 5x 8x 8x 3x 3x 3x 8x 8x 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 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 3x 3x 3x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 3x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 4x 8x 8x 8x 3x 3x 3x 3x 3x 3x 8x 8x 2x 2x 2x 2x 2x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 2x 2x 2x 4x 4x 2x 2x 2x 4x 5x 1x 5x 5x | /**
* 角色管理业务逻辑模块
* 提供角色的增删改查等功能
*
* @fileOverview 角色管理模块,基于Vue 3 Composition API实现
* @author InspectionFlowSystem Team
* @version 1.0.0
* @since 2025-11-04
*/
import { ref } from 'vue';
import api, { roleAPI } from '../../api/index';
import { ElMessage, ElMessageBox } from 'element-plus';
/**
* 角色数据接口
* @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 Permission
* @description 定义权限的完整数据结构
* @since 1.0.0
*/
export interface Permission {
/** 权限ID */
id: number;
/** 权限名称 */
name: string;
/** 权限代码 */
code: string;
}
/**
* 角色表单数据接口
* @interface RoleForm
* @description 定义角色表单的完整数据结构
* @since 1.0.0
*/
export interface RoleForm {
/** 角色名称 */
name: string;
/** 备注信息 */
remark: string;
/** 权限ID列表 */
permissions: number[];
}
/**
* 角色列表响应式引用
* @type {import('vue').Ref<Role[]>}
* @description 存储所有角色的列表数据
*/
export const roles = ref<Role[]>([]);
/**
* 所有权限列表响应式引用
* @type {import('vue').Ref<Permission[]>}
* @description 存储所有权限的列表数据
*/
export const allPermissions = ref<Permission[]>([
{ id: 1, name: '创建任务', code: 'create_task' },
{ id: 2, name: '编辑任务', code: 'edit_task' },
{ id: 3, name: '删除任务', code: 'delete_task' },
{ id: 4, name: '审核任务', code: 'review_task' },
{ id: 5, name: '管理用户', code: 'manage_user' }
]);
/**
* 对话框显示状态响应式引用
* @type {import('vue').Ref<boolean>}
* @description 控制角色编辑对话框的显示和隐藏
*/
export const dialogVisible = ref(false);
/**
* 表单响应式数据
* @type {import('vue').Ref<RoleForm>}
* @description 绑定到角色编辑表单的数据对象
*/
export const form = ref<RoleForm>({
name: '',
remark: '',
permissions: []
});
/**
* 编辑状态标识
* @type {import('vue').Ref<number|null>}
* @description 存储当前正在编辑的角色ID,为null时表示新建模式
*/
export const editId = ref<number | null>(null);
/**
* 获取角色列表
*
* @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: '',
remark: '',
permissions: []
};
}
/**
* 关闭对话框
*
* @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;
}
return true;
}
/**
* 提交角色信息(创建或更新)
*
* @async
* @function submitRole
* @returns {void}
* @description 验证表单数据并提交到后端,根据editId判断是创建还是更新操作
*
* @throws {Error} 当API请求失败时会显示错误消息
* @since 1.0.0
*
* @see {@link validateForm} - 表单验证函数
* @see {@link fetchRoles} - 刷新角色列表
* @see {@link closeDialog} - 关闭对话框
*/
export function submitRole() {
// 表单验证
if (!validateForm()) {
return;
}
const requestData = {
...form.value
};
// 在发起请求前保存editId的状态,避免异步操作中的状态变化影响消息提示
const isEditing = !!editId.value;
const request = editId.value
? roleAPI.updateRole(editId.value, requestData)
: roleAPI.createRole(requestData);
request
.then(response => {
console.log(isEditing ? '更新角色成功:' : '创建角色成功:', response.data);
fetchRoles(); // 刷新列表
closeDialog(); // 关闭对话框
// 使用保存的状态来显示正确的消息提示
ElMessage.success(isEditing ? '角色更新成功!' : '角色创建成功!');
})
.catch(error => {
console.error(isEditing ? '更新角色失败:' : '创建角色失败:', error);
// 错误提示
const errorMsg = error.response?.data?.message || '操作失败,请重试';
ElMessage.error(errorMsg);
});
}
/**
* 编辑角色
*
* @function editRole
* @param {Role} item - 要编辑的角色对象
* @returns {void}
* @description 将指定角色的数据填充到编辑表单中,进入编辑模式
*
* @example
* // 点击编辑按钮
* <el-button @click="editRole(role)">编辑</el-button>
*
* @since 1.0.0
*/
export function editRole(item: Role) {
dialogVisible.value = true;
editId.value = item.id;
// 填充表单数据
form.value = {
name: item.name || '',
remark: item.remark || '',
permissions: item.permissions || []
};
}
/**
* 删除角色
*
* @async
* @function deleteRole
* @param {Role} item - 要删除的角色对象
* @returns {void}
* @description 删除指定角色
*
* @throws {Error} 当API请求失败时会显示错误消息
* @since 1.0.0
*
* @see {@link fetchRoles} - 刷新角色列表
*/
export function deleteRole(item: Role) {
ElMessageBox.confirm(
`确定要删除角色"${item.name}"吗?`,
'删除确认',
{
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}
).then(() => {
roleAPI.deleteRole(item.id)
.then(() => {
console.log('删除角色成功:', item.id);
fetchRoles(); // 刷新列表
ElMessage.success('角色删除成功!');
})
.catch(error => {
console.error('删除角色失败:', error);
const errorMsg = error.response?.data?.message || '删除失败,请重试';
ElMessage.error(errorMsg);
});
}).catch(() => {
// 用户取消操作,什么都不做
});
} |