SpyunServer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. namespace App\Servers\printer;
  3. use App\Models\ErrorRecord;
  4. /**
  5. * 商鹏云打印机
  6. */
  7. class SpyunServer
  8. {
  9. static private $server = '';
  10. /**
  11. * 基本请求地址
  12. */
  13. const BASE_URI = 'https://open.spyun.net/v1/';
  14. /**
  15. * 连接超时时间
  16. */
  17. const CONNECTTIMEOUT = 1;
  18. /**
  19. * 超时时间
  20. */
  21. const TIMEOUT = 4;
  22. /**
  23. * @var string
  24. */
  25. protected $appid;
  26. /**
  27. * @var $appsecret
  28. */
  29. protected $appsecret;
  30. /**
  31. * @var string
  32. */
  33. protected $errorMsg='';
  34. private function __construct()
  35. {
  36. $this->appid = 'sp69ae68cc360fc';
  37. $this->appsecret = 'e13c6938c997f19e9e4ab0a34aa4fccd';
  38. }
  39. /**
  40. * 创建对象
  41. * @return SpyunServer
  42. */
  43. static function creatServer()
  44. {
  45. if (empty(self::$server)) {
  46. self::$server = new SpyunServer();
  47. }
  48. return self::$server;
  49. }
  50. /**
  51. * 添加打印机
  52. * @param $sn
  53. * @param $pkey
  54. * @param $name
  55. * @return mixed
  56. */
  57. public function addPrinter($sn, $pkey, $name)
  58. {
  59. $res = $this->http_request('POST','printer/add', $this->makeRequestParams([
  60. 'sn' => $sn,
  61. 'pkey' => $pkey,
  62. 'name' => $name,
  63. ]));
  64. if (empty($res) && !isset($res['errorcode'])){
  65. return false;
  66. }
  67. if ($res['errorcode'] != 0){
  68. $this->errorMsg = $res['errormsg'];
  69. ErrorRecord::create(['msg' => $sn . '尚鹏云打印机绑定失败', 'data' => json_encode($res)]);
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * 删除打印机
  76. * @param string $sn
  77. * @return bool
  78. */
  79. public function deletePrinter($sn)
  80. {
  81. $res = $this->http_request('DELETE','printer/delete', $this->makeRequestParams([
  82. 'sn' => $sn,
  83. ]));
  84. if (empty($res) && !isset($res['errorcode'])){
  85. return false;
  86. }
  87. if ($res['errorcode'] != 0){
  88. $this->errorMsg = $res['errormsg'];
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * 打印订单
  95. * @param int $sn
  96. * @param array $order
  97. * @param int $times
  98. * @return bool
  99. */
  100. public function prints($is_test,$sn, $order, $times = 1)
  101. {
  102. if ($is_test){
  103. $content = $this->getSpyunDemoContent();
  104. }else{
  105. $content = $this->getSpyunContent($order);
  106. }
  107. $res = $this->http_request('POST',"printer/print", $this->makeRequestParams([
  108. 'sn' => $sn,
  109. 'content' => $content,
  110. 'times' => $times
  111. ]));
  112. if (empty($res) && !isset($res['errorcode'])){
  113. return false;
  114. }
  115. if ($res['errorcode'] != 0){
  116. $this->errorMsg = $res['errormsg'];
  117. ErrorRecord::create(['msg' => $sn . '尚鹏云打印机打印失败', 'data' => $res['errormsg']]);
  118. return false;
  119. }
  120. return $res['id'];
  121. }
  122. /**
  123. * 用户取消订单打印
  124. * @param $sn
  125. * @param $order
  126. * @param $times
  127. * @return false|mixed
  128. */
  129. function cancelOrderPrints($sn, $order, $times = 1)
  130. {
  131. $content = $this->getSpyunCancelOrderContent($order);
  132. $res = $this->http_request('POST',"printer/print", $this->makeRequestParams([
  133. 'sn' => $sn,
  134. 'content' => $content,
  135. 'times' => $times
  136. ]));
  137. if (empty($res) && !isset($res['errorcode'])){
  138. return false;
  139. }
  140. if ($res['errorcode'] != 0){
  141. $this->errorMsg = $res['errormsg'];
  142. ErrorRecord::create(['msg' => $sn . '尚鹏云打印机打印失败', 'data' => $res['errormsg']]);
  143. return false;
  144. }
  145. return $res['id'];
  146. }
  147. /**
  148. * @param $order_id
  149. * @return string
  150. */
  151. function getSpyunContent($order)
  152. {
  153. $orderItem = $order->{'item'};
  154. if ($order->{'order_type'} == 1){
  155. $str = '堂食';
  156. }else{
  157. $str = '打包';
  158. }
  159. $NoteFormatterServer = NoteFormatterServer::creatServer();
  160. $content = '<CB>#' . $order->{'number'} .$str.'</CB><BR>';
  161. $content .= '<C>桌号:' . $order->{'table_number'} . '</C><BR>';
  162. $content .= str_repeat('-', 32) . "<BR>";
  163. //下单时间
  164. $content .= '下单时间:'.$order->{'created_at'}. '<BR>';
  165. $content .= '订单编号:'.$order->{'order_sn'}. '<BR>';
  166. $content .= str_repeat('-', 32) . "<BR>";
  167. //商品信息
  168. foreach ($orderItem as $value){
  169. $goods_name = $value->{'goods_name'} . '(' . $value->{'goods_attr'} . ')';
  170. $content .= '<H>'.$NoteFormatterServer::formatPrintOrderItem($goods_name, $value->{'goods_number'}, $value->{'goods_money'}). '</H><BR>';
  171. }
  172. // dd($content);
  173. $content .= '<B>共:' . $order->{'goods_number'} . '件</B><BR>';
  174. $content .= '<L1>' . '备注:'.$order->{'note'} . '</L1><BR>';
  175. $content .= str_repeat('-', 32) . '<BR>';
  176. $content .= '打包费:' . $order->{'pack_price'} . '<BR>';
  177. $content .= '就餐人数:' . $order->{'diner_number'} . '<BR>';
  178. $content .= '<H>' . '合计支付:' . $order->{'pay_money'} . '</H><BR>';
  179. $content .= '<CB>***结束***</CB><BR><BR>';
  180. return $content;
  181. }
  182. function getSpyunCancelOrderContent($order)
  183. {
  184. if ($order->{'order_type'} == 1){
  185. $str = '堂食';
  186. }else{
  187. $str = '打包';
  188. }
  189. $content = '<CB>#' . $order->{'number'} . $str.'</CB><BR>';
  190. $content .= str_repeat('-', 32) . "<BR>";
  191. $content .= '<CB>用户申请取消订单并退款,请处理</CB><BR>';
  192. //下单时间
  193. $content .= str_repeat('-', 32) . "<BR>";
  194. $content .= '下单时间:'.$order->{'created_at'}. '<BR>';
  195. $content .= '订单编号:'.$order->{'order_sn'}. '<BR>';
  196. $content .= '<BR>';
  197. $content .= '<CB>***结束***</CB><BR><BR>';
  198. return $content;
  199. }
  200. function getSpyunDemoContent()
  201. {
  202. $orderItem = [
  203. [
  204. 'goods_name' => '测试商品名称(规格名称)',
  205. 'goods_number' => '2',
  206. 'goods_price' => '9.9',
  207. ],
  208. [
  209. 'goods_name' => '测试商品名称(规格名称)',
  210. 'goods_number' => '3',
  211. 'goods_price' => '9.9',
  212. ],
  213. ];
  214. $NoteFormatterServer = NoteFormatterServer::creatServer();
  215. $content = '<CB>#1 平台名称</CB><BR>';
  216. $content .= '<C>店铺名称<C><BR>';
  217. $content .= str_repeat('-', 32) . "<BR>";
  218. $content .= '<CB>期望送达时间:' . date('Y-m-d H:i:s') . '</CB><BR>';
  219. //下单时间
  220. $content .= str_repeat('-', 32) . "<BR>";
  221. $content .= '下单时间:'.date('Y-m-d H:i:s'). '<BR>';
  222. $content .= '订单编号:123456789<BR>';
  223. $content .= str_repeat('-', 32) . "<BR>";
  224. //商品信息
  225. foreach ($orderItem as $value){
  226. $content .= '<H>'.$NoteFormatterServer::formatPrintOrderItem($value['goods_name'], $value['goods_number'], $value['goods_price']). '</H><BR>';
  227. }
  228. $content .= '<B>共:5件</B><BR>';
  229. $content .= '<L1>备注:无</L1><BR>';
  230. $content .= str_repeat('-', 32) . '<BR>';
  231. $content .= '打包费:5.00<BR>';
  232. $content .= '促销优惠:0.00<BR>';
  233. $content .= '<H>合计支付:54.5</H><BR>';
  234. $content .= str_repeat('-', 32) . '<BR>';
  235. $content .= '<L1>测试姓名<L1><BR>';
  236. $content .= '<L1>测试电话<L1><BR>';
  237. $content .= '<L1>测试地址地址地址地址<L1><BR>';
  238. $content .= '<QRCODE>123456789</QRCODE>';
  239. $content .= '<BR>';
  240. $content .= '<CB>***结束***</CB><BR><BR>';
  241. return $content;
  242. }
  243. /**
  244. * 修改打印机信息
  245. * @param $sn
  246. * @param string $name
  247. * @return array
  248. */
  249. public function updatePrinter($sn, $name)
  250. {
  251. return $this->http_request('PATCH','printer/update', $this->makeRequestParams([
  252. 'sn' => $sn,
  253. 'name' => $name
  254. ]));
  255. }
  256. /**
  257. * 修改打印机参数
  258. * @param $sn
  259. * @param null|int $auto_cut
  260. * @param null|string $voice
  261. * @return array
  262. */
  263. public function updatePrinterSetting($sn, $auto_cut = null, $voice = null)
  264. {
  265. return $this->http_request('PATCH','printer/setting', $this->makeRequestParams([
  266. 'sn' => $sn,
  267. 'auto_cut' => $auto_cut,
  268. 'voice' => $voice
  269. ]));
  270. }
  271. /**
  272. * 获取打印机信息
  273. * @param string $sn
  274. * @return array
  275. */
  276. public function getPrinter($sn)
  277. {
  278. return $this->http_request('GET','printer/info', $this->makeRequestParams([
  279. 'sn' => $sn
  280. ]));
  281. }
  282. /**
  283. * 清空待打印订单
  284. * @param int $sn
  285. * @return array
  286. */
  287. public function deletePrints($sn)
  288. {
  289. return $this->http_request('DELETE',"printer/cleansqs", $this->makeRequestParams([
  290. 'sn' => $sn
  291. ]));
  292. }
  293. /**
  294. * 查询打印订单状态
  295. * @param string $id
  296. * @return array
  297. */
  298. public function getPrintsStatus($id)
  299. {
  300. return $this->http_request('GET',"printer/order/status", $this->makeRequestParams([
  301. 'id' => $id
  302. ]));
  303. }
  304. /**
  305. * 查询打印机历史打印订单数
  306. * @param string $sn
  307. * @param string $date
  308. * @return array
  309. */
  310. public function getPrintsOrders($sn, $date)
  311. {
  312. return $this->http_request('GET',"printer/order/number", $this->makeRequestParams([
  313. 'sn' => $sn,
  314. 'date' => $date
  315. ]));
  316. }
  317. /**
  318. * 创建请求参数
  319. * @param array $params
  320. * @return array
  321. */
  322. public function makeRequestParams(array $params = [])
  323. {
  324. $params['appid'] = $this->appid;
  325. $params['timestamp'] = time();
  326. $params['sign'] = $this->makeSign($params);
  327. return $params;
  328. }
  329. /**
  330. * 创建签名
  331. * @param array $params
  332. * @return string
  333. */
  334. public function makeSign(array $params)
  335. {
  336. ksort($params);
  337. $sign = "";
  338. foreach ($params as $k => $v) {
  339. if ($k != "sign" && $k != "appsecret" && $v !== "" && !is_array($v) && !is_null($v)) {
  340. $sign .= $k . "=" . $v . "&";
  341. }
  342. }
  343. $sign = rtrim($sign, '&');
  344. $sign .= "&appsecret=" . $this->appsecret;
  345. return strtoupper(md5($sign));
  346. }
  347. protected function http_request($method, $uri, array $params = [])
  348. {
  349. $ch = curl_init();
  350. $url = self::BASE_URI . $uri;
  351. if ($method == 'GET' || $method == 'DELETE') {
  352. $url .= '?' . http_build_query($params);
  353. } else {
  354. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  355. }
  356. curl_setopt($ch, CURLOPT_URL, $url);
  357. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::CONNECTTIMEOUT);
  358. curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT);
  359. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  360. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  361. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  362. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  363. $response = curl_exec($ch);
  364. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  365. $http_error = curl_error($ch);
  366. $http_errno = curl_errno($ch);
  367. curl_close($ch);
  368. $response = json_decode($response, 1);
  369. if ($http_code != 200) {
  370. $this->errorMsg = $http_error;
  371. }
  372. return $response;
  373. }
  374. /**
  375. * 获取错误信息
  376. * @return string
  377. */
  378. function getErrorMsg()
  379. {
  380. return $this->errorMsg;
  381. }
  382. }