Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2 from dcb9/addLastErrors
Browse files Browse the repository at this point in the history
Add last errors
  • Loading branch information
maxwelldu committed Dec 4, 2015
2 parents b36f7f7 + f46034a commit f3634a6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 19 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ OR
$ composer require dcb9/yii2-yunpian
```

## Usage
## Configurtion

```php
\# file app/config/main.php
Expand All @@ -35,3 +35,21 @@ return [
],
];
```

## Usage

```php
$phone = '01234567890';
// $phone = ['01234567890']; # 可以为数组
// $phone = '12345678900,01234567890'; # 还可以号码与号码之间用空格隔开
$text ='sms content';
$sms = Yii::$app->yunpian;
if($sms->sendSms($phone, $text))
{
$responseBody = $sms->getBody();
# ["code"=>0, "msg"=>"OK", "result" => ["count" => 1, "fee" => 1, "sid" => 3995844410]]
} elseif ($sms->hasError()) {
$error = $sms->getLastError()
# ["code" => 2, "msg" => "请求参数格式错误", "detail" => "参数 text 格式不正确,text短信内容头部需要加签名,如【云片网】"]
}
```
59 changes: 42 additions & 17 deletions src/Yunpian.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ class Yunpian extends \yii\base\Component

// /{resource}/{function}.{format}?apikey={apikey}


private $lastError;

public function getLastError()
{
$lastError = $this->lastError;
$this->lastError = null;

return $lastError;
}

/**
* @throws InvalidConfigException
*/
Expand All @@ -200,7 +211,7 @@ public function init()
if ($this->apiKey === null) {
throw new InvalidConfigException('The apiKey property must be set.');
}
$this->urlFormat = static::YUNPIAN_URL_DOMAIN
$this->urlFormat = self::YUNPIAN_URL_DOMAIN
. '/'
. $this->version
. '/%s/%s.'
Expand Down Expand Up @@ -245,6 +256,17 @@ public function saveSms($mobile, $text)
return true;
}

public function hasError()
{
return $this->lastError != null;
}

protected function setError($msg, $code = null, $detail = '')
{
$this->lastError = compact('msg', 'code', 'detail');
Yii::error($msg, self::LOG_CATEGORY);
}

/**
* 发送短信
*
Expand All @@ -265,6 +287,8 @@ public function sendSms($mobile, $text)
}

if (empty($mobile)) {
$this->setError("手机号不得为空");

return false;
}

Expand All @@ -279,42 +303,41 @@ public function sendSms($mobile, $text)
if ($isPhoneNumber) {
return true;
} else {
Yii::error("Error phone number: " . $val);
$this->setError("Error phone number: " . $val);

return false;
}
});

$body = [
'mobile' => implode(',', $mobile),
'text' => $text,
];

$url = sprintf(
$this->urlFormat,
static::RESOURCE_SMS,
static::FUNCTION_SEND
);
$body = ['mobile' => implode(',', $mobile), 'text' => $text,];
$url = sprintf($this->urlFormat, self::RESOURCE_SMS, self::FUNCTION_SEND);

if (($body = $this->post($url, $body)) === false) {
return false;
}

$code = $body['code'];
if ($code != static::CODE_0) {
if ($code != self::CODE_0) {
$n = new ConstDocHelper(__CLASS__);
if ($code < 0) {
$const = 'CODE_N' . (-$code);
} else {
$const = 'CODE_' . $code;
}
$str = $n->getDocComment($const);
Yii::error($str);
$this->setError($n->getDocComment($const), $code, $body['detail']);

return false;
}
$this->body = $body;

return true;
}

protected $body;

return $body;
public function getBody()
{
return $this->body;
}

private function post($url, array $body)
Expand All @@ -337,7 +360,7 @@ private function post($url, array $body)
if ($e->hasResponse()) {
$message .= $e->getResponse() . "\n";
}
Yii::error($message, static::LOG_CATEGORY);
$this->setError($message);

return false;
}
Expand All @@ -355,6 +378,8 @@ private function post($url, array $body)

return $body;
} else {
$this->setError("http request status code is not 200");

return false;
}
}
Expand Down
13 changes: 12 additions & 1 deletion tests/YunpianTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use dcb9\Yunpian\sdk\Yunpian;
use Yii;

class YunpianTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -35,6 +34,18 @@ public function testFileTransport()
unlink($realFile);
}

public function testLastError()
{
$sms = $this->component;
$sms->useFileTransport = false;
if (!$sms->sendSms('01234567890', 'test content') && $sms->hasError()) {
$error = $sms->getLastError();
$this->assertTrue(isset($error['code']));
$this->assertTrue(isset($error['msg']));
$this->assertTrue(isset($error['detail']));
}
}

/**
* @expectedException yii\base\InvalidConfigException
*/
Expand Down

0 comments on commit f3634a6

Please sign in to comment.