Aliyun.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\sms\storage;
  12. use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
  13. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  14. use AlibabaCloud\Tea\Exception\TeaError;
  15. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  16. use crmeb\exceptions\ApiException;
  17. use crmeb\services\sms\BaseSms;
  18. use Darabonba\OpenApi\Models\Config as AliConfig;
  19. use think\exception\ValidateException;
  20. use think\facade\Config;
  21. /**
  22. * Class Aliyun
  23. * @package crmeb\services\sms\storage
  24. */
  25. class Aliyun extends BaseSms
  26. {
  27. protected $AccessKeyId = '';
  28. protected $AccessKeySecret = '';
  29. protected $SignName = '';
  30. /**
  31. * @param array $config
  32. * @return mixed|void
  33. */
  34. protected function initialize(array $config = [])
  35. {
  36. parent::initialize($config);
  37. $this->SignName = $config['aliyun_SignName'] ?? '';
  38. $this->AccessKeyId = $config['aliyun_AccessKeyId'] ?? '';
  39. $this->AccessKeySecret = $config['aliyun_AccessKeySecret'] ?? '';
  40. }
  41. /**
  42. * 发送短信
  43. * @param string $phone
  44. * @param string $templateId
  45. * @param array $data
  46. * @return array|bool|mixed
  47. */
  48. public function send(string $phone, string $templateId, array $data = [])
  49. {
  50. if (empty($phone)) {
  51. return $this->setError('电话号码不能为空');
  52. }
  53. $config = new AliConfig([
  54. "accessKeyId" => $this->AccessKeyId,
  55. "accessKeySecret" => $this->AccessKeySecret
  56. ]);
  57. // 访问的域名
  58. $config->endpoint = "dysmsapi.aliyuncs.com";
  59. $client = new Dysmsapi($config);
  60. if (!$templateId) throw new ApiException('模板不存在:' . $templateId);
  61. $sendSmsRequest = new SendSmsRequest([
  62. "phoneNumbers" => $phone,
  63. "signName" => $this->SignName,
  64. "templateCode" => $templateId,
  65. "templateParam" => json_encode($data),
  66. ]);
  67. $runtime = new RuntimeOptions([]);
  68. try {
  69. // 复制代码运行请自行打印 API 的返回值
  70. $resp = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
  71. if (isset($resp) && $resp->body->code !== 'OK') {
  72. throw new ApiException('【阿里云平台错误提示】:' . $resp->body->message);
  73. }
  74. return [
  75. 'id' => $resp->body->requestId,
  76. 'content' => json_encode($data),
  77. 'template' => $templateId,
  78. ];
  79. } catch (\Exception $e) {
  80. throw new ApiException('【阿里云平台错误提示】:' . $e->getMessage());
  81. }
  82. }
  83. public function open()
  84. {
  85. }
  86. public function modify(string $sign = null, string $phone, string $code)
  87. {
  88. }
  89. public function info()
  90. {
  91. }
  92. public function temps(int $page = 0, int $limit = 10, int $type = 1)
  93. {
  94. }
  95. public function apply(string $title, string $content, int $type)
  96. {
  97. }
  98. public function applys(int $tempType, int $page, int $limit)
  99. {
  100. }
  101. public function record($record_id)
  102. {
  103. }
  104. }