From bf7c753b496d3e1cb3b29e90aeb16d7976bb36ff Mon Sep 17 00:00:00 2001 From: liqi Date: Mon, 6 Jan 2025 14:42:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E7=9F=AD=E4=BF=A1=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/controllers/LoginController.php | 74 +++++++++++++++++++++++++-- common/config/params-local.php | 6 +-- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/admin/controllers/LoginController.php b/admin/controllers/LoginController.php index 7fa31df..904f15b 100644 --- a/admin/controllers/LoginController.php +++ b/admin/controllers/LoginController.php @@ -11,6 +11,7 @@ use common\helpers\ArrayHelper; use common\models\oldDb\AdminAccessToken; use common\models\oldDb\Role; use common\models\oldDb\Store; +use common\services\SmsService; use Yii; use yii\base\Exception; @@ -19,18 +20,31 @@ class LoginController extends BaseAdminController public $modelClass = Admin::class; public $enableCsrfValidation = false; public $defaultAction = 'register'; - public $optional = ['register', 'logout', 'login']; + public $optional = ['register', 'logout', 'login', 'send-code']; public $mallId; public function actionLogin() { $post = Yii::$app->request->post(); + $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; + $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) { @@ -40,6 +54,11 @@ class LoginController extends BaseAdminController try { if (Yii::$app->request->isPost) { + if (!$redis->exists($codeKey)) { + throw new Exception('验证码已过期'); + } elseif ($code != $redis->get($codeKey)) { + throw new Exception('验证码错误'); + } /** @var $adminAccessToken AdminAccessToken */ if ($model->load($post, '') && $adminAccessToken = $model->login()) { $admin = Admin::find()->where(['uid' => $adminAccessToken->admin_id])->one(); @@ -68,7 +87,9 @@ class LoginController extends BaseAdminController // 设置登录过期时间 $opKey = 'op_user_'. $admin['uid']; $redis->set($opKey, time()); - $redis->expire($opKey, 3600); +// $redis->expire($opKey, 3600); + // 删除验证码redis + $redis->del($codeKey); return [ 'userInfo' => $admin, 'token' => $adminAccessToken->access_token @@ -98,6 +119,53 @@ class LoginController extends BaseAdminController } + + + /** + * @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); + (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]; + } + return $mobile; + } + public function actionLogout() { Yii::$app->user->logout(); diff --git a/common/config/params-local.php b/common/config/params-local.php index 453b111..69c6c00 100644 --- a/common/config/params-local.php +++ b/common/config/params-local.php @@ -3,8 +3,8 @@ return [ 'product_order' => [ 'over_time' => 24*3600,//秒,自动取消时间 -// 'received_time' => 7*24*3600,//自动确认收货时间 - 'received_time' => 60,//自动确认收货时间 + 'received_time' => 7*24*3600,//自动确认收货时间 +// 'received_time' => 60,//自动确认收货时间 'forbidden_refund_time' => 30*24*3600, //不允许退款时间 'settlement_time' => 0//7*24*3600//订单发货 分账自动结算时间 ], @@ -135,7 +135,7 @@ JJvBfLfudYNUjUCyW3gAcOIJ 'secret' => '61ce8912-25e6-46cf-82eb-81412294a53d' ], 'aseList' => [ - 'contact', +// 'contact', 'mobile', 'express_mobile', 'doctor',