Aliyun.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace core\sms;
  12. use AlibabaCloud\Client\AlibabaCloud;
  13. use core\exception\NoticeException;
  14. use Exception;
  15. class Aliyun extends BaseSms
  16. {
  17. protected $app_key = '';
  18. protected $secret_key = '';
  19. protected $sign = '';
  20. /**
  21. * @param array $config
  22. * @return void
  23. */
  24. protected function initialize(array $config = [])
  25. {
  26. parent::initialize($config);
  27. $this->app_key = $config['app_key'] ?? '';
  28. $this->secret_key = $config['secret_key'] ?? '';
  29. $this->sign = $config['sign'] ?? '';
  30. }
  31. /**
  32. * 发送短信
  33. * @param string $mobile
  34. * @param string $template_id
  35. * @param array $data
  36. * @return array
  37. */
  38. public function send(string $mobile, string $template_id, array $data = [])
  39. {
  40. try {
  41. AlibabaCloud::accessKeyClient($this->app_key, $this->secret_key)
  42. ->regionId('cn-hangzhou')
  43. ->asDefaultClient();
  44. $result = AlibabaCloud::rpcRequest()
  45. ->product('Dysmsapi')
  46. ->host('dysmsapi.aliyuncs.com')
  47. ->version('2017-05-25')
  48. ->action('SendSms')
  49. ->method('POST')
  50. ->debug(false)
  51. ->options([
  52. 'query' => [
  53. 'PhoneNumbers' => $mobile,
  54. 'SignName' => $this->sign,
  55. 'TemplateCode' => $template_id,
  56. 'TemplateParam' => json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
  57. ],
  58. ])
  59. ->request();
  60. $res = $result->toArray();
  61. if (isset($res['Code']) && $res['Code'] == 'OK') {
  62. return $res;
  63. }
  64. $message = $res['Message'] ?? $res;
  65. throw new NoticeException($message);
  66. } catch ( Exception $e ) {
  67. throw new NoticeException($e->getMessage());
  68. }
  69. }
  70. public function modify(string $sign = null, string $mobile, string $code)
  71. {
  72. }
  73. public function template(int $page = 0, int $limit = 10, int $type = 1)
  74. {
  75. }
  76. public function apply(string $title, string $content, int $type)
  77. {
  78. }
  79. public function localTemplate(int $type, int $page, int $limit)
  80. {
  81. }
  82. public function record($id)
  83. {
  84. }
  85. }