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);
| | | | | | | | | | | | | | | | |----------------------------------------------------------------------------------------------------------------