Files
xk-api-yii/admin/controllers/LoginController.php

185 lines
6.5 KiB
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace admin\controllers;
use admin\models\Admin;
use admin\models\forms\LoginForm;
use admin\models\RegisterForm;
use common\enums\UserRoleEnum;
use common\helpers\ArrayHelper;
use common\models\AdminAccessToken;
use common\models\Role;
use common\models\Store;
use Yii;
use yii\base\Exception;
use common\core\BaseAdminController;
class LoginController extends BaseAdminController
{
public $modelClass = Admin::class;
public $enableCsrfValidation = false;
public $defaultAction = 'register';
public $optional = ['register', 'logout', 'login'];
public $mallId;
public function actionLogin()
{
2024-12-04 13:43:04 +08:00
$post = Yii::$app->request->post();
$key = 'login_err_'. $post['mobile']?? '';
$maxAttempts = 5;
$expireTime = 1800; // 30 分钟
$redis = Yii::$app->redis;
$errNum = $redis->get($key);
2024-12-04 14:19:43 +08:00
$txt = '当前账号因频繁登录失败已被锁定30分钟内无法重试预计解锁时间';
2024-12-04 13:43:04 +08:00
if ($errNum >= $maxAttempts) {
2024-12-04 14:19:43 +08:00
throw new Exception($txt. date('Y-m-d H:i:s', time() + $redis->ttl($key)));
2024-12-04 13:43:04 +08:00
}
2024-09-19 11:32:39 +08:00
$model = new LoginForm();
2024-12-04 13:43:04 +08:00
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;
2024-09-19 11:32:39 +08:00
// 设置登录过期时间
$opKey = 'op_user_'. $admin['uid'];
$redis->set($opKey, time());
$redis->expire($opKey, 3600);
2024-12-04 13:43:04 +08:00
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;
2024-09-19 11:32:39 +08:00
} else {
2024-12-04 13:43:04 +08:00
// 如果键存在,累加 1
$currentValue = $redis->incr($key);
// 如果是第一次累加,设置过期时间
2024-12-04 14:19:43 +08:00
if (in_array($currentValue, [1, 5])) {
2024-12-04 13:43:04 +08:00
$redis->expire($key, $expireTime);
2024-12-04 14:19:43 +08:00
throw new Exception($txt. date('Y-m-d H:i:s', time() + $redis->ttl($key)));
2024-12-04 13:43:04 +08:00
}
2024-09-19 11:32:39 +08:00
}
2024-12-04 13:43:04 +08:00
throw new Exception($e->getMessage().' 还有'.($maxAttempts - $currentValue).'次机会');
2024-09-19 11:32:39 +08:00
}
2024-12-04 13:43:04 +08:00
2024-09-19 11:32:39 +08:00
}
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();
2024-12-12 13:45:41 +08:00
$post['mobile'] = ase_decode($post['mobile']);
2024-09-19 11:32:39 +08:00
$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']
]);
2024-12-18 09:16:10 +08:00
$post['mobile'] = ase_decode($post['mobile']);
2024-09-19 11:32:39 +08:00
$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();
}
}