thinkphp5 使用阿里大于短息验证 短信仍使用自带captcha生成
2018-03-13 20:55
537 查看
1.修改captcha扩展类库,放置(覆盖)到:\vendor\topthink目录(附件think-captcha.zip);
2.新建控制器到Sms.php:\application\api\controller;
内容:<?php
namespace app\api\controller;
use think\Db;
use think\Controller;
use think\Request;
use think\Session;
class Sms extends Controller
{
//获取手机验证码
public function getValicode($dh='')
{
if(empty($dh)){
return array('code'=>0,'msg'=>'请输入电话号码');
};
$alidayu = new \app\common\alidayu\Sms();
$alidayu->sendCode($dh,getauthcode());
return array('code'=>1,'msg'=>'发送成功');
}
}3.复制阿里大于类库到:\application\common\alidayu\
配置\application\common\alidayu\Sms.php<?php
namespace app\common\alidayu;
use \think\Config;
class Sms{
public static function sendCode($tel,$code) {
$c = new TopClient;
$c->appkey = Config::get("alidayu.appkey");//appkey
$c->secretKey = Config::get("alidayu.appsecret");//appSec
$req = new AlibabaAliqinFcSmsNumSendRequest;
$req->setExtend("");
$req->setSmsType("normal");
$req->setSmsFreeSignName("身份验证");
$req->setSmsParam("{\"code\":\"$code\",\"product\":\"微站\"}");
$req->setRecNum($tel);
$req->setSmsTemplateCode("SMS_888888");//模板
$resp = $c->execute($req);
return json_encode($resp);
}
public static function sendMsg($tel,$pwd){
$c = new TopClient;
$c->appkey = Config::get("alidayu.appkey");//appkey
$c->secretKey = Config::get("alidayu.appsecret");//appSec
$req = new AlibabaAliqinFcSmsNumSendRequest;
$req->setExtend("");
$req->setSmsType("normal");
$req->setSmsFreeSignName("公司");
$req->setSmsParam("{\"dh\":\"$tel\",\"pwd\":\"$pwd\"}");
$req->setRecNum($tel);
$req->setSmsTemplateCode("SMS_111111");//模板,需要到大于官网配置
$resp = $c->execute($req);
writeLog('pwdmsg',$resp);
return json_encode($resp);
}
}
?>4.在config.php配置大于的appkey://短信配置
'alidayu' => [
'appkey' => '11111111',
'appsecret' => '8888888888888888888888888888'
],5.前端使用: //发送短信
function getValicode() {
if ($("#data_touser").val() == "") {
$.toast("请输入手机号", "cancel");
} else {
$.get('/api/sms/getValicode', {
dh: $("#data_touser").val()
}, function(json, textStatus) {
if (json.code === 0) {
$.toast("发送失败<br>" + json.msg, "cancel");
return;
}
$('#getmessage').html('已经发送');
$('#getmessage').attr('disabled', 'true');
$('#getmessage').css({
color: 'rgb(156,156,156)'
});
});
}
}6.后端验证:$captcha = new \think\captcha\Captcha();
if (!$captcha->check($vcode)){
return array('code'=>0,'msg'=>'验证码错误...');
};
2.新建控制器到Sms.php:\application\api\controller;
内容:<?php
namespace app\api\controller;
use think\Db;
use think\Controller;
use think\Request;
use think\Session;
class Sms extends Controller
{
//获取手机验证码
public function getValicode($dh='')
{
if(empty($dh)){
return array('code'=>0,'msg'=>'请输入电话号码');
};
$alidayu = new \app\common\alidayu\Sms();
$alidayu->sendCode($dh,getauthcode());
return array('code'=>1,'msg'=>'发送成功');
}
}3.复制阿里大于类库到:\application\common\alidayu\
配置\application\common\alidayu\Sms.php<?php
namespace app\common\alidayu;
use \think\Config;
class Sms{
public static function sendCode($tel,$code) {
$c = new TopClient;
$c->appkey = Config::get("alidayu.appkey");//appkey
$c->secretKey = Config::get("alidayu.appsecret");//appSec
$req = new AlibabaAliqinFcSmsNumSendRequest;
$req->setExtend("");
$req->setSmsType("normal");
$req->setSmsFreeSignName("身份验证");
$req->setSmsParam("{\"code\":\"$code\",\"product\":\"微站\"}");
$req->setRecNum($tel);
$req->setSmsTemplateCode("SMS_888888");//模板
$resp = $c->execute($req);
return json_encode($resp);
}
public static function sendMsg($tel,$pwd){
$c = new TopClient;
$c->appkey = Config::get("alidayu.appkey");//appkey
$c->secretKey = Config::get("alidayu.appsecret");//appSec
$req = new AlibabaAliqinFcSmsNumSendRequest;
$req->setExtend("");
$req->setSmsType("normal");
$req->setSmsFreeSignName("公司");
$req->setSmsParam("{\"dh\":\"$tel\",\"pwd\":\"$pwd\"}");
$req->setRecNum($tel);
$req->setSmsTemplateCode("SMS_111111");//模板,需要到大于官网配置
$resp = $c->execute($req);
writeLog('pwdmsg',$resp);
return json_encode($resp);
}
}
?>4.在config.php配置大于的appkey://短信配置
'alidayu' => [
'appkey' => '11111111',
'appsecret' => '8888888888888888888888888888'
],5.前端使用: //发送短信
function getValicode() {
if ($("#data_touser").val() == "") {
$.toast("请输入手机号", "cancel");
} else {
$.get('/api/sms/getValicode', {
dh: $("#data_touser").val()
}, function(json, textStatus) {
if (json.code === 0) {
$.toast("发送失败<br>" + json.msg, "cancel");
return;
}
$('#getmessage').html('已经发送');
$('#getmessage').attr('disabled', 'true');
$('#getmessage').css({
color: 'rgb(156,156,156)'
});
});
}
}6.后端验证:$captcha = new \think\captcha\Captcha();
if (!$captcha->check($vcode)){
return array('code'=>0,'msg'=>'验证码错误...');
};
相关文章推荐
- Thinkphp5使用阿里大于短信验证
- Thinkphp5使用阿里大于短信验证
- Thinkphp5使用阿里大于短信验证
- Thinkphp5使用阿里大于短信验证
- Thinkphp5使用阿里大于短信验证
- Thinkphp5使用阿里大于短信验证
- Thinkphp5使用阿里大于短信验证
- 使用阿里大于接口实现一个简单的短信验证(Java版)
- thinkPHP3.2.3实现阿里大于短信验证的方法
- 基于ThinkPHP与阿里大于的PHP短信验证功能
- thinkPHP3.2.3实现阿里大于短信验证(阿里云合并后的版本)
- thinkphp下阿里大于短信验证
- 使用“阿里大于”和 “企信通” 短信异步发送
- thinkphp引入 阿里大于短信平台
- 阿里云通信(原阿里大于)短信验证nodejs开发遇到的坑
- Laravel5中使用阿里大于(鱼)发送短信验证码
- ThinkPHP中集成阿里大于短信发送SDK
- JAVA对接阿里大于短信验证
- 使用阿里大于发送短信验证码并存储到redis中
- Laravel5中使用阿里大于(鱼)发送短信验证码