Files
xk-api-yii/admin/controllers/LoginController.php
2024-12-24 20:04:29 +08:00

186 lines
6.6 KiB
PHP
Raw 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 admin\controllers;
use admin\models\Admin;
use admin\models\forms\LoginForm;
use admin\models\RegisterForm;
use common\core\BaseAdminController;
use common\enums\UserRoleEnum;
use common\helpers\ArrayHelper;
use common\models\oldDb\AdminAccessToken;
use common\models\oldDb\Role;
use common\models\oldDb\Store;
use Yii;
use yii\base\Exception;
class LoginController extends BaseAdminController
{
public $modelClass = Admin::class;
public $enableCsrfValidation = false;
public $defaultAction = 'register';
public $optional = ['register', 'logout', 'login'];
public $mallId;
public function actionLogin()
{
$post = Yii::$app->request->post();
$key = 'login_err_'. $post['mobile']?? '';
$post['password'] = ase_decode($post['password']);
$maxAttempts = 5;
$expireTime = 1800; // 30 分钟
$redis = Yii::$app->redis;
$errNum = $redis->get($key);
$txt = '当前账号因频繁登录失败已被锁定30分钟内无法重试预计解锁时间';
if ($errNum >= $maxAttempts) {
throw new Exception($txt. date('Y-m-d H:i:s', time() + $redis->ttl($key)));
}
$model = new LoginForm();
try {
if (Yii::$app->request->isPost) {
/** @var $adminAccessToken AdminAccessToken */
if ($model->load($post, '') && $adminAccessToken = $model->login()) {
$admin = Admin::find()->where(['uid' => $adminAccessToken->admin_id])->one();
$admin = ArrayHelper::toArray($admin, [
Admin::class => ['uid', 'username', 'role', 'mobile', 'store_id','code','city_id','province_id'],
]);
$title = '萧康管理后台';
switch ($admin['role']) {
case UserRoleEnum::SUPER_ADMIN:
$title = '萧康管理后台';
break;
case UserRoleEnum::STORE_ADMIN:
$name = Store::find()->select(['name'])->where(['id' => $admin['store_id']])->one();
$title = $name->name . '管理后台';
break;
case UserRoleEnum::PROVINCE_DAI :
case UserRoleEnum::CITY_DAI:
case UserRoleEnum::SUPPLY:
$title = $admin['username'].'的管理后台';
break;
default:
$title = $admin['username'].'的管理后台';
}
$admin['title'] = $title;
// 设置登录过期时间
$opKey = 'op_user_'. $admin['uid'];
$redis->set($opKey, time());
$redis->expire($opKey, 3600);
return [
'userInfo' => $admin,
'token' => $adminAccessToken->access_token
];
} else {
throw new Exception("登陆失败,帐号或者密码错误". $errNum);
}
}
} catch (Exception $e) {
// 检查键是否存在
if (!$redis->exists($key)) {
// 如果键不存在,初始化为 1 并设置过期时间为30分钟
$redis->setex($key, $expireTime, 1);
$currentValue = 1;
} else {
// 如果键存在,累加 1
$currentValue = $redis->incr($key);
// 如果是第一次累加,设置过期时间
if (in_array($currentValue, [1, 5])) {
$redis->expire($key, $expireTime);
throw new Exception($txt. date('Y-m-d H:i:s', time() + $redis->ttl($key)));
}
}
throw new Exception($e->getMessage().' 还有'.($maxAttempts - $currentValue).'次机会');
}
}
public function actionLogout()
{
Yii::$app->user->logout();
return [];
}
/**
* @doc-name 后台注册
* @doc-param string mobile 手机号
* @doc-param string password 密码
* @doc-param string username 用户名
* @doc-param int role 角色id
* @doc-param int store_id 门店id / optional
* @doc-param int province_id 省份 / optional
* @doc-param int city_id 城市 / optional
*/
public function actionRegister()
{
$post = Yii::$app->request->post();
$post['mobile'] = ase_decode($post['mobile']);
$model = new RegisterForm();
$model->attributes = $post;
return $model->register();
}
/**
* @doc-name 重置密码或忘记密码
* @doc-param string mobile 手机号
* @doc-param string password 密码
*/
public function actionResetPassword()
{
$post = Yii::$app->request->post();
$this->requestValidate($post, [
[['mobile', 'password'], 'required']
]);
$post['mobile'] = ase_decode($post['mobile']);
$LoginForm = new LoginForm();
$LoginForm->attributes = $post;
return $LoginForm->ResetPassword();
}
/**
* @doc-name 所有角色
* @doc-return mixed @List{id,name-string-名字,role_code-string-role_code,description-string-描述} 医生列表
* @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据
*/
public function actionAllRole()
{
$post = Yii::$app->request->get();
$query = Role::find();
$this->field = [
Role::class => [
'id', 'name', 'role_code', 'description'
]
];
return $this->create($query, $post);
}
/**
* @doc-name 编辑管理员信息
* @doc-param string bank_user_name 开户人姓名 / optional
* @doc-param string bank_card 银行卡号 / optional
* @doc-param string bank_name 开户行 / optional
* @doc-param int bank_account_type 银行账户类型 1对公2对私5存折 / optional
* @doc-param string bank_no 银行联行号bank_account_type=1或5或非62开头的对私银行账户时必选 / optional
*/
public function actionEditSuperInfo()
{
$post = Yii::$app->request->post();
$admin = \Yii::$app->user->identity;
if ($admin->role!=UserRoleEnum::SUPER_ADMIN){
throw new \yii\db\Exception('您不是超管,不能编辑信息');
}
$model = new RegisterForm();
$model->attributes = $post;
return $model->EditSuper();
}
}