make_templet($content,$dev['prt_num']);//转换打印模板 $params = $this->params(); $params['machine_code'] = $dev['dev_id'];//打印机终端号 $params['origin_id'] = (string)time();//订单编号 $params['content'] = $content;//打印内容 $result = json_decode($this->http_post_json($params,'/print/index'),true); sleep(1);//延时1秒 if($result['error']!=0){ $this->error = $result['error_description']; return false; } return $result; } /** * 授权绑定打印机 */ public function add($data) { $params = $this->params(); $params['print_name'] = $data['dev_name'];//自定义打印机名称(可填) $params['machine_code'] = $data['dev_id'];//打印机终端号 $params['msign'] = $data['dev_key'];//打印机终端密钥 $result = json_decode($this->http_post_json($params,'/printer/addprinter'),true); if($result['error']!=0){ $this->error = $result['error_description']; return false; } return $result; } /** * 删除授权绑定的打印机 */ public function delete($dev_id) { $params = $this->params(); $params['machine_code'] = $dev_id;//打印机编号 $result = json_decode($this->http_post_json($params,'/printer/deleteprinter'),true); if($result['error']!=0){ $this->error = $result['error_description']; return false; } return $result; } /** * 获取打印机状态接口 */ public function status($dev_id) { $params = $this->params(); $params['machine_code'] = $dev_id;//打印机编号 $res = json_decode($this->http_post_json($params,'/printer/getprintstatus'),true); sleep(1);//延时1秒 if($res['error']==0){ $msg = '离线'; if($res['body']['state']==1){ $msg = '正常'; } if($res['body']['state']==2){ $msg = '缺纸'; } return $msg; } return '未知'; } /** * 公共参数 */ private function params(){ $time = time(); //请求时间 return $params = [ 'client_id' => $this->config['app_key'], 'access_token' => $this->getToken(), 'timestamp' => $time, 'sign' => $this->getSign($time), 'id' => $this->uuid4(), ]; } /** * 获取Token */ private function getToken() { $data = Cache::get('yilianyun_access_token',[]); $access_token = ''; $time = time(); $params = [ 'grant_type' => 'client_credentials', 'client_id' => $this->config['app_key'], 'timestamp' => $time, 'sign' => $this->getSign($time), 'id' => $this->uuid4(), 'scope' => 'all' ]; if(isset($data['access_token'])){ $access_token = $data['access_token']; //token即将过期 提前10天获取 $ext_time = $time + 10*24*60*60; if($data['expires_time'] < $ext_time){ $result = json_decode($this->http_post_json($params,'/oauth/oauth'),true); if($result['error']==0){ $data = $result['body']; $data['expires_time'] = $time + $result['body']['expires_in'];//过期时间戳 $access_token = $data['access_token']; Cache::set('yilianyun_access_token',$data,30*24*60*60);//有效期30天 } } }else{ //第一次请求,或者缓存过期 $result = json_decode($this->http_post_json($params,'/oauth/oauth'),true); if($result['error']==0){ $data = $result['body']; $data['expires_time'] = $time + $result['body']['expires_in'];//过期时间戳 $access_token = $data['access_token']; Cache::set('yilianyun_access_token',$data,30*24*60*60);//有效期30天 } } return $access_token; } /** * 生成签名 */ private function getSign($timestamp) { return md5($this->config['app_key'] . $timestamp . $this->config['app_secret']); } /** * 生成uuid4 */ private function uuid4(){ mt_srand((double)microtime() * 10000); $charid = strtolower(md5(uniqid(rand(), true))); $hyphen = '-'; $uuidV4 = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12); return $uuidV4; } //发送post请求 /** * PHP发送Json对象数据 * @return string */ private function http_post_json($data,$url) { $url = $this->config['api_url'] . $url; $data = http_build_query($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' )); // 解决数据包大不能提交 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循 curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $response; } /** * 制作订单模板 * $data 订单数据 * $n 打印份数 * 58mm的机器,一行打印16个汉字,32个字母; */ private function make_templet($data,$n) { $content = ''.$n.''; foreach ($data as $item){ $row_arr = explode(',',$item); $row = $row_arr[0]; for($m=1;$m','','','','']; $b_8 = ['','','','']; $h_8 = ['
','
','','']; if(in_array($row_arr[$m],$b_4)){ $row = "" . $row . "";//加粗 }elseif(in_array($row_arr[$m],$b_8)){ $row = "" . $row . "";//加粗字体放大一倍 }elseif(in_array($row_arr[$m],$h_8)){ $row = "" . $row . ""; } //居中 if($row_arr[$m] == ''){ $row = "
" . $row . "
"; } } $content .= $row . "\n"; } $content .= "\n\n"; return $content; } }