73 lines
1.7 KiB
PHP
73 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace service\models\forms;
|
|
|
|
use common\core\BaseModel;
|
|
use common\models\oldDb\ServiceUserToken;
|
|
use common\validators\MobileValidator;
|
|
use service\models\ServiceUser;
|
|
use yii\base\Exception;
|
|
|
|
class ResetPasswordForm extends BaseModel
|
|
{
|
|
public $mobile;
|
|
public $mobile_code;
|
|
public $password_new;
|
|
public $_user = null;
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['mobile', 'mobile_code', 'password_new'], 'required'],
|
|
['mobile', MobileValidator::class],
|
|
];
|
|
}
|
|
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'mobile' => '手机号',
|
|
'mobile_code' => '验证码',
|
|
'password_new' => '新密码',
|
|
];
|
|
}
|
|
|
|
public function resetPasswordAndLogin()
|
|
{
|
|
if (!$this->validate()) {
|
|
throw new Exception($this->getErrorMsg());
|
|
}
|
|
|
|
if ($this->mobile_code != $this->getCode()) {
|
|
throw new Exception('验证码错误');
|
|
}
|
|
|
|
$user = $this->getUser();
|
|
if (!$user) {
|
|
throw new Exception('用户信息不存在');
|
|
}
|
|
$user->setPassword($this->password_new);
|
|
$user->saveOrFail();
|
|
|
|
return [
|
|
'token' => ServiceUserToken::createToken($this->_user->id),
|
|
'user' => $this->_user,
|
|
];
|
|
}
|
|
|
|
public function getUser(): ?ServiceUser
|
|
{
|
|
if ($this->_user === null) {
|
|
$this->_user = ServiceUser::find()->where([
|
|
'mobile' => $this->mobile,
|
|
'is_delete' => 0
|
|
])->one();
|
|
}
|
|
return $this->_user;
|
|
}
|
|
|
|
public function getCode()
|
|
{
|
|
return 1234;
|
|
}
|
|
} |