AppearanceImPortForm.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace App\Admin\Forms;
  3. use App\Admin\Renderable\DistDistributorTable;
  4. use App\Admin\Repositories\DistAppearance;
  5. use App\Admin\Repositories\DistAppearanceTemplate;
  6. use App\Admin\Repositories\DistInquiry;
  7. use Dcat\Admin\Models\DistAdminUser;
  8. use Dcat\Admin\Widgets\Form;
  9. use Dcat\Admin\Contracts\LazyRenderable;
  10. use Dcat\Admin\Traits\LazyWidget;
  11. use ZipArchive;
  12. class AppearanceImPortForm extends Form implements LazyRenderable
  13. {
  14. use LazyWidget;
  15. protected $appearanceId = 0;
  16. // 处理请求
  17. public function handle(array $input)
  18. {
  19. // 获取外部传递参数
  20. $appearanceId = $this->payload['id'] ?? null;
  21. $file = $input['file'] ?? null;
  22. $appearanceRs = DistAppearance::getOneById($appearanceId);
  23. if ($file && $appearanceRs) {
  24. $folder = $appearanceRs->folder;
  25. $extractPath = resource_path().'/appearance/tmp';
  26. $path = resource_path().'/appearance/'.$folder;
  27. //解压
  28. $unzip = $this->unzip($file,$extractPath,$path);
  29. if (!$unzip['status']) {
  30. return $this->response()->error($unzip['error']);
  31. }
  32. //导入模板
  33. if (!is_dir($path)) {
  34. return $this->response()->error("The directory does not exist")->refresh();
  35. }
  36. $this->sourcePath = $path;
  37. $this->appearanceId = $appearanceId;
  38. // 清空旧的
  39. DistAppearanceTemplate::deleteTemplates($appearanceId, config('dictionary.base_dist_id'));
  40. // 导入模板
  41. $this->readDirectory($path);
  42. // 更新状态
  43. DistAppearance::setStatusToImported($appearanceId);
  44. return $this->response()->success('allocation successful')->refresh();
  45. }
  46. return $this->response()->error('No file data');
  47. }
  48. /*
  49. * 下载并解压
  50. */
  51. private function unzip($savePath,$extractPath,$trulyPath)
  52. {
  53. // 检查 $extractPath 是否存在且是否是一个目录
  54. if (!is_dir($extractPath)) {
  55. mkdir($extractPath, 0755, true);
  56. }
  57. if (!is_dir($trulyPath)) {
  58. mkdir($trulyPath, 0755, true);
  59. }
  60. $savePath = storage_path().'/app/'.$savePath;
  61. // 检查是否支持 ZipArchive 扩展
  62. if (!extension_loaded('zip')) {
  63. return ['status' => false, 'error' => 'ZipArchive 扩展未加载'];
  64. }
  65. // 保存文件到本地
  66. $zip = new ZipArchive;
  67. if ($zip->open($savePath) !== TRUE) {
  68. return ['status' => false, 'error' => 'ZipArchive 解压失败'];
  69. }
  70. if ($zip->extractTo($extractPath) !== TRUE) {
  71. return ['status' => false, 'error' => "解压文件到 $extractPath 失败。"];
  72. }
  73. $zip->close();
  74. //判断tmp下是否只有一个文件夹,如果是,移动到trulyPath
  75. // 递归删除 $trulyPath 下的所有文件和文件夹
  76. $deleteDirectoryContents = $this->deleteDirectoryContents($trulyPath);
  77. if ($deleteDirectoryContents) {
  78. return ['status' => false, 'error' => $deleteDirectoryContents];
  79. }
  80. // 获取 $extractPath 下的所有文件和文件夹
  81. $items = scandir($extractPath);
  82. // 过滤掉 '.' 和 '..'
  83. $items = array_diff($items, ['.', '..']);
  84. // 判断 $extractPath 是否只有一个文件夹
  85. if (count($items) != 1 || !is_dir($extractPath . '\\' . reset($items))) {
  86. return ['status' => false, 'error' => "Error: unzip file does not contain exactly one folder."];
  87. }
  88. // 获取文件夹名称
  89. $folderName = reset($items);
  90. $folderPath = $extractPath . '\\' . $folderName;
  91. // 获取文件夹下的所有文件和文件夹
  92. $folderContents = scandir($folderPath);
  93. $folderContents = array_diff($folderContents, ['.', '..']);
  94. // 将文件夹中的所有内容移动到 $trulyPath
  95. foreach ($folderContents as $fileOrDir) {
  96. $sourcePath = $folderPath . '\\' . $fileOrDir;
  97. $destinationPath = $trulyPath . '\\' . $fileOrDir;
  98. // 如果是文件,移动文件
  99. if (is_file($sourcePath)) {
  100. if (!rename($sourcePath, $destinationPath)) {
  101. return ['status' => false, 'error' => "Error: Failed to move file $fileOrDir\n"];
  102. }
  103. }
  104. // 如果是目录,递归移动目录内容
  105. elseif (is_dir($sourcePath)) {
  106. // 创建目标文件夹
  107. if (!mkdir($destinationPath)) {
  108. return ['status' => false, 'error' => "Error: Failed to create folder $fileOrDir\n"];
  109. }
  110. // 递归移动文件夹内容
  111. $folderFiles = scandir($sourcePath);
  112. $folderFiles = array_diff($folderFiles, ['.', '..']);
  113. foreach ($folderFiles as $subFile) {
  114. $subSourcePath = $sourcePath . '\\' . $subFile;
  115. $subDestinationPath = $destinationPath . '\\' . $subFile;
  116. if (!rename($subSourcePath, $subDestinationPath)) {
  117. return ['status' => false, 'error' => "Error: Failed to move subfolder file $subFile\n"];
  118. }
  119. }
  120. }
  121. }
  122. // 删除原文件夹及其内容
  123. rmdir($folderPath);
  124. return ['status' => true];
  125. }
  126. // 递归删除文件夹及其内容
  127. private function deleteDirectoryContents($dir)
  128. {
  129. // 获取目录下所有文件和文件夹
  130. $items = scandir($dir);
  131. // 过滤掉 '.' 和 '..'
  132. $items = array_diff($items, ['.', '..']);
  133. // 循环遍历目录下的每一项
  134. foreach ($items as $item) {
  135. $itemPath = $dir . '\\' . $item;
  136. // 如果是文件,删除文件
  137. if (is_file($itemPath)) {
  138. if (!unlink($itemPath)) {
  139. return "Error: Failed to delete file $itemPath\n";
  140. }
  141. }
  142. // 如果是目录,递归删除该目录
  143. elseif (is_dir($itemPath)) {
  144. deleteDirectoryContents($itemPath); // 递归删除目录内容
  145. if (!rmdir($itemPath)) {
  146. return "Error: Failed to delete directory $itemPath\n";
  147. }
  148. }
  149. }
  150. }
  151. public function form()
  152. {
  153. $this->file('file')
  154. ->disk('local') // 使用本地存储
  155. ->uniqueName()
  156. ->autoUpload()
  157. ->required()
  158. ->accept('zip')
  159. ->dir(config("admin.upload.directory.file").'/tmp/')
  160. ->help('Please upload a zip file');
  161. }
  162. private function readDirectory($dirPath) {
  163. // 检查目录是否存在
  164. if (!is_dir($dirPath)) {
  165. return;
  166. }
  167. // 打开目录
  168. $dir = opendir($dirPath);
  169. // 读取目录内容
  170. while (($file = readdir($dir)) !== false) {
  171. // 忽略当前目录和上级目录
  172. if ($file == '.' || $file == '..') {
  173. continue;
  174. }
  175. // 获取文件或文件夹的完整路径
  176. $fullPath = $dirPath . '/' . $file;
  177. // 如果是文件夹,递归读取
  178. if (is_dir($fullPath)) {
  179. $this->readDirectory($fullPath); // 递归调用
  180. } else {
  181. // 如果是文件,读取文件内容
  182. $content = file_get_contents($fullPath);
  183. $filePath = str_replace($this->sourcePath, '', $dirPath);
  184. $fileName = basename($fullPath);
  185. // 插入数据库
  186. DistAppearanceTemplate::insertTemplateContent(config('dictionary.base_dist_id'), $this->appearanceId, $filePath,$fileName, $content);
  187. }
  188. }
  189. // 关闭目录
  190. closedir($dir);
  191. }
  192. }