baseUrl = 'https://kp-open.keytop.cn/unite-api'; // $this->appid = '10749'; // $this->appsecret = 'f82b42c6f60246eba56ec4a77dfe7f9b'; // $this->parkId = '4603'; $this->appid = '13520'; $this->appsecret = 'dac06919e42c4974937ccb867792141a'; $this->parkId = '7818'; } /** * 停车场信息查询接口 * @return false|mixed */ public function getParkingLotInfo() { $url = $this->baseUrl . '/api/wec/GetParkingLotInfo'; $header[] = 'Content-Type: application/json;charset=UTF-8'; $header[] = 'version: 1.0.0'; $header[] = 'accept-language: zh-CN'; $params = [ 'appId' => $this->appid, 'parkId' => $this->parkId, 'serviceCode' => 'getParkingLotInfo', 'ts' => round(microtime(true) * 1000), 'reqId' => getBatchNo() ]; $params['key'] = $this->sign($params); $params = json_encode($params); try { $result = httpCurl($url, 'POST', $params, $header); $result = json_decode($result, true); if (empty($result['resCode'])) { return json_decode($result['data'], true); } else { return false; } } catch (\Exception $e) { return false; } } /** * 预约车位 */ public function appointment($model, $plateNo, &$out) { $url = $this->baseUrl . '/api/find/ReserveSpace'; $header[] = 'Content-Type: application/json;charset=UTF-8'; $header[] = 'version: 1.0.0'; $header[] = 'accept-language: zh-CN'; $params = [ 'appId' => $this->appid, 'parkId' => $this->parkId, 'serviceCode' => 'reserveSpace', 'ts' => round(microtime(true) * 1000), 'reqId' => getBatchNo(), 'plateNo' => $plateNo, 'type' => 1, 'addrId' => '4,6', 'enterTime' => date('Y-m-d 00:00:01', strtotime($model->start_time)), 'leaveTime' => date('Y-m-d 23:59:58', strtotime($model->end_time)), // 失效时间 'failureTime' => date('Y-m-d 23:59:59', strtotime($model->end_time)), 'flag' => 1, 'cardType' => 100, ]; $params['key'] = $this->sign($params); $params = json_encode($params); $result = []; $finally = $order_no = 0; $plate_status = 2; try { $resultJson = httpCurl($url, 'POST', $params, $header); $out = $resultJson; $result = json_decode($resultJson, true); if (empty($result['resCode'])) { $finally = 1; $order_no = json_decode($result['data'], true)['orderNo']; $plate_status = 1; return true; } else { return false; } } catch (\Exception $e) { $out = $e->getMessage(); return false; } finally { // 写日志 $thirdAppointmentLogModel = ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '用户车位预订'); // 写车牌记录信息 ThirdAppointmentLog::addPlate($thirdAppointmentLogModel, $plateNo, $plate_status, $order_no); } } /** * 预约车位(课程报名渠道) */ public function courseAppointment($course, $courseSigns, $plateNo, &$out) { $url = $this->baseUrl . '/api/find/ReserveSpace'; $header[] = 'Content-Type: application/json;charset=UTF-8'; $header[] = 'version: 1.0.0'; $header[] = 'accept-language: zh-CN'; $params = [ 'appId' => $this->appid, 'parkId' => $this->parkId, 'serviceCode' => 'reserveSpace', 'ts' => round(microtime(true) * 1000), 'reqId' => getBatchNo(), 'plateNo' => $plateNo, 'type' => 1, 'addrId' => '', 'enterTime' => $course->start_date . ' 00:00:00', 'leaveTime' => $course->end_date . ' 00:00:00', 'failureTime' => date('Y-m-d 59:59:59', strtotime($course->end_date)), 'flag' => 1, 'cardType' => 100, ]; $params['key'] = $this->sign($params); $params = json_encode($params); $result = []; $finally = $order_no = 0; $plate_status = 2; try { $resultJson = httpCurl($url, 'POST', $params, $header); $out = $resultJson; $result = json_decode($resultJson, true); if (empty($result['resCode'])) { $finally = 1; $order_no = json_decode($result['data'], true)['orderNo']; return true; } else { return false; } } catch (\Exception $e) { $out = $e->getMessage(); return false; } finally { // 写日志 $thirdAppointmentLogModel = ThirdAppointmentLog::add(0, $courseSigns->id, $courseSigns->user_id, 0, $url, $params, $result, $finally, '用户车位预订'); // 写车牌记录信息 ThirdAppointmentLog::addPlate($thirdAppointmentLogModel, $plateNo, $plate_status, $order_no); } } /** * 取消预约车位 */ public function cancelAppointment($appointment_id = 0, $course_sign_id = 0, $plateNo, &$out) { // 获取车位预约的订单号 $thirdPlateLog = ThirdAppointmentLog::where(function ($query) use ($appointment_id, $course_sign_id) { if ($appointment_id) { $query->where('appointment_id', $appointment_id); } if ($course_sign_id) { $query->where('course_sign_id', $course_sign_id); } })->where('plate', 'like', "%$plateNo%") ->where('plate_status', 1) ->first(); if (empty($thirdPlateLog)) { $out = '预约成功记录不存在'; return false; }; $url = $this->baseUrl . '/api/find/CancelReserveSpace'; $header[] = 'Content-Type: application/json;charset=UTF-8'; $header[] = 'version: 1.0.0'; $header[] = 'accept-language: zh-CN'; $params = [ 'appId' => $this->appid, 'parkId' => $this->parkId, 'serviceCode' => 'cancelReserveSpace', 'ts' => round(microtime(true) * 1000), 'reqId' => getBatchNo(), 'orderNo' => $thirdPlateLog->plate_order_no, ]; $params['key'] = $this->sign($params); $params = json_encode($params); $result = []; $finally = 0; try { $resultJson = httpCurl($url, 'POST', $params, $header); $out = $resultJson; $result = json_decode($resultJson, true); if (empty($result['resCode'])) { // 修改车牌预约状态 $thirdPlateLog->plate_status = 3; $thirdPlateLog->save(); $finally = 1; return true; } else { return false; } } catch (\Exception $e) { $out = $e->getMessage(); return false; } finally { // 写日志 ThirdAppointmentLog::add($appointment_id, $course_sign_id, 0, $url, $params, $result, $finally, '用户取消车位预订'); } } /** * 签名 */ public function sign($data) { // 过滤掉空值、null、空字符串及特定键(appId, appSercert) $filteredData = array_filter($data, function ($value) { return !is_null($value) && $value !== '' && !is_array($value) && !is_object($value); }, ARRAY_FILTER_USE_BOTH); // 移除特定键(appId, appSercert)如果存在的话 if (isset($filteredData['appId'])) { unset($filteredData['appId']); } if (isset($filteredData['appSercert'])) { unset($filteredData['appSercert']); } // 按ASCII码字典序排序属性名 uksort($filteredData, 'strcasecmp'); // 拼接成URL键值对格式的字符串 $stringA = ''; foreach ($filteredData as $key => $value) { $stringA .= $key . '=' . $value . '&'; } $stringA = rtrim($stringA, '&'); // 去除最后一个多余的& // 拼接上appSercert并计算MD5 $appSercert = $this->appsecret; // 假设'appSercert'始终存在 $stringSignTemp = $stringA . '&' . $appSercert; $signValue = strtoupper(md5($stringSignTemp)); // 转换为大写 return $signValue; } }