StoreMigrationController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use app\models\Migration;
  9. use app\models\Store;
  10. use app\jobs\migration\ExportJob;
  11. use app\jobs\migration\ImportJob;
  12. class StoreMigrationController extends BaseController
  13. {
  14. public function actionGetList()
  15. {
  16. $query = Migration::find();
  17. $pagination = pagination_make($query, true, 'id DESC');
  18. $list = $pagination['list'];
  19. foreach ($list as $k => $v) {
  20. $list[$k]['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
  21. $list[$k]['store_name'] = '';
  22. $store = Store::find()->where(['id' => $v['store_id']])->select('name')->one();
  23. if ($store) {
  24. $list[$k]['store_name'] = $store['name'];
  25. }
  26. }
  27. return $this->asJson([
  28. 'code' => 0,
  29. 'msg' => 'success',
  30. 'data' => [
  31. 'isDuli' => \Yii::$app->prod_is_duli(),
  32. 'data' => $list,
  33. 'pageNo' => $pagination['pageNo'],
  34. 'totalCount' => $pagination['totalCount'],
  35. ],
  36. ]);
  37. }
  38. public function actionGetStoreList()
  39. {
  40. $list = [];
  41. if (\Yii::$app->prod_is_duli()) {
  42. $store = Store::find()->where(['id' => DEFAULT_STORE_ID])->select('id, name')->one();
  43. if ($store) {
  44. $list[] = $store;
  45. }
  46. } else {
  47. $list = Store::find()->where([
  48. 'is_delete' => 0,
  49. ])->select('id, name')->asArray()->all();
  50. }
  51. return $this->asJson([
  52. 'code' => 0,
  53. 'msg' => 'success',
  54. 'data' => $list,
  55. ]);
  56. }
  57. public function actionAdd()
  58. {
  59. $store_id = \post_params('id');
  60. $store = Store::find()->where(['id' => $store_id, 'is_delete' => 0])->select('id')->one();
  61. if (!$store) {
  62. return $this->asJson([
  63. 'code' => 1,
  64. 'msg' =>'店铺不存在',
  65. ]);
  66. }
  67. $migration = new Migration();
  68. $migration->store_id = $store_id;
  69. $migration->status = 0;
  70. if ($migration->save()) {
  71. \queue_push(new ExportJob(['store_id' => $store_id, 'id' => $migration->id]));
  72. return $this->asJson([
  73. 'code' => 0,
  74. 'msg' =>'success',
  75. ]);
  76. }
  77. return $this->asJson([
  78. 'code' => 1,
  79. 'msg' =>'添加失败',
  80. ]);
  81. }
  82. public function actionDelete()
  83. {
  84. $id = \post_params('id');
  85. $migration = Migration::find()->where(['id' => $id])->one();
  86. if (!$migration) {
  87. return $this->asJson([
  88. 'code' => 1,
  89. 'msg' =>'迁移记录不存在',
  90. ]);
  91. }
  92. $file = $migration->down_url;
  93. $path = \Yii::$app->basePath;
  94. if ($file && file_exists($path . $file)) {
  95. unlink($path . $file);
  96. }
  97. if ($migration->delete()) {
  98. return $this->asJson([
  99. 'code' => 0,
  100. 'msg' =>'删除成功',
  101. ]);
  102. }
  103. return $this->asJson([
  104. 'code' => 1,
  105. 'msg' =>'删除失败',
  106. ]);
  107. }
  108. public function actionGetImport()
  109. {
  110. $runtime = \Yii::$app->basePath . '/runtime';
  111. // 获取runtime目录下以export_开头,并且.zip结尾的文件列表
  112. $files = glob($runtime . '/export_*.zip');
  113. // 只获取文件名
  114. $files = array_map(function ($file) {
  115. return basename($file);
  116. }, $files);
  117. return $this->asJson([
  118. 'code' => 0,
  119. 'msg' =>'success',
  120. 'data' => $files,
  121. ]);
  122. }
  123. public function actionImport()
  124. {
  125. $file = \post_params('file');
  126. $path = \Yii::$app->basePath . '/runtime/' . $file;
  127. if (!file_exists($path)) {
  128. return $this->asJson([
  129. 'code' => 1,
  130. 'msg' =>'文件不存在',
  131. ]);
  132. }
  133. // 在runtime目录下创建一个文件,用来做锁,防止多次创建导入任务
  134. $lock_file = \Yii::$app->basePath . '/runtime/import.lock';
  135. if (file_exists($lock_file)) {
  136. return $this->asJson([
  137. 'code' => 1,
  138. 'msg' =>'当前有导入任务正在排队或者执行中,请稍后再试',
  139. ]);
  140. }
  141. $id = \queue_push(new ImportJob(['file' => $file]));
  142. // 创建锁文件
  143. file_put_contents($lock_file, $id);
  144. return $this->asJson([
  145. 'code' => 0,
  146. 'msg' =>'导入任务已添加到队列, 请等待执行',
  147. ]);
  148. }
  149. }