| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace app\utils;
- use app\models\Printer;
- use Exception;
- class XhyPrint
- {
- protected int $debug = 0;
- protected string $host = 'https://open.xpyun.net';
- public string $app_id = '';
- public string $app_key = '';
- protected array $error = [
- '-1' => '请求头错误',
- '-2' => '参数不合法',
- '-3' => '参数签名失败',
- '-4' => '用户未注册',
- '1001' => '打印机编号和用户不匹配',
- '1002' => '打印机未注册',
- '1003' => '打印机不在线',
- '1004' => '添加订单失败',
- '1005' => '未找到订单信息',
- '1006' => '订单日期格式或大小不正确',
- '1007' => '打印内容不能超过12K',
- '1008' => '用户修改打印机记录失败',
- '1009' => '用户添加打印机时,打印机编号或名称不能为空',
- '1010' => '打印机设备编号无效',
- '1011' => '打印机已存在',
- '1012' => '添加打印设备失败',
- '1013' => '打印订单时触发幂等性',
- '1014' => '幂等因子过长',
- '1015' => '',
- '1016' => 'LOGO文件格式错误',
- '1017' => 'LOGO文件超出规定范围',
- '1018' => 'LOGO上传次数超限制',
- '1019' => '',
- '1020' => 'LOGO删除失败',
- '1021' => 'LOGO上传模式错误',
- '1022' => '该设备属于定制设备,您当前无权使用',
- ];
- protected function getError($code): string
- {
- return $this->error[$code] ?? '';
- }
- public function __construct($printer_id = 0)
- {
- $printerModel = Printer::findOne($printer_id);
- if ($printerModel) {
- $printer_setting = json_decode($printerModel->printer_setting, true);
- if ($printer_setting) {
- $this->app_id = $printer_setting['app_id'];
- $this->app_key = $printer_setting['app_key'];
- }
- }
- }
- /**
- * @throws Exception
- */
- protected function request($url, $data)
- {
- $timestamp = (string)time();
- $default = [
- 'user' => $this->app_id,
- 'timestamp' => $timestamp,
- 'sign' => sha1($this->app_id . $this->app_key . $timestamp),
- 'debug' => $this->debug,
- ];
- $data = array_merge($default, $data);
- $jsonStr = json_encode($data);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);// 发送一个常规的Post请求
- curl_setopt($ch, CURLOPT_URL, $url);// 要访问的地址
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
- curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json;charset=UTF-8',
- 'Content-Length: ' . strlen($jsonStr)
- )
- );
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- if (curl_errno($ch)) {
- throw new Exception(curl_error($ch));
- }
- curl_close($ch);
- if ($httpCode !== 200) {
- throw new Exception($httpCode);
- }
- $result = json_decode($response, true);
- if ($result['code'] !== 0) {
- $err_msg = $this->getError($result['code']);
- throw new Exception($err_msg === '' ? $result['msg'] : $err_msg);
- }
- return $result['data'];
- }
- /**
- * 添加打印设备
- * @param string $sn
- * @param string $name
- *
- * @return boolean
- * @throws Exception
- */
- public function add_printer(string $sn, string $name): bool
- {
- if (!$sn || !$name) {
- throw new Exception('缺少必要参数');
- }
- $url = $this->host . '/api/openapi/xprinter/addPrinters';
- $result = $this->request($url, [
- 'items' => [[
- 'sn' => $sn,
- 'name' => $name,
- ]]
- ]);
- $open = false;
- if (isset($result['failMsg']) && $result['failMsg']) {
- $error = '';
- foreach ($result['failMsg'] as $fail) {
- $fail_arr = explode(':', $fail);
- $open = true;
- if ($fail_arr[1] == 1011) {//打印机已经存在
- $open = false;
- }
- $error .= $this->getError($fail_arr[1]);
- }
- if (!$open) {
- return true;
- }
- throw new Exception($error);
- }
- return true;
- }
- /**
- * 更新打印设备信息
- *
- * @return boolean
- * @throws Exception
- */
- public function update_printer(string $sn, string $name): bool
- {
- if (!$sn || !$name) {
- throw new Exception('缺少必要参数');
- }
- $url = $this->host . '/api/openapi/xprinter/updPrinter';
- return $this->request($url, [
- 'sn' => $sn,
- 'name' => $name,
- ]);
- }
- /**
- * 删除打印设备
- * @param string $sn
- *
- * @return mixed
- * @throws Exception
- */
- public function delete_printer(string $sn)
- {
- $url = $this->host . '/api/openapi/xprinter/delPrinters';
- $result = $this->request($url, [
- 'snlist' => [$sn]
- ]);
- if (isset($result['failMsg']) && $result['failMsg']) {
- $error = '';
- foreach ($result['failMsg'] as $fail) {
- $fail_arr = explode(':', $fail);
- $error .= '删除失败:' . $this->getError($fail_arr[1]);
- }
- throw new Exception($error);
- }
- return $result;
- }
- /**
- * 设置声音
- * @param $sn string SN码
- * @param $voice_type int 声音类型
- * @param $level int 声音级别
- *
- * @return boolean
- * @throws Exception
- */
- public function set_voice_type(string $sn, int $voice_type, int $level): bool
- {
- $url = $this->host . '/api/openapi/xprinter/setVoiceType';
- if (!$sn) {
- throw new Exception('SN码错误');
- }
- if (!$voice_type || !in_array($voice_type, [0, 1, 2, 3, 4])) {
- throw new Exception('声音类型错误');
- }
- if (!$level || !in_array($level, [0, 1, 2, 3])) {
- throw new Exception('声音大小错误');
- }
- return $this->request($url, [
- 'sn' => $sn,
- 'voiceType' => (int)$voice_type,
- 'volumeLevel' => (int)$level,
- ]);
- }
- /**
- * 批量查询打印设备状态
- * @param string $sn
- *
- * @return mixed
- * @throws Exception
- */
- public function get_status(string $sn)
- {
- $url = $this->host . '/api/openapi/xprinter/queryPrintersStatus';
- return $this->request($url, [
- 'snlist' => [$sn]
- ])[0];
- }
- /**
- * 打印内容
- * @param string $sn SN码
- * @param string $content 内容
- * @param int $copies 份数
- * @param int $voice 声音播放模式,0 为取消订单模式,1 为静音模式,2 为来单播放模式,3 为有用户申请退单了,默认为 2 来单播放模式
- * @param int $mode 打印模式:0=不在线不打印;1=不在线加入打印队列
- * @param int $expiresIn 缓存有效期(mode=1时生效):0=系统默认,取值范围:0 < expiresIn < 86400
- *
- * @return mixed
- * @throws Exception
- * */
- public function print(string $sn, string $content, int $copies = 1, int $voice = 2, int $mode = 0, int $expiresIn = 0)
- {
- $url = $this->host . '/api/openapi/xprinter/print';
- return $this->request($url, [
- 'sn' => $sn,
- 'content' => $content,
- 'copies' => $copies,
- 'voice' => $voice,
- 'mode' => $mode,
- 'expiresIn' => $expiresIn,
- ]);
- }
- }
|