thinkphp5.0.24 uses Ali Dayu SMS service to send verification code

1- Download the PHPSDk of Ali Big Fish

A copy of the specific document is provided for download:

https://github.com/helloworldmy007/api_adk

Put this folder into the extend directory in the TP5 framework (the extend directory is used to store some third-party libraries).
We only need to copy the api_sdk in the directory to extend under the root directory of tp5. After the folder is copied, we'd better change the name, for example, we change it to alisms.

2- Write function calls in the function library common under the application directory

// 应用公共文件
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;

//阿里短信函数,$mobile为手机号码,$code为自定义随机数
function sendMsg($mobile,$code){

    //这里的路径EXTEND_PATH就是指tp5根目录下的extend目录,系统自带常量。alisms为我们复制api_sdk过来后更改的目录名称
    require_once EXTEND_PATH.'alisms/vendor/autoload.php';
    Config::load();             //加载区域结点配置

    $accessKeyId = '×××××××××××××';  //阿里云短信获取的accessKeyId

    $accessKeySecret = '×××××××××××××';    //阿里云短信获取的accessKeySecret

    //这个个是审核过的模板内容中的变量赋值,记住数组中字符串code要和模板内容中的保持一致
    //比如我们模板中的内容为:你的验证码为:${code},该验证码5分钟内有效,请勿泄漏!
    $templateParam = array("code"=>$code);           //模板变量替换

    $signName = 'xxxxxxxxx'; //这个是短信签名,要审核通过

    $templateCode = 'SMS_×××××××';   //短信模板ID,记得要审核通过的


    //短信API产品名(短信产品名固定,无需修改)
    $product = "Dysmsapi";
    //短信API产品域名(接口地址固定,无需修改)
    $domain = "dysmsapi.aliyuncs.com";
    //暂时不支持多Region(目前仅支持cn-hangzhou请勿修改)
    $region = "cn-hangzhou";

    // 初始化用户Profile实例
    $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
    // 增加服务结点
    DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
    // 初始化AcsClient用于发起请求
    $acsClient= new DefaultAcsClient($profile);

    // 初始化SendSmsRequest实例用于设置发送短信的参数
    $request = new SendSmsRequest();
    // 必填,设置雉短信接收号码
    $request->setPhoneNumbers($mobile);

    // 必填,设置签名名称
    $request->setSignName($signName);

    // 必填,设置模板CODE
    $request->setTemplateCode($templateCode);

    // 可选,设置模板参数
    if($templateParam) {
        $request->setTemplateParam(json_encode($templateParam));
    }

    //发起访问请求
    $acsResponse = $acsClient->getAcsResponse($request);

    //返回请求结果
    $result = json_decode(json_encode($acsResponse),true);
    return $result;
}

3-html code

Need to introduce layui plugin

<form action="index.php/index/index/reg" method="post" class="form form-horizontal" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-c-red">*</span>手机:</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" value="" placeholder="" id="phone" name="phone">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label layui-col-xs-4 layui-col-sm-3"><span class="c-red">*</span>验证码:</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" value="" placeholder="请输入短信验证码" name="captcha"  disabled="disabled" id="captcha"/>
        </div>
        <div class="layui-form-mid layui-word-aux"><input type="button" class="layui-btn layui-btn-xs" onclick="bindPhoneNum(this)" id="get_captcha" value="获取验证码"/></div>
    </div>

    <div class="layui-row cl">
        <div class="layui-col-xs-8 layui-col-sm-9 layui-col-xs-offset-4 layui-col-sm-offset-3">
            <input class="layui-btn layui-btn-primary radius"  type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
        </div>
    </div>
</form>

4-ajax request

Use layui to call bindPhoneNum method

layui.use(['layer','form'],function () {
        $ = layui.jquery;
        layer = layui.layer;
        form = layui.form;
 });
 function bindPhoneNum(){
    //ajaxt提交去后台发送验证码
    $.getJSON('/admin/user/sendSms?phone='+$("#phone").val(),function (data) {
        console.dir(data);
    })
    //启用输入框
    $('#captcha').prop('disabled',false);
 
    var time=30;
    var interval = setInterval(function(){
        time--;
        if(time<=0){
            clearInterval(interval);
            var html = '获取验证码';
            $('#get_captcha').prop('disabled',false);
        } else{
            var html = time + ' 秒后再次获取';
            $('#get_captcha').prop('disabled',true);
        }
 
        $('#get_captcha').val(html);
    },1000);
}

5- call in the controller

/*
        * 前台ajax请求发送短信验证码
        */
    public function sendSms($phone)
    {
        $mobile = $phone;
        //$code = 1111;
        $code = mt_rand(10000, 99999);
        $result = sendMsg($mobile, $code);
        $code=$code;
        if ($result['Code'] == 'OK') {
            //存到cookie
            echo $code;
            cookie("code",$code,3600);
        }
    }
    //注册时控制器的代码
    public function reg(){
        if(\request()->isPost()){
            //读取cookie的值
            $code=cookie("code");
            dump($code);
            if($code!=input("captcha")){        //判断输入的验证码跟存入cookie的验证码
                echo "验证码错误";exit;
            }
            $data=$_POST;
            var_dump($data);exit;
        }
    }

Guess you like

Origin blog.csdn.net/hgb24660/article/details/100021237