From 2a681438fe5f0fc2055dbcdd6ae6b71678dcad81 Mon Sep 17 00:00:00 2001 From: toplan Date: Sat, 24 Jun 2017 00:59:54 +0800 Subject: [PATCH 1/6] fix issue #120 --- README.md | 3 +++ src/phpsms/Sms.php | 7 +++++-- src/phpsms/Util.php | 31 +++++++++++++++++++++++++++++++ src/phpsms/agents/Agent.php | 18 ++++++++++++++++++ src/phpsms/agents/QcloudAgent.php | 24 +++++++++++++++++++++++- 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 875555e..cedd3bc 100644 --- a/README.md +++ b/README.md @@ -380,6 +380,9 @@ $sms = Sms::voice($code); 设置发送给谁,并返回实例。 ```php $sms->to('1828*******'); + +//兼容腾讯云 +$sms->to([86, '1828*******']) ``` ### template($templates) diff --git a/src/phpsms/Sms.php b/src/phpsms/Sms.php index d1c1676..f416120 100644 --- a/src/phpsms/Sms.php +++ b/src/phpsms/Sms.php @@ -538,13 +538,16 @@ public function type($type) /** * Set the recipient`s mobile number. * - * @param string $mobile + * @param string|array $mobile * * @return $this */ public function to($mobile) { - $this->smsData['to'] = trim((string) $mobile); + if (is_string($mobile)) { + $mobile = trim($mobile); + } + $this->smsData['to'] = $mobile; return $this; } diff --git a/src/phpsms/Util.php b/src/phpsms/Util.php index 987b639..c20aae2 100644 --- a/src/phpsms/Util.php +++ b/src/phpsms/Util.php @@ -53,4 +53,35 @@ public static function getClosureSerializer() return self::$closureSerializer; } + + public static function formatMobiles($target) + { + if (!is_array($target)) { + return [$target]; + } + $list = []; + $nation = $number = null; + $count = count($target); + if ($count === 2) { + $firstItem = $target[0]; + if (is_int($firstItem) && $firstItem > 0 && $firstItem <= 9999) { + $nation = $firstItem; + $number = $target[1]; + } + if (is_string($firstItem) && strlen($firstItem = trim($firstItem)) <= 4) { + $nation = $firstItem; + $number = $target[1]; + } + } + if (!is_null($nation)) { + return [compact('nation', 'number')]; + } + foreach ($target as $childTarget) { + $childList = self::formatMobiles($childTarget); + foreach ($childList as $childListItem) { + array_push($list, $childListItem); + } + } + return $list; + } } diff --git a/src/phpsms/agents/Agent.php b/src/phpsms/agents/Agent.php index d0a782a..6f7b10c 100644 --- a/src/phpsms/agents/Agent.php +++ b/src/phpsms/agents/Agent.php @@ -101,6 +101,8 @@ public function sendSms($to, $content = null, $tempId = null, array $data = [], { $this->reset(); $this->params($params, true); + $to = $this->formatMobile(Util::formatMobiles($to)); + if ($tempId && $this instanceof TemplateSms) { $this->sendTemplateSms($to, $tempId, $data); } elseif ($content && $this instanceof ContentSms) { @@ -123,6 +125,8 @@ public function sendVoice($to, $content = null, $tempId = null, array $data = [] { $this->reset(); $this->params($params, true); + $to = $this->formatMobile(Util::formatMobiles($to)); + if ($tempId && $this instanceof TemplateVoice) { $this->sendTemplateVoice($to, $tempId, $data); } elseif ($fileId && $this instanceof FileVoice) { @@ -134,6 +138,20 @@ public function sendVoice($to, $content = null, $tempId = null, array $data = [] } } + /** + * Formatting a mobile number from the list of mobile numbers. + * + * @param array $list + * + * @return string + */ + public function formatMobile(array $list) + { + return implode(',', array_unique(array_map(function($value) { + return is_array($value) ? "{$value['number']}" : $value; + }, $list))); + } + /** * @codeCoverageIgnore * diff --git a/src/phpsms/agents/QcloudAgent.php b/src/phpsms/agents/QcloudAgent.php index ac7a5a7..341f7b8 100644 --- a/src/phpsms/agents/QcloudAgent.php +++ b/src/phpsms/agents/QcloudAgent.php @@ -16,6 +16,20 @@ class QcloudAgent extends Agent implements TemplateSms, ContentSms, VoiceCode, C protected $sendVoicePrompt = 'https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoiceprompt'; protected $random; + public function formatMobile(array $list) + { + $list = array_map(function($value) { + return [ + 'nationcode' => $value['nation'], + 'mobile' => $value['number'], + ]; + }, array_filter($list, function($value) { + return is_array($value); + })); + + return count($list) === 1 ? array_pop($list) : array_values($list); + } + public function sendContentSms($to, $content) { $params = [ @@ -75,7 +89,15 @@ protected function request($sendUrl, array $params) protected function genSign($params) { - $signature = "appkey={$this->appKey}&random={$this->random}&time={$params['time']}&mobile={$params['tel']}"; + $mobileStr = null; + if (array_key_exists('mobile', $params['tel'])) { + $mobileStr = $params['tel']['mobile']; + } else { + $mobileStr = implode(',', array_map(function($value) { + return $value['mobile']; + }, $params['tel'])); + } + $signature = "appkey={$this->appKey}&random={$this->random}&time={$params['time']}&mobile={$mobileStr}"; return hash('sha256', $signature, false); } From 3b5cf242a028a2d0e4cef46188743cf6160c95b7 Mon Sep 17 00:00:00 2001 From: Koala Date: Fri, 23 Jun 2017 17:08:27 +0800 Subject: [PATCH 2/6] Fix Sign Fix Sign Bug --- src/phpsms/agents/AlidayuAgent.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/phpsms/agents/AlidayuAgent.php b/src/phpsms/agents/AlidayuAgent.php index c8429b8..b196e12 100644 --- a/src/phpsms/agents/AlidayuAgent.php +++ b/src/phpsms/agents/AlidayuAgent.php @@ -77,18 +77,19 @@ protected function request(array $params) ]); $this->setResult($result, $this->genResponseName($params['method'])); } - + protected function createParams(array $params) { - return $this->params(array_merge([ + $params = array_merge([ 'app_key' => $this->appKey, 'v' => '2.0', 'format' => 'json', 'sign_method' => 'md5', 'timestamp' => date('Y-m-d H:i:s'), - ], $params, [ - 'sign' => $this->genSign($params), - ])); + ], $params); + $params['sign'] = $this->genSign($params); + + return $this->params($params); } protected function genSign($params) From a55fd8866c82f6212715830540ff6816b3262e25 Mon Sep 17 00:00:00 2001 From: toplan Date: Sat, 24 Jun 2017 01:06:25 +0800 Subject: [PATCH 3/6] fix style --- src/phpsms/agents/AlidayuAgent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/phpsms/agents/AlidayuAgent.php b/src/phpsms/agents/AlidayuAgent.php index b196e12..0b6086b 100644 --- a/src/phpsms/agents/AlidayuAgent.php +++ b/src/phpsms/agents/AlidayuAgent.php @@ -77,7 +77,7 @@ protected function request(array $params) ]); $this->setResult($result, $this->genResponseName($params['method'])); } - + protected function createParams(array $params) { $params = array_merge([ From bdd1237bb3be988b17100ab70acd8e51511a6e90 Mon Sep 17 00:00:00 2001 From: lan tian peng Date: Fri, 23 Jun 2017 17:06:41 +0000 Subject: [PATCH 4/6] Apply fixes from StyleCI --- src/phpsms/Util.php | 1 + src/phpsms/agents/Agent.php | 2 +- src/phpsms/agents/QcloudAgent.php | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/phpsms/Util.php b/src/phpsms/Util.php index c20aae2..8ab098d 100644 --- a/src/phpsms/Util.php +++ b/src/phpsms/Util.php @@ -82,6 +82,7 @@ public static function formatMobiles($target) array_push($list, $childListItem); } } + return $list; } } diff --git a/src/phpsms/agents/Agent.php b/src/phpsms/agents/Agent.php index 6f7b10c..1d635b1 100644 --- a/src/phpsms/agents/Agent.php +++ b/src/phpsms/agents/Agent.php @@ -147,7 +147,7 @@ public function sendVoice($to, $content = null, $tempId = null, array $data = [] */ public function formatMobile(array $list) { - return implode(',', array_unique(array_map(function($value) { + return implode(',', array_unique(array_map(function ($value) { return is_array($value) ? "{$value['number']}" : $value; }, $list))); } diff --git a/src/phpsms/agents/QcloudAgent.php b/src/phpsms/agents/QcloudAgent.php index 341f7b8..f1a9710 100644 --- a/src/phpsms/agents/QcloudAgent.php +++ b/src/phpsms/agents/QcloudAgent.php @@ -18,12 +18,12 @@ class QcloudAgent extends Agent implements TemplateSms, ContentSms, VoiceCode, C public function formatMobile(array $list) { - $list = array_map(function($value) { + $list = array_map(function ($value) { return [ 'nationcode' => $value['nation'], 'mobile' => $value['number'], ]; - }, array_filter($list, function($value) { + }, array_filter($list, function ($value) { return is_array($value); })); @@ -93,7 +93,7 @@ protected function genSign($params) if (array_key_exists('mobile', $params['tel'])) { $mobileStr = $params['tel']['mobile']; } else { - $mobileStr = implode(',', array_map(function($value) { + $mobileStr = implode(',', array_map(function ($value) { return $value['mobile']; }, $params['tel'])); } From f9d0023d63c200463d80ddc6c6af360a245ea45d Mon Sep 17 00:00:00 2001 From: toplan Date: Sat, 24 Jun 2017 01:13:33 +0800 Subject: [PATCH 5/6] readme --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cedd3bc..c124610 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ Sms::voice('02343') ->to($to)->send(); ``` -### 3. 在laravel中使用 +### 3. 在laravel和lumen中使用 * 服务提供器 @@ -438,6 +438,7 @@ $sms->content('【签名】这是短信内容...'); $sms->files('Agent1', 'agent1_file_id') ->files('Agent2', 'agent2_file_id'); +//或 $sms->files([ 'Agent1' => 'agent1_file_id', 'Agent2' => 'agent2_fiile_id', @@ -603,7 +604,7 @@ Sms::scheme([ # Todo - [ ] 重新实现云通讯代理器,去掉`lib/CCPRestSmsSDK.php` -- [ ] 重新实现云子讯代理器,去掉`lib/Ucpaas.php` +- [ ] 重新实现云之讯代理器,去掉`lib/Ucpaas.php` - [ ] 升级云片接口到v2版本 # License From 5d941cfdc870763c8533a49a6b22f3291411363e Mon Sep 17 00:00:00 2001 From: toplan Date: Sat, 24 Jun 2017 01:21:46 +0800 Subject: [PATCH 6/6] fixbug --- src/phpsms/agents/AliyunAgent.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/phpsms/agents/AliyunAgent.php b/src/phpsms/agents/AliyunAgent.php index 13b0ca9..df8aeab 100644 --- a/src/phpsms/agents/AliyunAgent.php +++ b/src/phpsms/agents/AliyunAgent.php @@ -36,7 +36,7 @@ protected function request(array $params) protected function createParams(array $params) { - return $this->params(array_merge([ + $params = array_merge([ 'Format' => 'JSON', 'Version' => '2016-09-27', 'AccessKeyId' => $this->accessKeyId, @@ -44,9 +44,10 @@ protected function createParams(array $params) 'Timestamp' => date('Y-m-d\TH:i:s\Z'), 'SignatureVersion' => '1.0', 'SignatureNonce' => uniqid(), - ], $params, [ - 'Signature' => $this->computeSignature($params), - ])); + ], $params); + $params['Signature'] = $this->computeSignature($params); + + return $this->params($params); } private function computeSignature($parameters)