From 63fef55f654c9945ea9eb7349b307fcb4cad1c96 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Mon, 4 Mar 2024 16:26:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=94=B6=E9=9B=86=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=92=8C=E5=88=86=E4=BA=AB=E7=9A=84=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91=20=E5=88=A0=E9=99=A4=E2=86=92=E7=A6=81?= =?UTF-8?q?=E7=94=A8=20*=E5=B7=B2=E7=9F=A5=E7=BC=BA=E9=99=B7=EF=BC=8C?= =?UTF-8?q?=E5=88=86=E4=BA=AB=E5=92=8C=E6=94=B6=E9=9B=86=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E8=A2=AB=E5=85=B6=E4=BB=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/CollectionController.php | 19 +++++++++++++++---- controllers/ShareController.php | 16 ++++++++++++++-- models/CollectionTasks.php | 4 +++- models/Share.php | 4 +++- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/controllers/CollectionController.php b/controllers/CollectionController.php index 08efee3..af22c19 100644 --- a/controllers/CollectionController.php +++ b/controllers/CollectionController.php @@ -46,7 +46,7 @@ class CollectionController extends Controller { $searchModel = new CollectionSearch(); $dataProvider = $searchModel->search($this->request->queryParams); - + $dataProvider->query->andWhere(['!=', 'status', 0]); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, @@ -101,7 +101,18 @@ class CollectionController extends Controller */ public function actionDelete($id) { - $this->findModel($id)->delete(); + // 获取模型 + $model = $this->findModel($id); + + // 设置状态为禁用 + $model->status = 0; + + // 保存模型 + if ($model->save()) { + Yii::$app->session->setFlash('success', 'Task delete successfully.'); + } else { + Yii::$app->session->setFlash('error', 'Failed to delete task.'); + } return $this->redirect(['index']); } @@ -137,8 +148,8 @@ class CollectionController extends Controller $secret = $receive_secret; } $model = CollectionTasks::findOne(['id' => $id]); - if ($model === null) { - throw new NotFoundHttpException('请求的文件收集任务不存在'); + if ($model === null | $model->status === 0) { + throw new NotFoundHttpException('请求的文件收集任务已失效或不存在'); } elseif (!is_dir(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->user_id . '/' . $model->folder_path)) { throw new NotFoundHttpException('收集任务的目标路径不存在'); } elseif ($secret === null) { diff --git a/controllers/ShareController.php b/controllers/ShareController.php index 028494e..d696eb1 100644 --- a/controllers/ShareController.php +++ b/controllers/ShareController.php @@ -43,6 +43,7 @@ class ShareController extends Controller { $searchModel = new ShareSearch(); $dataProvider = $searchModel->search($this->request->queryParams); + $dataProvider->query->andWhere(['!=', 'status', 0]); return $this->render('index', [ 'searchModel' => $searchModel, @@ -138,7 +139,18 @@ class ShareController extends Controller */ public function actionDelete($share_id) { - $this->findModel($share_id)->delete(); + // 获取模型 + $model = $this->findModel($share_id); + + // 设置状态为禁用 + $model->status = 0; + + // 保存模型 + if ($model->save()) { + Yii::$app->session->setFlash('success', 'Share delete successfully.'); + } else { + Yii::$app->session->setFlash('error', 'Failed to delete share.'); + } return $this->redirect(['index']); } @@ -164,7 +176,7 @@ class ShareController extends Controller $model = $this->findModel($share_id); //检查文件/文件夹是否存在 $abp = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->sharer_id . '/' . $model->file_relative_path; - if (!file_exists($abp)) { + if (!file_exists($abp) || $model->status == 0) { throw new NotFoundHttpException('分享失效,文件或文件夹不存在'); } if ($this->request->isPost) { diff --git a/models/CollectionTasks.php b/models/CollectionTasks.php index bed7b5e..30afdcd 100644 --- a/models/CollectionTasks.php +++ b/models/CollectionTasks.php @@ -14,6 +14,7 @@ use yii\db\ActiveRecord; * @property string $folder_path 收集目标文件夹(相对路径) * @property string $created_at 收集任务创建时间 * @property string $secret 访问密钥 + * @property int|null $status 收集任务是否启用 * * @property CollectionUploaded[] $collectionUploadeds * @property User $user @@ -38,7 +39,7 @@ class CollectionTasks extends ActiveRecord return [ [['user_id', 'folder_path', 'secret'], 'required'], [['folder_path', 'secret'], 'required', 'on' => self::SCENARIO_CREATE], - [['user_id'], 'integer'], + [['user_id', 'status'], 'integer'], [['created_at'], 'safe'], [['folder_path', 'secret'], 'string', 'max' => 255], [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']], @@ -56,6 +57,7 @@ class CollectionTasks extends ActiveRecord 'folder_path' => '收集目标文件夹(相对路径)', 'created_at' => '任务创建时间', 'secret' => '访问密钥', + 'status' => 'Status', ]; } diff --git a/models/Share.php b/models/Share.php index 4ef6efe..917a474 100644 --- a/models/Share.php +++ b/models/Share.php @@ -14,6 +14,7 @@ use yii\db\ActiveRecord; * @property string $file_relative_path 文件的相对路径 * @property string $access_code 分享密钥 * @property string $creation_date 分享创建日期 + * @property int|null $status 分享是否启用 * * @property User $sharer */ @@ -35,7 +36,7 @@ class Share extends ActiveRecord { return [ [['file_relative_path', 'access_code'], 'required'], - [['sharer_id'], 'integer'], + [['sharer_id', 'status'], 'integer'], [['creation_date'], 'safe'], [['file_relative_path'], 'string', 'max' => 255], [['access_code'], 'string', 'max' => 4], @@ -55,6 +56,7 @@ class Share extends ActiveRecord 'file_relative_path' => '文件位置', 'access_code' => '访问密码', 'creation_date' => '分享创建日期', + 'status' => 'Status', ]; }