Sms.php 992 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User: 林志杰
  5. * Email: [email protected]
  6. * Time: 2020/1/11 17:56
  7. */
  8. declare(strict_types=1);
  9. namespace app\common\business;
  10. use AlibabaCloud\Client\Exception\ClientException;
  11. use app\common\lib\Num;
  12. use app\common\lib\sms\AliSms;
  13. class Sms
  14. {
  15. public static function sendCode(string $phoneNumber, int $len, string $type = 'ali'): bool
  16. {
  17. // 生成短信验证码
  18. $code = Num::getCode($len);
  19. // 普通模式
  20. // $sms = AliSms::sendCode($phoneNumber, $code);
  21. // 工厂模式1
  22. $type = ucfirst($type);
  23. $class = "app\common\lib\sms\{$type}Sms";
  24. $sms = $class::sendCode($phoneNumber, $code);
  25. if ($sms) {
  26. // 将验证码记录到redis,并设置失效时间
  27. // 检查php环境是否有redis拓展
  28. // redis服务
  29. cache(config('redis.code_pre') . $phoneNumber, $code, config('redis.expire'));
  30. }
  31. return true;
  32. }
  33. }