init($type); } public function init($type) { $typeEnName = ThirdPartyEnum::getThirdPartyDesc($type, 'en'); $this->key = ConfigService::get($typeEnName, 'key', ''); $this->secret = ConfigService::get($typeEnName, 'secret', ''); $this->siid = ConfigService::get($typeEnName, 'siid', ''); } /** * @notes 打印电子面单 * @param $data * @throws \Exception * @author Tab * @date 2021/11/22 18:18 */ public function print($data) { if (empty($this->key) || empty($this->secret) || empty($this->siid)) { throw new \Exception('请先进行面单设置'); } if (! in_array($data['express']['code100'], [ 'jd', 'shunfeng' ]) && empty($data['template']['template_id'])) { throw new \Exception('请在模板中设置电子面单客户账号'); } if (! in_array($data['express']['code100'], [ 'jd', 'shunfeng' ]) && empty($data['template']['partner_key'])) { throw new \Exception('请在模板中设置电子面单密钥'); } $code = ExpressEnum::getkuaidi100code($data['express']['code100']); $param = array( 'printType' => 'CLOUD', 'partnerId' => $data['template']['partner_id'], 'partnerKey' => $data['template']['partner_key'], 'net' => $data['template']['net'] ?? $data['express']['name'], 'kuaidicom' => $data['express']['code100'], 'tempid' => $data['template']['template_id'], 'siid' => $this->siid, 'cargo' => '商品', 'code' => $code, //申通快递需要code 'weight' => $data['totalWeight'] ? $data['totalWeight'] : 1, 'count' => 1, 'payType' => DeliveryEnum::getKuaidi100Desc($data['template']['pay_type']), 'expType' => '标准快递', 'remark' => $data['remark'], 'recMan' => array( 'name' => $data['order']['address']->contact, 'mobile' => $data['order']['address']->mobile, 'printAddr' => RegionService::getAddress([ $data['order']['address']->province, $data['order']['address']->city, $data['order']['address']->district, ], $data['order']['address']->address) ), 'sendMan' => array( 'name' => $data['sender']['name'], 'mobile' => $data['sender']['mobile'], 'printAddr' => RegionService::getAddress([ $data['sender']['province_id'], $data['sender']['city_id'], $data['sender']['district_id'], ], $data['sender']['address']) ) ); // 当前时间戳 list($msec, $sec) = explode(' ', microtime()); $t = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); // 格式化参数 $postData = array(); $postData["param"] = json_encode($param, JSON_UNESCAPED_UNICODE); $postData["method"] = "order"; $postData["key"] = $this->key; $postData["t"] = $t; $sign = md5($postData["param"] . $t . $this->key . $this->secret); $postData["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($postData)); 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; } }