AppearanceImPortForm.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. //解压后,停0.1秒
  75. usleep(100000);
  76. //判断tmp下是否只有一个文件夹,如果是,移动到trulyPath
  77. // 递归删除 $trulyPath 下的所有文件和文件夹
  78. $deleteDirectoryContents = $this->deleteDirectoryContents($trulyPath);
  79. if ($deleteDirectoryContents) {
  80. return ['status' => false, 'error' => $deleteDirectoryContents];
  81. }
  82. // 获取 $extractPath 下的所有文件和文件夹
  83. $items = scandir($extractPath);
  84. // 过滤掉 '.' 和 '..'
  85. $items = array_diff($items, ['.', '..']);
  86. // 判断 $extractPath 是否只有一个文件夹
  87. if (count($items) != 1 || !is_dir($extractPath . '\\' . reset($items))) {
  88. return ['status' => false, 'error' => "Error: unzip file does not contain exactly one folder."];
  89. }
  90. // 获取文件夹名称
  91. $folderName = reset($items);
  92. $folderPath = $extractPath . '\\' . $folderName;
  93. // 获取文件夹下的所有文件和文件夹
  94. $folderContents = scandir($folderPath);
  95. $folderContents = array_diff($folderContents, ['.', '..']);
  96. // 将文件夹中的所有内容移动到 $trulyPath
  97. foreach ($folderContents as $fileOrDir) {
  98. $sourcePath = $folderPath . '\\' . $fileOrDir;
  99. $destinationPath = $trulyPath . '\\' . $fileOrDir;
  100. // 如果是文件,移动文件
  101. if (is_file($sourcePath)) {
  102. if (!rename($sourcePath, $destinationPath)) {
  103. return ['status' => false, 'error' => "Error: Failed to move file $fileOrDir\n"];
  104. }
  105. }
  106. // 如果是目录,递归移动目录内容
  107. elseif (is_dir($sourcePath)) {
  108. // 创建目标文件夹
  109. if (!mkdir($destinationPath)) {
  110. return ['status' => false, 'error' => "Error: Failed to create folder $fileOrDir\n"];
  111. }
  112. // 递归移动文件夹内容
  113. $folderFiles = scandir($sourcePath);
  114. $folderFiles = array_diff($folderFiles, ['.', '..']);
  115. foreach ($folderFiles as $subFile) {
  116. $subSourcePath = $sourcePath . '\\' . $subFile;
  117. $subDestinationPath = $destinationPath . '\\' . $subFile;
  118. if (!rename($subSourcePath, $subDestinationPath)) {
  119. return ['status' => false, 'error' => "Error: Failed to move subfolder file $subFile\n"];
  120. }
  121. }
  122. }
  123. }
  124. // 删除原文件夹及其内容
  125. $this->deleteDirectoryContents($folderPath);
  126. return ['status' => true];
  127. }
  128. // 递归删除文件夹及其内容
  129. private function deleteDirectoryContents($dir)
  130. {
  131. // 获取目录下所有文件和文件夹
  132. $items = scandir($dir);
  133. // 过滤掉 '.' 和 '..'
  134. $items = array_diff($items, ['.', '..']);
  135. // 循环遍历目录下的每一项
  136. foreach ($items as $item) {
  137. $itemPath = $dir . '\\' . $item;
  138. // 如果是文件,删除文件
  139. if (is_file($itemPath)) {
  140. if (!unlink($itemPath)) {
  141. return "Error: Failed to delete file $itemPath\n";
  142. }
  143. }
  144. // 如果是目录,递归删除该目录
  145. elseif (is_dir($itemPath)) {
  146. $this->deleteDirectoryContents($itemPath); // 递归删除目录内容
  147. if (!rmdir($itemPath)) {
  148. return "Error: Failed to delete directory $itemPath\n";
  149. }
  150. }
  151. }
  152. }
  153. public function form()
  154. {
  155. $this->file('file')
  156. ->disk('local') // 使用本地存储
  157. ->uniqueName()
  158. ->autoUpload()
  159. ->required()
  160. ->accept('zip')
  161. ->dir(config("admin.upload.directory.file").'/tmp/')
  162. ->help('Please upload a zip file');
  163. }
  164. private function readDirectory($dirPath) {
  165. // 检查目录是否存在
  166. if (!is_dir($dirPath)) {
  167. return;
  168. }
  169. // 打开目录
  170. $dir = opendir($dirPath);
  171. // 读取目录内容
  172. while (($file = readdir($dir)) !== false) {
  173. // 忽略当前目录和上级目录
  174. if ($file == '.' || $file == '..') {
  175. continue;
  176. }
  177. // 获取文件或文件夹的完整路径
  178. $fullPath = $dirPath . '/' . $file;
  179. // 如果是文件夹,递归读取
  180. if (is_dir($fullPath)) {
  181. $this->readDirectory($fullPath); // 递归调用
  182. } else {
  183. // 如果是文件,读取文件内容
  184. $content = file_get_contents($fullPath);
  185. $filePath = str_replace($this->sourcePath, '', $dirPath);
  186. $filePath = trim($filePath, '/');
  187. $fileName = basename($fullPath);
  188. // 插入数据库
  189. DistAppearanceTemplate::insertTemplateContent(config('dictionary.base_dist_id'), $this->appearanceId, $filePath,$fileName, $content);
  190. }
  191. }
  192. // 关闭目录
  193. closedir($dir);
  194. }
  195. }