Aliyun.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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 app\common\repositories\system\notice\SystemNoticeConfigRepository;
  14. use crmeb\services\BaseSmss;
  15. use \Exception;
  16. use AlibabaCloud\Tea\Exception\TeaError;
  17. use AlibabaCloud\Tea\Utils\Utils;
  18. use Darabonba\OpenApi\Models\Config as AliConfig;
  19. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  20. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  21. use think\exception\ValidateException;
  22. use think\facade\Config;
  23. /**
  24. * Class Aliyun
  25. * @package crmeb\services\sms\storage
  26. */
  27. class Aliyun extends BaseSmss
  28. {
  29. protected $AccessKeySecret = '';
  30. /**
  31. * @param array $config
  32. * @return mixed|void
  33. */
  34. protected function initialize(array $config = [])
  35. {
  36. parent::initialize($config);
  37. }
  38. public static function createClient($accessKeyId, $accessKeySecret){
  39. $config = new AliConfig([
  40. // 您的 AccessKey ID
  41. "accessKeyId" => systemConfig('aliyun_AccessKeyId'),
  42. // 您的 AccessKey Secret
  43. "accessKeySecret" => systemConfig('aliyun_AccessKeySecret')
  44. ]);
  45. // 访问的域名
  46. $config->endpoint = "dysmsapi.aliyuncs.com";
  47. return new Dysmsapi($config);
  48. }
  49. /**
  50. * @param string $phone
  51. * @param string $templateId
  52. * @param array $data
  53. * @return array[]|bool|mixed
  54. */
  55. public function send(string $phone, string $templateId, array $data = [])
  56. {
  57. if (empty($phone)) {
  58. return $this->setError('电话号码不能为空');
  59. }
  60. //验证码只支持一个参数
  61. if ($templateId == 'VERIFICATION_CODE') {
  62. unset($data['time']);
  63. }
  64. if (isset($data['site'])) {
  65. unset($data['site']);
  66. }
  67. $client = self::createClient("accessKeyId", "accessKeySecret");
  68. $temp = app()->make(SystemNoticeConfigRepository::class)->getSmsTemplate($templateId);
  69. if (!$temp) throw new ValidateException('模板不存在或未开启通知:'. $templateId);
  70. $sendSmsRequest = new SendSmsRequest([
  71. "phoneNumbers" => $phone,
  72. "signName" => systemConfig('aliyun_SignName'),
  73. "templateCode" => $temp,
  74. "templateParam" => json_encode($data),
  75. ]);
  76. $runtime = new RuntimeOptions([]);
  77. try {
  78. // 复制代码运行请自行打印 API 的返回值
  79. $resp = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
  80. }
  81. catch (Exception $error) {
  82. if (!($error instanceof TeaError)) {
  83. $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
  84. }
  85. // 如有需要,请打印 error
  86. throw new ValidateException('【阿里云平台错误提示】:'.$error->message);
  87. //$resp = Utils::assertAsString($error->message);
  88. }
  89. if (isset($resp) && $resp->body->code !== 'OK') {
  90. throw new ValidateException('【阿里云平台错误提示】:'.$resp->body->message);
  91. }
  92. return 'ok';
  93. }
  94. public function open(){}
  95. public function modify(string $sign = null , string $phone, string $code){}
  96. public function info(){}
  97. public function temps(int $page = 0, int $limit = 10, int $type = 1){}
  98. public function apply(string $title, string $content, int $type){}
  99. public function applys(int $tempType, int $page, int $limit){}
  100. public function record($record_id){}
  101. }