实现开关夜间模式功能

This commit is contained in:
Chenx221 2024-03-08 16:20:22 +08:00
parent d1683d4c03
commit 4a5f4201af
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
3 changed files with 40 additions and 8 deletions

View File

@ -40,12 +40,12 @@ class UserController extends Controller
], ],
[ [
'allow' => true, 'allow' => true,
'actions' => ['login', 'register','verify-two-factor'], 'actions' => ['login', 'register', 'verify-two-factor'],
'roles' => ['?', '@'], // everyone can access public share 'roles' => ['?', '@'], // everyone can access public share
], ],
[ [
'allow' => true, '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 'roles' => ['@'], // only logged-in user can do these
] ]
], ],
@ -62,7 +62,8 @@ class UserController extends Controller
'setup-two-factor' => ['POST'], 'setup-two-factor' => ['POST'],
'download-recovery-codes' => ['GET'], 'download-recovery-codes' => ['GET'],
'remove-two-factor' => ['POST'], '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 //login without 2FA
$user->last_login = date('Y-m-d H:i:s'); $user->last_login = date('Y-m-d H:i:s');
$user->last_login_ip = Yii::$app->request->userIP; $user->last_login_ip = Yii::$app->request->userIP;
if (!$user->save(false)){ if (!$user->save(false)) {
Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误'); Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误');
} }
Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0); 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)) { if ($otp->verify($model->totp_input)) {
$user->last_login = date('Y-m-d H:i:s'); $user->last_login = date('Y-m-d H:i:s');
$user->last_login_ip = Yii::$app->request->userIP; $user->last_login_ip = Yii::$app->request->userIP;
if (!$user->save(false)){ if (!$user->save(false)) {
Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误'); Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误');
} }
Yii::$app->user->login($user, $model->rememberMe ? 3600 * 24 * 30 : 0); 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 $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]);
}
} }

View File

@ -62,6 +62,9 @@ if (!is_null($totp_secret)) {
// totp // totp
$user = new User(); $user = new User();
// Dark Mode
$darkMode = Yii::$app->user->identity->dark_mode;
?> ?>
<div class="user-info"> <div class="user-info">
@ -262,12 +265,14 @@ $user = new User();
<h4>主题</h4> <h4>主题</h4>
<hr> <hr>
<div class="form-check form-switch"> <div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="useDarkTheme"> <input class="form-check-input" type="checkbox" role="switch"
id="useDarkTheme" <?= $darkMode === 0 ? '' : ($darkMode === 1 ? 'checked' : 'disabled') ?>>
<label class="form-check-label" for="useDarkTheme">启用夜间模式</label> <label class="form-check-label" for="useDarkTheme">启用夜间模式</label>
</div> </div>
<div class="form-check form-switch"> <div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="autoTheme"> <input class="form-check-input" type="checkbox" role="switch"
<label class="form-check-label" for="autoTheme">Auto</label> id="followSystemTheme" <?= $darkMode === 2 ? 'checked' : '' ?>>
<label class="form-check-label" for="autoTheme">跟随设备主题</label>
</div> </div>
<br> <br>

View File

@ -18,5 +18,18 @@ $(document).ready(function () {
$('#totpSetupModal').on('hidden.bs.modal', function () { $('#totpSetupModal').on('hidden.bs.modal', function () {
$('#totp-enabled').prop('checked', false); $('#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();
});
});
}); });