key = 'HJLXEoGk2758'; // $this->secret = '52b09a0f84dc43029b3cd1a9897ad895'; // $this->siid = 'KX100L2104180314'; $kd100 = [ 'kd100_key' => ConfigServer::get('kd100', 'kd100_key'), 'kd100_secret' => ConfigServer::get('kd100', 'kd100_secret'), 'kd100_siid' => ConfigServer::get('kd100', 'kd100_siid'), ]; $this->key = $kd100['kd100_key'] ?? ''; $this->secret = $kd100['kd100_secret'] ?? ''; $this->siid = $kd100['kd100_siid'] ?? ''; } /** * @notes 打印面单 * @param $data * @return mixed * @throws Exception * @author 张无忌 * @date 2021/9/11 14:16 */ public function print($data) { try { if (!$this->key) { throw new Exception('请设置快递100的授权key'); } if (!$this->secret) { throw new Exception('请设置快递100的授权secret'); } if (!$this->siid) { throw new Exception('请设置打印机设备编码'); } if ($data['template']['expressName'] != '顺丰快递') { if (!$data['template']['partnerKey']) { throw new Exception('请设置电子面单客户账号partnerId'); } if (!$data['template']['partnerKey']) { throw new Exception('请设置电子面单客户密钥partnerKey'); } } $code = Express::getkuaidi100code($data['template']['expressCode']); // 请求参数 $param = array( 'printType' => 'CLOUD', 'partnerId' => $data['template']['partnerId'], 'partnerKey' => $data['template']['partnerKey'], 'net' => $data['template']['net'] ?? $data['template']['expressName'], 'kuaidicom' => $data['template']['expressCode'], 'tempid' => $data['template']['tempid'], 'siid' => $this->siid, 'cargo' => '商品', 'code' => $code, //申通快递需要code 'weight' => $data['total_weight'] ? $data['total_weight'] : 1, 'count' => 1, 'payType' => $data['template']['payType'], 'expType' => '标准快递', 'remark' => $data['remark'], 'recMan' => array( 'name' => $data['recMan']['name'], 'mobile' => $data['recMan']['mobile'], 'printAddr' => $data['recMan']['printAddr'], ), 'sendMan' => array( 'name' => $data['sendMan']['name'], 'mobile' => $data['sendMan']['mobile'], 'printAddr' => $data['sendMan']['printAddr'], ) ); // 当前时间戳 list($msec, $sec) = explode(' ', microtime()); $t = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); // 格式化参数 $post_data = array(); $post_data["param"] = json_encode($param, JSON_UNESCAPED_UNICODE); $post_data["key"] = $this->key; $post_data["t"] = $t; $sign = md5($post_data["param"] . $t . $this->key . $this->secret); $post_data["sign"] = strtoupper($sign); // 发送请求 // $url = 'http://poll.kuaidi100.com/printapi/printtask.do?method=eOrder'; //V2接口 $url = 'https://api.kuaidi100.com/label/order?method=order'; $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = json_decode(curl_exec($ch), true); if (!$result || $result['code'] != '200') { throw new \Exception($result['message'] ?? '打印电子面单异常,原因未知'); } return $result; } catch (\Exception $e) { throw new Exception($e->getMessage()); } } }