Home > PHP Framework > ThinkPHP > Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

wpj
Release: 2020-04-30 11:40:47
Original
4149 people have browsed it

如今,短信验证码已成为网站、APP的基础必备应用,应用场景十分丰富,随着移动互联网的发展会越来越多。作为一名码农,对第三方短信接口也是必须掌握的。本文php中文网将介绍使用工厂模式怎么实现ThinkPHP6.0接入阿里云短信。

一、环境要求

    PHP版本 >= 7.1.0

    开发环境必须安装有Composer

    已开通阿里云短信服务,并且已获取AccessKey,创建模板和签名

    最重要的,阿里云账户余额一定要有钱。

这里我就不演示开通短信服务和创建签名模板了,小伙伴们可以查看官方文档:https://help.aliyun.com/document_detail/108072.html?spm=a2c4g.11186623.6.565.1b4825903BoqGV

二、使用Composer安装Thinkphp6.0

如果您是第一次安装,请在命令行中切换到您的web目录执行下面的命令

composer create-project topthink/think sms
Copy after login

本教程将安装在C盘www目录下 Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS三、使用Composer安装 Alibaba Cloud SDK for PHP

进到刚刚创建的sms项目下执行下面的命令

composer require alibabacloud/sdk
Copy after login

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS四、使用编辑器打开项目 ,并在config文件夹下创建sms.php配置文件来管理阿里短信配置信息

<?php

return [
    //阿里云短信API接口地址
    &#39;host&#39;              => &#39;dysmsapi.aliyuncs.com&#39;,

    //AccessKey ID
    &#39;access_key_id&#39;     => &#39;您的AccessKey ID&#39;,

    //Access Key Secret
    &#39;access_key_secret&#39; => &#39;您的Access Key Secret&#39;,

    //地区ID
    &#39;region_id&#39;         => &#39;cn-hangzhou&#39;,

    //模板CODE
    &#39;template_code&#39;     => &#39;您的模板CODE&#39;,

    //签名名称
    &#39;sign_name&#39;         => &#39;您的短信签名名称&#39;,
];
Copy after login

五、顺便在config文件夹下打开cache.php添加Redis缓存配置,后面发送短信验证码会用到

<?php

// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------

return [
    // 默认缓存驱动
    &#39;default&#39; => env(&#39;cache.driver&#39;, &#39;redis&#39;),

    // 缓存连接方式配置
    &#39;stores&#39;  => [
        &#39;file&#39; => [
            // 驱动方式
            &#39;type&#39;       => &#39;File&#39;,
            // 缓存保存目录
            &#39;path&#39;       => &#39;&#39;,
            // 缓存前缀
            &#39;prefix&#39;     => &#39;&#39;,
            // 缓存有效期 0表示永久缓存
            &#39;expire&#39;     => 0,
            // 缓存标签前缀
            &#39;tag_prefix&#39; => &#39;tag:&#39;,
            // 序列化机制 例如 [&#39;serialize&#39;, &#39;unserialize&#39;]
            &#39;serialize&#39;  => [],
        ],
        // Redis缓存
        &#39;redis&#39; =>  [
            //服务器地址
            &#39;host&#39;      =>  &#39;127.0.0.1&#39;,
            //redis端口
            &#39;port&#39;      =>  6379,
            //驱动方式
            &#39;type&#39;      =>  &#39;redis&#39;,
            //缓存前缀
            &#39;prefix&#39;    => &#39;sms_code_&#39;,
        ]
    ],
];
Copy after login

六、在app目录下创建common/lib/sms/Sms.php接口类,用来约束发送短信验证码的方法

<?php

namespace app\common\lib\sms;

//定义实现发送短信验证码的接口类,用来约束发送验证码的方法
interface Sms
{
    /**
     * @desc 发送短信验证码的方法
     * @param string $phone 手机号
     * @param int $code     验证码
     * @return mixed
     */
    public static function sendCode(string $phone, int $code);
}
Copy after login

七、在common/lib/sms目录下创建AliSms类来实现Sms接口的smsSend()

<?php

namespace app\common\lib\sms;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class AliSms implements Sms
{

