ZhoushanSms.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Aix\Sms;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Models\SmsTemplate;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Support\Facades\Cache;
  7. class ZhoushanSms implements Smser
  8. {
  9. /**
  10. * @var Client
  11. */
  12. private $httpClient;
  13. private $app_key;
  14. private $app_secret;
  15. private $sign_key;
  16. /**
  17. * SmsService constructor.
  18. * @param $app_key
  19. * @param $app_secret
  20. * @param $sign_key
  21. */
  22. public function __construct($app_key, $app_secret, $sign_key)
  23. {
  24. $this->app_key = $app_key;
  25. $this->app_secret = $app_secret;
  26. $this->sign_key = $sign_key;
  27. $this->httpClient=new Client([
  28. 'http_errors' => false
  29. ]);
  30. }
  31. /**
  32. * 发送基本短信
  33. * @param string $mobile
  34. * @param SmsTemplate $template
  35. * @param array $params
  36. * @return bool
  37. */
  38. public function sendSms(string $mobile, SmsTemplate $template, array $params = []): bool
  39. {
  40. $time=(int)(microtime(true)*1000);
  41. $id=$time;
  42. $appKey=$this->app_key;
  43. $phoneNums=$mobile;
  44. $content="【舟山市就业局】".$template->value;
  45. $flag=1;
  46. $wsdlUrl="http://115.236.191.137:8080/APL-SMSService/SMSService?wsdl";
  47. $post_data = "<SOAP-ENV:Envelope xmlns:ns0='http://ws.sms.zjapl.com' xmlns:ns1='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>"
  48. . "<SOAP-ENV:Header/>"
  49. . "<ns1:Body>"
  50. . "<ns0:sendSms >"
  51. . "<id>" . $id . "</id>"
  52. . "<appKey>" . $appKey . "</appKey>"
  53. . "<phoneNums>" . $phoneNums . "</phoneNums>"
  54. . "<content>" . $content . "</content>"
  55. . "<time>" . $time . "</time>"
  56. . "<flag>" . $flag . "</flag>"
  57. . "</ns0:sendSms>"
  58. . "</ns1:Body>"
  59. . "</SOAP-ENV:Envelope>";
  60. $response=$this->httpClient->post($wsdlUrl, ['body'=>$post_data]);
  61. return true;
  62. }
  63. /**
  64. * 发送确认短信,需要缓存验证码code
  65. * @param string $mobile
  66. * @param SmsTemplate $template
  67. * @return bool
  68. */
  69. public function sendAuthSms(string $mobile, SmsTemplate $template): bool
  70. {
  71. $params['code']=rand(100000, 999999);
  72. $template->value=render_template($template->value, $params);
  73. $this->sendSms($mobile, $template, $params);
  74. Cache::put($template->alias.'_'.$mobile, $params['code'], 15);
  75. return true;
  76. }
  77. /**
  78. * 批量发送短信
  79. * @param array $mobiles
  80. * @param SmsTemplate $template
  81. * @param array $params
  82. * @return bool
  83. */
  84. public function sendBatchSms(array $mobiles, SmsTemplate $template, array $params = []): bool
  85. {
  86. //
  87. }
  88. }