|
|
@@ -0,0 +1,82 @@
|
|
|
+<?php
|
|
|
+declare(strict_types=1);
|
|
|
+namespace SixShop\TencentCloudSms;
|
|
|
+
|
|
|
+
|
|
|
+use TencentCloud\Common\Credential;
|
|
|
+use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
|
+use TencentCloud\Common\Profile\ClientProfile;
|
|
|
+use TencentCloud\Common\Profile\HttpProfile;
|
|
|
+use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
|
|
|
+use TencentCloud\Sms\V20190711\SmsClient as TencentCloudSmsClient;
|
|
|
+
|
|
|
+class SmsClient
|
|
|
+{
|
|
|
+ private array $templateMap = [];
|
|
|
+
|
|
|
+ private TencentCloudSmsClient $client;
|
|
|
+
|
|
|
+ private array $errorMesageMap = [
|
|
|
+ 'FailedOperation.ContainSensitiveWord' => '包含敏感词',
|
|
|
+ 'FailedOperation.InsufficientBalanceInSmsPackage' => '套餐包余量不足',
|
|
|
+ 'FailedOperation.PhoneNumberInBlacklist' => '号码在黑名单中',
|
|
|
+ 'FailedOperation.SignatureIncorrectOrUnapproved' => '签名未审核通过',
|
|
|
+ 'FailedOperation.TemplateIncorrectOrUnapproved' => '模板未审核通过',
|
|
|
+ 'FailedOperation.TemplateUnapprovedOrNotExist' => '模板未审核通过或不存在',
|
|
|
+ 'InvalidParameterValue.ContentLengthLimit' => '内容长度超出限制',
|
|
|
+ 'InvalidParameterValue.IncorrectPhoneNumber' => '号码格式错误',
|
|
|
+ ];
|
|
|
+ public function __construct(private Config $config)
|
|
|
+ {
|
|
|
+ $this->templateMap = array_to_map($this->config->tencent_sms_templates, 'template_id', 'template_content');
|
|
|
+ $cred = new Credential($this->config->secret_id, $this->config->secret_key);
|
|
|
+ $httpProfile = new HttpProfile();
|
|
|
+ $httpProfile->setEndpoint('sms.tencentcloudapi.com');
|
|
|
+ $clientProfile = new ClientProfile();
|
|
|
+ $clientProfile->setHttpProfile($httpProfile);
|
|
|
+ $this->client = new TencentCloudSmsClient($cred, 'ap-guangzhou', $clientProfile);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送短信
|
|
|
+ *
|
|
|
+ * @param array|string $phoneNumberSet 手机号码列表
|
|
|
+ * @param string $templateId 模板ID
|
|
|
+ * @param array|string $templateParamSet 模板参数
|
|
|
+ */
|
|
|
+ public function sendSms(array|string $phoneNumberSet, string $templateId, array|string $templateParamSet): array
|
|
|
+ {
|
|
|
+ $phoneNumberSet = (array)$phoneNumberSet;
|
|
|
+ $templateParamSet = (array)$templateParamSet;
|
|
|
+ if (!isset($this->templateMap[$templateId])) {
|
|
|
+ throw new \InvalidArgumentException("template_id {$templateId} not found");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ $resp = $this->client->SendSms($this->getSendSmsRequest($phoneNumberSet, $templateId, $templateParamSet));
|
|
|
+ } catch (TencentCloudSDKException $e) {
|
|
|
+ throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
|
|
|
+ }
|
|
|
+ $resp = json_decode($resp->toJsonString(), true);
|
|
|
+ $this->checkError($resp);
|
|
|
+ return $resp;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getSendSmsRequest(array $phoneNumberSet, string $templateId, array $templateParamSet): SendSmsRequest
|
|
|
+ {
|
|
|
+ $req = new SendSmsRequest();
|
|
|
+ $req->setPhoneNumberSet($phoneNumberSet);
|
|
|
+ $req->setSmsSdkAppid($this->config->app_id);
|
|
|
+ $req->setTemplateID($templateId);
|
|
|
+ $req->setSign($this->config->sign_name);
|
|
|
+ $req->setTemplateParamSet($templateParamSet);
|
|
|
+ return $req;
|
|
|
+ }
|
|
|
+ private function checkError(array $resp): void
|
|
|
+ {
|
|
|
+ foreach ($resp['SendStatusSet'] as $statusItem) {
|
|
|
+ if ($statusItem['Code'] !== 'Ok') {
|
|
|
+ throw new \RuntimeException($this->errorMesageMap[$statusItem['Code']] ?? $statusItem['Message']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|