EasySms.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: MADAO
  5. * Date: 2018/3/16
  6. * Time: 19:18
  7. */
  8. namespace App\Libs;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Redis;
  11. use Overtrue\EasySms\EasySms as Sms;
  12. use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
  13. class EasySms extends Sms
  14. {
  15. public function __construct(array $config = [])
  16. {
  17. $init = config('sms.init');
  18. $config = array_merge($init, $config);
  19. parent::__construct($config);
  20. }
  21. public function send($to, $message = [], array $gateways = [])
  22. {
  23. if (!is_mobile($to)) {
  24. return response()->json(['message' => '手机号格式错误'], 400);
  25. }
  26. $cacheData = EasySms::getSmsCodeCache($to);
  27. if ($cacheData) {
  28. if (time() - $cacheData['send_time'] <= config('sms.sendTimeout') ?? 60) {
  29. return response()->json(['message' => '短信消息发送不能太过频繁'], 400);
  30. }
  31. }
  32. $smsCode = $this->createSmsCode();
  33. $default = [
  34. 'content' => '您好!您的验证码为${code},请在15分钟内填写。',
  35. 'template' => 'SMS_162110318',
  36. 'data' => [
  37. 'code' => $smsCode
  38. ],
  39. ];
  40. $message = array_merge($default, $message);
  41. try {
  42. parent::send($to, $message, $gateways);
  43. $this->setSmsCodeCache($to, $smsCode); //缓存至redis
  44. return response()->json(['message' => '发送成功']);
  45. } catch (NoGatewayAvailableException $exception) {
  46. Log::error(var_export($exception->getResults(), 1));
  47. return response()->json(['message' => '网关发送失败'], 400);
  48. } catch (\Throwable $throwable) {
  49. Log::error(var_export($throwable->getMessage(), 1));
  50. return response()->json(['message' => '短信发送失败'], 400);
  51. }
  52. }
  53. /**
  54. * 设置短信验证码缓存
  55. * @author hjc
  56. * @param $mobile
  57. * @param $smsCode
  58. */
  59. private function setSmsCodeCache($mobile, $smsCode)
  60. {
  61. $cacheKey = 'hash_sms_' . strtolower(config('name')) . '_' . $mobile;
  62. $cacheData = [
  63. 'mobile' => $mobile,
  64. 'code' => $smsCode,
  65. 'send_time' => time()
  66. ];
  67. Redis::hmset($cacheKey, $cacheData);
  68. Redis::expire($cacheKey, config('sms.redisExpire') ?? 600);
  69. }
  70. /**
  71. * 获得短信验证码
  72. * @author hjc
  73. * @param $mobile
  74. * @return mixed
  75. */
  76. public static function getSmsCodeCache($mobile)
  77. {
  78. $cacheKey = 'hash_sms_' . strtolower(config('name')) . '_' . $mobile;
  79. $cacheData = Redis::hgetall($cacheKey);
  80. return $cacheData;
  81. }
  82. /**
  83. * 随机生成的code码
  84. * @author hjc
  85. * @return string
  86. */
  87. private function createSmsCode()
  88. {
  89. $num = config('sms.codeSize') ?? 4; //生成的随机数个数
  90. $isIncludeStr = config('sms.codeIsIncludeStr') ?? false; //是否包含字符串
  91. $value = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
  92. if ($isIncludeStr) {
  93. $strVal = [
  94. 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  95. 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  96. 'o', 'p', 'q', 'r', 's', 't', 'u',
  97. 'v', 'w', 'x', 'y', 'z'
  98. ];
  99. $value = array_merge($value, $strVal);
  100. }
  101. shuffle($value); //排序打乱
  102. $key = array_rand($value, $num);
  103. $string = '';
  104. foreach ($key as $v) {
  105. $string .= $value[$v];
  106. }
  107. return $string;
  108. }
  109. }