TP5腾讯云短信
1,把Sms放到TP5的extend
2,控制器引入扩展类
Loader::import('Sms.SmsSingleSender', EXTEND_PATH);
3,调用方法发送短信即可
/**
* 发送短信
*/
private function sendSms($phone,$verify){
try {
$sender = new \SmsSingleSender(config("setting.sms_appid"), config("setting.sms_appkey"));
$params = [$verify,'10'];
// 假设模板内容为:测试短信,{1},{2},{3},上学。
$result = $sender->sendWithParam("86", $phone, config("setting.templId"),
$params, "", "", "");
$rsp = json_decode($result);
return $rsp;
} catch(\Exception $e) {
return $e;
}
}
完整代码
<?php
namespace app\api\controller\v1;
use think\Lang;
use think\Cache;
use think\config;
use think\Loader;
use think\DB;
use app\lib\IAuth;
use app\lib\Aes;
use app\lib\exception\ParameterException;
use app\api\validate\Sms as V_Sms;
use sms\SmsMultiSender;
use sms\SmsSenderUtil;
use sms\SmsSingleSender;
Loader::import('Sms.SmsSingleSender', EXTEND_PATH);
class Sms extends Base
{
function _initialize()
{
parent::_initialize();
$this->validate = new V_Sms();
}
/**
* 检查每次app请求的数据是否合法
*/
public function send()
{
$this->validate->goCheck('send',$param=$this->param);
//判断是否上次发送时间
if(Cache::get($param['mobile'].$param['type'])){
return show($this->res,"一分钟内不能频繁发送短信!",'-1');
}
$verify = rand(1000,9999);
$mesali = $this->sendSms($param['mobile'],$verify);
if($mesali){
//放入缓存
Cache::set($param['mobile'].$param['type'],$verify,60);
return show($this->res,"发送成功!",'1');
}else{
return show($this->res,"发送失败!",'-1');
}
}
/**
* 发送短信
*/
private function sendSms($phone,$verify){
try {
$sender = new \SmsSingleSender(config("setting.sms_appid"), config("setting.sms_appkey"));
$params = [$verify,'10'];
// 假设模板内容为:测试短信,{1},{2},{3},上学。
$result = $sender->sendWithParam("86", $phone, config("setting.templId"),
$params, "", "", "");
$rsp = json_decode($result);
return $rsp;
} catch(\Exception $e) {
return $e;
}
}
}
本文为Pasa吴原创文章,转载无需和我联系,但请注明来自Pasa吴博客https://www.pasawu.top
最新评论