feat(security): 优化登录与支付密码错误提示文案并完善双端安全中心交互
- 安全校验提示:优化 AccountSecurity 与 MasterSecurity 密码错误提示,区分登录密码与支付密码,并增加剩余可重试次数提示 - 接口响应对齐:优化 Auth、MasterAuth、Security、Sms 控制器在短信验证码发送与密码校验时的异常处理与返回格式 - 腾讯云短信SDK:优化 SmsSingleSender::sendWithParam 方法入参默认值 - 双端小程序:优化用户端与师傅端登录页及账号安全中心的密码修改与找回交互逻辑
这个提交包含在:
@@ -62,9 +62,13 @@ class Auth extends Base
|
||||
$this->error('该账号尚未设置登录密码,请使用微信一键登录后设置密码');
|
||||
}
|
||||
try {
|
||||
\app\common\library\AccountSecurity::verify((int)$user['id'], 'login', $password);
|
||||
\app\common\library\AccountSecurity::verify((int)$user['id'], 'login', $password, '登录密码');
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage() === '密码不正确' ? '手机号或密码不正确' : $e->getMessage());
|
||||
$msg = $e->getMessage();
|
||||
if (strpos($msg, '不正确') !== false) {
|
||||
$msg = str_replace('登录密码不正确', '手机号或密码不正确', $msg);
|
||||
}
|
||||
$this->error($msg);
|
||||
}
|
||||
$this->auth->direct($user['id']);
|
||||
}
|
||||
@@ -102,12 +106,13 @@ class Auth extends Base
|
||||
|
||||
try {
|
||||
$res = SmsService::sendVerifyCode($mobile, 'user_resetpwd', $this->request->ip());
|
||||
$this->success('验证码已发送,请注意查收', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('验证码已发送,请注意查收', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -56,9 +56,13 @@ class MasterAuth extends MasterBase
|
||||
$this->error('该师傅账号尚未设置登录密码,请使用微信一键登录后在账号安全中设置密码');
|
||||
}
|
||||
try {
|
||||
MasterSecurity::verify((int)$master['id'], 'login', $password);
|
||||
MasterSecurity::verify((int)$master['id'], 'login', $password, '登录密码');
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage() === '密码不正确' ? '手机号或密码不正确' : $e->getMessage());
|
||||
$msg = $e->getMessage();
|
||||
if (strpos($msg, '不正确') !== false) {
|
||||
$msg = str_replace('登录密码不正确', '手机号或密码不正确', $msg);
|
||||
}
|
||||
$this->error($msg);
|
||||
}
|
||||
if (!$this->masterAuth->direct((int)$master['id'])) {
|
||||
$this->error($this->masterAuth->getError() ?: '登录失败');
|
||||
@@ -199,12 +203,13 @@ class MasterAuth extends MasterBase
|
||||
|
||||
try {
|
||||
$res = SmsService::sendVerifyCode($mobile, 'master_resetpwd', $this->request->ip());
|
||||
$this->success('验证码已发送,请注意查收', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('验证码已发送,请注意查收', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -288,12 +293,13 @@ class MasterAuth extends MasterBase
|
||||
|
||||
try {
|
||||
$res = SmsService::sendVerifyCode($mobile, 'master_changepwd', $this->request->ip());
|
||||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -322,9 +328,13 @@ class MasterAuth extends MasterBase
|
||||
$ignoreOld = true;
|
||||
} else {
|
||||
try {
|
||||
MasterSecurity::verify($mid, 'login', $old);
|
||||
MasterSecurity::verify($mid, 'login', $old, '原登录密码');
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
$msg = $e->getMessage();
|
||||
if (strpos($msg, '15分钟') !== false) {
|
||||
$msg .= ',或点击下方改用短信验证码';
|
||||
}
|
||||
$this->error($msg);
|
||||
}
|
||||
$ignoreOld = false;
|
||||
}
|
||||
@@ -384,12 +394,13 @@ class MasterAuth extends MasterBase
|
||||
|
||||
try {
|
||||
$res = SmsService::sendVerifyCode($mobile, 'master_paypwd', $this->request->ip());
|
||||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -419,9 +430,13 @@ class MasterAuth extends MasterBase
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
MasterSecurity::verify($mid, 'payment', (string)$this->request->post('old_password', ''));
|
||||
MasterSecurity::verify($mid, 'payment', (string)$this->request->post('old_password', ''), '原支付密码');
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
$msg = $e->getMessage();
|
||||
if (strpos($msg, '15分钟') !== false) {
|
||||
$msg .= ',或点击下方改用短信验证码';
|
||||
}
|
||||
$this->error($msg);
|
||||
}
|
||||
}
|
||||
$updated = Db::name('master_security')
|
||||
|
||||
@@ -44,12 +44,13 @@ class Security extends Base
|
||||
|
||||
try {
|
||||
$res = SmsService::sendVerifyCode($mobile, 'user_changepwd', $this->request->ip());
|
||||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
}
|
||||
|
||||
public function changeLoginPassword()
|
||||
@@ -70,7 +71,15 @@ class Security extends Base
|
||||
}
|
||||
$ignoreOld = true;
|
||||
} else {
|
||||
try { AccountSecurity::verify($uid, 'login', $old); } catch (\Throwable $e) { $this->error($e->getMessage()); }
|
||||
try {
|
||||
AccountSecurity::verify($uid, 'login', $old, '原登录密码');
|
||||
} catch (\Throwable $e) {
|
||||
$msg = $e->getMessage();
|
||||
if (strpos($msg, '15分钟') !== false) {
|
||||
$msg .= ',或点击下方改用短信验证码';
|
||||
}
|
||||
$this->error($msg);
|
||||
}
|
||||
$ignoreOld = false;
|
||||
}
|
||||
} else {
|
||||
@@ -111,12 +120,13 @@ class Security extends Base
|
||||
|
||||
try {
|
||||
$res = SmsService::sendVerifyCode($mobile, 'user_paypwd', $this->request->ip());
|
||||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('验证码已发送至绑定手机', array_merge($res, [
|
||||
'mobile' => substr_replace($mobile, '****', 3, 4),
|
||||
]));
|
||||
}
|
||||
|
||||
public function changePaymentPassword()
|
||||
@@ -139,8 +149,15 @@ class Security extends Base
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
} else {
|
||||
try { AccountSecurity::verify($uid, 'payment', (string)$this->request->post('old_password', '')); }
|
||||
catch (\Throwable $e) { $this->error($e->getMessage()); }
|
||||
try {
|
||||
AccountSecurity::verify($uid, 'payment', (string)$this->request->post('old_password', ''), '原支付密码');
|
||||
} catch (\Throwable $e) {
|
||||
$msg = $e->getMessage();
|
||||
if (strpos($msg, '15分钟') !== false) {
|
||||
$msg .= ',或点击下方改用短信验证码';
|
||||
}
|
||||
$this->error($msg);
|
||||
}
|
||||
}
|
||||
$updated = Db::name('user_security')->where('user_id', $uid)->where('payment_password_hash', $oldHash)->update([
|
||||
'payment_password_hash' => password_hash($new, PASSWORD_DEFAULT),
|
||||
|
||||
@@ -87,10 +87,11 @@ class Sms extends Api
|
||||
|
||||
try {
|
||||
$res = SmsService::sendVerifyCode($mobile, $event, $this->request->ip());
|
||||
$this->success('验证码已发送,请注意查收', $res);
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('验证码已发送,请注意查收', $res);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,9 +109,10 @@ class Sms extends Api
|
||||
|
||||
try {
|
||||
SmsService::verifyCode($rawMobile, $captcha, $event, false);
|
||||
$this->success('验证码正确');
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
return;
|
||||
}
|
||||
$this->success('验证码正确');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,12 @@ class AccountSecurity
|
||||
Db::execute("INSERT IGNORE INTO `{$prefix}user_security` (`user_id`) VALUES (?)", [(int)$uid]);
|
||||
}
|
||||
|
||||
public static function verify($uid, $kind, $password)
|
||||
public static function verify($uid, $kind, $password, $label = null)
|
||||
{
|
||||
if (!in_array($kind, ['login', 'payment'], true)) {
|
||||
throw new \InvalidArgumentException('验证类型错误');
|
||||
}
|
||||
$label = ($label !== null && $label !== '') ? (string)$label : ($kind === 'payment' ? '支付密码' : '登录密码');
|
||||
$attemptsField = $kind === 'payment' ? 'payment_failed_attempts' : 'failed_attempts';
|
||||
$lockedField = $kind === 'payment' ? 'payment_locked_until' : 'locked_until';
|
||||
self::ensure($uid);
|
||||
@@ -27,7 +28,7 @@ class AccountSecurity
|
||||
try {
|
||||
$row = Db::name('user_security')->where('user_id', (int)$uid)->lock(true)->find();
|
||||
if ((int)$row[$lockedField] > time()) {
|
||||
throw new \RuntimeException('密码错误次数过多,请15分钟后重试');
|
||||
throw new \RuntimeException($label . '错误次数过多,请15分钟后重试');
|
||||
}
|
||||
if ($kind === 'login') {
|
||||
$user = Db::name('user')->field('password,salt')->where('id', (int)$uid)->find();
|
||||
@@ -49,7 +50,11 @@ class AccountSecurity
|
||||
throw $e;
|
||||
}
|
||||
if (!$correct) {
|
||||
throw new \RuntimeException($failures >= 5 ? '密码错误次数过多,请15分钟后重试' : '密码不正确');
|
||||
if ($failures >= 5) {
|
||||
throw new \RuntimeException($label . '错误次数过多,请15分钟后重试');
|
||||
}
|
||||
$remain = 5 - $failures;
|
||||
throw new \RuntimeException($label . '不正确,还可尝试 ' . $remain . ' 次');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -18,11 +18,12 @@ class MasterSecurity
|
||||
Db::execute("INSERT IGNORE INTO `{$prefix}master_security` (`master_id`) VALUES (?)", [(int)$masterId]);
|
||||
}
|
||||
|
||||
public static function verify($masterId, $kind, $password)
|
||||
public static function verify($masterId, $kind, $password, $label = null)
|
||||
{
|
||||
if (!in_array($kind, ['login', 'payment'], true)) {
|
||||
throw new \InvalidArgumentException('验证类型错误');
|
||||
}
|
||||
$label = ($label !== null && $label !== '') ? (string)$label : ($kind === 'payment' ? '支付密码' : '登录密码');
|
||||
$masterId = (int)$masterId;
|
||||
$attemptsField = $kind === 'payment' ? 'payment_failed_attempts' : 'failed_attempts';
|
||||
$lockedField = $kind === 'payment' ? 'payment_locked_until' : 'locked_until';
|
||||
@@ -32,7 +33,7 @@ class MasterSecurity
|
||||
try {
|
||||
$row = Db::name('master_security')->where('master_id', $masterId)->lock(true)->find();
|
||||
if ((int)$row[$lockedField] > time()) {
|
||||
throw new \RuntimeException('密码错误次数过多,请15分钟后重试');
|
||||
throw new \RuntimeException($label . '错误次数过多,请15分钟后重试');
|
||||
}
|
||||
if ($kind === 'login') {
|
||||
$master = Db::name('master')->field('password,salt')->where('id', $masterId)->find();
|
||||
@@ -54,7 +55,11 @@ class MasterSecurity
|
||||
throw $e;
|
||||
}
|
||||
if (!$correct) {
|
||||
throw new \RuntimeException($failures >= 5 ? '密码错误次数过多,请15分钟后重试' : '密码不正确');
|
||||
if ($failures >= 5) {
|
||||
throw new \RuntimeException($label . '错误次数过多,请15分钟后重试');
|
||||
}
|
||||
$remain = 5 - $failures;
|
||||
throw new \RuntimeException($label . '不正确,还可尝试 ' . $remain . ' 次');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户