diff --git a/app/miniapi/controller/CustomerCenter.php b/app/miniapi/controller/CustomerCenter.php index d7962d5..d8f1f57 100644 --- a/app/miniapi/controller/CustomerCenter.php +++ b/app/miniapi/controller/CustomerCenter.php @@ -40,4 +40,57 @@ final class CustomerCenter extends Base return $this->ok((array)($result['data'] ?? [])); } + + public function applicant() + { + $userId = $this->requireLogin(); + if (!is_int($userId)) { + return $userId; + } + + $applicantId = (int)$this->request->param('applicant_id', 0); + if ($applicantId <= 0) { + return $this->fail('参数错误'); + } + $result = (new CustomerCenterService())->applicant($userId, $applicantId); + if ((int)($result['code'] ?? 0) !== 1) { + return $this->fail((string)($result['msg'] ?? '服务异常,请稍后重试')); + } + + return $this->ok((array)($result['data'] ?? [])); + } + + public function saveApplicant() + { + $userId = $this->requireLogin(); + if (!is_int($userId)) { + return $userId; + } + + $result = (new CustomerCenterService())->saveApplicant($userId, $this->postData()); + if ((int)($result['code'] ?? 0) !== 1) { + return $this->fail((string)($result['msg'] ?? '保存失败,请稍后重试')); + } + + return $this->ok((array)($result['data'] ?? []), '保存成功'); + } + + public function removeApplicant() + { + $userId = $this->requireLogin(); + if (!is_int($userId)) { + return $userId; + } + + $applicantId = (int)$this->request->param('applicant_id', 0); + if ($applicantId <= 0) { + return $this->fail('参数错误'); + } + $result = (new CustomerCenterService())->removeApplicant($userId, $applicantId); + if ((int)($result['code'] ?? 0) !== 1) { + return $this->fail((string)($result['msg'] ?? '移除失败,请稍后重试')); + } + + return $this->ok([], '已移除'); + } } diff --git a/app/miniapi/controller/Evus.php b/app/miniapi/controller/Evus.php index be1429b..0ec1d8c 100644 --- a/app/miniapi/controller/Evus.php +++ b/app/miniapi/controller/Evus.php @@ -5,8 +5,10 @@ namespace app\miniapi\controller; use api\Httpcurl; use app\miniapi\service\Context\MiniAppContext; +use app\miniapi\service\CustomerCenter\CustomerCenterService; use app\miniapi\service\Payment\MiniPayService; use app\miniapi\service\Product\ProductService; +use think\facade\Log; class Evus extends Base { @@ -74,6 +76,8 @@ class Evus extends Base return $this->fail($result['msg'] ?? '提交失败,请联系管理员'); } + $this->syncApplicantAfterOrderCreated($userId, $orderSn, $data); + if (!empty($data['openid'])) { $pay = $this->requestMiniPay($context, $orderSn, $totalPrice, $data['openid']); if ($pay['code'] !== 1) { @@ -119,7 +123,9 @@ class Evus extends Base $payload['passport_issue_place'], $payload['product_value'], $payload['product_name'], - $payload['product_desc'] + $payload['product_desc'], + $payload['customer_applicant_id'], + $payload['customer_profile_version_id'] ); $totalPrice = (string)$product['value']; $payload['user_id'] = $userId; @@ -159,4 +165,23 @@ class Evus extends Base { return (new MiniPayService())->request($context, $orderSn, $money, $openid, 'miniapi evus mini pay'); } + + private function syncApplicantAfterOrderCreated(int $userId, string $orderSn, array $data): void + { + $applicantId = (int)($data['customer_applicant_id'] ?? 0); + if ($applicantId <= 0) { + return; + } + + $result = (new CustomerCenterService())->syncEvusApplicant($userId, $applicantId, $orderSn, $data); + if ((int)($result['code'] ?? 0) !== 1) { + // The order already exists, so contact synchronization must never turn a successful order into a failure. + Log::warning(sprintf( + 'miniapi evus contact sync failed: applicant_id=%d order_sn=%s msg=%s', + $applicantId, + $orderSn, + (string)($result['msg'] ?? 'unknown') + )); + } + } } diff --git a/app/miniapi/service/CustomerCenter/CustomerCenterService.php b/app/miniapi/service/CustomerCenter/CustomerCenterService.php index dc70a87..9d0eae7 100644 --- a/app/miniapi/service/CustomerCenter/CustomerCenterService.php +++ b/app/miniapi/service/CustomerCenter/CustomerCenterService.php @@ -21,6 +21,37 @@ final class CustomerCenterService ]); } + public function applicant(int $userId, int $applicantId): array + { + return $this->request('applicant', $userId, ['applicant_id' => $applicantId]); + } + + public function saveApplicant(int $userId, array $params): array + { + return $this->request('saveApplicant', $userId, $params); + } + + public function removeApplicant(int $userId, int $applicantId): array + { + return $this->request('removeApplicant', $userId, ['applicant_id' => $applicantId]); + } + + public function syncEvusApplicant( + int $userId, + int $applicantId, + string $orderSn, + array $profile + ): array { + return $this->request('syncEvusApplicant', $userId, [ + 'applicant_id' => $applicantId, + 'order_sn' => $orderSn, + 'profile_json' => json_encode( + $profile, + JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE + ), + ], 5); + } + public function payload(int $userId, array $params = []): array { return ['user_id' => $userId] + $params; @@ -28,7 +59,14 @@ final class CustomerCenterService public function endpoint(string $userUrl, string $action): string { - if (!in_array($action, ['applicants', 'applications'], true)) { + if (!in_array($action, [ + 'applicants', + 'applications', + 'applicant', + 'saveApplicant', + 'removeApplicant', + 'syncEvusApplicant', + ], true)) { return ''; } @@ -43,7 +81,7 @@ final class CustomerCenterService return is_string($endpoint) && $endpoint !== $userUrl ? $endpoint : ''; } - private function request(string $action, int $userId, array $params = []): array + private function request(string $action, int $userId, array $params = [], int $timeout = 0): array { $url = $this->endpoint(trim((string)config('app.user_url')), $action); if ($url === '') { @@ -51,7 +89,7 @@ final class CustomerCenterService } $payload = $this->payload($userId, $params); - $response = Httpcurl::request($url, 'post', $payload); + $response = Httpcurl::request($url, 'post', $payload, [], $timeout); if (!is_array($response) || !isset($response[0]) || !empty($response[3])) { Log::error('miniapi customer center ' . $action . ' request failed'); return ['code' => 0, 'msg' => '服务异常,请稍后重试', 'data' => []]; diff --git a/route/app.php b/route/app.php index fb26de0..9e9a343 100644 --- a/route/app.php +++ b/route/app.php @@ -36,6 +36,9 @@ Route::group('miniapi', function () { Route::get('customer-center/applicants', 'CustomerCenter/applicants'); Route::get('customer-center/applications', 'CustomerCenter/applications'); + Route::post('customer-center/applicant', 'CustomerCenter/applicant'); + Route::post('customer-center/applicant/save', 'CustomerCenter/saveApplicant'); + Route::post('customer-center/applicant/remove', 'CustomerCenter/removeApplicant'); Route::get('ocr/signature', 'Ocr/signature'); Route::post('upload/image', 'Upload/image');