['文件不存在'], ]); } $import = new UserImport(); Excel::import($import, $realPath); // 删除临时文件 if (file_exists($realPath)) { unlink($realPath); } return [ 'success_count' => $import->getSuccessCount(), 'error_count' => $import->getErrorCount(), 'errors' => $import->getErrors(), ]; } /** * 导入部门数据 */ public function importDepartments(string $filePath, string $realPath): array { if (!file_exists($realPath)) { throw ValidationException::withMessages([ 'file' => ['文件不存在'], ]); } $import = new DepartmentImport(); Excel::import($import, $realPath); // 删除临时文件 if (file_exists($realPath)) { unlink($realPath); } return [ 'success_count' => $import->getSuccessCount(), 'error_count' => $import->getErrorCount(), 'errors' => $import->getErrors(), ]; } /** * 获取导出文件路径 */ public function getExportFilePath(string $filename): string { return storage_path('app/exports/' . $filename); } /** * 下载角色导入模板 */ public function downloadRoleTemplate(): string { $filename = 'role_import_template_' . date('YmdHis') . '.xlsx'; // 确保目录存在 if (!is_dir(storage_path('app/exports'))) { mkdir(storage_path('app/exports'), 0755, true); } $templateData = [ [ '角色名称*', '角色编码*', '描述', '权限(多个用逗号分隔)', '排序', ], [ '测试角色', 'test_role', '这是一个测试角色', '查看用户,编辑用户', '1', ], ]; Excel::store(new \App\Exports\GenericExport($templateData), 'exports/' . $filename); return $filename; } /** * 导出角色数据 */ public function exportRoles(array $roleIds = []): string { $filename = 'roles_export_' . date('YmdHis') . '.xlsx'; // 确保目录存在 if (!is_dir(storage_path('app/exports'))) { mkdir(storage_path('app/exports'), 0755, true); } Excel::store(new RoleExport($roleIds), 'exports/' . $filename); return $filename; } /** * 导入角色数据 */ public function importRoles(string $filePath, string $realPath): array { if (!file_exists($realPath)) { throw ValidationException::withMessages([ 'file' => ['文件不存在'], ]); } $import = new RoleImport(); Excel::import($import, $realPath); // 删除临时文件 if (file_exists($realPath)) { unlink($realPath); } return [ 'success_count' => $import->getSuccessCount(), 'error_count' => $import->getErrorCount(), 'errors' => $import->getErrors(), ]; } /** * 下载权限导入模板 */ public function downloadPermissionTemplate(): string { $filename = 'permission_import_template_' . date('YmdHis') . '.xlsx'; // 确保目录存在 if (!is_dir(storage_path('app/exports'))) { mkdir(storage_path('app/exports'), 0755, true); } $templateData = [ [ '权限标题*', '权限编码*', '权限类型*', '父级ID', '路由路径', '前端组件', '元数据', '排序', ], [ '系统管理', 'system', '菜单', '0', '/system', null, 'icon:Setting', '1', ], ]; Excel::store(new \App\Exports\GenericExport($templateData), 'exports/' . $filename); return $filename; } /** * 导出权限数据 */ public function exportPermissions(array $permissionIds = []): string { $filename = 'permissions_export_' . date('YmdHis') . '.xlsx'; // 确保目录存在 if (!is_dir(storage_path('app/exports'))) { mkdir(storage_path('app/exports'), 0755, true); } Excel::store(new PermissionExport($permissionIds), 'exports/' . $filename); return $filename; } /** * 导入权限数据 */ public function importPermissions(string $filePath, string $realPath): array { if (!file_exists($realPath)) { throw ValidationException::withMessages([ 'file' => ['文件不存在'], ]); } $import = new PermissionImport(); Excel::import($import, $realPath); // 删除临时文件 if (file_exists($realPath)) { unlink($realPath); } return [ 'success_count' => $import->getSuccessCount(), 'error_count' => $import->getErrorCount(), 'errors' => $import->getErrors(), ]; } /** * 删除导出文件 */ public function deleteExportFile(string $filename): bool { $path = $this->getExportFilePath($filename); if (file_exists($path)) { return unlink($path); } return true; } }