ImportJob.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\jobs\migration;
  3. use yii\base\BaseObject;
  4. use yii\queue\JobInterface;
  5. use app\models\Option;
  6. use app\models\AboutArticle;
  7. use app\models\Article;
  8. use app\models\AccountLog;
  9. use app\models\NewDiyTemplate;
  10. use app\models\Admin;
  11. use app\models\AdminRole;
  12. use app\models\Address;
  13. class ImportJob extends BaseObject implements JobInterface
  14. {
  15. public $file;
  16. public function execute($queue)
  17. {
  18. $file = \Yii::$app->basePath . '/runtime/' . $this->file;
  19. if (!file_exists($file)) {
  20. return;
  21. }
  22. // 判断runtime目录下是否存在import目录,如果有就删除
  23. $import_dir = \Yii::$app->basePath . '/runtime/import/';
  24. if (is_dir($import_dir)) {
  25. $this->deleteDirectory($import_dir);
  26. }
  27. // 解压缩文件
  28. $zip = new \ZipArchive();
  29. if ($zip->open($file) === true) {
  30. $zip->extractTo(\Yii::$app->basePath . '/runtime/import/');
  31. $zip->close();
  32. } else {
  33. return;
  34. }
  35. $this->importSql();
  36. $this->importFile();
  37. // 删除解压缩后的文件
  38. $this->deleteDirectory(\Yii::$app->basePath . '/runtime/import/');
  39. // 删除锁文件
  40. $lock_file = \Yii::$app->basePath . '/runtime/import.lock';
  41. if (file_exists($lock_file)) {
  42. unlink($lock_file);
  43. }
  44. }
  45. // 执行sql
  46. public function importSql()
  47. {
  48. try {
  49. // 删除option表中store_id为1的记录
  50. Option::deleteAll(['store_id' => 1]);
  51. // 删除about_article表中store_id为1的记录
  52. AboutArticle::deleteAll(['store_id' => 1]);
  53. Article::deleteAll(['store_id' => 1]);
  54. AccountLog::deleteAll(['store_id' => 1]);
  55. NewDiyTemplate::deleteAll(['store_id' => 1]);
  56. Address::deleteAll(['store_id' => 1]);
  57. // 删除admin表中id 不为1 的记录
  58. Admin::deleteAll(['!=', 'id', 1]);
  59. AdminRole::deleteAll();
  60. // 获取runtime目录下的sql文件
  61. // 设置max_allowed_packet
  62. \Yii::$app->db->createCommand('SET GLOBAL max_allowed_packet=1073741824')->execute();
  63. $sql_files = glob(\Yii::$app->basePath . '/runtime/import/sql/*.sql');
  64. foreach ($sql_files as $file) {
  65. $sql = file_get_contents($file);
  66. // 执行sql
  67. try {
  68. \Yii::$app->db->createCommand($sql)->execute();
  69. } catch (\Exception $ex) {
  70. // 记录具体SQL文件执行错误
  71. debug_log($ex->getMessage(), 'import_sql.log');
  72. continue;
  73. }
  74. }
  75. } catch (\Throwable $e) {
  76. // 记录错误日志
  77. debug_log($e->getMessage(), 'import_sql.log');
  78. return;
  79. }
  80. }
  81. public function importFile()
  82. {
  83. try {
  84. // 判断web/uploads目录下是否存在images/store_1目录, 没有就添加
  85. $image_path = \Yii::$app->basePath . '/web/uploads/images/store_1/';
  86. if (!is_dir($image_path)) {
  87. mkdir($image_path, 0777, true);
  88. }
  89. $this->copyDirectory(\Yii::$app->basePath . '/runtime/import/uploads/images/', $image_path);
  90. // 判断web/uploads目录下是否存在videos/store_1目录, 没有就添加
  91. $video_path = \Yii::$app->basePath . '/web/uploads/videos/store_1/';
  92. if (!is_dir($video_path)) {
  93. mkdir($video_path, 0777, true);
  94. }
  95. $this->copyDirectory(\Yii::$app->basePath . '/runtime/import/uploads/videos/', $video_path);
  96. } catch (\Throwable $e) {
  97. // 记录错误日志
  98. debug_log($e->getMessage(), 'import_file.log');
  99. return;
  100. }
  101. }
  102. // 删除文件夹
  103. public function deleteDirectory($dir)
  104. {
  105. if (!is_dir($dir)) {
  106. return;
  107. }
  108. $files = scandir($dir);
  109. foreach ($files as $file) {
  110. if ($file != '.' && $file != '..') {
  111. if (is_dir($dir . '/' . $file)) {
  112. $this->deleteDirectory($dir . '/' . $file);
  113. } else {
  114. unlink($dir . '/' . $file);
  115. }
  116. }
  117. }
  118. rmdir($dir);
  119. }
  120. public function copyDirectory($source, $destination)
  121. {
  122. if (!is_dir($source)) {
  123. return;
  124. }
  125. if (!is_dir($destination)) {
  126. mkdir($destination, 0777, true);
  127. }
  128. $files = scandir($source);
  129. foreach ($files as $file) {
  130. if ($file != '.' && $file != '..') {
  131. if (is_dir($source . '/' . $file)) {
  132. $this->copyDirectory($source . '/' . $file, $destination . '/' . $file);
  133. } else {
  134. copy($source . '/' . $file, $destination . '/' . $file);
  135. }
  136. }
  137. }
  138. }
  139. }