SmsService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ConfigModel;
  13. /**
  14. * 手机短信管理-服务类
  15. * @author
  16. * @since 2020/11/11
  17. * Class SmsService
  18. * @package App\Services
  19. */
  20. class SmsService extends BaseService
  21. {
  22. // 静态对象
  23. protected static $instance = null;
  24. /**
  25. * 初始化配置
  26. * @return bool
  27. */
  28. public function __construct()
  29. {
  30. $this->config = ConfigService::make()->getConfigByGroup(2);
  31. if (empty($this->config)) {
  32. return false;
  33. }
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 发送手机短信
  48. * @param $email
  49. * @param string $scene 场景:默认reg-注册
  50. * @return bool
  51. */
  52. public function sendCode($mobile, $scene = 'reg')
  53. {
  54. try {
  55. $cacheKey = "stores:codes:sms_{$scene}:" . substr($mobile, -3, 3) . '_' . md5($mobile);
  56. $apiUrl = isset($this->config['sms_api_url']['value']) ? $this->config['sms_api_url']['value'] : '';
  57. if (empty($mobile) || empty($apiUrl)) {
  58. return false;
  59. }
  60. if (RedisService::get($cacheKey . '_lock')) {
  61. $this->error = '1014';
  62. return false;
  63. }
  64. // 生成验证码
  65. $code = rand(100000, 999999);
  66. $template = isset($this->config['sms_template_' . $scene]['value']) && $this->config['sms_template_' . $scene]['value'] ? $this->config['sms_template_' . $scene]['value'] : '您的验证码是:{code},请在5分钟内使用!!!';
  67. $template = str_replace('{code}', $code, $template);
  68. $data = [
  69. 'userid' => isset($this->config['sms_id']['value']) ? $this->config['sms_id']['value'] : '',
  70. 'account' => isset($this->config['sms_account']['value']) ? $this->config['sms_account']['value'] : '',
  71. 'password' => isset($this->config['sms_password']['value']) ? $this->config['sms_password']['value'] : '',
  72. 'mobile' => $mobile,
  73. 'content' => $template,
  74. 'action' => 'send',
  75. 'sendTime' => '', // 发送时间:空立即发送
  76. ];
  77. RedisService::set($cacheKey . '_lock', ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], rand(10, 20));
  78. $result = curl_post($apiUrl, $data);
  79. $result = $result ? xmlToArray($result) : [];
  80. $status = isset($result['returnstatus']) ? $result['returnstatus'] : '';
  81. if ($status == 'Success') {
  82. RedisService::set($cacheKey, ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], 300);
  83. return true;
  84. }
  85. return false;
  86. } catch (\Exception $exception) {
  87. $this->error = $exception->getMessage();
  88. return false;
  89. }
  90. }
  91. /**
  92. * 验证手机短信
  93. * @param $mobile
  94. * @param $code
  95. * @param string $scene 场景:默认reg-注册
  96. * @return bool
  97. */
  98. public function check($mobile, $code, $scene = 'reg')
  99. {
  100. $cacheKey = "stores:codes:sms_{$scene}:". substr($mobile, -3, 3) . '_' . md5($mobile);
  101. $codeData = RedisService::get($cacheKey);
  102. $checkCode = isset($codeData['code']) ? $codeData['code'] : 0;
  103. $checkMobile = isset($codeData['mobile']) ? $codeData['mobile'] : '';
  104. if (empty($codeData) || empty($checkCode)) {
  105. $this->error = 2010;
  106. return false;
  107. }
  108. if ($checkMobile != $mobile || $checkCode != $code) {
  109. $this->error = 2006;
  110. return false;
  111. }
  112. return true;
  113. }
  114. }