'https://freight.xiaojukeji.com', 'app_key' => '6724e08ff1ac4afdbf1c000cfe722449', // TODO: 请填入正式环境的 appId 和 secret 'app_id' => '', 'secret' => '', ]; $this->isSandbox = false; } else { $cfg = [ 'base_url' => 'http://pinzhi.didichuxing.com/kop_osim', 'app_key' => '4329d266f40144829ca5fd47a025e106', // 请填入测试环境的 appId 和 secret 'app_id' => 'MDdovjIS', 'secret' => 'b61603625ebcf69a6364fdc30ae47944630c738c', ]; $this->isSandbox = true; } $this->baseUrl = $cfg['base_url']; $this->appKey = $cfg['app_key']; $this->appId = $cfg['app_id']; $this->appSecret = $cfg['secret']; $this->accessToken = $accessToken; } /** * 构建API请求参数 * * @param array $businessParams 业务参数 * @return array */ public function buildRequestPayload($businessParams = []) { // 1. 准备公共参数 $timestamp = (int)(microtime(true) * 1000); // 毫秒级时间戳 // 2. 处理业务参数 logisticsParam (JSON字符串) $logisticsParam = json_encode($businessParams, JSON_UNESCAPED_UNICODE); // 注意:根据文档,logisticsParam 是 json 字符串 // 3. 生成签名 $sign = $this->generateSignature($logisticsParam, $timestamp); // 4. 构建最终请求体 $payload = [ 'appId' => $this->appId, 'timestamp' => $timestamp, 'appSign' => $sign, 'logisticsParam' => $logisticsParam, ]; // Token 可选,如果存在则添加 if (!empty($this->accessToken)) { $payload['token'] = $this->accessToken; } // 默认自主模式 businessMode=0,如果有需要可扩展 // $payload['businessMode'] = 0; return $payload; } /** * 生成签名 * 签名规则:MD5(logisticsParam + secret + timestamp) * * @param string $logisticsParam json字符串 * @param int $timestamp 毫秒时间戳 * @return string */ protected function generateSignature($logisticsParam, $timestamp) { $str = $logisticsParam . $this->appSecret . $timestamp; return md5($str); } /** * 获取完整的API请求URL * * @param string $apiMethod API名称 * @return string */ public function getApiUrl($apiMethod) { // url格式:{host}/gateway?api=xxx&apiVersion=1.0.0&appKey=xxx $queryParams = [ 'api' => $apiMethod, 'apiVersion' => $this->apiVersion, 'appKey' => $this->appKey, ]; return $this->baseUrl . '/gateway?' . http_build_query($queryParams); } public function getIsSandbox() { return $this->isSandbox; } }