bool, 'authUrl' => string, 'error' => string] */ public function getAuthUrl($thirdUid, $cityId = '', $lat = '', $lng = '') { // 此接口不需要传token参数 $didi = new Didi(); // 校验:城市编码与经纬度二选一 if (empty($cityId) && (empty($lat) || empty($lng))) { return [ 'success' => false, 'error' => '参数错误:城市编码(cityId)与经纬度(lat,lng)必须二选一传递' ]; } $params = [ 'thirdUid' => (string)$thirdUid, ]; if ($cityId) { $params['cityId'] = (string)$cityId; } if ($lat) { $params['lat'] = (float)$lat; } if ($lng) { $params['lng'] = (float)$lng; } $payload = $didi->buildRequestPayload($params); $url = $didi->getApiUrl(self::API_AUTH_URL); $response = HttpClient::post($url, $payload); return $this->parseResponse($response); } /** * 取消授权 * * 用户不再使用滴滴平台发单,取消授权后token失效,后续再使用需要重新授权 * * @param string $accessToken 授权token * @return array ['success' => bool, 'result' => int, 'error' => string] */ public function cancelAuth($accessToken) { // 实例化 Didi 类传入 token,buildRequestPayload 会自动处理 token $didi = new Didi($accessToken); // 入参说明无业务参数 $payload = $didi->buildRequestPayload(self::API_CANCEL_AUTH, []); $url = $didi->getApiUrl(self::API_CANCEL_AUTH); $response = HttpClient::post($url, $payload); return $this->parseResponse($response); } /** * 解析 API 响应 * * @param array $response * @return array */ protected function parseResponse($response) { // 检查 HttpClient 返回的错误结构 if (isset($response['code']) && isset($response['body']) && !isset($response['errno'])) { return [ 'success' => false, 'error' => 'HTTP Request Failed: ' . ($response['error'] ?? $response['body']), 'code' => $response['code'] ]; } // 检查滴滴业务错误码 (errno) if (isset($response['errno'])) { if ($response['errno'] != 0) { return [ 'success' => false, 'error' => $response['errmsg'] ?? 'Unknown API error', 'errno' => $response['errno'] ]; } $data = $response['data'] ?? []; // 构造统一返回格式 $result = [ 'success' => true, 'data' => $data, ]; // 提取关键字段到顶层,方便调用 if (isset($data['authUrl'])) { $result['authUrl'] = $data['authUrl']; } if (isset($data['result'])) { $result['result'] = $data['result']; } return $result; } // 未知格式 return [ 'success' => false, 'error' => 'Invalid response format', 'raw' => $response ]; } }