Commit 8c982e00 authored by rlgy's avatar rlgy

登录逻辑修改

parent 437d9244
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace backend\controllers; namespace backend\controllers;
use common\models\Admin;
use common\models\AdminSession; use common\models\AdminSession;
use common\models\NewLoginForm; use common\models\NewLoginForm;
use yii\filters\AccessControl; use yii\filters\AccessControl;
...@@ -86,10 +85,10 @@ class LoginController extends Controller ...@@ -86,10 +85,10 @@ class LoginController extends Controller
} else { } else {
$code = isset($info) && is_string($info) ? $info : 'LOGIN_002'; $code = isset($info) && is_string($info) ? $info : 'LOGIN_002';
$del = Admin::findOne(['username' => $model->username]); // $del = Admin::findOne(['username' => $model->username]);
if ($del) { // if ($del) {
$del->delete(); // $del->delete();
} // }
} }
} }
$error = Yii::t('error', $code); $error = Yii::t('error', $code);
......
...@@ -27,7 +27,7 @@ use common\core\ActiveForm; ...@@ -27,7 +27,7 @@ use common\core\ActiveForm;
'iconPos' => 'left', 'iconPos' => 'left',
'iconClass' => 'icon-user', 'iconClass' => 'icon-user',
'placeholder' => '登录账号', 'placeholder' => '登录账号',
'readonly' =>'readonly', // 'readonly' =>'readonly',
])->label(false) ?> ])->label(false) ?>
<?= $form->field($model, 'password')->iconTextInput([ <?= $form->field($model, 'password')->iconTextInput([
......
...@@ -10,22 +10,6 @@ AppAsset::register($this); ...@@ -10,22 +10,6 @@ AppAsset::register($this);
/* user */ /* user */
if (!Yii::$app->user->isGuest) { if (!Yii::$app->user->isGuest) {
$user = Yii::$app->user->identity->username; $user = Yii::$app->user->identity->username;
$auth = isset(Yii::$app->user->identity->auth) ? Yii::$app->user->identity->auth : null;
if (!isset($auth->access_token) || !$auth->access_token) {
Yii::$app->user->logout();
$token = '';
} else {
if ($auth->addtime + $auth->expires < time()) {
\common\models\FxeeAuth::findOne(['access_token' => $auth->access_token])->delete();
Yii::$app->user->logout();
$token = '';
} else {
$token = isset($auth->access_token) ? $auth->access_token : '';
}
}
} else { } else {
$user = '访客'; $user = '访客';
$token = null; $token = null;
...@@ -41,15 +25,6 @@ if ($route == 'public/error') { ...@@ -41,15 +25,6 @@ if ($route == 'public/error') {
$picAsset = $this->getAssetManager()->getBundle(AppAsset::className())->baseUrl; $picAsset = $this->getAssetManager()->getBundle(AppAsset::className())->baseUrl;
$logout = \yii\helpers\Url::toRoute('login/logout'); $logout = \yii\helpers\Url::toRoute('login/logout');
$this->registerJs(<<< JS
var request_token="$token";
if (!request_token) {
window.location.href="$logout";
}
JS
,\yii\web\View::POS_BEGIN);
$this->beginPage() $this->beginPage()
?> ?>
......
<?php
namespace common\business;
use Yii;
use common\models\Admin;
use common\core\Exception;
use common\service\LoginService;
/**
* 登录判断
* Class LoginBusiness
* @package common\service
*/
class LoginBusiness
{
public static function Login()
{
try {
if (Yii::$app->request->isPost) {
$params = Yii::$app->request->post();
if (!isset($params['info']['username']) || !isset($params['info']['password']))
throw new Exception('信息填写不完整');
} else {
throw new Exception('非法访问方式');
}
$model = new LoginService();
$ret = $model->Login($params['info']['username'], $params['info']['password']);
if (isset($ret['data']['group']) && !in_array($ret['data']['group'], ['kf', 'admin'])) {
throw new Exception('用户权限不足');
}
if ($ret['code'] == 200 && isset($ret['data']['userid'])) {
$one = Admin::findByUid($ret['data']['userid']);
if (!$one) {
//自动注册个用户
$add = new Admin();
$data = [];
$data['bind_uid'] = $ret['data']['userid'];
$data['username'] = $data['email'] = $params['info']['username'];
$data['password'] = $params['info']['password'] . '_auto';
$data['mobile'] = '';
$data['status'] = 1;
$data['reg_time'] = time();
$data['reg_ip'] = ip2long(Yii::$app->request->getUserIP());
$data['last_login_time'] = 0;
$data['last_login_ip'] = ip2long('127.0.0.1');
$data['update_time'] = 0;
$add->setAttributes($data);
$add->generateAuthKey();
$add->setPassword($data['password']);
if (!$add->save()) {
$errors = $add->firstErrors;
return '注册用户失败: ' . array_shift($errors);
} else {
//admin
$uid = $add->id;
$addRole = 'admin';
$auth = Yii::$app->authManager;
$uRole = array_keys($auth->getRolesByUser($uid));
if (!in_array($addRole, $uRole)) {
$role = $auth->getRole($addRole);
$auth->assign($role, $uid);
}
}
}
}
return isset($ret['data']) ? $ret['data'] : null;
} catch (Exception $e) {
return $e->getMessage();
}
}
}
\ No newline at end of file
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace common\models; namespace common\models;
use common\core\Exception;
use Yii; use Yii;
use yii\base\NotSupportedException; use yii\base\NotSupportedException;
use yii\web\IdentityInterface; use yii\web\IdentityInterface;
...@@ -31,7 +30,7 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface ...@@ -31,7 +30,7 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface
*/ */
public static function findIdentity($uid) public static function findIdentity($uid)
{ {
return static::find()->with('auth')->where(['uid' => $uid, 'status' => self::STATUS_ACTIVE])->one(); return static::find()->where(['uid' => $uid, 'status' => self::STATUS_ACTIVE])->one();
} }
/** /**
...@@ -43,17 +42,6 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface ...@@ -43,17 +42,6 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface
} }
/** /**
* 根据用户uid获取账号信息
*
* @param string $bind_uid
* @return static|null
*/
public static function findByUid($uid)
{
return static::findOne(['bind_uid' => $uid, 'status' => self::STATUS_ACTIVE]);
}
/**
* 根据用户名获取账号信息 * 根据用户名获取账号信息
* *
* @param string $username * @param string $username
...@@ -168,18 +156,4 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface ...@@ -168,18 +156,4 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface
$this->password = null; $this->password = null;
} }
/**
* Fxee登录权限
*/
public static function fxeeAuth()
{
$model = isset(Yii::$app->user->identity->auth) ? Yii::$app->user->identity->auth : null;
if ($model == null) {
throw new Exception('无操作权限');
} else if ($model->addtime + $model->expires < time()) {
throw new Exception('登录过期');
}
return $model;
}
} }
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
namespace common\modelsgii; namespace common\modelsgii;
use common\models\FxeeAuth;
use common\models\fxee\Member;
use common\models\fxee\MemberDetail;
use yii\helpers\HtmlPurifier; use yii\helpers\HtmlPurifier;
/** /**
...@@ -48,31 +45,6 @@ class Admin extends \common\core\BaseActiveRecord ...@@ -48,31 +45,6 @@ class Admin extends \common\core\BaseActiveRecord
} }
/** /**
* @return mixed
*/
public function getAuth()
{
return $this->hasOne(FxeeAuth::className(), ['user_id' => 'bind_uid']);
}
/**
* @return mixed
*/
public function getName()
{
return $this->hasOne(MemberDetail::className(), ['uid' => 'bind_uid'])->select('uid, name');
}
/**
* @return mixed
*/
public function getGroup()
{
return $this->hasOne(Member::className(), ['id' => 'bind_uid'])->select('id, group, wfrom');
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function attributeLabels() public function attributeLabels()
......
<?php
namespace common\service;
use common\core\Exception;
use common\helpers\Curl;
use Yii;
use yii\helpers\HtmlPurifier;
/**
* 登录服务
* Class LoginService
* @package common\service
*/
class LoginService
{
private $_redirect = "risk.zhaobi.com"; //跳转地址,随意
private $_type = "email"; //登录方式
private $_loginUser = null;//账号
private $_loginPwd = null;//密码
public function Login($email, $pwd)
{
$this->_loginUser = HtmlPurifier::process($email);
$this->_loginPwd = HtmlPurifier::process($pwd);
return $this->_send();
}
protected function _send()
{
$post = [
'type' => $this->_type,
'redirect_uri' => $this->_redirect,
'email' => $this->_loginUser,
'password' => $this->_loginPwd,
];
$curl = new Curl();
$curl->setPostParams($post);
$loginUrl = Yii::$app->params['fxee_domain'] . Yii::$app->params['fxee_backend']['管理员登录'];
$result = json_decode($curl->post($loginUrl), true);
if ($curl->errorText)
throw new Exception($curl->errorText);
Exception::fxeeException($result);
return $result;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment