| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\models\Migration;
- use app\models\Store;
- use app\jobs\migration\ExportJob;
- use app\jobs\migration\ImportJob;
- class StoreMigrationController extends BaseController
- {
- public function actionGetList()
- {
- $query = Migration::find();
- $pagination = pagination_make($query, true, 'id DESC');
- $list = $pagination['list'];
- foreach ($list as $k => $v) {
- $list[$k]['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
- $list[$k]['store_name'] = '';
- $store = Store::find()->where(['id' => $v['store_id']])->select('name')->one();
- if ($store) {
- $list[$k]['store_name'] = $store['name'];
- }
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'isDuli' => \Yii::$app->prod_is_duli(),
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ]);
- }
- public function actionGetStoreList()
- {
- $list = [];
- if (\Yii::$app->prod_is_duli()) {
- $store = Store::find()->where(['id' => DEFAULT_STORE_ID])->select('id, name')->one();
- if ($store) {
- $list[] = $store;
- }
- } else {
- $list = Store::find()->where([
- 'is_delete' => 0,
- ])->select('id, name')->asArray()->all();
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- ]);
- }
- public function actionAdd()
- {
- $store_id = \post_params('id');
- $store = Store::find()->where(['id' => $store_id, 'is_delete' => 0])->select('id')->one();
- if (!$store) {
- return $this->asJson([
- 'code' => 1,
- 'msg' =>'店铺不存在',
- ]);
- }
- $migration = new Migration();
- $migration->store_id = $store_id;
- $migration->status = 0;
- if ($migration->save()) {
- \queue_push(new ExportJob(['store_id' => $store_id, 'id' => $migration->id]));
- return $this->asJson([
- 'code' => 0,
- 'msg' =>'success',
- ]);
- }
-
- return $this->asJson([
- 'code' => 1,
- 'msg' =>'添加失败',
- ]);
- }
- public function actionDelete()
- {
- $id = \post_params('id');
- $migration = Migration::find()->where(['id' => $id])->one();
- if (!$migration) {
- return $this->asJson([
- 'code' => 1,
- 'msg' =>'迁移记录不存在',
- ]);
- }
- $file = $migration->down_url;
- $path = \Yii::$app->basePath;
- if ($file && file_exists($path . $file)) {
- unlink($path . $file);
- }
- if ($migration->delete()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' =>'删除成功',
- ]);
- }
-
- return $this->asJson([
- 'code' => 1,
- 'msg' =>'删除失败',
- ]);
- }
- public function actionGetImport()
- {
- $runtime = \Yii::$app->basePath . '/runtime';
- // 获取runtime目录下以export_开头,并且.zip结尾的文件列表
- $files = glob($runtime . '/export_*.zip');
- // 只获取文件名
- $files = array_map(function ($file) {
- return basename($file);
- }, $files);
- return $this->asJson([
- 'code' => 0,
- 'msg' =>'success',
- 'data' => $files,
- ]);
- }
- public function actionImport()
- {
- $file = \post_params('file');
- $path = \Yii::$app->basePath . '/runtime/' . $file;
- if (!file_exists($path)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' =>'文件不存在',
- ]);
- }
- // 在runtime目录下创建一个文件,用来做锁,防止多次创建导入任务
- $lock_file = \Yii::$app->basePath . '/runtime/import.lock';
- if (file_exists($lock_file)) {
- return $this->asJson([
- 'code' => 1,
- 'msg' =>'当前有导入任务正在排队或者执行中,请稍后再试',
- ]);
- }
- $id = \queue_push(new ImportJob(['file' => $file]));
- // 创建锁文件
- file_put_contents($lock_file, $id);
- return $this->asJson([
- 'code' => 0,
- 'msg' =>'导入任务已添加到队列, 请等待执行',
- ]);
- }
- }
|