<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\SMS\M3Result;
use App\TempPhone;
use App\SMS\SendTemplateSMS;
use Illuminate\Support\Facades\Cache;
class SendSMSController extends Controller
{
private $limits = array(
'register',
'login',
'forget-password',
);
public function sendSMS(Request $request,$type){
if(array_search($type,$this->limits) === false){
abort(404);
}
$m3_result = new M3Result();
$phone = $request->input('phone','');
if(!preg_match("/^1[34578]{1}[0-9]{9}$/",$phone)){
$m3_result->status = 1;
$m3_result->message = '不是正确的手机号码';
return $m3_result->toJson();
}
$sendTemplateSMS = new SendTemplateSMS();
$code = '';
$charset = '1234567890';
$_len = strlen($charset)-1;
for($i=0;$i<6;++$i){
$code .= $charset[mt_rand(0,$_len)];
}
$tempPhone = $this->verify($phone,$type,$code);
$m3_result = $sendTemplateSMS->sendTemplateSMS($phone,array($code,60),$type);
if($m3_result->status == 0){
$expiresAt = \Carbon\Carbon::now()->addMinutes(5);
$key = $this->get_vcode_key($phone,$type);
Cache::put($key, $tempPhone, $expiresAt);
$tempPhone->save();
}
return $m3_result->toJson();
}
/*
* 将验证码存储到数据库中 短信验证码
* @params $tempPhone 验证类短信添加发送控制
* @params $type register 注册 | login登录 | forget-password 忘记密码 | reset 会员中心修改手机或者邮箱发送验证码
* */
public function verify($phone,$type,$code){
$key = $this->get_vcode_key($phone,$type);
$tempPhone = Cache::get($key) ? Cache::get($key) : $tempPhone = new TempPhone();
$m3_result = new M3Result();
if($tempPhone && ($type =="register" || $type =="login" || $type =="forget-password")){
if( $tempPhone->createtime == date('Y-m-d',time()) && $tempPhone->count > 3 ){
$m3_result->status = 1;
$m3_result->message = '24小时内只能进行3次验证';
return $m3_result->toJson();
}
if( time() - $tempPhone->lastmodify < 60 ){
$m3_result->status = 1;
$m3_result->message = '1分钟发送一次,还没到一分钟则不进行发送';
return $m3_result->toJson();
}
if( $tempPhone['createtime'] != date('Y-m-d',time())){
$tempPhone['count'] = 0;
}
}
$tempPhone->phone=$phone;
$tempPhone->code=$code;
$tempPhone->lastmodify=time();
$tempPhone->count += 1;
$tempPhone->type = $type;
$tempPhone->createtime = date('Y-m-d',time());
return $tempPhone;
}
public function get_vcode_key($account,$type='register'){
return md5($account.$type);
}
}
class SendTemplateSMS
{
//主帐号
private $accountSid= 'aaf98f8947afdd330147b443bda50924';
//主帐号Token
private $accountToken= '407924769f1540d0973c2e439de22216';
//应用Id
private $appId='8aaf070858f8050b0158fb263252019d';
//请求地址,格式如下,不需要写https://
private $serverIP='app.cloopen.com';
//请求端口
private $serverPort='8883';
//REST版本号
private $softVersion='2013-12-26';
/**
* 发送模板短信
* @param to 手机号码集合,用英文逗号分开
* @param datas 内容数据 格式为数组 例如:array('Marry','Alon'),如不需替换请填 null
* @param $tempId 模板Id
*/
public function sendTemplateSMS($to,$datas,$type)
{
$maps= config('sms');
$tempId = $maps[$type] ?? false;
if($tempId){
throw new \Exception("cont found {$type}");
}
$m3_result = new M3Result;
// 初始化REST SDK
$rest = new CCPRestSDK($this->serverIP,$this->serverPort,$this->softVersion);
$rest->setAccount($this->accountSid,$this->accountToken);
$rest->setAppId($this->appId);
// 发送模板短信
// echo "Sending TemplateSMS to $to <br/>";
$result = $rest->sendTemplateSMS($to,$datas,$tempId);
if($result == NULL ) {
$m3_result->status = 3;
$m3_result->message = 'result error!';
}
if($result->statusCode != 0) {
$m3_result->status = $result->statusCode;
$m3_result->message = $result->statusMsg;
}else{
$m3_result->status = 0;
$m3_result->message = '发送成功';
}
return $m3_result;
}
}
代码类的下载:http://download.csdn.net/detail/q824178949/9724198