Sms.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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\ClassArr;
  12. use app\common\lib\Num;
  13. use app\common\lib\sms\AliSms;
  14. class Sms
  15. {
  16. public static function sendCode(string $phoneNumber, int $len, string $type = 'ali'): bool
  17. {
  18. // 生成短信验证码
  19. $code = Num::getCode($len);
  20. // 普通模式
  21. // $sms = AliSms::sendCode($phoneNumber, $code);
  22. // 工厂模式1
  23. // $type = ucfirst($type);
  24. // $class = "app\common\lib\sms\{$type}Sms";
  25. // $sms = $class::sendCode($phoneNumber, $code);
  26. // 工厂模式2
  27. $classStats = ClassArr::smsClassStat();
  28. $class = ClassArr::initClass($type, $classStats);
  29. $sms = $class::sendCode($phoneNumber, $code);
  30. if ($sms) {
  31. // 将验证码记录到redis,并设置失效时间
  32. // 检查php环境是否有redis拓展
  33. // redis服务
  34. cache(config('redis.code_pre') . $phoneNumber, $code, config('redis.expire'));
  35. }
  36. return true;
  37. }
  38. }