diff --git a/app/miniapi/controller/CustomerCenter.php b/app/miniapi/controller/CustomerCenter.php new file mode 100644 index 0000000..d7962d5 --- /dev/null +++ b/app/miniapi/controller/CustomerCenter.php @@ -0,0 +1,43 @@ +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'] ?? [])); + } +} diff --git a/app/miniapi/service/CustomerCenter/CustomerCenterService.php b/app/miniapi/service/CustomerCenter/CustomerCenterService.php new file mode 100644 index 0000000..f662049 --- /dev/null +++ b/app/miniapi/service/CustomerCenter/CustomerCenterService.php @@ -0,0 +1,66 @@ +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; + } +} diff --git a/route/app.php b/route/app.php index 3473273..d458a95 100644 --- a/route/app.php +++ b/route/app.php @@ -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'); diff --git a/tests/customer_center_proxy_self_check.php b/tests/customer_center_proxy_self_check.php new file mode 100644 index 0000000..2398188 --- /dev/null +++ b/tests/customer_center_proxy_self_check.php @@ -0,0 +1,33 @@ + 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";