Files
fast_response/digital_doctor/application/api/controller/Sms.php
2025-03-17 10:56:09 +08:00

80 lines
2.4 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
/*
* description
* authorwh
* 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
* authorwh
*/
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);
});
}
}