Skip to content

Commit

Permalink
Merge pull request #125 from toplan/dev
Browse files Browse the repository at this point in the history
1.8.1
  • Loading branch information
toplan authored Jun 24, 2017
2 parents 9a2a1bd + 5d941cf commit e5192af
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 13 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Sms::voice('02343')
->to($to)->send();
```

### 3. 在laravel中使用
### 3. 在laravel和lumen中使用

* 服务提供器

Expand Down Expand Up @@ -380,6 +380,9 @@ $sms = Sms::voice($code);
设置发送给谁,并返回实例。
```php
$sms->to('1828*******');

//兼容腾讯云
$sms->to([86, '1828*******'])
```

### template($templates)
Expand Down Expand Up @@ -435,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',
Expand Down Expand Up @@ -600,7 +604,7 @@ Sms::scheme([
# Todo

- [ ] 重新实现云通讯代理器,去掉`lib/CCPRestSmsSDK.php`
- [ ] 重新实现云子讯代理器,去掉`lib/Ucpaas.php`
- [ ] 重新实现云之讯代理器,去掉`lib/Ucpaas.php`
- [ ] 升级云片接口到v2版本

# License
Expand Down
7 changes: 5 additions & 2 deletions src/phpsms/Sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
32 changes: 32 additions & 0 deletions src/phpsms/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,36 @@ 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;
}
}
18 changes: 18 additions & 0 deletions src/phpsms/agents/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
*
Expand Down
9 changes: 5 additions & 4 deletions src/phpsms/agents/AlidayuAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ protected function request(array $params)

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)
Expand Down
9 changes: 5 additions & 4 deletions src/phpsms/agents/AliyunAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ 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,
'SignatureMethod' => 'HMAC-SHA1',
'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)
Expand Down
24 changes: 23 additions & 1 deletion src/phpsms/agents/QcloudAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit e5192af

Please sign in to comment.