文件
yjyf/server/application/api/controller/Sms.php
MiAn 894da56623 feat(security): 优化登录与支付密码错误提示文案并完善双端安全中心交互
- 安全校验提示:优化 AccountSecurity 与 MasterSecurity 密码错误提示,区分登录密码与支付密码,并增加剩余可重试次数提示
- 接口响应对齐:优化 Auth、MasterAuth、Security、Sms 控制器在短信验证码发送与密码校验时的异常处理与返回格式
- 腾讯云短信SDK:优化 SmsSingleSender::sendWithParam 方法入参默认值
- 双端小程序:优化用户端与师傅端登录页及账号安全中心的密码修改与找回交互逻辑
2026-11-26 17:29:23 +08:00

119 行
4.1 KiB
PHP
可执行文件

<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\library\MasterAuth;
use app\common\library\SmsService;
use think\Db;
/**
* 手机短信接口
*/
class Sms extends Api
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
/**
* 发送短信验证码
* @ApiMethod (POST)
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
* @ApiParams (name="event", type="string", required=true, description="事件名称")
*/
public function send()
{
$rawMobile = (string)$this->request->post('mobile', '');
$event = trim((string)$this->request->post('event', ''));
if ($event === '') {
$event = 'user_resetpwd';
}
try {
$mobile = SmsService::normalizeMobile($rawMobile);
} catch (\InvalidArgumentException $e) {
$this->error($e->getMessage());
return;
}
if (!in_array($event, SmsService::ALLOWED_EVENTS, true)) {
$this->error('不支持的短信验证码类型');
return;
}
if (in_array($event, ['changepwd', 'user_changepwd', 'paypwd', 'user_paypwd'], true)) {
if (!$this->auth->isLogin()) {
$this->error(__('Please login first'), null, 401);
return;
}
$user = $this->auth->getUser();
if (!$user || (string)$user['mobile'] !== $mobile) {
$this->error('仅支持向当前账号绑定的手机号发送验证码');
return;
}
} elseif (in_array($event, ['master_changepwd', 'master_paypwd'], true)) {
$token = $this->request->server('HTTP_TOKEN', $this->request->request('token', ''));
$masterAuth = MasterAuth::instance();
if (!$token || !$masterAuth->init($token)) {
$this->error(__('Please login first'), null, 401);
return;
}
$master = $masterAuth->getMaster();
if (!$master || (string)$master['mobile'] !== $mobile) {
$this->error('仅支持向当前师傅账号绑定的手机号发送验证码');
return;
}
} elseif (in_array($event, ['resetpwd', 'user_resetpwd'], true)) {
$user = Db::name('user')->field('id,status')->where('mobile', $mobile)->find();
if (!$user) {
$this->error('该手机号尚未注册账号');
return;
}
if (isset($user['status']) && $user['status'] !== 'normal') {
$this->error('账号已被禁用');
return;
}
} elseif ($event === 'master_resetpwd') {
$master = Db::name('master')->field('id,status')->where('mobile', $mobile)->find();
if (!$master) {
$this->error('该手机号尚未注册师傅账号');
return;
}
if (isset($master['status']) && $master['status'] !== 'normal') {
$this->error('师傅账号已被禁用');
return;
}
}
try {
$res = SmsService::sendVerifyCode($mobile, $event, $this->request->ip());
} catch (\Throwable $e) {
$this->error($e->getMessage());
return;
}
$this->success('验证码已发送,请注意查收', $res);
}
/**
* 检测短信验证码(不消耗验证码,仅预校验)
* @ApiMethod (POST)
*/
public function check()
{
$rawMobile = (string)$this->request->post('mobile', '');
$event = trim((string)$this->request->post('event', ''));
$captcha = trim((string)$this->request->post('captcha', $this->request->post('sms_code', '')));
if ($event === '') {
$event = 'user_resetpwd';
}
try {
SmsService::verifyCode($rawMobile, $captcha, $event, false);
} catch (\Throwable $e) {
$this->error($e->getMessage());
return;
}
$this->success('验证码正确');
}
}