<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\User;
use Illuminate\Session\Middleware\StartSession;
class CodeController extends Controller{
public $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
public $request;
public $session;
public $user;
public function __construct(Request $request, StartSession $session, User $user)
{
$this->request = $request;
$this->session = $session;
$this->user = $user;
}
//验证码生成方法
public function createCode($length){
$key='';
$pattern='1234567890';
for($i=0;$i<$length;++$i) {
$key .= $pattern{mt_rand(0,9)}; // 生成php随机数
}
return $key;
}
public function sendCode(Request $request){
$tel = $this->request->input('tel');
$checkTel = $this->user->where('tel',$tel)->first();
if ($checkTel){
return response()->json([
"code" => 0,
"message" => "手机号无效或已注册"
]);
}
$code = $this->createCode(6);
$smsConf = array(
'key' => 'key', //您申请的APPKEY
'mobile' => $tel, //接受短信的用户手机号码
'tpl_id' => '95249', //您申请的短信模板ID,根据实际情况修改
'tpl_value' => '#code#='. $code //您设置的模板变量,根据实际情况修改
);
$content = $this->sendCodeFunc($this->sendUrl, $smsConf,1); //请求发送短信
if($content){
$result = json_decode($content,true);
$error_code = $result['error_code'];
if($error_code == 0){
//状态为0,说明短信发送成功 把验证码存入缓存以便对比
$this->request->session()->pull('code','');
$this->request->session()->pull('phone','');
$this->request->session()->put('code',$code);
$this->request->session()->put('phone',$tel);
return response()->json([
"code" => 200,
"message" => "验证码发送成功"
]);
}else{
//状态非0,说明失败
$msg = $result['reason'];
return response()->json([
"code" => 0,
"message" => "短信发送失败(".$error_code."):".$msg
]);
}
}else{
//返回内容异常,以下可根据业务逻辑自行修改
return response()->json([
"code" => 0,
"message" => "请求发送短信失败"
]);
}
}
public function sendMessage($tel,$tempId){
$smsConf = array(
'key' => '', //您申请的APPKEY
'mobile' => $tel, //接受短信的用户手机号码
'tpl_id' => $tempId, //您申请的短信模板ID,根据实际情况修改
);
$content = $this->sendCodeFunc($this->sendUrl, $smsConf,1); //请求发送短信
if($content){
$result = json_decode($content,true);
$error_code = $result['error_code'];
if($error_code == 0){
return true;
}else{
return false;
}
}else{
return false;
}
}
public function checkCode(){
$code = $this->request->input('code');
$tel = $this->request->input('tel');
$sCode = $this->request->session()->get('code');
$phone = $this->request->session()->get('phone');
if ($code == $sCode && $tel == $phone){
return response()->json([
"code" => 200,
"message" => "验证码正确"
]);
}else{
return response()->json([
"code" => 0,
"message" => "验证码错误或手机号不匹配"
]);
}
}
public function sendCodeFunc($url, $params = false, $ispost = 0)
{
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
}
聚合短信laravel框架后台PHP代码
猜你喜欢
转载自blog.csdn.net/zgb4687199/article/details/82818000
今日推荐
周排行