80 lines
2.4 KiB
PHP
80 lines
2.4 KiB
PHP
<?php
|
||
/*
|
||
* description:
|
||
* author:wh
|
||
* email:
|
||
* createTime:{2024/8/13} {11:22}
|
||
*/
|
||
|
||
namespace app\api\controller;
|
||
|
||
|
||
use app\common\model\TabConf;
|
||
use think\Db;
|
||
use wanghua\general_utility_tools_php\Mmodel;
|
||
use wanghua\general_utility_tools_php\sms\AliSms;
|
||
use wanghua\general_utility_tools_php\tool\Tools;
|
||
use wanghua\general_utility_tools_php\Validate;
|
||
|
||
class Sms extends BaseHttpApi
|
||
{
|
||
|
||
/**
|
||
* desc:发送短信验证码
|
||
* 参数:
|
||
* mobile 手机号
|
||
* event 发送场景
|
||
*
|
||
* api/sms/send
|
||
* author:wh
|
||
*/
|
||
function send(){
|
||
return Mmodel::catchJson(function (){
|
||
$mobile = input('mobile');
|
||
if(empty($mobile)){
|
||
return Tools::set_fail('手机号错误');
|
||
}
|
||
if(!Validate::is_mobile($mobile)){
|
||
return Tools::set_fail('手机号错误');
|
||
}
|
||
//发送间隔不能低于60秒
|
||
$sms_record = Db::table(TabConf::$fa_sms)
|
||
->where('create_time','>',date('Y-m-d H:i:s',time()-60))
|
||
->where('mobile',$mobile)
|
||
->order('id desc')
|
||
->find();
|
||
if($sms_record){
|
||
return Tools::set_fail('发送太频繁,请稍后再试');
|
||
}
|
||
|
||
|
||
//查询记录,10分钟内超过10次禁止发送
|
||
$count = Db::table(TabConf::$fa_sms)
|
||
->where('create_time','>',date('Y-m-d H:i:s',time()-600))
|
||
->where('mobile',$mobile)
|
||
->count();
|
||
if($count>=10){
|
||
return Tools::set_fail('发送次数过多,请稍后再试');
|
||
}
|
||
$config = config('sms_config');
|
||
$code = Tools::rand_str(6,3);
|
||
Tools::log_to_write_txt(['发送短信,开始','mobile'=>$mobile,'code'=>$code,'$config'=>$config]);
|
||
$res = (new AliSms($config,$config['sms_sign_name'],$config['sms_template_code']))
|
||
->send($mobile,json_encode(['code'=>$code]));
|
||
Tools::log_to_write_txt(['发送短信,结束',$res]);
|
||
|
||
|
||
//写入发送记录
|
||
$sms_data = [
|
||
'event'=>input('event'),
|
||
'mobile'=>$mobile,
|
||
'code'=>$code,
|
||
'times'=>0,
|
||
'ip'=>request()->ip(),
|
||
];
|
||
Db::table(TabConf::$fa_sms)->insert($sms_data);
|
||
|
||
return Tools::set_ok('操作成功',$res);
|
||
});
|
||
}
|
||
} |