SmsService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  13. use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
  14. use Darabonba\OpenApi\Models\Config;
  15. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  16. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  17. /**
  18. * 创信短信管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * Class SmsService
  22. * @package App\Services
  23. */
  24. class SmsService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 静态入口
  30. * @return SmsService|static|null
  31. */
  32. public static function make(){
  33. if(!self::$instance){
  34. self::$instance = new static();
  35. }
  36. return self::$instance;
  37. }
  38. /**
  39. * 发送短信验证码
  40. * @param $mobile
  41. * @param string $type
  42. * @return bool
  43. */
  44. public function send($mobile, $type='login')
  45. {
  46. $cacheKey = "caches:sms:cx_{$mobile}:{$type}";
  47. if(RedisService::get($cacheKey.'_lock')){
  48. $this->error = '1060';
  49. return false;
  50. }
  51. $config = ConfigService::make()->getConfigOptionByGroup(16);
  52. $apiUrl = isset($config['cx_api_url'])? trim($config['cx_api_url']) : '';
  53. $account = isset($config['cx_sms_account'])? trim($config['cx_sms_account']) : '';
  54. $password = isset($config['cx_sms_password'])? trim($config['cx_sms_password']) : '';
  55. $extno = isset($config['cx_sms_extno'])? trim($config['cx_sms_extno']) : '';
  56. $signName = isset($config['cx_sms_signname']) && $config['cx_sms_signname']? trim($config['cx_sms_signname']) : '星链社交';
  57. if(empty($apiUrl) || empty($account) || empty($password) || empty($extno)){
  58. $this->error = 1056;
  59. return false;
  60. }
  61. // 发送逻辑
  62. $code = rand(1000,9999);
  63. $content="【{$signName}】您的验证码是:{$code},该验证码5分钟内有效,请在登录页面中相应位置输入完成验证。请勿向他人泄漏该验证码。";//短信内容
  64. // $content="【{$signName}】您的验证码是:{$code},请在10分钟内使用,请不要将验证码泄漏个他人。";//短信内容
  65. $content1 = iconv('UTF-8','GBK', $content);
  66. $url="{$apiUrl}/sms?action=send&password={$password}&extno={$extno}&mobile={$mobile}&account={$account}&content=";
  67. try {
  68. $response = httpRequest($url.$content1,'','get','',5,[],'text');
  69. $responseObject = simplexml_load_string($response);
  70. $response = json_decode(json_encode((array)$responseObject),true);
  71. $returnStatus = isset($response['returnstatus'])? $response['returnstatus'] : '';
  72. if($returnStatus != 'Failed'){
  73. $this->error = 1063;
  74. RedisService::set($cacheKey,['code'=> $code,'mobile'=>$mobile,'config'=>$config,'code'=>$code,'result'=>$response,'date'=> date('Y-m-d H:i:s')], 600);
  75. return true;
  76. }else{
  77. $this->error = 1059;
  78. RedisService::set($cacheKey.'_fail', ['url'=>$url,'mobile'=> $mobile,'config'=>$config,'response'=>$response,'error'=>$this->error], 6 * 3600);
  79. return false;
  80. }
  81. } catch (TeaUnableRetryError $e){
  82. $date = date('Y-m-d H:i:s');
  83. logger()->error("【{$date} 创信SMS短信验证码】发送失败:".$e->getMessage());
  84. RedisService::set($cacheKey.'_error', ['mobile'=> $mobile,'config'=>$config,'error'=>$e->getMessage()], 6 * 3600);
  85. }
  86. $this->error = 1059;
  87. return false;
  88. }
  89. /**
  90. * 短信验证码验证
  91. * @param string $mobile 手机号
  92. * @param string $code 当前验证码
  93. * @param string $type 验证码场景类型,login-登录,reg-注册
  94. * @return bool
  95. */
  96. public function check($mobile, $code, $type='login')
  97. {
  98. if($code == '1100'){
  99. return true;
  100. }
  101. $cacheKey = "caches:sms:cx_{$mobile}:{$type}";
  102. $data = RedisService::get($cacheKey);
  103. $smsCode = isset($data['code'])? $data['code'] : '';
  104. if(empty($data) || empty($smsCode)){
  105. $this->error = '1061';
  106. return false;
  107. }
  108. if($smsCode != $code){
  109. $this->error = '1062';
  110. return false;
  111. }
  112. return true;
  113. }
  114. }