Skip to content

Commit

Permalink
Merge branch 'feature/v2.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 committed Oct 25, 2016
2 parents 9162fb0 + b8ad202 commit 8661584
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 14 deletions.
35 changes: 33 additions & 2 deletions README.md
@@ -1,14 +1,23 @@
# 阿里大于(鱼) - v2.0

![build=passing](https://img.shields.io/badge/build-passing-brightgreen.svg?maxAge=2592000) [![composer](https://img.shields.io/badge/composer-flc/alidayu-yellowgreen.svg?maxAge=2592000)](https://packagist.org/packages/flc/alidayu) [![tag=v2.0.3](https://img.shields.io/badge/tag-v2.0.3-yellow.svg?maxAge=2592000)](https://github.com/flc1125/alidayu/archive/v2.0.2.zip) ![php>=5.4](https://img.shields.io/badge/php->%3D5.4-orange.svg?maxAge=2592000) [![license=MIT](https://img.shields.io/badge/license-MIT-blue.svg?maxAge=2592000)](https://github.com/flc1125/alidayu/blob/master/LICENSE)
![build=passing](https://img.shields.io/badge/build-passing-brightgreen.svg?maxAge=2592000) [![composer](https://img.shields.io/badge/composer-flc/alidayu-yellowgreen.svg?maxAge=2592000)](https://packagist.org/packages/flc/alidayu) [![tag=v2.0.4](https://img.shields.io/badge/tag-v2.0.4-yellow.svg?maxAge=2592000)](https://github.com/flc1125/alidayu/archive/v2.0.4.zip) ![php>=5.4](https://img.shields.io/badge/php->%3D5.4-orange.svg?maxAge=2592000) [![license=MIT](https://img.shields.io/badge/license-MIT-blue.svg?maxAge=2592000)](https://github.com/flc1125/alidayu/blob/master/LICENSE)

> `v2.0`不支持从`v1.0`直接升级,请抛弃`v1.0`
## 更新

#### v2.0.4 (2016-10-12)

```
新增自动载入功能(不依靠composer)
新增Client::request快捷调用方法
```

#### v2.0.3 (2016-10-12)

- 新增沙箱配置
```
新增沙箱配置
```

## 功能

Expand Down Expand Up @@ -36,13 +45,20 @@
composer require flc/alidayu
```


```php
require '/path/to/alidayu/autoload.php';
```

## 使用

```php
<?php
use Flc\Alidayu\Client;
use Flc\Alidayu\App;
use Flc\Alidayu\Requests\AlibabaAliqinFcSmsNumSend;
use Flc\Alidayu\Requests\IRequest;

// 配置信息
$config = [
Expand All @@ -51,6 +67,8 @@ $config = [
// 'sandbox' => true, // 是否为沙箱环境,默认false
];


// 使用方法一
$client = new Client(new App($config));
$req = new AlibabaAliqinFcSmsNumSend;

Expand All @@ -63,6 +81,19 @@ $req->setRecNum('13312311231')

$resp = $client->execute($req);

// 使用方法二
Client::configure($config); // 全局定义配置(定义一次即可,无需重复定义)

$resp = Client::request('alibaba.aliqin.fc.sms.num.send', function (IRequest $req) {
$req->setRecNum('13312311231')
->setSmsParam([
'number' => rand(100000, 999999)
])
->setSmsFreeSignName('叶子坑')
->setSmsTemplateCode('SMS_15105357');
});

// 返回结果
print_r($resp);
print_r($resp->result->model);
?>
Expand Down
17 changes: 17 additions & 0 deletions autoload.php
@@ -0,0 +1,17 @@
<?php
/**
* 自动载入
*
* @author Flc <2016-10-25 17:35:52>
* @link http://flc.ren
*/
spl_autoload_register(function ($classname) {
$baseDir = __DIR__ . '/src/Alidayu/';

if (strpos($classname, "Flc\\Alidayu\\") === 0) {
$file = $baseDir . substr($classname, strlen('Flc\\Alidayu\\')) . '.php';

if (is_file($file))
require_once $file;
}
});
74 changes: 74 additions & 0 deletions src/Alidayu/Client.php
Expand Up @@ -42,6 +42,12 @@ class Client
*/
protected $format = 'json';

/**
* 静态配置
* @var array
*/
protected static $config = [];

/**
* 初始化
* @param array $config 阿里大于配置App类
Expand Down Expand Up @@ -214,6 +220,74 @@ protected static function sortParams(&$params = [])
ksort($params);
}

/**
* 请求API
* @param string $method 接口名称
* @param callable $callable 执行函数
* @return [type] [description]
*/
public static function request($method, callable $callable)
{
// A. 校验
if (empty($method) ||
! $classname = self::getMethodClassName($method)
) {
throw new Exception("method错误");
}

// B. 获取带命名空间的类
$classNameSpace = __NAMESPACE__ . '\\Requests\\' . $classname;

if (! class_exists($classNameSpace))
throw new Exception("method不存在");

// C. 创建对象
$request = new $classNameSpace;

// D. 执行匿名函数
if (is_callable($callable)) {
call_user_func($callable, $request);
}

// E. 创建CLIENT对象
$client = new self(new App(self::$config));

return call_user_func_array([$client, 'execute'], [$request]);
}

/**
* 静态配置(全局)
* @param array $config 配置项
* @return [type] [description]
*/
public static function configure($config)
{
self::$config = $config;
}

/**
* 通过接口名称获取对应的类名称
* @param string $method 接口名称
* @return string
*/
protected static function getMethodClassName($method)
{
$methods = explode('.', $method);

if (!is_array($methods))
return false;

$tmp = array();

foreach ($methods as $value) {
$tmp[] = ucwords($value);
}

$className = implode('', $tmp);

return $className;
}

/**
* curl请求
* @param string $url string
Expand Down
39 changes: 27 additions & 12 deletions tests/index.php
Expand Up @@ -10,8 +10,10 @@
use Flc\Alidayu\Requests\AlibabaAliqinFcFlowCharge;
use Flc\Alidayu\Requests\AlibabaAliqinFcFlowQuery;
use Flc\Alidayu\Requests\AlibabaAliqinFcFlowChargeProvince;
use Flc\Alidayu\Requests\IRequest;

require __DIR__ . '/../vendor/autoload.php';
//require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../autoload.php';

// 配置信息
$config = [
Expand All @@ -20,16 +22,29 @@
'sandbox' => true, // 是否为沙箱环境,默认false
];

$client = new Client(new App($config));
// $client = new Client(new App($config));

// 短信发送 passed
$req = new AlibabaAliqinFcSmsNumSend;
$req->setRecNum('13312311231')
->setSmsParam([
'number' => rand(100000, 999999)
])
->setSmsFreeSignName('叶子坑')
->setSmsTemplateCode('SMS_15105357');
// // 短信发送 passed
// $req = new AlibabaAliqinFcSmsNumSend;
// $req->setRecNum('13312311231')
// ->setSmsParam([
// 'number' => rand(100000, 999999)
// ])
// ->setSmsFreeSignName('叶子坑')
// ->setSmsTemplateCode('SMS_15105357');

Client::configure($config); // 只需定义一次

$rs = Client::request('alibaba.aliqin.fc.sms.num.send', function (IRequest $req) {
$req->setRecNum('13312311231')
->setSmsParam([
'number' => rand(100000, 999999)
])
->setSmsFreeSignName('叶子坑')
->setSmsTemplateCode('SMS_15105357');
});

print_r($rs);

// 文本转语音通知 passed
// $req = new AlibabaAliqinFcTtsNumSinglecall;
Expand Down Expand Up @@ -82,6 +97,6 @@
// ->setGrade('50')
// ->setOutRechargeId('111111');

print_r($req->getParams());
// print_r($req->getParams());

print_r($client->execute($req));
// print_r($client->execute($req));

0 comments on commit 8661584

Please sign in to comment.