feat(店铺收款): 开放账户账单只读接口
为店铺收款账户账单全部 12 个页签开放只读外部 API。
This commit is contained in:
parent
9f0523e456
commit
68f0e83d53
146
docs/店铺收款账户账单外部接口.md
Normal file
146
docs/店铺收款账户账单外部接口.md
Normal file
@ -0,0 +1,146 @@
|
||||
# 店铺收款账户账单外部接口
|
||||
|
||||
用于向外部系统只读开放「账单流水数据 › 店铺收款账户账单」各账户页签的数据。接口复用页面现有的数据源、筛选、汇总、余额和总账备注口径。
|
||||
|
||||
## 鉴权与公司范围
|
||||
|
||||
接口使用系统现有 API 鉴权:
|
||||
|
||||
```http
|
||||
Authorization: Basic <base64(用户邮箱:密码)>
|
||||
```
|
||||
|
||||
如果系统启用了 OAuth,也可以使用:
|
||||
|
||||
```http
|
||||
Authorization: Bearer <OAuth access token>
|
||||
```
|
||||
|
||||
调用账号需要具备:
|
||||
|
||||
- API 基础权限 `read-api`;
|
||||
- 「账单流水数据 › 店铺收款账户账单」页面权限。
|
||||
|
||||
公司范围使用 `X-Company: <company_id>` 请求头或 `company_id` 查询参数。未指定时使用调用账号的首个公司。
|
||||
|
||||
## 账户目录
|
||||
|
||||
```http
|
||||
GET /api/ecommerce-finance/store-receiving-bills
|
||||
```
|
||||
|
||||
返回 12 个可用页签及各自的数据地址:
|
||||
|
||||
| `account` | 页面名称 |
|
||||
| --- | --- |
|
||||
| `taobao-alipay` | 全部支付宝账户 |
|
||||
| `taobao-wechat` | 淘系微信账户 |
|
||||
| `pinduoduo` | 拼多多账户 |
|
||||
| `jd-wallet` | 京东网银钱包账户 |
|
||||
| `jd-transactions` | 京东账单 |
|
||||
| `jingxi` | 京喜账单 |
|
||||
| `douyin` | 抖音账户 |
|
||||
| `dewu` | 得物账户 |
|
||||
| `dewu-alipay` | 得物支付宝账户 |
|
||||
| `vipshop` | 唯品会MP账户 |
|
||||
| `vipshop-jx` | 唯品会JX账户 |
|
||||
| `xiaohongshu` | 小红书账户 |
|
||||
|
||||
## 分页查询
|
||||
|
||||
```http
|
||||
GET /api/ecommerce-finance/store-receiving-bills/{account}
|
||||
```
|
||||
|
||||
常用参数:
|
||||
|
||||
| 参数 | 说明 |
|
||||
| --- | --- |
|
||||
| `start` | 开始日期,`YYYY-MM-DD` |
|
||||
| `end` | 结束日期,`YYYY-MM-DD` |
|
||||
| `store_name` | 原始店铺/店铺名称 |
|
||||
| `ledger_remark` | 总账备注 |
|
||||
| `bill_item` | 账单项目 |
|
||||
| `keyword` | 关键词;各页签沿用页面实际匹配字段 |
|
||||
| `sort` | `occurred_at`、`income`、`expense`、`balance` 或 `record_code` |
|
||||
| `dir` | `asc` 或 `desc` |
|
||||
| `page` | 页码,从 1 开始 |
|
||||
| `limit` | 每页条数,1–100,默认 10 |
|
||||
|
||||
日期规则:
|
||||
|
||||
- `start` 和 `end` 需要同时提供;
|
||||
- 两者都不提供时,默认查询当前自然月,与页面默认口径一致;
|
||||
- 不提供日期不会自动扫描全部历史数据。
|
||||
|
||||
每个 `account` 支持的筛选字段与页面一致。例如京东账单支持 `keyword_match`,唯品会按账单期间筛选。
|
||||
|
||||
示例:
|
||||
|
||||
```bash
|
||||
curl --get 'https://caiwu.example.com/api/ecommerce-finance/store-receiving-bills/taobao-alipay' \
|
||||
--user 'api-user@example.com:password' \
|
||||
-H 'Accept: application/json' \
|
||||
-H 'X-Company: 1' \
|
||||
--data-urlencode 'start=2026-11-01' \
|
||||
--data-urlencode 'end=2026-11-30' \
|
||||
--data-urlencode 'store_name=天猫-亿家达旗舰店' \
|
||||
--data-urlencode 'page=1' \
|
||||
--data-urlencode 'limit=100'
|
||||
```
|
||||
|
||||
响应沿用页面的分页结构,并增加 `account` 标识:
|
||||
|
||||
```json
|
||||
{
|
||||
"data": [],
|
||||
"total": 0,
|
||||
"current_page": 1,
|
||||
"last_page": 1,
|
||||
"per_page": 100,
|
||||
"summary": {
|
||||
"income": 0,
|
||||
"expense": 0,
|
||||
"balance": 0,
|
||||
"count": 0
|
||||
},
|
||||
"columns": {
|
||||
"title": "全部支付宝账户",
|
||||
"groups": [],
|
||||
"columns": []
|
||||
},
|
||||
"account": {
|
||||
"slug": "taobao-alipay",
|
||||
"title": "全部支付宝账户"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
不同账户的 `summary` 和 `columns.columns` 字段会按页面实际列配置返回。
|
||||
|
||||
## 筛选选项
|
||||
|
||||
```http
|
||||
GET /api/ecommerce-finance/store-receiving-bills/{account}/filter-options
|
||||
```
|
||||
|
||||
返回该页签可用的 `shops`、`bill_items`、`ledger_remarks`,供外部系统生成筛选条件。
|
||||
|
||||
## 分页与同步建议
|
||||
|
||||
1. 先调用账户目录,按目标页签选择 `account`。
|
||||
2. 按月份或明确日期范围查询,`limit` 使用 100。
|
||||
3. 依次读取到 `last_page`,不要只取第一页。
|
||||
4. 外部同步记录最后成功账期与 `account`,下次从下一个账期继续读取。
|
||||
5. 接口为只读 GET 接口,不暴露新增、修改、删除或 CSV 导出能力。
|
||||
|
||||
## 状态码
|
||||
|
||||
| 状态码 | 说明 |
|
||||
| --- | --- |
|
||||
| `200` | 成功 |
|
||||
| `401` | 未提供有效凭据 |
|
||||
| `403` | 调用账号没有 API 或账单页面权限 |
|
||||
| `404` | 账户类型或公司不存在 |
|
||||
| `422` | 日期、筛选参数或分页参数不合法 |
|
||||
| `503` | 对应账户的共享业务镜像尚未完成首次同步,稍后重试 |
|
||||
@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\EcommerceFinance\Http\Controllers\Api;
|
||||
|
||||
use App\Abstracts\Http\ApiController;
|
||||
use App\Utilities\SidebarPermissions;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Modules\EcommerceFinance\Http\Controllers\AccountBills;
|
||||
use Modules\EcommerceFinance\Http\Controllers\AlipayBills;
|
||||
use Modules\EcommerceFinance\Http\Controllers\DewuAccountBills;
|
||||
use Modules\EcommerceFinance\Http\Controllers\PinduoduoAccountBills;
|
||||
use Modules\EcommerceFinance\Http\Controllers\TaobaoWechatAccountBills;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* 店铺收款账户账单的只读外部接口。
|
||||
*
|
||||
* 各账户页签继续调用页面现有控制器,确保分页、汇总、余额和总账备注口径一致。
|
||||
*/
|
||||
class StoreReceivingBills extends ApiController
|
||||
{
|
||||
/** @var array<string,array{title:string,controller:class-string}> */
|
||||
private const ACCOUNTS = [
|
||||
'taobao-alipay' => ['title' => '全部支付宝账户', 'controller' => AlipayBills::class],
|
||||
'taobao-wechat' => ['title' => '淘系微信账户', 'controller' => TaobaoWechatAccountBills::class],
|
||||
'pinduoduo' => ['title' => '拼多多账户', 'controller' => PinduoduoAccountBills::class],
|
||||
'jd-wallet' => ['title' => '京东网银钱包账户', 'controller' => AccountBills::class],
|
||||
'jd-transactions' => ['title' => '京东账单', 'controller' => AccountBills::class],
|
||||
'jingxi' => ['title' => '京喜账单', 'controller' => AccountBills::class],
|
||||
'douyin' => ['title' => '抖音账户', 'controller' => AccountBills::class],
|
||||
'dewu' => ['title' => '得物账户', 'controller' => DewuAccountBills::class],
|
||||
'dewu-alipay' => ['title' => '得物支付宝账户', 'controller' => AccountBills::class],
|
||||
'vipshop' => ['title' => '唯品会MP账户', 'controller' => AccountBills::class],
|
||||
'vipshop-jx' => ['title' => '唯品会JX账户', 'controller' => AccountBills::class],
|
||||
'xiaohongshu' => ['title' => '小红书账户', 'controller' => AccountBills::class],
|
||||
];
|
||||
|
||||
public function accounts(): JsonResponse
|
||||
{
|
||||
$this->authorizeStoreReceivingBills();
|
||||
|
||||
$accounts = collect(self::ACCOUNTS)
|
||||
->map(fn (array $account, string $slug): array => [
|
||||
'slug' => $slug,
|
||||
'title' => $account['title'],
|
||||
'url' => route('api.ecommerce-finance.store-receiving-bills.index', ['account' => $slug]),
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return response()->json(['data' => $accounts]);
|
||||
}
|
||||
|
||||
public function index(Request $request, string $account): Response
|
||||
{
|
||||
$this->authorizeStoreReceivingBills();
|
||||
$this->prepareDataRequest($request, $account);
|
||||
|
||||
$controller = app(self::ACCOUNTS[$account]['controller']);
|
||||
$parameters = $controller instanceof AccountBills ? ['bill_type' => $account] : [];
|
||||
$response = app()->call([$controller, 'index'], $parameters);
|
||||
|
||||
return $this->decorateResponse($response, $account);
|
||||
}
|
||||
|
||||
public function filterOptions(string $account): Response
|
||||
{
|
||||
$this->authorizeStoreReceivingBills();
|
||||
$this->ensureAccountExists($account);
|
||||
|
||||
$controller = app(self::ACCOUNTS[$account]['controller']);
|
||||
$parameters = $controller instanceof AccountBills ? ['bill_type' => $account] : [];
|
||||
$response = app()->call([$controller, 'filterOptions'], $parameters);
|
||||
|
||||
return $this->decorateResponse($response, $account);
|
||||
}
|
||||
|
||||
private function prepareDataRequest(Request $request, string $account): void
|
||||
{
|
||||
$this->ensureAccountExists($account);
|
||||
|
||||
// 外部接口固定返回 JSON,不允许通过 export=1 触发 CSV 下载。
|
||||
$request->query->remove('export');
|
||||
|
||||
$start = trim((string) $request->query->get('start', ''));
|
||||
$end = trim((string) $request->query->get('end', ''));
|
||||
|
||||
if (($start === '') !== ($end === '')) {
|
||||
throw ValidationException::withMessages([
|
||||
'start' => ['开始日期和结束日期需要同时提供。'],
|
||||
'end' => ['开始日期和结束日期需要同时提供。'],
|
||||
]);
|
||||
}
|
||||
|
||||
// 与页面默认口径一致:未指定日期时只读当前自然月,避免无界扫描全表。
|
||||
// 指定日期时完全遵循调用方参数,不做账期改写。
|
||||
if ($start === '') {
|
||||
$request->query->set('start', now()->startOfMonth()->toDateString());
|
||||
$request->query->set('end', now()->endOfMonth()->toDateString());
|
||||
}
|
||||
}
|
||||
|
||||
private function ensureAccountExists(string $account): void
|
||||
{
|
||||
abort_unless(isset(self::ACCOUNTS[$account]), 404, '未知的店铺收款账户类型。');
|
||||
}
|
||||
|
||||
private function authorizeStoreReceivingBills(): void
|
||||
{
|
||||
$section = collect(SidebarPermissions::navigationFor(user()))
|
||||
->firstWhere('slug', 'billing-transactions');
|
||||
$page = collect($section['pages'] ?? [])
|
||||
->firstWhere('slug', 'store-receiving-bills');
|
||||
|
||||
abort_if(empty($page), 403, '当前账号没有店铺收款账户账单权限。');
|
||||
}
|
||||
|
||||
private function decorateResponse(Response $response, string $account): Response
|
||||
{
|
||||
if (! $response instanceof JsonResponse || ! is_array($response->getData(true))) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$payload = $response->getData(true);
|
||||
$payload['account'] = [
|
||||
'slug' => $account,
|
||||
'title' => self::ACCOUNTS[$account]['title'],
|
||||
];
|
||||
$response->setData($payload);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Modules\EcommerceFinance\Http\Controllers\Api\StoreReceivingBills;
|
||||
|
||||
/**
|
||||
* 'api' middleware and prefix applied to all routes
|
||||
@ -9,6 +10,14 @@ use Illuminate\Support\Facades\Route;
|
||||
*/
|
||||
|
||||
Route::group(['as' => 'api.'], function () {
|
||||
// 店铺收款账户账单:只读分页数据、账户目录和筛选项。
|
||||
Route::get('ecommerce-finance/store-receiving-bills', [StoreReceivingBills::class, 'accounts'])
|
||||
->name('ecommerce-finance.store-receiving-bills.accounts');
|
||||
Route::get('ecommerce-finance/store-receiving-bills/{account}/filter-options', [StoreReceivingBills::class, 'filterOptions'])
|
||||
->name('ecommerce-finance.store-receiving-bills.filter-options');
|
||||
Route::get('ecommerce-finance/store-receiving-bills/{account}', [StoreReceivingBills::class, 'index'])
|
||||
->name('ecommerce-finance.store-receiving-bills.index');
|
||||
|
||||
// Ping
|
||||
Route::get('ping', 'Common\Ping@pong')->name('ping');
|
||||
|
||||
|
||||
208
tests/Feature/Business/StoreReceivingBillsApiTest.php
Normal file
208
tests/Feature/Business/StoreReceivingBillsApiTest.php
Normal file
@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Business;
|
||||
|
||||
use Mockery;
|
||||
use Modules\EcommerceFinance\Repositories\StoreReceivingAlipayBillMirrorRepository;
|
||||
use Modules\EcommerceFinance\Repositories\StoreReceivingDewuAlipayBillMirrorRepository;
|
||||
use Tests\Feature\FeatureTestCase;
|
||||
|
||||
class StoreReceivingBillsApiTest extends FeatureTestCase
|
||||
{
|
||||
public function test_account_directory_lists_all_receiving_bill_tabs(): void
|
||||
{
|
||||
$this->loginAs();
|
||||
|
||||
$this->withHeaders($this->apiHeaders())
|
||||
->getJson(route('api.ecommerce-finance.store-receiving-bills.accounts'))
|
||||
->assertOk()
|
||||
->assertJsonCount(12, 'data')
|
||||
->assertJsonFragment(['slug' => 'taobao-alipay', 'title' => '全部支付宝账户'])
|
||||
->assertJsonFragment(['slug' => 'xiaohongshu', 'title' => '小红书账户']);
|
||||
}
|
||||
|
||||
public function test_taobao_alipay_api_uses_existing_query_and_returns_account_metadata(): void
|
||||
{
|
||||
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
||||
$repository->shouldReceive('paginate')
|
||||
->once()
|
||||
->with(
|
||||
Mockery::on(fn (array $filters): bool => $filters['store_name'] === '天猫-亿家达旗舰店'
|
||||
&& $filters['start'] === '2026-11-01'
|
||||
&& $filters['end'] === '2026-11-30'),
|
||||
'occurred_at',
|
||||
'desc',
|
||||
2,
|
||||
20,
|
||||
)
|
||||
->andReturn($this->emptyAlipayResult(2, 20));
|
||||
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
||||
|
||||
$this->loginAs();
|
||||
|
||||
$this->withHeaders($this->apiHeaders())
|
||||
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']) . '?' . http_build_query([
|
||||
'store_name' => '天猫-亿家达旗舰店',
|
||||
'start' => '2026-11-01',
|
||||
'end' => '2026-11-30',
|
||||
'page' => 2,
|
||||
'limit' => 20,
|
||||
]))
|
||||
->assertOk()
|
||||
->assertJsonPath('account.slug', 'taobao-alipay')
|
||||
->assertJsonPath('account.title', '全部支付宝账户')
|
||||
->assertJsonPath('current_page', 2)
|
||||
->assertJsonPath('per_page', 20);
|
||||
}
|
||||
|
||||
public function test_api_defaults_to_current_month_when_dates_are_omitted(): void
|
||||
{
|
||||
$this->travelTo('2026-12-02 10:00:00');
|
||||
|
||||
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
||||
$repository->shouldReceive('paginate')
|
||||
->once()
|
||||
->with(
|
||||
Mockery::on(fn (array $filters): bool => $filters['start'] === '2026-12-01'
|
||||
&& $filters['end'] === '2026-12-31'),
|
||||
'occurred_at',
|
||||
'desc',
|
||||
1,
|
||||
10,
|
||||
)
|
||||
->andReturn($this->emptyAlipayResult());
|
||||
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
||||
|
||||
$this->loginAs();
|
||||
|
||||
$this->withHeaders($this->apiHeaders())
|
||||
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']))
|
||||
->assertOk();
|
||||
}
|
||||
|
||||
public function test_generic_account_tab_uses_the_shared_account_bills_query(): void
|
||||
{
|
||||
$repository = Mockery::mock(StoreReceivingDewuAlipayBillMirrorRepository::class);
|
||||
$repository->shouldReceive('paginate')
|
||||
->once()
|
||||
->with(
|
||||
Mockery::on(fn (array $filters): bool => $filters['start'] === '2026-11-01'
|
||||
&& $filters['end'] === '2026-11-30'),
|
||||
'occurred_at',
|
||||
'desc',
|
||||
1,
|
||||
5,
|
||||
)
|
||||
->andReturn($this->emptyAlipayResult(1, 5));
|
||||
$this->app->instance(StoreReceivingDewuAlipayBillMirrorRepository::class, $repository);
|
||||
|
||||
$this->loginAs();
|
||||
|
||||
$this->withHeaders($this->apiHeaders())
|
||||
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'dewu-alipay']) . '?' . http_build_query([
|
||||
'start' => '2026-11-01',
|
||||
'end' => '2026-11-30',
|
||||
'limit' => 5,
|
||||
]))
|
||||
->assertOk()
|
||||
->assertJsonPath('account.slug', 'dewu-alipay')
|
||||
->assertJsonPath('account.title', '得物支付宝账户')
|
||||
->assertJsonPath('columns.title', '得物支付宝账户');
|
||||
}
|
||||
|
||||
public function test_api_rejects_an_incomplete_date_range(): void
|
||||
{
|
||||
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
||||
$repository->shouldNotReceive('paginate');
|
||||
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
||||
|
||||
$this->loginAs();
|
||||
|
||||
$this->withExceptionHandling()
|
||||
->withHeaders($this->apiHeaders())
|
||||
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']) . '?start=2026-11-01')
|
||||
->assertStatus(422)
|
||||
->assertJsonValidationErrors(['start', 'end']);
|
||||
}
|
||||
|
||||
public function test_filter_options_endpoint_uses_the_same_page_options(): void
|
||||
{
|
||||
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
||||
$repository->shouldReceive('filterOptionsForChannel')
|
||||
->once()
|
||||
->with('alipayCSV', StoreReceivingAlipayBillMirrorRepository::DATASET, true)
|
||||
->andReturn([
|
||||
'shops' => ['天猫-亿家达旗舰店'],
|
||||
'bill_items' => ['订单收入'],
|
||||
]);
|
||||
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
||||
|
||||
$this->loginAs();
|
||||
|
||||
$this->withHeaders($this->apiHeaders())
|
||||
->getJson(route('api.ecommerce-finance.store-receiving-bills.filter-options', ['account' => 'taobao-alipay']))
|
||||
->assertOk()
|
||||
->assertJsonPath('account.slug', 'taobao-alipay')
|
||||
->assertJsonPath('shops.0', '天猫-亿家达旗舰店')
|
||||
->assertJsonPath('bill_items.0', '订单收入');
|
||||
}
|
||||
|
||||
public function test_export_parameter_still_returns_json_data(): void
|
||||
{
|
||||
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
||||
$repository->shouldReceive('paginate')
|
||||
->once()
|
||||
->andReturn($this->emptyAlipayResult());
|
||||
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
||||
|
||||
$this->loginAs();
|
||||
|
||||
$this->withHeaders($this->apiHeaders())
|
||||
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']) . '?' . http_build_query([
|
||||
'start' => '2026-11-01',
|
||||
'end' => '2026-11-30',
|
||||
'export' => 1,
|
||||
]))
|
||||
->assertOk()
|
||||
->assertHeader('content-type', 'application/json')
|
||||
->assertJsonPath('account.slug', 'taobao-alipay');
|
||||
}
|
||||
|
||||
public function test_unknown_account_returns_not_found(): void
|
||||
{
|
||||
$this->loginAs();
|
||||
|
||||
$this->withExceptionHandling()
|
||||
->withHeaders($this->apiHeaders())
|
||||
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'unknown-account']))
|
||||
->assertNotFound();
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
private function emptyAlipayResult(int $page = 1, int $perPage = 10): array
|
||||
{
|
||||
return [
|
||||
'data' => [],
|
||||
'total' => 0,
|
||||
'current_page' => $page,
|
||||
'last_page' => 1,
|
||||
'per_page' => $perPage,
|
||||
'summary' => [
|
||||
'income' => 0,
|
||||
'expense' => 0,
|
||||
'opening_balance' => 0,
|
||||
'opening_balance_date' => null,
|
||||
'opening_balance_source' => null,
|
||||
'balance' => 0,
|
||||
'count' => 0,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function apiHeaders(): array
|
||||
{
|
||||
return [
|
||||
'Authorization' => 'Basic ' . base64_encode('test@company.com:123456'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user