thinkphp5 使用阿里大于短息验证 短信仍使用自带captcha生成

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值