GpConfig.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @link:http://www.gdqijianshi.com/
  4. * @copyright: Copyright (c) 2020 广东七件事集团
  5. * Created by PhpStorm
  6. * Author: zal
  7. * Date: 2020-04-01
  8. * Time: 21:49
  9. */
  10. namespace common\helpers\printer\config;
  11. class GpConfig extends BaseConfig
  12. {
  13. public $memberCode;
  14. public $deviceNo;
  15. public $apiKey;
  16. public $time;
  17. public $orderNo;
  18. private $url = 'http://api.poscom.cn/apisc/sendMsg';
  19. public function print($content)
  20. {
  21. header("Content-Type: text/html;charset=utf-8");
  22. $reqTime = $this->getMillisecond();
  23. $securityCode = md5($this->memberCode . $this->deviceNo . $this->orderNo . $reqTime . $this->apiKey);
  24. $result = $this->requestPost($this->url, [
  25. 'charset' => 'UTF-8',
  26. 'reqTime' => $reqTime,
  27. 'memberCode' => $this->memberCode,
  28. 'deviceNo' => $this->deviceNo,
  29. 'securityCode' => $securityCode,
  30. 'msgDetail' => $content,
  31. 'msgNo' => $this->orderNo,
  32. 'mode' => 2,
  33. 'times' => $this->time,
  34. 'reprint' => 1
  35. ]);
  36. $result = json_decode($result, true);
  37. if ($result['code'] != 0) {
  38. throw new \Exception($result['msg'], $result, 1);
  39. }
  40. return $result;
  41. }
  42. private function requestPost($url = '', $post_data = array())
  43. {
  44. if (empty($url) || empty($post_data)) {
  45. return false;
  46. }
  47. $postUrl = $url;
  48. $curlPost = $post_data;
  49. $ch = curl_init();//初始化curl
  50. curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
  51. curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  53. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  54. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  55. $data = curl_exec($ch);//运行curl
  56. curl_close($ch);
  57. return $data;
  58. }
  59. private function getMillisecond()
  60. {
  61. list($t1, $t2) = explode(' ', microtime());
  62. return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
  63. }
  64. }