Skip to content

Commit

Permalink
同时支持自用型和工具型授权,增加新的api (#14)
Browse files Browse the repository at this point in the history
* 支持 自用型应用授权、工具型应用授权
* 新增账单api,btas,增加自用型使用demo.
  • Loading branch information
iMactool authored Jul 5, 2021
1 parent 3f5bbca commit 008944c
Show file tree
Hide file tree
Showing 13 changed files with 469 additions and 3 deletions.
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ $ composer require imactool/jinritemai
```

## Usage
> 具体可以看 example 示例代码 ,需要 composer install
> 具体可以看 example 示例代码 ,需要 `composer install `
>
> 其中 `example/index.php` 是针对 工具类型
>
> `example/self_use.php` 则是针对的 自用型
>

### 授权相关

Expand All @@ -43,6 +49,10 @@ $config = [
$servic = new DouDianApp($config);
```

### 工具型应用授权
```
#1、先获取 获取店铺授权URL
$authUrl = $servic->Auth->generateAuthUrl('state');
Expand All @@ -54,7 +64,33 @@ $accessInfo = $servic->Auth->requestAccessToken($code);
```

### 自用型应用授权

```
$shopid = 23; //$shopid 为授权方店铺的ID shop_id
try {
$accessInfo = $servic->Auth->getShopAccessToken($shopid);
echo "调用结果:";
var_dump($accessInfo);
}catch (Exception $exception){
var_dump($exception);
}
```


目前工具已支持 `自用型应用授权``工具型应用授权` 两种授权类型。主要区别如下

|店铺授权类型 |调用方式|说明|
|---|---|---|
|工具型应用授权 |1、`$authUrl = $servic->Auth->generateAuthUrl('state');`| 先获取 获取店铺授权URL|
|工具型应用授权 |2、 ` $code = 'URL code'; $accessInfo = $servic->Auth->requestAccessToken($code); ` |拿到 URL code 之后,需要调用一次 requestAccessToken() 获取授权方授权信息 ;店铺同意授权后,使用授权code,GET方式请求可获取access_token:|
||
|自用型应用授权|1、`$shopid = 23; $servic->Auth->getShopAccessToken($shopid)`|自用型 - 获取access_token|



### 获取授权方实例
> 这里 `自用型应用授权``工具型应用授权` 都是一样的用法
```
# 授权方已经把店铺授权给你的抖店开放平台了,接下来的代授权方实现业务只需一行代码即可获得授权方实例。
$shopid = 2222; //$shopid 为授权方店铺的ID shop_id
Expand All @@ -70,7 +106,7 @@ $result = $shopAccount->getShopBrandList();


## 实现接口
> 基于抖店开放平台的工具类型SDK,暂时不支持自用型。即本`SDK`是服务第三方开发者创建工具型应用(工具型应用必须上架,才能走授权流程)
> 基于抖店开放平台的(工具型、自用型)SDK ,即本`SDK`是服务第三方开发者创建工具型应用(工具型应用必须上架,才能走授权流程)
> 以下列出来的接口都是已实现的
> 具体可以看 src/DouDianApp.php .
> $app 在本文档都是指的 `$servic->shopApp($shopid,$refresh_token);` 得到的实例
Expand All @@ -84,7 +120,7 @@ $result = $shopAccount->getShopBrandList();
- 获取店铺的已授权品牌列表 getShopBrandList()
- 获取店铺后台供商家发布商品的类目 getShopCategory()
- 售后地址列表接口 addRessList()

#### 商品api `$app->Shop`
> `$app->Shop->addProduct($params)`
Expand Down Expand Up @@ -114,6 +150,8 @@ $result = $shopAccount->getShopBrandList();
- 删除规格 delSpec()
- 获取规格列表 getSpecList()
- 获取规格详情 getSpec()
- 获取商品列表新版 getProductListV2()


#### 订单 api `$app->Order`

Expand Down
8 changes: 8 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
//ini_set('date.timezone','Asia/Shanghai');
date_default_timezone_set('PRC');

/**
* ---------------------------------------------------------------------------------
* ---------------------------------------------------------------------------------
* 这是 工具型应用授权 调用示例.
*
* ---------------------------------------------------------------------------------
* ---------------------------------------------------------------------------------
*/
$config = [
'app_key' => '你的appkey',
'app_secret' => '你的秘钥',
Expand Down
189 changes: 189 additions & 0 deletions example/self_use.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

/*
* Author: cc
* Created by PhpStorm.
* Copyright (c) cc Inc. All rights reserved.
*/

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

use Imactool\Jinritemai\DouDianApp;

//ini_set('date.timezone','Asia/Shanghai');
date_default_timezone_set('PRC');

/**
* ---------------------------------------------------------------------------------
* ---------------------------------------------------------------------------------
* 这是 自用型应用授权 调用示例
* 你会发现和 工具型应用授权 除了获取 token 稍有不同,其他的调用方式基本是一致的
* ---------------------------------------------------------------------------------
* ---------------------------------------------------------------------------------.
*/
$config = [
'app_key' => '你的appkey',
'app_secret' => '你的秘钥',
'service_id' => '你的服务id',
];

$servic = new DouDianApp($config);

//商家授权成功之后,即可调用
try {
$shopid = 2322;
$accessInfo = $servic->Auth->getShopAccessToken($shopid);
echo '调用结果:';
var_dump($accessInfo);
} catch (Exception $exception) {
var_dump($exception);
}

// 删除店铺缓存,一般在
try {
$shopid = 2322;
$result = $servic->Auth->clearShopCache($shopid);
var_export($result);
} catch (Exception $exception) {
var_dump($exception);
}

// 授权方已经把店铺授权给你的抖店开放平台了,接下来的代授权方实现业务只需一行代码即可获得授权方实例。
$shopid = 2322; //$shopid 为授权方店铺的ID shop_id
$refresh_token = '授权店铺token'; //$refresh_token 为授权方的 refresh_token,
$app = $servic->shopApp($shopid, $refresh_token);

//获取店铺的已授权品牌列表
try {
$result = $app->Shop->getShopBrandList();
echo '调用结果:';
var_dump($result);
} catch (Exception $exception) {
var_dump($exception);
}

//获取店铺后台供商家发布商品的类目
try {
$result = $app->Shop->getShopCategory(['cid' => 0]);
//var_export($result);
file_put_contents('getShopCategory.log', print_r($result, 1));
$result = $app->Shop->getShopCategory(['cid' => 20038]);
file_put_contents('getShopCategory.log', PHP_EOL.' - 继续请求获取 20038 下的 '.print_r($result, 1), 8);
$result = $app->Shop->getShopCategory(['cid' => 20666]);
file_put_contents('getShopCategory.log', PHP_EOL.' - 继续请求获取 20666 下的 '.print_r($result, 1), 8);
} catch (Exception $exception) {
var_dump($exception);
}

//添加规格
try {
$spec = [];
$spec['specs'] = '材质|乳胶床垫^软硬度|软硬适中^面料|针织棉面料';
//$spec['name'] = '';
$result = $app->Product->addSpec($spec);
var_export($result);
file_put_contents('spec.log', PHP_EOL.' - 添加规格 '.print_r($result, 1), 8);
} catch (Exception $exception) {
var_dump($exception);
}

//添加商品
try {
$product = [];
$product['out_product_id'] = '1232432423432';
$product['name'] = '慕思乳胶床垫静音独立筒弹簧1.5m单双人';
$product['pic'] = 'https://img10.360buyimg.com/imgzone/jfs/t1/54292/8/7852/571691/5d553b67E6e5102ce/f74fcde197507dc6.jpg|https://img10.360buyimg.com/imgzone/jfs/t1/57312/39/7619/675304/5d553b68Ec7e042b2/0c1ecfa059c93b4d.jpg';
$product['description'] = 'https://img10.360buyimg.com/imgzone/jfs/t1/75892/29/7333/488315/5d553b68E5b74c058/afad6cd48608b03d.jpg|https://img10.360buyimg.com/imgzone/jfs/t1/52084/36/7728/336579/5d553b6bE5e83d5f3/5cdf4a12847f872a.jpg|https://img10.360buyimg.com/imgzone/jfs/t1/67703/7/494/245238/5ceb5cf3E326e7e7f/1fde813c74926d26.jpg';
$product['market_price'] = '619800';
$product['discount_price'] = '1';
//$product['first_cid'] = '2';
//$product['second_cid'] = '100';
//$product['third_cid'] = '1000';
$product['spec_id'] = '1692818797410320';
$product['mobile'] = '13122225555';
$product['weight'] = '50000';
$product['product_format'] = '材质|乳胶床垫^软硬度|软硬适中^面料|针织棉面料';
$product['pay_type'] = '1';
$product['category_leaf_id'] = '25019'; // 如果没传category_leaf_id,走之前的逻辑,需要把老的类目ID传入first_cid、second_cid、third_cid这三个字段
//$result = $app->Product->addProduct($product);
//var_export($result);
//file_put_contents('product.log',PHP_EOL.' - 添加商品 '.print_r($result,1).PHP_EOL,8);
} catch (Exception $exception) {
var_dump($exception);
}
/*
* array (
'data' =>
array (
'product_id' => 3466902402467262633,
'out_product_id' => 1232432423432,
'create_time' => '2021-02-27 13:03:56',
),
'err_no' => 0,
'message' => 'success',
*/

//添加商品(新的商品发布接口)
try {
$product = [];
$product['product_type'] = '0';
$product['product_format'] = '材质|乳胶床垫^软硬度|软硬适中^面料|针织棉面料';
$product['name'] = '慕思乳胶床垫智能超静音独立筒弹簧';
$product['pic'] = 'https://img10.360buyimg.com/imgzone/jfs/t1/54292/8/7852/571691/5d553b67E6e5102ce/f74fcde197507dc6.jpg|https://img10.360buyimg.com/imgzone/jfs/t1/57312/39/7619/675304/5d553b68Ec7e042b2/0c1ecfa059c93b4d.jpg';
$product['description'] = 'https://img10.360buyimg.com/imgzone/jfs/t1/75892/29/7333/488315/5d553b68E5b74c058/afad6cd48608b03d.jpg|https://img10.360buyimg.com/imgzone/jfs/t1/52084/36/7728/336579/5d553b6bE5e83d5f3/5cdf4a12847f872a.jpg|https://img10.360buyimg.com/imgzone/jfs/t1/67703/7/494/245238/5ceb5cf3E326e7e7f/1fde813c74926d26.jpg';
$product['pay_type'] = '1';
$product['market_price'] = '619800';
$product['discount_price'] = '1';
$product['reduce_type'] = '1';
$product['freight_id'] = '0';
$product['delivery_delay_day'] = '7';
$product['supply_7day_return'] = '1';
$product['commit'] = 'true';
$product['mobile'] = '13122225555';
$product['weight'] = '50000';
$product['weight_unit'] = '1';
$product['specs'] = '颜色|黑色,白色,黄色^尺码|S,M,L';
$product_spec_prices = [
[
'spec_detail_name1' => '黑色',
'stock_num' => 2,
'price' => 1,
],
];
$product['spec_prices'] = json_encode($product_spec_prices);

$product['category_leaf_id'] = '25019';

$result = $app->Product->addProductV2($product);
var_export($result);
if (0 === $result['err_no']) {
file_put_contents('product.log', PHP_EOL.' - 添加商品addProductV2 '.print_r($result, 1).PHP_EOL, 8);
}
} catch (Exception $exception) {
var_dump($exception);
}

//查询库存
try {
$params = [];
$params['sku_id'] = '11222';
$params['out_warehouse_id'] = 'abcd';
$result = $app->Stock->getStockNum($params);
var_export($result);
} catch (Exception $exception) {
var_dump($exception);
}

//获取订单列表
try {
$params = [];
$params['start_time'] = '2020/01/01 00:01:00';
$params['end_time'] = '2021/04/07 00:01:00';
$params['order_by'] = 'create_time';
$result = $app->Order->getOrderList($params);
file_put_contents(date('Ymd').'-order.log', print_r($result, 1));
var_dump($result);
} catch (Exception $exception) {
var_dump($exception);
}
29 changes: 29 additions & 0 deletions src/Auth/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,33 @@ public function clearShopCache(int $shopId)

return CacheAdapter::getInstance()->delete($key);
}

/**
* 自用型 - 获取access_token.
*
* @see https://op.jinritemai.com/docs/guide-docs/9/21
* 对于自用型的应用来说,初始化就需要直接 获取 token
*/
public function getShopAccessToken($shop_id)
{
$params = [
'app_id' => $this->appRunConfig['app_key'],
'app_secret' => $this->appRunConfig['app_secret'],
'code' => '',
'grant_type' => 'authorization_self',
'shop_id' => $shop_id,
];

$options = [
'headers' => [],
'query' => $params,
];
$result = $this->httpClient()->request('get', 'oauth2/access_token', $options);

if (0 !== $result['err_no']) {
return $result;
}

return $result;
}
}
32 changes: 32 additions & 0 deletions src/Bill/Bill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* Author: cc
* Created by PhpStorm.
* Copyright (c) cc Inc. All rights reserved.
*/

namespace Imactool\Jinritemai\Bill;

use Imactool\Jinritemai\Core\BaseService;

class Bill extends BaseService
{
//仅自用型应用可使调用该接口 查询订单账单明细
public function settle($params)
{
return $this->post('order/settle', $params);
}

//查询联盟订单明细
public function getOrderList($params)
{
return $this->post('alliance/getOrderList', $params);
}

//查询账单明细
public function getSettleBillDetail($params)
{
return $this->post('order/getSettleBillDetail', $params);
}
}
22 changes: 22 additions & 0 deletions src/Bill/BillProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* Author: cc
* Created by PhpStorm.
* Copyright (c) cc Inc. All rights reserved.
*/

namespace Imactool\Jinritemai\Bill;

use Imactool\Jinritemai\Core\Container;
use Imactool\Jinritemai\Interfaces\Provider;

class BillProvider implements Provider
{
public function serviceProvider(Container $container)
{
$container['Bill'] = function ($container) {
return new Bill($container);
};
}
}
Loading

0 comments on commit 008944c

Please sign in to comment.