登录注册HASH值错误? [ 2.0 版本 ]
<?php
/**
* @abstract 用户表单模型
* @author Yxl <zccem@163.com>
*/
namespace frontend\models;
use Yii;
use yii\base\Model;
use common\models\User;
class MemberForm extends Model {
public $username;
public $password;
public $repassword;
public $nickname;
private $_user;
/**
* @abstract 验证规则
*/
public function rules() {
return [
// 必填
[['username'], 'required', 'message' => '用户名必须填写!'],
// unique表示唯一性,targetClass表示的数据模型 这里就是说UserBackend模型对应的数据表字段username必须唯一
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '用户名已存在!', 'on' => 'reg'],
// unique表示唯一性,targetClass表示的数据模型 这里就是说UserBackend模型对应的数据表字段nickname必须唯一
['nickname', 'unique', 'targetClass' => '\common\models\User', 'message' => '昵称已存在!', 'on' => 'reg'],
// email has to be a valid email address
[['nickname'], 'required', 'message' => '请输入属于你的昵称!', 'on' => 'reg'],
// 用户名过滤
['username', 'filter', 'filter' => 'trim'],
// 用户名长度最小为6位,最大16位
['username', 'string', 'min' => 6, 'max' => 30],
// 用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。
// ['username', 'match', 'pattern' => '/^[(\x{4E00}-\x{9FA5})a-zA-Z]+[(\x{4E00}-\x{9FA5})a-zA-Z_\d]*$/u', 'message' => '用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。'],
['password', 'required', 'message' => '密码必须填写!'],
['password', 'string', 'min' => 6, 'max' => 30],
// 确认密码
[['repassword'], 'required', 'message' => '确认密码没有输入!', 'on' => 'reg'],
// 确认密码和密码是否一致
['repassword', 'compare', 'compareAttribute' => 'password', 'message' => '两次输入的密码不一致!', 'on' => 'reg'],
// password is validated by validatePassword()
['password', 'validatePassword', 'on' => 'login'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
];
}
/**
* @abstract 指定属性标签
*/
public function attributeLabels() {
}
/**
* @abstract 场景
*/
public function scenarios() {
$scenarios = parent::scenarios();
$scenarios['login'] = ['username', 'password'];
$scenarios['reg'] = ['username', 'password', 'repassword', 'nickname'];
return $scenarios;
}
/**
* @abstract 注册
* @return true|false 添加成功或者添加失败
*/
public function signup() {
// 调用validate方法对表单数据进行验证,验证规则参考上面的rules方法
if (!$this->validate()) {
return FALSE;
}
$Cls = new User();
$Cls->username = $this->username;
$Cls->password = $Cls->setPassword($this->password);
$Cls->nickname = $this->nickname;
$Cls->last_login_time = time();
$Cls->reg_time = time();
$Cls->last_set_time = time();
$Cls->is_microhurt = 'Off';
$Cls->is_head = 'Off';
$Cls->is_display = 'On';
$Cls->is_using = 'Not';
$Cls->rkey = 'R7';
// save(false)的意思是:不调用UserBackend的rules再做校验并实现数据入库操作
// 这里这个false如果不加,save底层会调用UserBackend的rules方法再对数据进行一次校验,因为我们上面已经调用Signup的rules校验过了,这里就没必要在用UserBackend的rules校验了
if (!$Cls->save(FALSE)) {
return FALSE;
}
return $Cls;
}
/**
* @abstract 登录
* @return true|false 添加成功或者添加失败
*/
public function signin() {
// 调用validate方法对表单数据进行验证,验证规则参考上面的rules方法
if (!$this->validate()) {
return FALSE;
}
return Yii::$app->user->login($this->getUser(), (3600 * 24 * 30));
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params) {
if (!$this->hasErrors()) {
// 获取用户
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, '帐号密码有误!');
}
}
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
protected function getUser() {
if ($this->_user == NULL) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
<?php
/**
* @abstract 用户模型
* @author Yxl <zccem@163.com>
*/
namespace common\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface {
public $username;
public $password;
public $auth_key;
/**
* @abstract 数据库表名
*/
public static function tableName() {
return '{{%user}}';
}
// /**
// * @abstract 行为(对类的功能进行扩充)
// * @inheritdoc
// */
// public function behaviors() {
// return [
// TimestampBehavior::className(),
// ];
// }
/**
* @inheritdoc
*/
public function rules() {
return [
[['username', 'password',], 'required'],
[['username'], 'string', 'max' => 20],
[['password'], 'string', 'max' => 32]
];
}
/**
* @abstract 查找用户ID
*/
public static function findById($id) {
return static::findOne($id);
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username) {
return static::findOne(['username' => $username]);
}
/**
* @inheritdoc 登录会调用
*/
public static function findIdentity($id) {
return static::findOne(['user_id' => $id]);
// return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null) {
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* @abstract 获取产品所属用户
*/
public function getUser() {
return $this->hasMany(Product::className(), ['id' => 'product_id']);
}
/**
* @inheritdoc
*/
public function getId() {
return $this->getPrimaryKey();
}
/**
* @inheritdoc
*/
public function getAuthKey() {
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey) {
return $this->getAuthKey() === $authKey;
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password) {
$this->password = Yii::$app->security->generatePasswordHash($password);
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password) {
return Yii::$app->security->validatePassword($password, $this->password);
}
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey() {
$this->auth_key = Yii::$app->security->generateRandomString();
}
/**
* Generates new password reset token
*/
public function generatePasswordResetToken() {
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}
/**
* Removes password reset token
*/
public function removePasswordResetToken() {
$this->password_reset_token = null;
}
}
/**
* @abstract 登录
*/
public function actionLogin() {
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new MemberForm();
if (Yii::$app->request->isPost) {
$model->scenario = 'login';
if ($model->load(Yii::$app->request->post()) && $model->signin()) {
return \yii\helpers\Json::encode(TRUE);
}
// 验证失败
else {
return \yii\helpers\Json::encode($model->getErrors());
}
}
return $this->render('login', ['model' => $model]);
}
搞了一天了,还不知道究竟怎么回事,已经按照官方的例子来了,但还是HASH值有误?
38383 补充于 2016-10-21 10:37
注册已经弄好了,就差登录了,一登录就出现这个HASH值不行..
共 2 个回答
38383
注册时间:2016-10-31
最后登录:1970-01-01
在线时长:0小时0分
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝0
- 金钱170
- 威望0
- 积分170