user = isset($config["client_id"]) ? trim($config["client_id"]) : ""; $this->ukey = isset($config["client_secret"]) ? trim($config["client_secret"]) : ""; $this->sn = isset($config["sn"]) ? trim($config["sn"]) : ""; } /** * 查询芯烨云打印机状态 */ public function queryStatus() { $stime = time(); $sig = sha1($this->user . $this->ukey . $stime); $params = [ "user" => $this->user, "stime" => $stime, "sig" => $sig, "apiname" => "Open_queryPrinterStatus", "sn" => $this->sn ]; return $this->sendRequest($params); } /** * 打印订单小票 */ public function printOrder($orderData = [], $times = 1) { $stime = time(); $sig = sha1($this->user . $this->ukey . $stime); $content = $this->buildReceiptContent($orderData); $params = [ "user" => $this->user, "stime" => $stime, "sig" => $sig, "apiname" => "Open_printMsg", "sn" => $this->sn, "content" => $content, "times" => $times ]; return $this->sendRequest($params); } /** * 拼接排版小票格式 */ public function buildReceiptContent($order = []) { $title = isset($order["shop_name"]) ? $order["shop_name"] : "小众厨房设备商城"; $orderSn = isset($order["order_sn"]) ? $order["order_sn"] : "TEST" . date("YmdHis"); $createTime = isset($order["createtime"]) ? date("Y-m-d H:i:s", $order["createtime"]) : date("Y-m-d H:i:s"); $consignee = isset($order["consignee"]) ? $order["consignee"] : "测试用户"; $phone = isset($order["phone"]) ? $order["phone"] : "138****8888"; $address = isset($order["address"]) ? $order["address"] : "贵州省修文县城内"; $totalAmount = isset($order["total_fee"]) ? $order["total_fee"] : "0.01"; $info = "{$title}
"; $info .= "-- 订单小票 --
"; $info .= "--------------------------------
"; $info .= "订单编号:" . $orderSn . "
"; $info .= "下单时间:" . $createTime . "
"; $info .= "--------------------------------
"; $info .= "商品名称 数量 单价
"; if (!empty($order["items"]) && is_array($order["items"])) { foreach ($order["items"] as $item) { $info .= $item["name"] . " " . $item["num"] . " ¥" . $item["price"] . "
"; } } else { $info .= "测试商品-厨房专用设备 1 ¥" . $totalAmount . "
"; } $info .= "--------------------------------
"; $info .= "合计金额:¥" . $totalAmount . " 元
"; $info .= "客户姓名:" . $consignee . "
"; $info .= "联系电话:" . $phone . "
"; $info .= "送货地址:" . $address . "
"; $info .= "--------------------------------
"; $info .= "谢谢惠顾,欢迎再次光临!

"; return $info; } /** * 发送 cURL 请求给芯烨云 API 网关 */ protected function sendRequest($data) { $cookieFile = "/tmp/xprinter_cookie.txt"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); curl_setopt($ch, CURLOPT_HTTPHEADER, ["Cookie: _gp_pc=1"]); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $result = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if ($error) { return json_encode(["ret" => -1, "msg" => "Curl Error: " . $error]); } return $result; } }