提交
This commit is contained in:
43
app/miniapi/controller/CustomerCenter.php
Normal file
43
app/miniapi/controller/CustomerCenter.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\miniapi\controller;
|
||||
|
||||
use app\miniapi\service\CustomerCenter\CustomerCenterService;
|
||||
|
||||
final class CustomerCenter extends Base
|
||||
{
|
||||
public function applicants()
|
||||
{
|
||||
$userId = $this->requireLogin();
|
||||
if (!is_int($userId)) {
|
||||
return $userId;
|
||||
}
|
||||
|
||||
$result = (new CustomerCenterService())->applicants($userId);
|
||||
if ((int)($result['code'] ?? 0) !== 1) {
|
||||
return $this->fail((string)($result['msg'] ?? '服务异常,请稍后重试'));
|
||||
}
|
||||
|
||||
return $this->ok((array)($result['data'] ?? []));
|
||||
}
|
||||
|
||||
public function applications()
|
||||
{
|
||||
$userId = $this->requireLogin();
|
||||
if (!is_int($userId)) {
|
||||
return $userId;
|
||||
}
|
||||
|
||||
$result = (new CustomerCenterService())->applications(
|
||||
$userId,
|
||||
(int)$this->request->param('page', 1),
|
||||
(int)$this->request->param('page_size', 20)
|
||||
);
|
||||
if ((int)($result['code'] ?? 0) !== 1) {
|
||||
return $this->fail((string)($result['msg'] ?? '服务异常,请稍后重试'));
|
||||
}
|
||||
|
||||
return $this->ok((array)($result['data'] ?? []));
|
||||
}
|
||||
}
|
||||
66
app/miniapi/service/CustomerCenter/CustomerCenterService.php
Normal file
66
app/miniapi/service/CustomerCenter/CustomerCenterService.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\miniapi\service\CustomerCenter;
|
||||
|
||||
use api\Httpcurl;
|
||||
use think\facade\Log;
|
||||
|
||||
final class CustomerCenterService
|
||||
{
|
||||
public function applicants(int $userId): array
|
||||
{
|
||||
return $this->request('applicants', $userId);
|
||||
}
|
||||
|
||||
public function applications(int $userId, int $page, int $pageSize): array
|
||||
{
|
||||
return $this->request('applications', $userId, [
|
||||
'page' => max(1, $page),
|
||||
'page_size' => max(1, min(50, $pageSize)),
|
||||
]);
|
||||
}
|
||||
|
||||
public function payload(int $userId, array $params = []): array
|
||||
{
|
||||
return ['user_id' => $userId] + $params;
|
||||
}
|
||||
|
||||
public function endpoint(string $userUrl, string $action): string
|
||||
{
|
||||
if (!in_array($action, ['applicants', 'applications'], true)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$endpoint = preg_replace(
|
||||
'#/users/[^/?]+(?:\?.*)?$#i',
|
||||
'/customer_center/' . $action,
|
||||
trim($userUrl)
|
||||
);
|
||||
|
||||
return is_string($endpoint) && $endpoint !== $userUrl ? $endpoint : '';
|
||||
}
|
||||
|
||||
private function request(string $action, int $userId, array $params = []): array
|
||||
{
|
||||
$url = $this->endpoint(trim((string)config('app.user_url')), $action);
|
||||
if ($url === '') {
|
||||
return ['code' => 0, 'msg' => '客户中心接口地址异常', 'data' => []];
|
||||
}
|
||||
|
||||
$payload = $this->payload($userId, $params);
|
||||
$response = Httpcurl::request($url, 'post', $payload);
|
||||
if (!is_array($response) || !isset($response[0]) || !empty($response[3])) {
|
||||
Log::error('miniapi customer center ' . $action . ' request failed');
|
||||
return ['code' => 0, 'msg' => '服务异常,请稍后重试', 'data' => []];
|
||||
}
|
||||
|
||||
$result = json_decode($response[0], true);
|
||||
if (!is_array($result)) {
|
||||
Log::error('miniapi customer center ' . $action . ' response decode failed');
|
||||
return ['code' => 0, 'msg' => '服务异常,请稍后重试', 'data' => []];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,9 @@ Route::group('miniapi', function () {
|
||||
Route::post('order/pay', 'Order/pay');
|
||||
Route::post('order/addInvoice', 'Order/addInvoice');
|
||||
|
||||
Route::get('customer-center/applicants', 'CustomerCenter/applicants');
|
||||
Route::get('customer-center/applications', 'CustomerCenter/applications');
|
||||
|
||||
Route::get('ocr/signature', 'Ocr/signature');
|
||||
Route::post('upload/image', 'Upload/image');
|
||||
|
||||
|
||||
33
tests/customer_center_proxy_self_check.php
Normal file
33
tests/customer_center_proxy_self_check.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use app\miniapi\service\CustomerCenter\CustomerCenterService;
|
||||
|
||||
function customerCenterProxyAssertSame(mixed $expected, mixed $actual, string $message): void
|
||||
{
|
||||
if ($expected !== $actual) {
|
||||
throw new RuntimeException($message . PHP_EOL
|
||||
. 'Expected: ' . var_export($expected, true) . PHP_EOL
|
||||
. 'Actual: ' . var_export($actual, true));
|
||||
}
|
||||
}
|
||||
|
||||
$service = new CustomerCenterService();
|
||||
customerCenterProxyAssertSame([
|
||||
'user_id' => 42,
|
||||
'page' => 2,
|
||||
], $service->payload(42, ['page' => 2]), 'The proxy request must use the original unsigned parameter style');
|
||||
customerCenterProxyAssertSame(
|
||||
'https://uniuser.jzvisa.com/home/customer_center/applications',
|
||||
$service->endpoint('https://uniuser.jzvisa.com/home/users/userLogin', 'applications'),
|
||||
'The proxy endpoint must stay on the configured unified API host'
|
||||
);
|
||||
customerCenterProxyAssertSame(
|
||||
'',
|
||||
$service->endpoint('https://example.com/unexpected', 'applications'),
|
||||
'Unexpected user API URLs must fail closed'
|
||||
);
|
||||
|
||||
echo "Customer center proxy self-check passed\n";
|
||||
Reference in New Issue
Block a user