From 4a22f950e544de598d723825a9f93d00cb0fba64 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Mon, 4 Mar 2024 16:51:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=88=A0=E9=99=A4=E8=87=AA?= =?UTF-8?q?=E8=BA=AB=E8=B4=A6=E6=88=B7=E5=8A=9F=E8=83=BD=20*=E4=BB=85?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E6=B5=8B=E8=AF=95=E4=BA=86=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/UserController.php | 29 +-- models/User.php | 16 ++ views/user/info.php | 343 ++++++++++++++++++++------------- web/js/user-info.js | 9 + 4 files changed, 256 insertions(+), 141 deletions(-) create mode 100644 web/js/user-info.js diff --git a/controllers/UserController.php b/controllers/UserController.php index d75d033..1c7a3e2 100644 --- a/controllers/UserController.php +++ b/controllers/UserController.php @@ -110,18 +110,23 @@ class UserController extends Controller ]); } - /** - * Deletes an existing User model. - * If deletion is successful, the browser will be redirected to the 'index' page. - * @param int $id ID - * @return Response - * @throws NotFoundHttpException if the model cannot be found - */ - public function actionDelete($id) + public function actionDelete(): Response { - $this->findModel($id)->delete(); + if (Yii::$app->user->isGuest) { + Yii::$app->session->setFlash('error', '滚'); + return $this->goHome(); + } - return $this->redirect(['index']); + $model = Yii::$app->user->identity; + + if ($model->deleteAccount()) { + Yii::$app->user->logout(); + Yii::$app->session->setFlash('success', 'Account deleted successfully.'); + } else { + Yii::$app->session->setFlash('error', 'Failed to delete account.'); + } + + return $this->redirect(['user/login']); } /** @@ -182,10 +187,10 @@ class UserController extends Controller Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误'); } } else { - Yii::$app->session->setFlash('error', 'Invalid username or password.'); + Yii::$app->session->setFlash('error', '用户名密码错误或账户已禁用'); } } else { - Yii::$app->session->setFlash('error', 'Invalid captcha.'); + Yii::$app->session->setFlash('error', '请等待验证码加载并完成验证'); } } return $this->render('login', [ diff --git a/models/User.php b/models/User.php index 4b0d479..8727582 100644 --- a/models/User.php +++ b/models/User.php @@ -252,4 +252,20 @@ class User extends ActiveRecord implements IdentityInterface return $url; } + public function deleteAccount(): false|int + { + // 设置用户状态为禁用 + $this->status = 0; + + // 保存用户模型 + if (!$this->save()) { + return false; // something wrong + } + // 更新与用户相关的所有 CollectionTasks 和 Share 记录的状态为禁用 + CollectionTasks::updateAll(['status' => 0], ['user_id' => $this->id]); + Share::updateAll(['status' => 0], ['sharer_id' => $this->id]); + + return true; + } + } \ No newline at end of file diff --git a/views/user/info.php b/views/user/info.php index 21c4b3c..59f0ca0 100644 --- a/views/user/info.php +++ b/views/user/info.php @@ -15,10 +15,14 @@ use app\utils\FileSizeHelper; use app\utils\IPLocation; use yii\bootstrap5\ActiveForm; use yii\bootstrap5\Html; +use yii\bootstrap5\Modal; use yii\helpers\Url; +use yii\web\JqueryAsset; +use yii\web\View; $this->title = '个人设置'; FontAwesomeAsset::register($this); +JqueryAsset::register($this); $this->registerCssFile('@web/css/user-info.css'); $details = IPLocation::getDetails($model->last_login_ip); // IP LOCATION @@ -33,55 +37,55 @@ $vaultUsedPercent = $is_unlimited ? 0 : round($vaultUsedSpace / ($storageLimit * $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百分比 ?> -
+