优化更新

This commit is contained in:
2026-02-11 17:13:18 +08:00
parent ada5e027fa
commit e265bcc28d
28 changed files with 1661 additions and 155 deletions

View File

@@ -10,8 +10,12 @@ use Illuminate\Validation\ValidationException;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\UserExport;
use App\Exports\DepartmentExport;
use App\Exports\RoleExport;
use App\Exports\PermissionExport;
use App\Imports\UserImport;
use App\Imports\DepartmentImport;
use App\Imports\RoleImport;
use App\Imports\PermissionImport;
class ImportExportService
{
@@ -187,6 +191,166 @@ class ImportExportService
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(),
];
}
/**
* 删除导出文件
*/