SpyunServer.php 13 KB

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