SpyunServer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. if ($order->{'is_reserve'} == 1){
  165. $content = '<CB>预订单</CB><BR>';
  166. $content .= '<C>预约时间:' . $order->{'reserve_time'} . '</C><BR>';
  167. $content .= str_repeat('-', 32) . "<BR>";
  168. }
  169. //下单时间
  170. $content .= '下单时间:'.$order->{'created_at'}. '<BR>';
  171. $content .= '订单编号:'.$order->{'order_sn'}. '<BR>';
  172. $content .= str_repeat('-', 32) . "<BR>";
  173. //商品信息
  174. foreach ($orderItem as $value){
  175. $goods_name = $value->{'goods_name'} . '(' . $value->{'goods_attr'} . ')';
  176. $content .= '<H>'.$NoteFormatterServer::formatPrintOrderItem($goods_name, $value->{'goods_number'}, $value->{'goods_money'}). '</H><BR>';
  177. }
  178. // dd($content);
  179. $content .= '<B>共:' . $order->{'goods_number'} . '件</B><BR>';
  180. $content .= '<L1>' . '备注:'.$order->{'note'} . '</L1><BR>';
  181. $content .= str_repeat('-', 32) . '<BR>';
  182. $content .= '打包费:' . $order->{'pack_price'} . '<BR>';
  183. $content .= '就餐人数:' . $order->{'diner_number'} . '<BR>';
  184. $content .= '<H>' . '合计支付:' . $order->{'pay_money'} . '</H><BR>';
  185. $content .= '<CB>***结束***</CB><BR><BR>';
  186. return $content;
  187. }
  188. function getSpyunCancelOrderContent($order)
  189. {
  190. if ($order->{'order_type'} == 1){
  191. $str = '堂食';
  192. }else{
  193. $str = '打包';
  194. }
  195. $content = '<CB>#' . $order->{'number'} . $str.'</CB><BR>';
  196. $content .= str_repeat('-', 32) . "<BR>";
  197. $content .= '<CB>用户申请取消订单并退款,请处理</CB><BR>';
  198. //下单时间
  199. $content .= str_repeat('-', 32) . "<BR>";
  200. $content .= '下单时间:'.$order->{'created_at'}. '<BR>';
  201. $content .= '订单编号:'.$order->{'order_sn'}. '<BR>';
  202. $content .= '<BR>';
  203. $content .= '<CB>***结束***</CB><BR><BR>';
  204. return $content;
  205. }
  206. function getSpyunDemoContent()
  207. {
  208. $orderItem = [
  209. [
  210. 'goods_name' => '测试商品名称(规格名称)',
  211. 'goods_number' => '2',
  212. 'goods_price' => '9.9',
  213. ],
  214. [
  215. 'goods_name' => '测试商品名称(规格名称)',
  216. 'goods_number' => '3',
  217. 'goods_price' => '9.9',
  218. ],
  219. ];
  220. $NoteFormatterServer = NoteFormatterServer::creatServer();
  221. $content = '<CB>#1 平台名称</CB><BR>';
  222. $content .= '<C>店铺名称<C><BR>';
  223. $content .= str_repeat('-', 32) . "<BR>";
  224. $content .= '<CB>期望送达时间:' . date('Y-m-d H:i:s') . '</CB><BR>';
  225. //下单时间
  226. $content .= str_repeat('-', 32) . "<BR>";
  227. $content .= '下单时间:'.date('Y-m-d H:i:s'). '<BR>';
  228. $content .= '订单编号:123456789<BR>';
  229. $content .= str_repeat('-', 32) . "<BR>";
  230. //商品信息
  231. foreach ($orderItem as $value){
  232. $content .= '<H>'.$NoteFormatterServer::formatPrintOrderItem($value['goods_name'], $value['goods_number'], $value['goods_price']). '</H><BR>';
  233. }
  234. $content .= '<B>共:5件</B><BR>';
  235. $content .= '<L1>备注:无</L1><BR>';
  236. $content .= str_repeat('-', 32) . '<BR>';
  237. $content .= '打包费:5.00<BR>';
  238. $content .= '促销优惠:0.00<BR>';
  239. $content .= '<H>合计支付:54.5</H><BR>';
  240. $content .= str_repeat('-', 32) . '<BR>';
  241. $content .= '<L1>测试姓名<L1><BR>';
  242. $content .= '<L1>测试电话<L1><BR>';
  243. $content .= '<L1>测试地址地址地址地址<L1><BR>';
  244. $content .= '<QRCODE>123456789</QRCODE>';
  245. $content .= '<BR>';
  246. $content .= '<CB>***结束***</CB><BR><BR>';
  247. return $content;
  248. }
  249. /**
  250. * 修改打印机信息
  251. * @param $sn
  252. * @param string $name
  253. * @return array
  254. */
  255. public function updatePrinter($sn, $name)
  256. {
  257. return $this->http_request('PATCH','printer/update', $this->makeRequestParams([
  258. 'sn' => $sn,
  259. 'name' => $name
  260. ]));
  261. }
  262. /**
  263. * 修改打印机参数
  264. * @param $sn
  265. * @param null|int $auto_cut
  266. * @param null|string $voice
  267. * @return array
  268. */
  269. public function updatePrinterSetting($sn, $auto_cut = null, $voice = null)
  270. {
  271. return $this->http_request('PATCH','printer/setting', $this->makeRequestParams([
  272. 'sn' => $sn,
  273. 'auto_cut' => $auto_cut,
  274. 'voice' => $voice
  275. ]));
  276. }
  277. /**
  278. * 获取打印机信息
  279. * @param string $sn
  280. * @return array
  281. */
  282. public function getPrinter($sn)
  283. {
  284. return $this->http_request('GET','printer/info', $this->makeRequestParams([
  285. 'sn' => $sn
  286. ]));
  287. }
  288. /**
  289. * 清空待打印订单
  290. * @param int $sn
  291. * @return array
  292. */
  293. public function deletePrints($sn)
  294. {
  295. return $this->http_request('DELETE',"printer/cleansqs", $this->makeRequestParams([
  296. 'sn' => $sn
  297. ]));
  298. }
  299. /**
  300. * 查询打印订单状态
  301. * @param string $id
  302. * @return array
  303. */
  304. public function getPrintsStatus($id)
  305. {
  306. return $this->http_request('GET',"printer/order/status", $this->makeRequestParams([
  307. 'id' => $id
  308. ]));
  309. }
  310. /**
  311. * 查询打印机历史打印订单数
  312. * @param string $sn
  313. * @param string $date
  314. * @return array
  315. */
  316. public function getPrintsOrders($sn, $date)
  317. {
  318. return $this->http_request('GET',"printer/order/number", $this->makeRequestParams([
  319. 'sn' => $sn,
  320. 'date' => $date
  321. ]));
  322. }
  323. /**
  324. * 创建请求参数
  325. * @param array $params
  326. * @return array
  327. */
  328. public function makeRequestParams(array $params = [])
  329. {
  330. $params['appid'] = $this->appid;
  331. $params['timestamp'] = time();
  332. $params['sign'] = $this->makeSign($params);
  333. return $params;
  334. }
  335. /**
  336. * 创建签名
  337. * @param array $params
  338. * @return string
  339. */
  340. public function makeSign(array $params)
  341. {
  342. ksort($params);
  343. $sign = "";
  344. foreach ($params as $k => $v) {
  345. if ($k != "sign" && $k != "appsecret" && $v !== "" && !is_array($v) && !is_null($v)) {
  346. $sign .= $k . "=" . $v . "&";
  347. }
  348. }
  349. $sign = rtrim($sign, '&');
  350. $sign .= "&appsecret=" . $this->appsecret;
  351. return strtoupper(md5($sign));
  352. }
  353. protected function http_request($method, $uri, array $params = [])
  354. {
  355. $ch = curl_init();
  356. $url = self::BASE_URI . $uri;
  357. if ($method == 'GET' || $method == 'DELETE') {
  358. $url .= '?' . http_build_query($params);
  359. } else {
  360. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  361. }
  362. curl_setopt($ch, CURLOPT_URL, $url);
  363. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::CONNECTTIMEOUT);
  364. curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT);
  365. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  366. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  367. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  368. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  369. $response = curl_exec($ch);
  370. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  371. $http_error = curl_error($ch);
  372. $http_errno = curl_errno($ch);
  373. curl_close($ch);
  374. $response = json_decode($response, 1);
  375. if ($http_code != 200) {
  376. $this->errorMsg = $http_error;
  377. }
  378. return $response;
  379. }
  380. /**
  381. * 获取错误信息
  382. * @return string
  383. */
  384. function getErrorMsg()
  385. {
  386. return $this->errorMsg;
  387. }
  388. }