AppearanceImPortForm.php 8.1 KB

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