| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- namespace App\Servers\printer;
- use App\Models\ErrorRecord;
- /**
- * 商鹏云打印机
- */
- class SpyunServer
- {
- static private $server = '';
- /**
- * 基本请求地址
- */
- const BASE_URI = 'https://open.spyun.net/v1/';
- /**
- * 连接超时时间
- */
- const CONNECTTIMEOUT = 1;
- /**
- * 超时时间
- */
- const TIMEOUT = 4;
- /**
- * @var string
- */
- protected $appid;
- /**
- * @var $appsecret
- */
- protected $appsecret;
- /**
- * @var string
- */
- protected $errorMsg='';
- private function __construct()
- {
- $this->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 = '<CB>#' . $order->{'number'} .$str.'</CB><BR>';
- $content .= '<C>桌号:' . $order->{'table_number'} . '</C><BR>';
- $content .= str_repeat('-', 32) . "<BR>";
- //下单时间
- $content .= '下单时间:'.$order->{'created_at'}. '<BR>';
- $content .= '订单编号:'.$order->{'order_sn'}. '<BR>';
- $content .= str_repeat('-', 32) . "<BR>";
- //商品信息
- foreach ($orderItem as $value){
- $goods_name = $value->{'goods_name'} . '(' . $value->{'goods_attr'} . ')';
- $content .= '<H>'.$NoteFormatterServer::formatPrintOrderItem($goods_name, $value->{'goods_number'}, $value->{'goods_money'}). '</H><BR>';
- }
- // dd($content);
- $content .= '<B>共:' . $order->{'goods_number'} . '件</B><BR>';
- $content .= '<L1>' . '备注:'.$order->{'note'} . '</L1><BR>';
- $content .= str_repeat('-', 32) . '<BR>';
- $content .= '打包费:' . $order->{'pack_price'} . '<BR>';
- $content .= '就餐人数:' . $order->{'diner_number'} . '<BR>';
- $content .= '<H>' . '合计支付:' . $order->{'pay_money'} . '</H><BR>';
- $content .= '<CB>***结束***</CB><BR><BR>';
- return $content;
- }
- function getSpyunCancelOrderContent($order)
- {
- if ($order->{'order_type'} == 1){
- $str = '堂食';
- }else{
- $str = '打包';
- }
- $content = '<CB>#' . $order->{'number'} . $str.'</CB><BR>';
- $content .= str_repeat('-', 32) . "<BR>";
- $content .= '<CB>用户申请取消订单并退款,请处理</CB><BR>';
- //下单时间
- $content .= str_repeat('-', 32) . "<BR>";
- $content .= '下单时间:'.$order->{'created_at'}. '<BR>';
- $content .= '订单编号:'.$order->{'order_sn'}. '<BR>';
- $content .= '<BR>';
- $content .= '<CB>***结束***</CB><BR><BR>';
- 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 = '<CB>#1 平台名称</CB><BR>';
- $content .= '<C>店铺名称<C><BR>';
- $content .= str_repeat('-', 32) . "<BR>";
- $content .= '<CB>期望送达时间:' . date('Y-m-d H:i:s') . '</CB><BR>';
- //下单时间
- $content .= str_repeat('-', 32) . "<BR>";
- $content .= '下单时间:'.date('Y-m-d H:i:s'). '<BR>';
- $content .= '订单编号:123456789<BR>';
- $content .= str_repeat('-', 32) . "<BR>";
- //商品信息
- foreach ($orderItem as $value){
- $content .= '<H>'.$NoteFormatterServer::formatPrintOrderItem($value['goods_name'], $value['goods_number'], $value['goods_price']). '</H><BR>';
- }
- $content .= '<B>共:5件</B><BR>';
- $content .= '<L1>备注:无</L1><BR>';
- $content .= str_repeat('-', 32) . '<BR>';
- $content .= '打包费:5.00<BR>';
- $content .= '促销优惠:0.00<BR>';
- $content .= '<H>合计支付:54.5</H><BR>';
- $content .= str_repeat('-', 32) . '<BR>';
- $content .= '<L1>测试姓名<L1><BR>';
- $content .= '<L1>测试电话<L1><BR>';
- $content .= '<L1>测试地址地址地址地址<L1><BR>';
- $content .= '<QRCODE>123456789</QRCODE>';
- $content .= '<BR>';
- $content .= '<CB>***结束***</CB><BR><BR>';
- 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;
- }
- }
|