- 插件管理:集成 qcloudsms 腾讯云短信发送插件,配置应用AppID/AppKey、短信签名及各业务模板ID(登录、修改密码、重置密码、支付密码、订单核销通知) - 业务封装:新增 SmsService 统一业务短信服务,实现防刷限流、冷却控制、短信验证码发送与防重放校验,支持完工订单自动下发客户核销码 - 接口层:恢复并升级 Sms 控制器,并在 Auth/MasterAuth/Security 控制器中支持手机验证码登录、找回登录密码及短信验证设置/修改支付密码 - 小程序端:用户端与师傅端登录页、账号安全中心、支付密码组件全面适配短信验证码交互,优化用户找回密码与支付密码安全操作流程 - 部署脚本:优化 deploy_all.py 打包剪枝逻辑,全局过滤深层 .git 目录并规范打包相对路径
117 行
4.0 KiB
PHP
可执行文件
117 行
4.0 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());
|
|
$this->success('验证码已发送,请注意查收', $res);
|
|
} catch (\Throwable $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检测短信验证码(不消耗验证码,仅预校验)
|
|
* @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);
|
|
$this->success('验证码正确');
|
|
} catch (\Throwable $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|