Skip to content

Commit

Permalink
新版 SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanson committed Oct 9, 2018
1 parent 949bf72 commit e38213a
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 229 deletions.
52 changes: 13 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Youzan SDK

有赞SDK (有赞 3.0.0)
有赞SDK (支持有赞所有版本)

base on [foundation-sdk](https://github.com/HanSon/foundation-sdk)

## Requirement

- PHP >= 5.5
- PHP >= 7
- **[composer](https://getcomposer.org/)**

## Installation

```
composer require hanson/youzan-sdk
composer require hanson/youzan-sdk -vvv
```

## Usage
Expand All @@ -25,9 +25,10 @@ composer require hanson/youzan-sdk
$youzan = new \Hanson\Youzan\Youzan([
'client_id' => '',
'client_secret' => '',
'type' => \Hanson\Youzan\Youzan::PERSONAL, // 自用型应用
'debug' => true, // 调试模式
'kdt_id' => '19144834', // 店铺ID
'kdt_id' => '', // 店铺ID
'exception_as_array' => true, // 错误返回数组还是异常
'version' => '4.0.0',
'log' => [
'name' => 'youzan',
'file' => __DIR__.'/youzan.log',
Expand All @@ -36,8 +37,11 @@ $youzan = new \Hanson\Youzan\Youzan([
]
]);

// 获取门店信息
$result = $youzan->request('youzan.shop.get');
// 获取订单
$result = $youzan->request('youzan.trade.get', ['tid' => 'xxx']);

// 获取门店信息(你可以设置调用api的版本)
$result = $youzan->setVersion('3.0.0')->request('youzan.shop.get');
```

### 工具型应用
Expand All @@ -50,6 +54,8 @@ $youzan = new \Hanson\Youzan\Youzan([
'client_secret' => '',
'debug' => true,
'redirect_uri' => 'http://xxx.com',
'exception_as_array' => true,
'version' => '4.0.0',
'log' => [
'name' => 'youzan',
'file' => __DIR__.'/youzan.log',
Expand All @@ -76,38 +82,6 @@ $youzan = $youzan->oauth->createAuthorization($token['token']);
$result = $youzan->request('youzan.shop.get');
```

### 平台型应用

```php
<?php

$youzan = new \Hanson\Youzan\Youzan([
'client_id' => '',
'client_secret' => '',
'type' => \Hanson\Youzan\Youzan::PLATFORM,
'debug' => true,
// 'kdt_id' => '19144834', // 可选,用于控制某个门店
'log' => [
'name' => 'youzan',
'file' => __DIR__.'/youzan.log',
'level' => 'debug',
'permission' => 0777,
]
]);

// 平台创建门店
$result = $youzan->request('youzan.shop.create', [
'name' => 'HanSon的教学课堂',
]);

// 平台已授权门店
$youzan = $youzan->setKdtId('19144834');
$result = $youzan->request('youzan.shop.get');

// 获取订单
$result = $youzan->request('youzan.trade.get', ['tid' => 'xxxxx']);
```

## Help

QQ 群: 570769430
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
}
],
"require": {
"hanson/foundation-sdk": "^2.0"
"hanson/foundation-sdk": "^2.0",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
36 changes: 3 additions & 33 deletions src/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ class AccessToken extends AbstractAccessToken
*/
protected $kdtId;

/**
* Youzan sdk type.
*
* @var
*/
protected $type;

/**
* cache prefix.
*
Expand Down Expand Up @@ -80,32 +73,14 @@ public function getToken($forceRefresh = false)
*/
public function getTokenFromServer()
{
$params = $this->type === Youzan::PERSONAL ? $this->personalTokenParams() : $this->platformTokenParams();

$response = $this->getHttp()->post(self::TOKEN_API, $params);

return json_decode(strval($response->getBody()), true);
}

private function personalTokenParams()
{
return [
$response = $this->getHttp()->post(self::TOKEN_API, [
'client_id' => $this->clientId,
'client_secret' => $this->secret,
'grant_type' => 'silent',
'kdt_id' => $this->kdtId,
];
}

private function platformTokenParams()
{
$params = [
'client_id' => $this->clientId,
'client_secret' => $this->secret,
'grant_type' => 'authorize_platform',
];
]);

return $this->kdtId ? array_merge($params, ['kdt_id' => $this->kdtId]) : $params;
return json_decode(strval($response->getBody()), true);
}

/**
Expand All @@ -122,11 +97,6 @@ public function checkTokenResponse($result)
}
}

public function setType($type)
{
$this->type = $type;
}

/**
* @param mixed $kdtId
*/
Expand Down
50 changes: 25 additions & 25 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,58 @@
class Api extends AbstractAPI
{

const API = 'https://open.youzan.com/api/oauthentry/';

/**
* @var AccessToken
* @var Youzan
*/
protected $accessToken;
protected $youzan;

const API = 'https://open.youzan.com/api/oauthentry/';

public function __construct(AbstractAccessToken $accessToken)
public function __construct(Youzan $youzan)
{
$this->accessToken = $accessToken;
$this->youzan = $youzan;
}

/**
* 请求API
*
* @param $method
* @param array $params
* @param string $version
* @param array $files
* @return array
* @throws YouzanException
*/
public function request($method, $params = [], $version = '3.0.0', $files = [])
public function request($method, $params = [], $files = [])
{
$url = $this->url($method, $version);
$url = $this->url($method);

$http = $this->getHttp();

$params['access_token'] = $this->accessToken->getToken();
$params['access_token'] = $this->youzan['access_token']->getToken();

$response = $files ? $http->upload($url, $params, $files) : $http->post($url, $params);

$result = json_decode(strval($response->getBody()), true);

if (isset($result['error_response'])) {
return $this->errorResponse($result);
}

return $result['response'];
}

public function errorResponse(array $result)
{
if ($this->youzan->getResponse()) {
return $result['error_response'];
} else {
// 有赞有些接口中返回的错误信息包含在msg里,有的返回message属性中。
$message = isset($result['error_response']['msg'])
? $result['error_response']['msg']
: $result['error_response']['message'];
? $result['error_response']['msg']
: $result['error_response']['message'];

throw new YouzanException($message, $result['error_response']['code']);
}

return $result['response'];
}

/**
Expand All @@ -62,24 +70,16 @@ public function request($method, $params = [], $version = '3.0.0', $files = [])
* @param $method
* @param $version
* @return string
* @throws YouzanException
*/
private function url($method, $version)
private function url($method)
{
$methodArray = explode('.', $method);

$method = '/' . $version . '/' . $methodArray[count($methodArray) - 1];
$method = '/' . $this->youzan->getVersion() . '/' . $methodArray[count($methodArray) - 1];

array_pop($methodArray);

return self::API . implode('.', $methodArray) . $method;
}

/**
* Push guzzle middleware before request.
*
* @return mixed
*/
public function middlewares()
{
}
}
5 changes: 0 additions & 5 deletions src/App/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,4 @@ public function request($method, $params = [])
return $result;
}

public function middlewares()
{
// TODO: Implement middlewares() method.
}

}
3 changes: 3 additions & 0 deletions src/App/Sso.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Sso extends Api
* 初始化 token
*
* @return mixed
* @throws \Hanson\Youzan\YouzanException
*/
public function initToken()
{
Expand All @@ -28,6 +29,7 @@ public function initToken()
* @param $open_user_id
* @param array $params
* @return mixed
* @throws \Hanson\Youzan\YouzanException
*/
public function login($open_user_id, $params = [])
{
Expand All @@ -41,6 +43,7 @@ public function login($open_user_id, $params = [])
*
* @param $open_user_id
* @return mixed
* @throws \Hanson\Youzan\YouzanException
*/
public function logout($open_user_id)
{
Expand Down
13 changes: 0 additions & 13 deletions src/Oauth/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ class AccessToken extends BaseAccessToken

protected $redirectUri;

/**
* 获取 token from server.
*
* @param $params
* @return mixed
*/
public function token($params)
{
$response = $this->getHttp()->post(self::TOKEN_API, $params);

return json_decode(strval($response->getBody()), true);
}

public function setRequest(Request $request)
{
$this->request = $request;
Expand Down
69 changes: 0 additions & 69 deletions src/Oauth/Api.php

This file was deleted.

Loading

0 comments on commit e38213a

Please sign in to comment.