    /**
     * @desc 阿里云发送短信验证码
     * @param string $phone 手机号
     * @param int $code 验证码
     * @return mixed|void
     * @throws ClientException
     */
    public static function sendCode(string $phone, int $code)
    {
        //判断手机号和验证码是否为空
        if (empty($phone) || empty($code)){
            return false;
        }

        AlibabaCloud::accessKeyClient(config(&#39;sms.access_key_id&#39;), config(&#39;sms.access_key_secret&#39;))->regionId(config(&#39;sms.region_id&#39;))->asDefaultClient();

        try {
            $result = AlibabaCloud::rpc()
                ->product(&#39;Dysmsapi&#39;)
                // ->scheme(&#39;https&#39;) // https | http
                ->version(&#39;2017-05-25&#39;)
                ->action(&#39;SendSms&#39;)
                ->method(&#39;POST&#39;)
                ->host(config(&#39;sms.host&#39;))
                ->options([
                    &#39;query&#39; => [
                        &#39;RegionId&#39; =>config(&#39;sms.region_id&#39;),
                        &#39;SignName&#39; => config(&#39;sms.sign_name&#39;),
                        &#39;PhoneNumbers&#39;  =>  $phone,
                        &#39;TemplateCode&#39;  =>  config(&#39;sms.template_code&#39;),
                        &#39;TemplateParam&#39; =>  json_encode([&#39;code&#39;  =>  $code]),
                    ],
                ])->request();
        } catch (ClientException $e) {
            return false;
        } catch (ServerException $e) {
            return false;
        }
        return true;
    }
}
Copy after login

八、在common\lib目录下创建生成短信验证码的类 Code.php

<?php

namespace app\common\lib;

class Code
{
    /**
     * @desc 生成4位或6位短信验证码,默认为4位
     * @param int $length 验证码长度
     * @return int
     */
    public static function getCode(int $length = 4)
    {
        $code = rand(1000,9999);
        if ($length == 6){
            $code = rand(100000,999999);
        }
        return $code;
    }
}
Copy after login

九、在common目录下创建service/Sms.php

<?php

namespace app\common\Service;

use app\common\lib\Code;

class Sms
{
    /**
     * @param string $phone 手机号
     * @param int $lengthCode 验证码长度
     * @param string $type 短信厂家,默认选用AliSms
     * @return mixed
     */
    public static function sendCode(string $phone,int $lengthCode,string $type=&#39;AliSms&#39;)
    {
        //生成短信验证码
        $code = Code::getCode(4);

        //使用工厂模式 调用Lib层发送短信
        $class = "app\common\lib\sms\\".$type;
        $sms = $class::sendCode($phone,$code);

        if ($sms){
            //发送成功,把短信验证码存储Redis缓存中,并给失效时间
            cache($phone,$code,300);
        }
        return $sms;
    }
}
Copy after login

十、在common目录下创建validate/SmsValidate验证器

<?php

namespace app\common\validate;

use think\Validate;

class SmsValidate extends Validate
{
    //验证规则
    protected $rule = [
        &#39;phone&#39;     =>  &#39;require|mobile&#39;,
        &#39;code&#39;      =>  &#39;require|number&#39;
    ];

    //错误信息
    protected $message = [
        &#39;phone.require&#39;     =>  &#39;请输入手机号&#39;,
        &#39;phone.mobile&#39;      =>  &#39;手机号格式错误&#39;,
        &#39;code.require&#39;      =>  &#39;短信验证码不能为空&#39;,
        &#39;code.number&#39;       =>  &#39;短信验证码必须为纯数字&#39;
    ];

    //验证场景
    protected $scene = [
        &#39;sendCode&#39;  =>  [&#39;phone&#39;]
    ];
}
Copy after login

十一、在controller目录下创建Sms.php

<?php

namespace app\controller;

use app\common\validate\SmsValidate;

class Sms
{
    /**
     * @desc 发送短信验证码
     * @return \think\response\Json
     */
    public function code()
    {
        if (request()->isPost()){
            //获取手机号
            $data =  [
                &#39;phone&#39; =>  request()->param(&#39;phone&#39;,&#39;&#39;,&#39;trim&#39;),
            ];

            //参数校验
            $validate = new SmsValidate();
            if (!$validate->scene(&#39;sendCode&#39;)->check($data)){
                return json([&#39;code&#39;=>0,&#39;msg&#39;=>$validate->getError()]);
            }
            //发送短信验证码
            if (\app\common\Service\Sms::sendCode($data[&#39;phone&#39;],6,&#39;AliSms&#39;)){
                return json([&#39;code&#39;=>1,&#39;msg&#39;=>&#39;发送成功,请注意查收。&#39;]);
            }else{
                return json([&#39;code&#39;=>0,&#39;msg&#39;=>&#39;发送失败,请稍后重试!&#39;]);
            }

        }
    }
}
Copy after login

十二、使用PostMan测试发送短信验证码

不输入手机号或输入错误手机号会给相应的提示。

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

输入正确的手机号,提示发送短信验证成功Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS这时我们来看看收到的验证码和缓存中的验证码是否一致Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS到这里我们的发送验证码已经完成

十三、这时我们需要校验验证码是否正确,在app\controller目录下创建Login.php

<?php

namespace app\controller;

use app\common\validate\SmsValidate;

class Login
{
    public function index()
    {
        //接收参数
        $data = [
            &#39;phone&#39; =>  request()->param(&#39;phone&#39;,&#39;&#39;,&#39;trim&#39;),
            &#39;code&#39;  =>  request()->param(&#39;code&#39;,&#39;&#39;,&#39;trim&#39;),
        ];
        //参数校验
        $validate = new SmsValidate();
        if (!$validate->check($data)){
            return json([&#39;code&#39;=>0,&#39;msg&#39;=>$validate->getError()]);
        }

        //从Redis中获取验证码
        $redisCode = cache($data[&#39;phone&#39;]);

        //判断验证码是否正确
        if (empty($redisCode)){
            return json([&#39;code&#39;=>0,&#39;msg&#39;=>&#39;验证码已过期,请重新发送!&#39;]);
        }
        if ($redisCode != $data[&#39;code&#39;]){
            return json([&#39;code&#39;=>0,&#39;msg&#39;=>&#39;验证码输入错误,请重新输入!&#39;]);
        }

        return &#39;验证成功&#39;;
    }
}
Copy after login

十四、使用PostMan校验验证码是否正确

输入错误的验证码,会给出响应的提示

Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

输入正确的验证码,提示验证成功Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

看到这里我相信很多小伙伴的验证码都已发送成功了。

不知道小伙伴们有没有发现文中有两处参数校验的代码相识度很高,在后续的thinkphp技术文章中我会对这个问题进行优化,感兴趣的小伙伴请关注。

The above is the detailed content of Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template