123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- /**
- * Created by PhpStorm
- * User: 林志杰
- * Email: [email protected]
- * Time: 2020/1/11 17:56
- */
- declare(strict_types=1);
- namespace app\common\business;
- use AlibabaCloud\Client\Exception\ClientException;
- use app\common\lib\ClassArr;
- use app\common\lib\Num;
- use app\common\lib\sms\AliSms;
- class Sms
- {
- public static function sendCode(string $phoneNumber, int $len, string $type = 'ali'): bool
- {
- // 生成短信验证码
- $code = Num::getCode($len);
- // 普通模式
- // $sms = AliSms::sendCode($phoneNumber, $code);
- // 工厂模式1
- // $type = ucfirst($type);
- // $class = "app\common\lib\sms\{$type}Sms";
- // $sms = $class::sendCode($phoneNumber, $code);
- // 工厂模式2
- $classStats = ClassArr::smsClassStat();
- $class = ClassArr::initClass($type, $classStats);
- $sms = $class::sendCode($phoneNumber, $code);
- if ($sms) {
- // 将验证码记录到redis,并设置失效时间
- // 检查php环境是否有redis拓展
- // redis服务
- cache(config('redis.code_pre') . $phoneNumber, $code, config('redis.expire'));
- }
- return true;
- }
- }
|