$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' =>'导入任务已添加到队列, 请等待执行', ]); } }