cxfoot/modules/api/models/LoginForm.php
2023-10-27 14:17:13 +08:00

102 lines
2.7 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2020-12-2
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\models;
use app\models\User;
use app\components\auth\AToken;
use app\components\EncryptHelper;
class LoginForm extends ApiModel
{
public $username;
public $password;
public $cx_mch_id;
public $token_type;
public function rules()
{
return [
[['username', 'password'], 'trim'],
[['username', 'password'], 'string'],
[['cx_mch_id', 'token_type'], 'integer'],
[['username', 'password', 'token_type'], 'required'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'username' => '用户名',
'password' => '密码',
];
}
public function login()
{
if(!$this->validate()){
return $this->getModelError();
}
$mobile_phone = EncryptHelper::encryptMobilePhone($this->username);
$mobile_phone = $mobile_phone ? $mobile_phone : $this->username;
$user = User::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'status' => User::STATUS_NORMAL
])
->andWhere([
'OR',
['username' => $this->username],
['mobile_phone' => $mobile_phone],
])
->one();
if($user == null){
return [
'code' => 1,
'msg' => '用户名或者密码错误'
];
}
if(!$user->validatePassword($this->password)){
$err_msg = "密码错误";
User::lastLogin($user->id, 0, $err_msg, $this->cx_mch_id);
return [
'code' => 1,
'msg' => '用户名或者密码错误'
];
}
if(\Yii::$app->user->login($user,\Yii::$app->params['loginAuthExpireTime'])){
$args = [];
$args['cx_mch_id'] = $this->cx_mch_id;
$args['token_type'] = $this->token_type;
$atoken = new AToken($args);
$data = $atoken->generate_access_token();
if($data['code'] == 0){
$err_msg = "登录成功";
User::lastLogin($user->id, 1, $err_msg, $this->cx_mch_id);
//是否需要绑定
$data = User::bindQuery($user, $data);
}
} else {
$data = [
'code' => 1,
'msg' => '登录失败'
];
}
return $data;
}
}