SmsService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. $this->error = '1013';
  59. return false;
  60. }
  61. if (RedisService::get($cacheKey . '_lock')) {
  62. $this->error = '1014';
  63. return false;
  64. }
  65. // 生成验证码
  66. $code = rand(100000, 999999);
  67. $template = isset($this->config['sms_template_' . $scene]['value']) && $this->config['sms_template_' . $scene]['value'] ? $this->config['sms_template_' . $scene]['value'] : '【鱼豆小帮手】您的验证码是:{code}';
  68. $template = str_replace('{code}', $code, $template);
  69. $data = [
  70. 'userid' => isset($this->config['sms_id']['value']) ? $this->config['sms_id']['value'] : '',
  71. 'account' => isset($this->config['sms_account']['value']) ? $this->config['sms_account']['value'] : '',
  72. 'password' => isset($this->config['sms_password']['value']) ? $this->config['sms_password']['value'] : '',
  73. 'mobile' => $mobile,
  74. 'content' => $template,
  75. 'action' => 'send',
  76. 'sendTime' => '', // 发送时间:空立即发送
  77. ];
  78. RedisService::set($cacheKey . '_temp', ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], 600);
  79. RedisService::set($cacheKey . '_lock', ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], rand(10, 20));
  80. $result = curl_post($apiUrl, $data);
  81. $result = $result ? xmlToArray($result) : [];
  82. $status = isset($result['returnstatus']) ? $result['returnstatus'] : '';
  83. if ($status === 'Success') {
  84. RedisService::set($cacheKey, ['mobile' => $mobile, 'code' => $code, 'date' => date('Y-m-d H:i:s')], 300);
  85. return true;
  86. }
  87. $this->error = '1011';
  88. return false;
  89. } catch (\Exception $exception) {
  90. $this->error = $exception->getMessage();
  91. return false;
  92. }
  93. }
  94. /**
  95. * 验证手机短信
  96. * @param $mobile
  97. * @param $code
  98. * @param string $scene 场景:默认reg-注册
  99. * @return bool
  100. */
  101. public function check($mobile, $code, $scene = 'reg')
  102. {
  103. $cacheKey = "stores:codes:sms_{$scene}:". substr($mobile, -3, 3) . '_' . md5($mobile);
  104. $codeData = RedisService::get($cacheKey);
  105. $checkCode = isset($codeData['code']) ? $codeData['code'] : 0;
  106. $checkMobile = isset($codeData['mobile']) ? $codeData['mobile'] : '';
  107. if (empty($codeData) || empty($checkCode)) {
  108. $this->error = 2026;
  109. return false;
  110. }
  111. if ($checkMobile != $mobile || $checkCode != $code) {
  112. $this->error = 2006;
  113. return false;
  114. }
  115. return true;
  116. }
  117. }