Files
YwxAppThink/app/api/controller/v1/Sms.php
T

142 lines
5.4 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp<admin@ywxapp.cn>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace app\Api\Controller\V1;
use think\facade\Event;
use think\exception\ValidateException;
use ywxapp\controller\ApiController;
use ywxapp\library\Sms as Smslib;
use ywxapp\model\Sms as SmsModel;
use ywxapp\model\MemberUser as UserModel;
use ywxapp\utils\Random;
/**
* Sms 类
*
* @author ywxapp <admin@ywxapp.cn>
*/
class Sms extends ApiController
{
public function index()
{
return json(['message' => 'SMS API']);
}
/**
* 发送验证码
*
* @ApiMethod (POST)
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
* @ApiParams (name="event", type="string", required=true, description="事件名称")
* @ApiParams (name="type", type="string", required=false, description="验证类型,auto为自动验证,system为系统验证码")
* @ApiParams (name="source_id", type="string", required=false, description="来源ID")
*/
public function send()
{
$mobile = $this->request->post("mobile");
$event = $this->request->post("event", 'register');
$type = $this->request->post("type", 'auto');
$source_id = $this->request->post("source_id", '');
try {
validate([
'mobile' => 'require|max:11',
'event' => 'require|alpha|lower',
])->message([
'mobile.require' => '手机号必须',
'mobile.max' => '手机号最多不能超过11个字符',
'event.require' => '事件名称必须',
'event.alpha' => '事件名称必须是字母',
'event.lower' => '事件名称必须是小写字母',
])->check([
'mobile' => $mobile,
'event' => $event,
]);
$last = Smslib::get($mobile, $event);
if ($last && time() - (int) $last['create_at'] < 60) {
$this->result->error('发送频繁');
}
$ipSendTotal = SmsModel::where(['ip' => $this->request->ip()])->whereTime('create_at', '-1 hours')->count();
if ($ipSendTotal >= 5) {
$this->result->error('发送频繁');
}
if ($event) {
$userinfo = UserModel::getByMobile($mobile);
if ($event == 'register' && $userinfo) {
//已被注册
$this->result->error('已被注册');
} elseif (in_array($event, ['changemobile']) && $userinfo) {
//被占用
$this->result->error('已被占用');
} elseif (in_array($event, ['changepwd', 'resetpwd']) && ! $userinfo) {
//未注册
$this->result->error('未注册');
}
}
if (! Event::hasListener('SmsSend')) {
$this->result->error('请在后台插件管理安装短信验证插件');
}
$ret = Smslib::send($mobile, null, $event);
if ($ret) {
$this->result->success('发送成功');
} else {
$this->result->error('发送失败,请检查短信配置是否正确');
}
} catch (ValidateException $e) {
$this->result->error($e->getError());
}
}
/**
* 检测验证码
*
* @ApiMethod (POST)
* @ApiParams (name="mobile", type="string", required=true, description="手机号")
* @ApiParams (name="event", type="string", required=true, description="事件名称")
* @ApiParams (name="captcha", type="string", required=true, description="验证码")
*/
public function check()
{
$mobile = $this->request->post("mobile");
$event = $this->request->post("event", 'register');
$captcha = $this->request->post("captcha");
if (! $mobile || ! \think\Validate::regex($mobile, "^1\d{10}$")) {
$this->result->error('手机号不正确');
}
if (! preg_match("/^[a-z0-9_\-]{3,30}\$/i", $event)) {
$this->result->error('事件名称错误');
}
if (! preg_match("/^[a-z0-9]{4,6}\$/i", $captcha)) {
$this->result->error('验证码格式错误');
}
if ($event) {
$userinfo = UserModel::getByMobile($mobile);
if ($event == 'register' && $userinfo) {
//已被注册
$this->result->error('已被注册');
} elseif (in_array($event, ['changemobile']) && $userinfo) {
//被占用
$this->result->error('已被占用');
} elseif (in_array($event, ['changepwd', 'resetpwd']) && ! $userinfo) {
//未注册
$this->result->error('未注册');
}
}
$ret = Smslib::check($mobile, $captcha, $event);
if ($ret) {
$this->result->success('成功');
} else {
$this->result->error('验证码不正确');
}
}
}