Files
yusheng-php/application/api/controller/Sms.php
2025-12-08 12:06:53 +08:00

108 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\User;
use think\Db;
use think\Env;
/**
* 手机短信接口
*/
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()
{
$mobile = $this->request->post("mobile");
$event = $this->request->post("event" , 'default');
if (!checkMobile($mobile)) {
return V(0, '手机号不正确');
}
//检测是否注销过
$config_time = get_system_config_value('cancel_no_login');
$is_del = db::name('user')->where(['mobile' => $mobile,'delete_time' => ['<>', 0]])->find();
if (time() - $is_del['delete_time'] < $config_time * 24 * 3600) {//30天内注销过
return V(0, '注销30天内不可操作。');
}
//频率控制
$last = db::name('sms')->where(['mobile' => $mobile, 'event' => $event])->order('id', 'DESC')->find();
if ($last && time() - $last['createtime'] < 60) {
return V(0, '发送频繁');
}
$ipSendTotal = db::name('sms')->where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
if ($ipSendTotal >= 7) {
return V(0, '发送频繁');
}
//发送短信
$ret = $this->send_smsbao_msg($mobile, $event);
if ($ret['code'] == 1) {
return V($ret['code'], $ret['msg'],$ret['data']);
} else {
return V(0, '系统错误,请检查短信配置!');
}
}
/**
* 检测验证码
*
* @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, $code, $event = 'default')
{
if (!checkMobile($mobile)) {
return V(0, '手机号不正确');
}
$is_code = db::name('sms')->where(['mobile' => $mobile, 'event' => $event])->find();
if ($is_code && $is_code['code'] == $code) {
//验证码正确
db::name('sms')->where(['mobile' => $mobile, 'event' => $event])->delete();
return V(1, '验证码正确');
} else {
return V(0, '验证码错误');
}
}
private function send_smsbao_msg($mobile,$event = 'default'){
$dxb_temp = Env::get('smsbao.dxb_temp');
$dxb_name = Env::get('smsbao.dxb_name');
$dxb_pwd = Env::get('smsbao.dxb_pwd');
$sms_code = generateRandoms();
//随机生成4位数
$content = str_replace('{code}',$sms_code,$dxb_temp);//要发送的短信内容
//短信宝
$url = "https://api.smsbao.com/sms?u=".$dxb_name."&p=".md5($dxb_pwd)."&m=".$mobile."&c=".urlencode($content);
$result = myCurl($url);
$result_arr = json_decode($result, true);
if ($result_arr == 0) {
db::name('sms')->insert([
'event' => $event,
'mobile' => $mobile,
'code' => $sms_code,
'ip' => $this->request->ip(),
'createtime' => time(),
]);
return ['code' => 1, 'msg' => '发送成功', 'data' => null];
} else {
return ['code' => 0, 'msg' => '发送失败', 'data' => null];
}
}
}