123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace crmeb\services\sms\storage;
- use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
- use crmeb\basic\BaseSms;
- use \Exception;
- use AlibabaCloud\Tea\Exception\TeaError;
- use Darabonba\OpenApi\Models\Config as AliConfig;
- use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
- use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
- use think\exception\ValidateException;
- use think\facade\Config;
- /**
- * Class Aliyun
- * @package crmeb\services\sms\storage
- */
- class Aliyun extends BaseSms
- {
- /**
- * 短信签名
- * @var string
- */
- protected $sign = '';
- /**
- * 模板id
- * @var array
- */
- protected $templateIds = [];
- /**
- * @param array $config
- * @return mixed|void
- */
- protected function initialize(array $config = [])
- {
- parent::initialize($config);
- $this->templateIds = Config::get($this->configFile . '.stores.' . $this->name . '.template_id', []);
- $this->sign = Config::get($this->configFile . '.stores.' . $this->name . '.sign_name', '');
- }
- public function createClient()
- {
- $config = new AliConfig([
- // 您的 AccessKey ID
- "accessKeyId" => Config::get($this->configFile . '.stores.' . $this->name . '.access_key_id'),
- // 您的 AccessKey Secret
- "accessKeySecret" => Config::get($this->configFile . '.stores.' . $this->name . '.access_key_secret')
- ]);
- // 访问的域名
- $config->endpoint = "dysmsapi.aliyuncs.com";
- return new Dysmsapi($config);
- }
- protected function getTemplateCode(string $templateId)
- {
- return $this->templateIds[$templateId] ?? null;
- }
- /**
- * @param string $phone
- * @param string $templateId
- * @param array $data
- * @return array[]|bool|mixed
- */
- public function send(string $phone, string $templateId, array $data = [])
- {
- if (empty($phone)) {
- return $this->setError('电话号码不能为空');
- }
- //验证码只支持一个参数
- if ($templateId == 'VERIFICATION_CODE') {
- unset($data['time']);
- }
- $temp = $this->getTemplateCode($templateId);
- if (is_null($temp)) {
- throw new ValidateException('模版ID不存在');
- }
- $client = $this->createClient();
- $sendSmsRequest = new SendSmsRequest([
- "phoneNumbers" => $phone,
- "signName" => $this->sign,
- "templateCode" => $temp,
- "templateParam" => json_encode($data),
- ]);
- $runtime = new RuntimeOptions([]);
- try {
- $resp = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
- } catch (Exception $error) {
- if (!($error instanceof TeaError)) {
- $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
- }
- throw new ValidateException('【阿里云平台错误提示】:' . $error->message);
- }
- if (isset($resp) && $resp->body->code !== 'OK') {
- throw new ValidateException('【阿里云平台错误提示】:' . $resp->body->message);
- }
- return 'ok';
- }
- public function open()
- {
- }
- public function modify(string $sign = null, string $phone, string $code)
- {
- }
- public function info()
- {
- }
- public function temps(int $page = 0, int $limit = 10, int $type = 1)
- {
- }
- public function apply(string $title, string $content, int $type)
- {
- }
- public function applys(int $tempType, int $page, int $limit)
- {
- }
- public function record($record_id)
- {
- }
- }
|