cxgj/models/StoreUser.php
2023-11-27 09:45:13 +08:00

195 lines
5.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\models;
use app\models\log\UserLoginLog;
use Yii;
use yii\web\IdentityInterface;
/**
* This is the model class for table "cx_store_user".
*
* @property int $id
* @property int $user_id 用户ID
* @property int $store_id 门店ID
* @property string $username 用户名
* @property string $password 密码
* @property int $user_type 1-门店管理员 2-门店服务员 3-门店财务人员
* @property int|null $is_delete 是否删除0=否1=是
* @property int|null $deleted_at 删除时间
* @property int $status 状态0=正常1=封禁2=挂起3=注销
* @property int $created_at 添加时间
* @property int $updated_at 更新时间
*/
class StoreUser extends \yii\db\ActiveRecord implements IdentityInterface
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'cx_store_user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['user_id', 'store_id', 'user_type', 'is_delete', 'deleted_at', 'status', 'created_at', 'updated_at'], 'integer'],
[['store_id'], 'required'],
[['username'], 'string', 'max' => 64],
[['password'], 'string', 'max' => 256],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'store_id' => 'Store ID',
'username' => 'Username',
'password' => 'Password',
'user_type' => 'User Type',
'is_delete' => 'Is Delete',
'deleted_at' => 'Deleted At',
'status' => 'Status',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
/**
* 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);
}
/**
* Finds an identity by the given ID.
* @param string|int $id the ID to be looked for
* @return IdentityInterface the identity object that matches the given ID.
* Null should be returned if such an identity cannot be found
* or the identity is not in an active state (disabled, deleted, etc.)
*/
public static function findIdentity($id)
{
$admin = self::findOne([
"id" => $id,
"is_delete" => 0
]);
return $admin;
}
/**
* Finds an identity by the given token.
* @param mixed $token the token to be looked for
* @param mixed $type the type of the token. The value of this parameter depends on the implementation.
* For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
* @return IdentityInterface the identity object that matches the given token.
* Null should be returned if such an identity cannot be found
* or the identity is not in an active state (disabled, deleted, etc.)
*/
public static function findIdentityByAccessToken($token, $type = null)
{
$user_token = UserToken::find()
->where([
'is_delete' => 0,
'token' => $token,
])
->andFilterWhere([
'type' => $type,
])->one();
if ($user_token && $user_token->expire_time > time()) {
return static::findOne([
'id' => $user_token->user_id,
'is_delete' => 0,
'status' => 0,
]);
}
return static::findOne([
'id' => 0,
'is_delete' => 0,
'status' => 0,
]);
}
public function setPassword($password)
{
$user = StoreUser::findOne([
"user_id" => $this->user_id,
"is_delete" => 0,
"status" => User::STATUS_NORMAL
]);
if ($user != null) {
$user->password = Yii::$app->security->generatePasswordHash($password);
}
return $user->save();
}
/**
* Returns an ID that can uniquely identify a user identity.
* @return string|int an ID that uniquely identifies a user identity.
*/
public function getId()
{
return $this->id;
}
/**
* Returns a key that can be used to check the validity of a given identity ID.
*
* The key should be unique for each individual user, and should be persistent
* so that it can be used to check the validity of the user identity.
*
* The space of such keys should be big enough to defeat potential identity attacks.
*
* This is required if [[User::enableAutoLogin]] is enabled.
* @return string a key that is used to check the validity of a given identity ID.
* @see validateAuthKey()
*/
public function getAuthKey()
{
$user = User::findOne([
"id" => $this->user_id,
"is_delete" => 0,
"status" => User::STATUS_NORMAL
]);
if ($user != null) {
return $user->auth_key;
}
return null;
}
/**
* Validates the given auth key.
*
* This is required if [[User::enableAutoLogin]] is enabled.
* @param string $authKey the given auth key
* @return bool whether the given auth key is valid.
* @see getAuthKey()
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
public static function lastLogin($user_id, $is_pass = 1, $err_msg = null)
{
return log\UserLoginLog::logger($user_id, $is_pass, $err_msg);
}
}