'https://freight.xiaojukeji.com', 'app_key' => '6724e08ff1ac4afdbf1c000cfe722449', 'app_id' => 'MDdovjIS', 'secret' => 'b61603625ebcf69a6364fdc30ae47944630c738c', ]; } else { $cfg = [ 'base_url' => 'http://pinzhi.didichuxing.com/kop_osim', 'app_key' => '4329d266f40144829ca5fd47a025e106', 'app_id' => 'MDdovjIS', 'secret' => 'b61603625ebcf69a6364fdc30ae47944630c738c', ]; } $this->baseUrl = $cfg['base_url']; $this->appKey = $cfg['app_key']; $this->appId = $cfg['app_id']; $this->appSecret = $cfg['secret']; $this->accessToken = $accessToken; $this->thirdUid = $thirdUid; } /** * 开通城市查询 * @param int|null $cityId 城市id(城市编码,经纬度二传一) 国标码 * @param float|null $longitude 经度 火星坐标系(高德) * @param float|null $latitude 纬度 火星坐标系(高德) * @return array */ public function queryCityInfo($cityId = null, $longitude = null, $latitude = null) { // 暂时仅支持快送业务查询,bizType 固定为 12 $businessParams = [ 'bizType' => 12, ]; if (!empty($cityId)) { $businessParams['cityId'] = (int)$cityId; } // 拉货有城市开城但部分区域不支持的情况,如不传经纬度则按照城市返回 if (!empty($longitude) && !empty($latitude)) { $businessParams['longitude'] = (float)$longitude; $businessParams['latitude'] = (float)$latitude; } $payload = $this->buildRequestPayload($businessParams); $url = $this->getApiUrl('freight.open.platform.channel.standard.queryCityInfo'); $headers = ["Content-Type" => "application/x-www-form-urlencoded"]; $resp =HttpClient::post($url, $payload, $headers); if($resp['code'] == 200 && isset($resp['result'])){ return $resp['result']; } return []; } /** * 查询钱包余额 * @return array */ public function walletBalance() { $payload = $this->buildRequestPayload([]); $url = $this->getApiUrl('freight.open.platform.channel.standard.walletBalance'); $headers = ["Content-Type" => "application/x-www-form-urlencoded"]; $resp = HttpClient::post($url, $payload, $headers); if ($resp['code'] == 200 && isset($resp['data']['balance'])) { return [ 'success' => true, 'balance' => round($resp['data']['balance'], 2), 'message' => $resp['msg'] ?? '查询余额成功', ]; } return [ 'success' => false, 'balance' => 0, 'message' => $resp['msg'] ?? '查询余额失败', ]; } /** * 构建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 */ public 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); } /** * 生成唯一的跑腿订单号 * @param $orderId * @return string */ public static function generateUniOroder($orderId, $timeStamp) { return $orderId . '-' . $timeStamp; } /** * 还原为数据库中的订单号 * @param $orderId * @return false|string */ public static function getSystemOrder($orderId) { $pos = strpos($orderId, '-'); if ($pos !== false) { // 确实找到了 $left = substr($orderId, 0, $pos); // a-b-c $right = substr($orderId, $pos + 1); // d } else { $left = $orderId; // 没有 - $right = ''; } return $left; } /** * 车型 * @param $number 车型编号 * @return string */ public function carType($number) { $map = [ //拉货车型 1001 => '小面包', 1002 => '中面包', 1003 => '小型平板', 1004 => '中型平板', 1005 => '4.2米平板', 1006 => '依维柯', 1007 => '4.2米厢货', 1008 => '5.2米货车', 1009 => '6.8米货车', 1010 => '7.6米货车', 1011 => '13米货车', 1012 => '微面', 1013 => '小厢货', 1014 => '中厢货', 1015 => '小高栏', 1016 => '4.2米高栏', 1019 => '9.6米货车', 1020 => '三轮车', 1022 => '集装箱卡车', 1023 => '微平板', 1024 => '微厢货', 1025 => '微高栏', 1026 => '中高栏', 1028 => '17.5米', 1029 => '16米', 1030 => '15米', 1031 => '13.5米', //快送车型 1017 => '小轿车', 1018 => '两轮车', ]; return isset($map[$number]) ? $map[$number] : $number; } }