You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use App\Constants\v3\ErrorCode;
  7. use App\Exception\ErrorCodeException;
  8. use Exception;
  9. use App\Commons\Log;
  10. use App\Constants\v3\SmsTemplateCode;
  11. use Hyperf\Di\Annotation\Inject;
  12. use App\Service\v3\Interfaces\SmsSendServiceInterface;
  13. class SmsAliSendService implements SmsSendServiceInterface
  14. {
  15. /**
  16. * @Inject
  17. * @var Log
  18. */
  19. protected $log;
  20. public function do($tel, $template, $templateParams)
  21. {
  22. try {
  23. $aliSms = config('alisms');
  24. AlibabaCloud::accessKeyClient($aliSms['app_key'], $aliSms['app_secret'])
  25. ->regionId($aliSms['regionid'])
  26. ->asDefaultClient();
  27. $result = AlibabaCloud::rpc()
  28. ->product($aliSms['product'])
  29. ->version('2017-05-25')
  30. ->action('SendSms')
  31. ->method('POST')
  32. ->host($aliSms['host'])
  33. ->options([
  34. 'query' => [
  35. 'RegionId' => $aliSms['regionid'],
  36. 'PhoneNumbers' => $tel,
  37. 'SignName' => $aliSms['sign_name'],
  38. 'TemplateCode' => $template,
  39. 'TemplateParam' => $templateParams,
  40. ],
  41. ])
  42. ->request();
  43. return $result->toArray();
  44. } catch (ClientException $e) {
  45. $this->log->event('alisms', ['alisms_error_ClientException' => $e->getErrorMessage()]);
  46. throw new ErrorCodeException(ErrorCode::SMS_SEND_FAILURE);
  47. } catch (ServerException $e) {
  48. $this->log->event('alisms', ['alisms_error_ServerException' => $e->getErrorMessage()]);
  49. throw new ErrorCodeException(ErrorCode::SMS_SEND_FAILURE);
  50. } catch (Exception $e) {
  51. $this->log->event('alisms', ['alisms_error_Exception' => $e->getErrorMessage()]);
  52. throw new ErrorCodeException(ErrorCode::SMS_SEND_FAILURE);
  53. }
  54. }
  55. public function check()
  56. {
  57. // TODO: Implement check() method.
  58. }
  59. public function undo()
  60. {
  61. // TODO: Implement undo() method.
  62. }
  63. public function doVerifyCode($tel, $code)
  64. {
  65. $params = ['user_name' => '疯狂的水叔叔', 'market_name' => '验证码', 'money' => $code];
  66. return $this->do($tel, SmsTemplateCode::ALI_VERIFY_CODE, json_encode($params));
  67. }
  68. public function checkVerifyCode($tel, $code)
  69. {
  70. // TODO: Implement checkVerifyCode() method.
  71. }
  72. public function undoVerifyCode($tel, $code)
  73. {
  74. // TODO: Implement removeVerifyCode() method.
  75. }
  76. }