appid = 'sp69ae68cc360fc'; $this->appsecret = 'e13c6938c997f19e9e4ab0a34aa4fccd'; } /** * 创建对象 * @return SpyunServer */ static function creatServer() { if (empty(self::$server)) { self::$server = new SpyunServer(); } return self::$server; } /** * 添加打印机 * @param $sn * @param $pkey * @param $name * @return mixed */ public function addPrinter($sn, $pkey, $name) { $res = $this->http_request('POST','printer/add', $this->makeRequestParams([ 'sn' => $sn, 'pkey' => $pkey, 'name' => $name, ])); if (empty($res) && !isset($res['errorcode'])){ return false; } if ($res['errorcode'] != 0){ $this->errorMsg = $res['errormsg']; ErrorRecord::create(['msg' => $sn . '尚鹏云打印机绑定失败', 'data' => json_encode($res)]); return false; } return true; } /** * 删除打印机 * @param string $sn * @return bool */ public function deletePrinter($sn) { $res = $this->http_request('DELETE','printer/delete', $this->makeRequestParams([ 'sn' => $sn, ])); if (empty($res) && !isset($res['errorcode'])){ return false; } if ($res['errorcode'] != 0){ $this->errorMsg = $res['errormsg']; return false; } return true; } /** * 打印订单 * @param int $sn * @param array $order * @param int $times * @return bool */ public function prints($is_test,$sn, $order, $times = 1) { if ($is_test){ $content = $this->getSpyunDemoContent(); }else{ $content = $this->getSpyunContent($order); } $res = $this->http_request('POST',"printer/print", $this->makeRequestParams([ 'sn' => $sn, 'content' => $content, 'times' => $times ])); if (empty($res) && !isset($res['errorcode'])){ return false; } if ($res['errorcode'] != 0){ $this->errorMsg = $res['errormsg']; ErrorRecord::create(['msg' => $sn . '尚鹏云打印机打印失败', 'data' => $res['errormsg']]); return false; } return $res['id']; } /** * 用户取消订单打印 * @param $sn * @param $order * @param $times * @return false|mixed */ function cancelOrderPrints($sn, $order, $times = 1) { $content = $this->getSpyunCancelOrderContent($order); $res = $this->http_request('POST',"printer/print", $this->makeRequestParams([ 'sn' => $sn, 'content' => $content, 'times' => $times ])); if (empty($res) && !isset($res['errorcode'])){ return false; } if ($res['errorcode'] != 0){ $this->errorMsg = $res['errormsg']; ErrorRecord::create(['msg' => $sn . '尚鹏云打印机打印失败', 'data' => $res['errormsg']]); return false; } return $res['id']; } /** * @param $order_id * @return string */ function getSpyunContent($order) { $orderItem = $order->{'item'}; if ($order->{'order_type'} == 1){ $str = '堂食'; }else{ $str = '打包'; } $NoteFormatterServer = NoteFormatterServer::creatServer(); $content = '#' . $order->{'number'} .$str.'
'; $content .= '桌号:' . $order->{'table_number'} . '
'; $content .= str_repeat('-', 32) . "
"; //下单时间 $content .= '下单时间:'.$order->{'created_at'}. '
'; $content .= '订单编号:'.$order->{'order_sn'}. '
'; $content .= str_repeat('-', 32) . "
"; //商品信息 foreach ($orderItem as $value){ $goods_name = $value->{'goods_name'} . '(' . $value->{'goods_attr'} . ')'; $content .= ''.$NoteFormatterServer::formatPrintOrderItem($goods_name, $value->{'goods_number'}, $value->{'goods_money'}). '
'; } // dd($content); $content .= '共:' . $order->{'goods_number'} . '件
'; $content .= '' . '备注:'.$order->{'note'} . '
'; $content .= str_repeat('-', 32) . '
'; $content .= '打包费:' . $order->{'pack_price'} . '
'; $content .= '就餐人数:' . $order->{'diner_number'} . '
'; $content .= '' . '合计支付:' . $order->{'pay_money'} . '
'; $content .= '***结束***

'; return $content; } function getSpyunCancelOrderContent($order) { if ($order->{'order_type'} == 1){ $str = '堂食'; }else{ $str = '打包'; } $content = '#' . $order->{'number'} . $str.'
'; $content .= str_repeat('-', 32) . "
"; $content .= '用户申请取消订单并退款,请处理
'; //下单时间 $content .= str_repeat('-', 32) . "
"; $content .= '下单时间:'.$order->{'created_at'}. '
'; $content .= '订单编号:'.$order->{'order_sn'}. '
'; $content .= '
'; $content .= '***结束***

'; return $content; } function getSpyunDemoContent() { $orderItem = [ [ 'goods_name' => '测试商品名称(规格名称)', 'goods_number' => '2', 'goods_price' => '9.9', ], [ 'goods_name' => '测试商品名称(规格名称)', 'goods_number' => '3', 'goods_price' => '9.9', ], ]; $NoteFormatterServer = NoteFormatterServer::creatServer(); $content = '#1 平台名称
'; $content .= '店铺名称
'; $content .= str_repeat('-', 32) . "
"; $content .= '期望送达时间:' . date('Y-m-d H:i:s') . '
'; //下单时间 $content .= str_repeat('-', 32) . "
"; $content .= '下单时间:'.date('Y-m-d H:i:s'). '
'; $content .= '订单编号:123456789
'; $content .= str_repeat('-', 32) . "
"; //商品信息 foreach ($orderItem as $value){ $content .= ''.$NoteFormatterServer::formatPrintOrderItem($value['goods_name'], $value['goods_number'], $value['goods_price']). '
'; } $content .= '共:5件
'; $content .= '备注:无
'; $content .= str_repeat('-', 32) . '
'; $content .= '打包费:5.00
'; $content .= '促销优惠:0.00
'; $content .= '合计支付:54.5
'; $content .= str_repeat('-', 32) . '
'; $content .= '测试姓名
'; $content .= '测试电话
'; $content .= '测试地址地址地址地址
'; $content .= '123456789'; $content .= '
'; $content .= '***结束***

'; return $content; } /** * 修改打印机信息 * @param $sn * @param string $name * @return array */ public function updatePrinter($sn, $name) { return $this->http_request('PATCH','printer/update', $this->makeRequestParams([ 'sn' => $sn, 'name' => $name ])); } /** * 修改打印机参数 * @param $sn * @param null|int $auto_cut * @param null|string $voice * @return array */ public function updatePrinterSetting($sn, $auto_cut = null, $voice = null) { return $this->http_request('PATCH','printer/setting', $this->makeRequestParams([ 'sn' => $sn, 'auto_cut' => $auto_cut, 'voice' => $voice ])); } /** * 获取打印机信息 * @param string $sn * @return array */ public function getPrinter($sn) { return $this->http_request('GET','printer/info', $this->makeRequestParams([ 'sn' => $sn ])); } /** * 清空待打印订单 * @param int $sn * @return array */ public function deletePrints($sn) { return $this->http_request('DELETE',"printer/cleansqs", $this->makeRequestParams([ 'sn' => $sn ])); } /** * 查询打印订单状态 * @param string $id * @return array */ public function getPrintsStatus($id) { return $this->http_request('GET',"printer/order/status", $this->makeRequestParams([ 'id' => $id ])); } /** * 查询打印机历史打印订单数 * @param string $sn * @param string $date * @return array */ public function getPrintsOrders($sn, $date) { return $this->http_request('GET',"printer/order/number", $this->makeRequestParams([ 'sn' => $sn, 'date' => $date ])); } /** * 创建请求参数 * @param array $params * @return array */ public function makeRequestParams(array $params = []) { $params['appid'] = $this->appid; $params['timestamp'] = time(); $params['sign'] = $this->makeSign($params); return $params; } /** * 创建签名 * @param array $params * @return string */ public function makeSign(array $params) { ksort($params); $sign = ""; foreach ($params as $k => $v) { if ($k != "sign" && $k != "appsecret" && $v !== "" && !is_array($v) && !is_null($v)) { $sign .= $k . "=" . $v . "&"; } } $sign = rtrim($sign, '&'); $sign .= "&appsecret=" . $this->appsecret; return strtoupper(md5($sign)); } protected function http_request($method, $uri, array $params = []) { $ch = curl_init(); $url = self::BASE_URI . $uri; if ($method == 'GET' || $method == 'DELETE') { $url .= '?' . http_build_query($params); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::CONNECTTIMEOUT); curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $http_error = curl_error($ch); $http_errno = curl_errno($ch); curl_close($ch); $response = json_decode($response, 1); if ($http_code != 200) { $this->errorMsg = $http_error; } return $response; } /** * 获取错误信息 * @return string */ function getErrorMsg() { return $this->errorMsg; } }