SmsServer.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\SmsException;
  4. use Illuminate\Support\Facades\Redis;
  5. use PHPUnit\Util\Exception;
  6. use App\Models\SmsRecord;
  7. use Illuminate\Support\Str;
  8. use Overtrue\EasySms\EasySms;
  9. //use Overtrue\EasySms\Exceptions\Exception;
  10. use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
  11. class SmsServer
  12. {
  13. const EVENT_LOGIN = 100;
  14. const DEFAULT_SMS_CODE_LENGTH = 4;
  15. const DEFAULT_SMS_CODE_EXP_TIME = 5*60;
  16. const EVENTS_TO_TEMPLATE = [
  17. '63' => [ //菲律宾
  18. 'login' => 'SMS_234414831',
  19. 'forget' => 'SMS_234414831',
  20. ],
  21. '86' => [ //中国
  22. //'login' => 'SMS_137515003',
  23. //'forget' => 'SMS_137515003',
  24. 'login' => 'SMS_227262168',
  25. 'forget' => 'SMS_227262168',
  26. ],
  27. ];
  28. const EVENT_ITEM_FUNCTION = [
  29. ];
  30. /**
  31. * 获取easySms配置
  32. * @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed
  33. */
  34. static public function getEasySmsConfig()
  35. {
  36. return config('easysms');
  37. }
  38. /**
  39. * 获取 easySms 实例
  40. * @return EasySms
  41. */
  42. static public function easySms()
  43. {
  44. return new EasySms(self::getEasySmsConfig());
  45. }
  46. static public function send(int $mobile, string $event, int $prefix)
  47. {
  48. try {
  49. $templateId = self::getTemplateIdByEvent($prefix, $event);
  50. $smsCode = self::getSmsCode();
  51. //注意! 实际发送的电话号码,要加 国际号码前缀
  52. $result = self::easySms()->send($prefix.$mobile, [
  53. 'template' => $templateId,
  54. 'content' => 'null',
  55. 'data' => [
  56. 'code' => $smsCode,
  57. 'expTime' => 5,
  58. ]
  59. ]);
  60. //缓存验证码
  61. self::cacheSmsCodeByVerifyKey($mobile, $smsCode);
  62. //存入发送记录
  63. self::storeSmsRecord($prefix, $mobile, $event, $smsCode, '', $result);
  64. return [
  65. 'smsCode' => $smsCode
  66. ];
  67. } catch (SmsException $e) {
  68. return ['error' => $e->getMessage()];
  69. } catch (NoGatewayAvailableException $e) {
  70. return ['error' => trans('api.SMS_SENDING_FAILED')];
  71. } catch (Exception $e) {
  72. return ['error' => trans('api.SMS_SENDING_FAILED')];
  73. }
  74. }
  75. /**
  76. * 生成随机字符串用户校验验证码
  77. * @param int $length
  78. * @return string
  79. */
  80. static public function getVerifyKey(int $length = 10)
  81. {
  82. return Str::random($length);
  83. }
  84. /**
  85. * 生成短信验证码
  86. * @param int $length
  87. * @return int
  88. */
  89. static public function getSmsCode($length = self::DEFAULT_SMS_CODE_LENGTH)
  90. {
  91. return rand(1000, 9999);
  92. }
  93. /**
  94. * @param string $mobile
  95. * @param string $smsCode
  96. * @return bool
  97. */
  98. static public function checkSmsCodeByVerifyKey(string $mobile, string $smsCode)
  99. {
  100. $CacheSmsCode = Redis::get('smsCode:verifyCode' . $mobile);
  101. if (empty($CacheSmsCode)) {
  102. throw new Exception('验证码过期或已失效');
  103. }
  104. if ($smsCode != $CacheSmsCode) {
  105. throw new Exception('验证码错误');
  106. }
  107. Redis::del('smsCode:verifyCode' . $mobile);
  108. return true;
  109. }
  110. /**
  111. * 缓存短信验证码
  112. * @param int $mobile
  113. * @param string $smsCode
  114. * @param int $expTime
  115. * @return mixed
  116. */
  117. static public function cacheSmsCodeByVerifyKey(int $mobile, string $smsCode, int $expTime = self::DEFAULT_SMS_CODE_EXP_TIME)
  118. {
  119. return Redis::setex('smsCode:verifyCode' . $mobile, $expTime, $smsCode);
  120. }
  121. /**
  122. * 根据事件获取 短信模板ID
  123. * @param string $event
  124. * @return string
  125. * @throws SmsException
  126. */
  127. static public function getTemplateIdByEvent($prefix, string $event)
  128. {
  129. //self::verifySmsEvent($event);
  130. return self::EVENTS_TO_TEMPLATE[$prefix][$event];
  131. }
  132. /**
  133. * 验证发送事件
  134. * @param string $event
  135. * @return bool
  136. * @throws SmsException
  137. */
  138. static public function verifySmsEvent(string $event)
  139. {
  140. if (!array_key_exists($event, self::EVENTS_TO_TEMPLATE)) {
  141. throw new Exception(trans('api.WRONG_EVENT_TYPE'));
  142. }
  143. return true;
  144. }
  145. /**
  146. * 将发送记录存到数据库以便调试
  147. * @param string $prefix
  148. * @param int $mobile
  149. * @param string $event
  150. * @param string $smsCode
  151. * @param $sendResult
  152. * @return mixed
  153. */
  154. static public function storeSmsRecord(string $prefix, int $mobile, string $event, string $smsCode, $sendResult)
  155. {
  156. return SmsRecord::create([
  157. 'prefix' => $prefix,
  158. 'mobile' => $mobile,
  159. 'event' => $event,
  160. 'sms_code' => $smsCode,
  161. 'verify_key' => '',
  162. 'sms_result' => $sendResult
  163. ]);
  164. }
  165. }