Aliyun.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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 crmeb\basic\BaseSms;
  14. use \Exception;
  15. use AlibabaCloud\Tea\Exception\TeaError;
  16. use Darabonba\OpenApi\Models\Config as AliConfig;
  17. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  18. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  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. /**
  28. * 短信签名
  29. * @var string
  30. */
  31. protected $sign = '';
  32. /**
  33. * 模板id
  34. * @var array
  35. */
  36. protected $templateIds = [];
  37. /**
  38. * @param array $config
  39. * @return mixed|void
  40. */
  41. protected function initialize(array $config = [])
  42. {
  43. parent::initialize($config);
  44. $this->templateIds = Config::get($this->configFile . '.stores.' . $this->name . '.template_id', []);
  45. $this->sign = Config::get($this->configFile . '.stores.' . $this->name . '.sign_name', '');
  46. }
  47. public function createClient()
  48. {
  49. $config = new AliConfig([
  50. // 您的 AccessKey ID
  51. "accessKeyId" => Config::get($this->configFile . '.stores.' . $this->name . '.access_key_id'),
  52. // 您的 AccessKey Secret
  53. "accessKeySecret" => Config::get($this->configFile . '.stores.' . $this->name . '.access_key_secret')
  54. ]);
  55. // 访问的域名
  56. $config->endpoint = "dysmsapi.aliyuncs.com";
  57. return new Dysmsapi($config);
  58. }
  59. protected function getTemplateCode(string $templateId)
  60. {
  61. return $this->templateIds[$templateId] ?? null;
  62. }
  63. /**
  64. * @param string $phone
  65. * @param string $templateId
  66. * @param array $data
  67. * @return array[]|bool|mixed
  68. */
  69. public function send(string $phone, string $templateId, array $data = [])
  70. {
  71. if (empty($phone)) {
  72. return $this->setError('电话号码不能为空');
  73. }
  74. //验证码只支持一个参数
  75. if ($templateId == 'VERIFICATION_CODE') {
  76. unset($data['time']);
  77. }
  78. $temp = $this->getTemplateCode($templateId);
  79. if (is_null($temp)) {
  80. throw new ValidateException('模版ID不存在');
  81. }
  82. $client = $this->createClient();
  83. $sendSmsRequest = new SendSmsRequest([
  84. "phoneNumbers" => $phone,
  85. "signName" => $this->sign,
  86. "templateCode" => $temp,
  87. "templateParam" => json_encode($data),
  88. ]);
  89. $runtime = new RuntimeOptions([]);
  90. try {
  91. $resp = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
  92. } catch (Exception $error) {
  93. if (!($error instanceof TeaError)) {
  94. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  95. }
  96. throw new ValidateException('【阿里云平台错误提示】:' . $error->message);
  97. }
  98. if (isset($resp) && $resp->body->code !== 'OK') {
  99. throw new ValidateException('【阿里云平台错误提示】:' . $resp->body->message);
  100. }
  101. return 'ok';
  102. }
  103. public function open()
  104. {
  105. }
  106. public function modify(string $sign = null, string $phone, string $code)
  107. {
  108. }
  109. public function info()
  110. {
  111. }
  112. public function temps(int $page = 0, int $limit = 10, int $type = 1)
  113. {
  114. }
  115. public function apply(string $title, string $content, int $type)
  116. {
  117. }
  118. public function applys(int $tempType, int $page, int $limit)
  119. {
  120. }
  121. public function record($record_id)
  122. {
  123. }
  124. }