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'=>'验证码错误...');
};