进一步完善登录注册功能

依旧没有测试
This commit is contained in:
Chenx221 2024-02-09 13:54:07 +08:00
parent 7303af1048
commit c066f92a33
3 changed files with 76 additions and 26 deletions

View File

@ -17,6 +17,9 @@ use yii\web\IdentityInterface;
*/ */
class User extends \yii\db\ActiveRecord implements IdentityInterface class User extends \yii\db\ActiveRecord implements IdentityInterface
{ {
public $password2; // 重复密码
public $rememberMe; // 记住我
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -26,15 +29,27 @@ class User extends \yii\db\ActiveRecord implements IdentityInterface
} }
/** /**
* // rules说明
* // 1. username, password, password2, email 必填
* // 2. username 长度在3-12之间
* // 3. password 长度在6-12之间
* // 4. password2 必须和password一致
* // 5. email 必须是邮箱格式
* // 6. username, email 必须是唯一的
* *
* {@inheritdoc} * {@inheritdoc}
*/ */
public function rules() public function rules()
{ {
return [ return [
[['username', 'password'], 'required', 'on' => 'login'], [['username', 'password', 'password2'], 'required', 'on' => 'login'],
[['username', 'password', 'email'], 'required', 'on' => 'register'], [['username', 'password', 'email', 'password2'], 'required', 'on' => 'register'],
[['username', 'password', 'auth_key', 'email'], 'string', 'max' => 255], ['username', 'string', 'min' => 3, 'max' => 12],
[['status'], 'integer'], ['password', 'string', 'min' => 6, 'max' => 12],
['password2', 'compare', 'compareAttribute' => 'password', 'on' => 'register'],
['email', 'email', 'on' => 'register'],
['username', 'unique', 'on' => 'register'],
['email', 'unique', 'on' => 'register'],
]; ];
} }
@ -110,7 +125,7 @@ class User extends \yii\db\ActiveRecord implements IdentityInterface
} }
/** /**
* 验证用户名密码 * 用户登录处理
* *
* @return bool 返回用户名密码验证状态 * @return bool 返回用户名密码验证状态
*/ */
@ -119,13 +134,22 @@ class User extends \yii\db\ActiveRecord implements IdentityInterface
$user = User::findOne(['username' => $this->username]); $user = User::findOne(['username' => $this->username]);
if ($user !== null && $user->validatePassword($this->password)) { if ($user !== null && $user->validatePassword($this->password)) {
return Yii::$app->user->login($user); // check user status
if ($user->status == 0) {
$this->addError('username', '此用户已被禁用,请联系管理员获取支持');
return false;
}
$rememberMe = $this->rememberMe ? 3600 * 24 * 30 : 0;
return Yii::$app->user->login($user, $rememberMe);
} }
return false; return false;
} }
/** /**
* 验证密码
*
* @param $password * @param $password
* @return bool * @return bool
*/ */

View File

@ -1,22 +1,35 @@
<?php <?php
use yii\helpers\Html; use yii\bootstrap5\Html;
use yii\widgets\ActiveForm; use yii\bootstrap5\ActiveForm;
/** @var yii\web\View $this */ /** @var yii\web\View $this */
/** @var app\models\User $model */ /** @var app\models\User $model */
/** @var ActiveForm $form */ /** @var ActiveForm $form */
$this->title = '用户登录';
$this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="user-login"> <div class="user-login">
<h1><?= Html::encode($this->title) ?></h1>
<?php $form = ActiveForm::begin(); ?> <p>请在下方输入你的用户凭证:</p>
<?= $form->field($model, 'username')->label('用户名') ?> <div class="row">
<?= $form->field($model, 'password')->label('密码') ?> <div class="col-lg-5">
<?php $form = ActiveForm::begin();?>
<div class="form-group"> <?= $form->field($model, 'username')->label('用户名')->textInput(['autofocus' => true]) ?>
<?= Html::submitButton('登录', ['class' => 'btn btn-primary']) ?> <?= $form->field($model, 'password')->passwordInput()->label('密码') ?>
<?= $form->field($model, 'rememberMe')->checkbox()->label('记住本次登录') ?>
<div class="form-group">
<?= Html::submitButton('登录', ['class' => 'btn btn-primary']) ?>
</div>
<div class="form-group">
<?= Html::a('还没有账户? 点击注册', ['user/register']) ?>
</div>
<?php ActiveForm::end(); ?>
</div> </div>
<?php ActiveForm::end(); ?> </div>
</div><!-- user-login --> </div><!-- user-login -->

View File

@ -1,23 +1,36 @@
<?php <?php
use yii\helpers\Html; use yii\bootstrap5\Html;
use yii\widgets\ActiveForm; use yii\bootstrap5\ActiveForm;
/** @var yii\web\View $this */ /** @var yii\web\View $this */
/** @var app\models\User $model */ /** @var app\models\User $model */
/** @var ActiveForm $form */ /** @var ActiveForm $form */
$this->title = '用户注册';
$this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="user-register"> <div class="user-register">
<h1><?= Html::encode($this->title) ?></h1>
<?php $form = ActiveForm::begin(); ?> <p>请在下方输入注册用户所需的信息:</p>
<?= $form->field($model, 'username')->label('用户名') ?> <div class="row">
<?= $form->field($model, 'password')->label('密码') ?> <div class="col-lg-5">
<?= $form->field($model, 'email')->label('电子邮箱') ?> <?php $form = ActiveForm::begin(); ?>
<div class="form-group"> <?= $form->field($model, 'username')->label('用户名')->textInput(['autofocus' => true]) ?>
<?= Html::submitButton('注册', ['class' => 'btn btn-primary']) ?> <?= $form->field($model, 'password')->passwordInput()->label('密码') ?>
<?= $form->field($model, 'password2')->passwordInput()->label('重复密码') ?>
<?= $form->field($model, 'email')->label('电子邮箱') ?>
<div class="form-group">
<?= Html::submitButton('注册', ['class' => 'btn btn-primary']) ?>
</div>
<div class="form-group">
<?= Html::a('已经有账户? 点击登录', ['user/login']) ?>
</div>
<?php ActiveForm::end(); ?>
</div> </div>
<?php ActiveForm::end(); ?> </div>
</div><!-- user-register --> </div><!-- user-register -->