后台短信验证码登录

This commit is contained in:
2025-01-06 14:42:52 +08:00
parent de9753c64d
commit bf7c753b49
2 changed files with 74 additions and 6 deletions

View File

@@ -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();

View File

@@ -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',