您当前的位置: 首页 > 

苗先生的PHP记录

暂无认证

  • 5浏览

    0关注

    190博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

简单的工厂模式(使用多种SMS发送短信)

苗先生的PHP记录 发布时间:2020-06-04 17:13:16 ,浏览量:5

singwa tp6shop 7-10

使用接口,创建同级文件接口类SmsBase

interface SmsBase
{
    public static function sendCode(string $phone ,int $code);
}

让SDK直接实现接口

class AliSms implements SmsBase
在业务逻辑里不再调用单个短信SDK(阿里云),而是使用工厂模式,在控制器调用短信时直接传一个sms名称

controller层

if (SmsBusiness::sendCode($phoneNumber ,6 ,'jd')){
            return show(config('status.success') , "短信验证码发送成功");
        }

business层

 public static function sendCode(string $phoneNumber ,int $length ,string $type ='ali') : bool
    {
        //生成验证码
        $code = Num::getCode($length);

        //发送验证码
//        $sms = AliSms::sendCode($phoneNumber ,$code);
        $type = ucfirst($type); //首字母大写(类文件首字母都是大写)
        $class = "app\common\lib\sms\\".$type."Sms";
        $sms = $class::sendCode($phoneNumber , $code);

| | | | | | | | | | | | | | | | |----------------------------------------------------------------------------------------------------------------

关注
打赏
1665468453
查看更多评论
立即登录/注册

微信扫码登录

0.0726s