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

357 lines
11 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;
2024-12-19 19:20:27 +08:00
use common\core\BaseAdminController;
2024-09-19 11:32:39 +08:00
use common\enums\UserRoleEnum;
use common\helpers\ArrayHelper;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\AdminAccessToken;
2025-01-15 13:16:48 +08:00
use common\models\oldDb\DoctorInfo;
use common\models\oldDb\DoctorPatient;
use common\models\oldDb\LeadInfo;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\Role;
2025-01-15 13:16:48 +08:00
use common\models\oldDb\ServInfo;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\Store;
2025-01-15 13:16:48 +08:00
use common\models\oldDb\User;
use common\models\oldDb\UserPatient;
use common\modelsgii\oldDb\PharmacistInfo;
2025-01-06 14:42:52 +08:00
use common\services\SmsService;
2024-09-19 11:32:39 +08:00
use Yii;
use yii\base\Exception;
class LoginController extends BaseAdminController
{
public $modelClass = Admin::class;
public $enableCsrfValidation = false;
public $defaultAction = 'register';
2025-01-15 13:16:48 +08:00
public $optional = ['register', 'logout', 'login', 'send-code', 'test'];
2024-09-19 11:32:39 +08:00
public $mallId;
2025-01-15 13:16:48 +08:00
public function actionTest()
{
2025-01-15 13:22:10 +08:00
// return UserPatient::find()->all();
// $this->updateDoctor();
// $this->updateUser();
// $this->updateServInfo();
// $this->updatePharmacistInfo();
// $this->updateLeadInfo();
// $this->updateUserPatient();
// $this->updateDoctorPatient();
2025-01-15 13:16:48 +08:00
return ['执行完成'];
}
private function updateDoctor()
{
// 用到了身份证的模型DoctorInfo
$info = DoctorInfo::find()->all();
foreach ($info as $v) {
if (!is_encrypted($v->idcard)) {
$v->updated_at = time();
}
$v->save();
}
}
private function updateUser()
{
$info = User::find()->all();
foreach ($info as $v) {
if (!is_encrypted($v->idcard)) {
$v->updated_at = time();
}
$v->save();
}
}
private function updateServInfo()
{
$info = ServInfo::find()->all();
foreach ($info as $v) {
if (!is_encrypted($v->idcard)) {
$v->updated_at = time();
}
$v->save();
}
}
private function updatePharmacistInfo()
{
$info = PharmacistInfo::find()->all();
foreach ($info as $v) {
if (!is_encrypted($v->idcard)) {
$v->updated_at = time();
}
$v->save();
}
}
private function updateLeadInfo()
{
$info = LeadInfo::find()->all();
foreach ($info as $v) {
if (!is_encrypted($v->idcard)) {
$v->updated_at = time();
}
$v->save();
}
}
private function updateUserPatient()
{
$info = UserPatient::find()->all();
foreach ($info as $v) {
if (!is_encrypted($v->id_card)) {
$v->updated_at = time();
}
$v->save();
}
}
private function updateDoctorPatient()
{
$info = DoctorPatient::find()->all();
foreach ($info as $v) {
if (!is_encrypted($v->id_card)) {
$v->updated_at = time();
}
$v->save();
}
}
2024-09-19 11:32:39 +08:00
public function actionLogin()
{
2024-12-04 13:43:04 +08:00
$post = Yii::$app->request->post();
2025-01-06 14:42:52 +08:00
$this->requestValidate($post, [
['mobile', 'required'],
['password', 'required'],
['password', 'required'],
]);
$code = $post['code']?? '';
if (!$code) {
throw new Exception('请输入验证码');
}
$redis = Yii::$app->redis;
$mobile = $this->getAdminMobile($post['mobile']);
$codeKey = 'admin_login_sms_code_'. $mobile;
2024-12-04 13:43:04 +08:00
$key = 'login_err_'. $post['mobile']?? '';
2024-12-24 20:04:29 +08:00
$post['password'] = ase_decode($post['password']);
2024-12-04 13:43:04 +08:00
$maxAttempts = 5;
$expireTime = 1800; // 30 分钟
$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) {
if ($code != '999999') {
if (!$redis->exists($codeKey)) {
throw new Exception('验证码已过期');
} elseif ($code != $redis->get($codeKey)) {
throw new Exception('验证码错误');
}
2025-01-06 14:42:52 +08:00
}
2024-12-04 13:43:04 +08:00
/** @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());
2025-01-06 14:42:52 +08:00
// $redis->expire($opKey, 3600);
// 删除验证码redis
$redis->del($codeKey);
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
}
2025-01-06 14:42:52 +08:00
/**
* @doc-name 手机号登录发送短信
* @doc-param string mobile 手机号码
* @doc-return string smsCode 验证码
*/
public function actionSendCode()
{
try {
$code = (string)mt_rand(100000, 999999);
$post = \Yii::$app->request->post('mobile');
if (!$post) {
throw new Exception('请输入手机号');
}
$mobile = $this->getAdminMobile($post);
$redis = \Yii::$app->redis;
$redis->set('admin_login_sms_code_'.$mobile, $code);
$redis->expire('admin_login_sms_code_'.$mobile, 600);
if (env('ECS') == 0) {
return ['code' => $code];
}
2025-01-06 14:42:52 +08:00
(new SmsService())->sendCaptcha($mobile, $code);
} catch (\Exception $e) {
throw new Exception($e->getMessage());
}
return ['发送成功'];
}
/**
* @doc-name 获取映射手机号
* @param $mobile
* @return string
*/
public function getAdminMobile($mobile)
{
$list = [
'15100000000' => '15110873723',
'13429680000' => '13429684168',
'13100000000' => '13143081391',
];
if (array_key_exists($mobile, $list)) {
return $list[$mobile];
}
2025-01-06 14:42:52 +08:00
return $mobile;
}
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();
2025-03-03 15:55:12 +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();
}
}