From 4a5f4201af21562ea0a4295592a78a229f9d1903 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Fri, 8 Mar 2024 16:20:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=BC=80=E5=85=B3=E5=A4=9C?= =?UTF-8?q?=E9=97=B4=E6=A8=A1=E5=BC=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/UserController.php | 24 +++++++++++++++++++----- views/user/info.php | 11 ++++++++--- web/js/user-info.js | 13 +++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/controllers/UserController.php b/controllers/UserController.php index be265cd..5b59527 100644 --- a/controllers/UserController.php +++ b/controllers/UserController.php @@ -40,12 +40,12 @@ class UserController extends Controller ], [ 'allow' => true, - 'actions' => ['login', 'register','verify-two-factor'], + 'actions' => ['login', 'register', 'verify-two-factor'], 'roles' => ['?', '@'], // everyone can access public share ], [ 'allow' => true, - 'actions' => ['logout', 'setup-two-factor', 'change-password', 'download-recovery-codes', 'remove-two-factor'], + 'actions' => ['logout', 'setup-two-factor', 'change-password', 'download-recovery-codes', 'remove-two-factor', 'set-theme'], 'roles' => ['@'], // only logged-in user can do these ] ], @@ -62,7 +62,8 @@ class UserController extends Controller 'setup-two-factor' => ['POST'], 'download-recovery-codes' => ['GET'], 'remove-two-factor' => ['POST'], - 'verify-two-factor' => ['GET','POST'], + 'verify-two-factor' => ['GET', 'POST'], + 'set-theme' => ['POST'], ], ], ] @@ -149,7 +150,7 @@ class UserController extends Controller //login without 2FA $user->last_login = date('Y-m-d H:i:s'); $user->last_login_ip = Yii::$app->request->userIP; - if (!$user->save(false)){ + if (!$user->save(false)) { Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误'); } Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0); @@ -186,7 +187,7 @@ class UserController extends Controller if ($otp->verify($model->totp_input)) { $user->last_login = date('Y-m-d H:i:s'); $user->last_login_ip = Yii::$app->request->userIP; - if (!$user->save(false)){ + if (!$user->save(false)) { Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误'); } Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0); @@ -488,4 +489,17 @@ class UserController extends Controller return $this->redirect(['user/info']); } } + + /** + * 更改用户主题 + * @return Response + */ + public function actionSetTheme(): Response + { + $darkMode = Yii::$app->request->post('dark_mode', 0); + $user = Yii::$app->user->identity; + $user->dark_mode = $darkMode; + $user->save(); + return $this->asJson(['success' => true]); + } } diff --git a/views/user/info.php b/views/user/info.php index d39a34c..7abd304 100644 --- a/views/user/info.php +++ b/views/user/info.php @@ -62,6 +62,9 @@ if (!is_null($totp_secret)) { // totp $user = new User(); + +// Dark Mode +$darkMode = Yii::$app->user->identity->dark_mode; ?>
@@ -262,12 +265,14 @@ $user = new User();

主题


- + >
- - + > +

diff --git a/web/js/user-info.js b/web/js/user-info.js index b87787a..03fc9e2 100644 --- a/web/js/user-info.js +++ b/web/js/user-info.js @@ -18,5 +18,18 @@ $(document).ready(function () { $('#totpSetupModal').on('hidden.bs.modal', function () { $('#totp-enabled').prop('checked', false); }); + $('#useDarkTheme').change(function() { + var darkMode = this.checked ? 1 : 0; + $.post('index.php?r=user%2Fset-theme', {dark_mode: darkMode}, function() { + location.reload(); + }); + }); + $('#followSystemTheme').change(function() { + $('#useDarkTheme').prop('checked', false); + var darkMode = this.checked ? 2 : 0; + $.post('index.php?r=user%2Fset-theme', {dark_mode: darkMode}, function() { + location.reload(); + }); + }); }); \ No newline at end of file