初始化代码

This commit is contained in:
2025-12-22 14:33:31 +08:00
parent d02b31a8b9
commit c2c5ae2fdd
2313 changed files with 467239 additions and 0 deletions

View File

@@ -0,0 +1,263 @@
<?php
namespace longbingcore\printer;
use longbingcore\printer\HttpClient;
header("Content-type: text/html; charset=utf-8");
define('USER', 'xxxxxxxxx'); //*必填*:飞鹅云后台注册账号
define('UKEY', 'xxxxxxxxxxxx'); //*必填*: 飞鹅云注册账号后生成的UKEY
define('SN', 'xxxxxxxxx'); //*必填*打印机编号必须要在管理后台里添加打印机或调用API接口添加之后才能调用API
//以下参数不需要修改
define('IP','api.feieyun.cn'); //接口IP或域名
define('PORT',80); //接口IP端口
define('PATH','/Api/Open/'); //接口路径
define('STIME', time()); //公共参数,请求时间
define('SIG', sha1(USER.UKEY.STIME)); //公共参数,请求公钥
//===========添加打印机接口(支持批量)=============
//***接口返回值说明***
//正确例子:{"msg":"ok","ret":0,"data":{"ok":["sn#key#remark#carnum","316500011#abcdefgh#快餐前台"],"no":["316500012#abcdefgh#快餐前台#13688889999 (错误:识别码不正确)"]},"serverExecutedTime":3}
//错误:{"msg":"参数错误 : 该帐号未注册.","ret":-2,"data":null,"serverExecutedTime":37}
//打开注释可测试
//提示:打印机编号(必填) # 打印机识别码(必填) # 备注名称(选填) # 流量卡号码(选填),多台打印机请换行(\n添加新打印机信息每次最多100行(台)。
//$snlist = "sn1#key1#remark1#carnum1\nsn2#key2#remark2#carnum2";
//addprinter($snlist);
//==================方法1.打印订单==================
//***接口返回值说明***
//正确例子:{"msg":"ok","ret":0,"data":"316500004_20160823165104_1853029628","serverExecutedTime":6}
//错误:{"msg":"错误信息.","ret":非零错误码,"data":null,"serverExecutedTime":5}
//标签说明:
//单标签:
//"<BR>"为换行,"<CUT>"为切刀指令(主动切纸,仅限切刀打印机使用才有效果)
//"<LOGO>"为打印LOGO指令(前提是预先在机器内置LOGO图片),"<PLUGIN>"为钱箱或者外置音响指令
//成对标签:
//"<CB></CB>"为居中放大一倍,"<B></B>"为放大一倍,"<C></C>"为居中,<L></L>字体变高一倍
//<W></W>字体变宽一倍,"<QR></QR>"为二维码,"<BOLD></BOLD>"为字体加粗,"<RIGHT></RIGHT>"为右对齐
//拼凑订单内容时可参考如下格式
//根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
$orderInfo = '<CB>测试打印</CB><BR>';
$orderInfo .= '名称      单价 数量 金额<BR>';
$orderInfo .= '--------------------------------<BR>';
$orderInfo .= '饭       10.0 10 10.0<BR>';
$orderInfo .= '炒饭      10.0 10 10.0<BR>';
$orderInfo .= '蛋炒饭     10.0 100 100.0<BR>';
$orderInfo .= '鸡蛋炒饭    100.0 100 100.0<BR>';
$orderInfo .= '西红柿炒饭   1000.0 1 100.0<BR>';
$orderInfo .= '西红柿蛋炒饭  100.0 100 100.0<BR>';
$orderInfo .= '西红柿鸡蛋炒饭 15.0 1 15.0<BR>';
$orderInfo .= '备注:加辣<BR>';
$orderInfo .= '--------------------------------<BR>';
$orderInfo .= '合计xx.0元<BR>';
$orderInfo .= '送货地点广州市南沙区xx路xx号<BR>';
$orderInfo .= '联系电话13888888888888<BR>';
$orderInfo .= '订餐时间2014-08-08 08:08:08<BR>';
$orderInfo .= '<QR>http://www.dzist.com</QR>';//把二维码字符串用标签套上即可自动生成二维码
//打开注释可测试
wp_print(SN,$orderInfo,2);
//===========方法2.查询某订单是否打印成功=============
//***接口返回值说明***
//正确例子:
//已打印:{"msg":"ok","ret":0,"data":true,"serverExecutedTime":6}
//未打印:{"msg":"ok","ret":0,"data":false,"serverExecutedTime":6}
//打开注释可测试
//$orderid = "xxxxxxxx_xxxxxxxxxx_xxxxxxxx";//订单ID从方法1返回值中获取
//queryOrderState($orderid);
//===========方法3.查询指定打印机某天的订单详情============
//***接口返回值说明***
//正确例子:{"msg":"ok","ret":0,"data":{"print":6,"waiting":1},"serverExecutedTime":9}
//打开注释可测试
//$date = "2017-04-02";//注意时间格式为"yyyy-MM-dd",如2016-08-27
//queryOrderInfoByDate(SN,$date);
//===========方法4.查询打印机的状态==========================
//***接口返回值说明***
//正确例子:
//{"msg":"ok","ret":0,"data":"离线","serverExecutedTime":9}
//{"msg":"ok","ret":0,"data":"在线,工作状态正常","serverExecutedTime":9}
//{"msg":"ok","ret":0,"data":"在线,工作状态不正常","serverExecutedTime":9}
//打开注释可测试
//queryPrinterStatus(SN);
function addprinter($snlist){
$content = array(
'user'=>USER,
'stime'=>STIME,
'sig'=>SIG,
'apiname'=>'Open_printerAddlist',
'printerContent'=>$snlist
);
$client = new HttpClient(IP,PORT);
if(!$client->post(PATH,$content)){
echo 'error';
}
else{
echo $client->getContent();
}
}
/*
* 方法1
拼凑订单内容时可参考如下格式
根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
*/
function wp_print($printer_sn,$orderInfo,$times){
$content = array(
'user'=>USER,
'stime'=>STIME,
'sig'=>SIG,
'apiname'=>'Open_printMsg',
'sn'=>$printer_sn,
'content'=>$orderInfo,
'times'=>$times//打印次数
);
$client = new HttpClient(IP,PORT);
if(!$client->post(PATH,$content)){
echo 'error';
}
else{
//服务器返回的JSON字符串建议要当做日志记录起来
echo $client->getContent();
}
}
/*
* 方法2
根据订单索引,去查询订单是否打印成功,订单索引由方法1返回
*/
function queryOrderState($index){
$msgInfo = array(
'user'=>USER,
'stime'=>STIME,
'sig'=>SIG,
'apiname'=>'Open_queryOrderState',
'orderid'=>$index
);
$client = new HttpClient(IP,PORT);
if(!$client->post(PATH,$msgInfo)){
echo 'error';
}
else{
$result = $client->getContent();
echo $result;
}
}
/*
* 方法3
查询指定打印机某天的订单详情
*/
function queryOrderInfoByDate($printer_sn,$date){
$msgInfo = array(
'user'=>USER,
'stime'=>STIME,
'sig'=>SIG,
'apiname'=>'Open_queryOrderInfoByDate',
'sn'=>$printer_sn,
'date'=>$date
);
$client = new HttpClient(IP,PORT);
if(!$client->post(PATH,$msgInfo)){
echo 'error';
}
else{
$result = $client->getContent();
echo $result;
}
}
/*
* 方法4
查询打印机的状态
*/
function queryPrinterStatus($printer_sn){
$msgInfo = array(
'user'=>USER,
'stime'=>STIME,
'sig'=>SIG,
'apiname'=>'Open_queryPrinterStatus',
'sn'=>$printer_sn
);
$client = new HttpClient(IP,PORT);
if(!$client->post(PATH,$msgInfo)){
echo 'error';
}
else{
$result = $client->getContent();
echo $result;
}
}
?>

View File

@@ -0,0 +1,309 @@
<?php
namespace longbingcore\printer;
class HttpClient {
// Request vars
var $host;
var $port;
var $path;
var $method;
var $postdata = '';
var $cookies = array();
var $referer;
var $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
var $accept_encoding = 'gzip';
var $accept_language = 'en-us';
var $user_agent = 'Incutio HttpClient v0.9';
var $timeout = 20;
var $use_gzip = true;
var $persist_cookies = true;
var $persist_referers = true;
var $debug = false;
var $handle_redirects = true;
var $max_redirects = 5;
var $headers_only = false;
var $username;
var $password;
var $status;
var $headers = array();
var $content = '';
var $errormsg;
var $redirect_count = 0;
var $cookie_host = '';
function __construct($host, $port=80) {
$this->host = $host;
$this->port = $port;
}
function get($path, $data = false) {
$this->path = $path;
$this->method = 'GET';
if ($data) {
$this->path .= '?'.$this->buildQueryString($data);
}
return $this->doRequest();
}
function post($path, $data) {
$this->path = $path;
$this->method = 'POST';
$this->postdata = $this->buildQueryString($data);
return $this->doRequest();
}
function buildQueryString($data) {
$querystring = '';
if (is_array($data)) {
foreach ($data as $key => $val) {
if (is_array($val)) {
foreach ($val as $val2) {
$querystring .= urlencode($key).'='.urlencode($val2).'&';
}
} else {
$querystring .= urlencode($key).'='.urlencode($val).'&';
}
}
$querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
} else {
$querystring = $data;
}
return $querystring;
}
function doRequest() {
if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) {
switch($errno) {
case -3:
$this->errormsg = 'Socket creation failed (-3)';
case -4:
$this->errormsg = 'DNS lookup failure (-4)';
case -5:
$this->errormsg = 'Connection refused or timed out (-5)';
default:
$this->errormsg = 'Connection failed ('.$errno.')';
$this->errormsg .= ' '.$errstr;
$this->debug($this->errormsg);
}
return false;
}
socket_set_timeout($fp, $this->timeout);
$request = $this->buildRequest();
$this->debug('Request', $request);
fwrite($fp, $request);
$this->headers = array();
$this->content = '';
$this->errormsg = '';
$inHeaders = true;
$atStart = true;
while (!feof($fp)) {
$line = fgets($fp, 4096);
if ($atStart) {
$atStart = false;
if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
$this->errormsg = "Status code line invalid: ".htmlentities($line);
$this->debug($this->errormsg);
return false;
}
$http_version = $m[1];
$this->status = $m[2];
$status_string = $m[3];
$this->debug(trim($line));
continue;
}
if ($inHeaders) {
if (trim($line) == '') {
$inHeaders = false;
$this->debug('Received Headers', $this->headers);
if ($this->headers_only) {
break;
}
continue;
}
if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
continue;
}
$key = strtolower(trim($m[1]));
$val = trim($m[2]);
if (isset($this->headers[$key])) {
if (is_array($this->headers[$key])) {
$this->headers[$key][] = $val;
} else {
$this->headers[$key] = array($this->headers[$key], $val);
}
} else {
$this->headers[$key] = $val;
}
continue;
}
$this->content .= $line;
}
fclose($fp);
if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') {
$this->debug('Content is gzip encoded, unzipping it');
$this->content = substr($this->content, 10);
$this->content = gzinflate($this->content);
}
if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) {
$cookies = $this->headers['set-cookie'];
if (!is_array($cookies)) {
$cookies = array($cookies);
}
foreach ($cookies as $cookie) {
if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) {
$this->cookies[$m[1]] = $m[2];
}
}
$this->cookie_host = $this->host;
}
if ($this->persist_referers) {
$this->debug('Persisting referer: '.$this->getRequestURL());
$this->referer = $this->getRequestURL();
}
if ($this->handle_redirects) {
if (++$this->redirect_count >= $this->max_redirects) {
$this->errormsg = 'Number of redirects exceeded maximum ('.$this->max_redirects.')';
$this->debug($this->errormsg);
$this->redirect_count = 0;
return false;
}
$location = isset($this->headers['location']) ? $this->headers['location'] : '';
$uri = isset($this->headers['uri']) ? $this->headers['uri'] : '';
if ($location || $uri) {
$url = parse_url($location.$uri);
return $this->get($url['path']);
}
}
return true;
}
function buildRequest() {
$headers = array();
$headers[] = "{$this->method} {$this->path} HTTP/1.0";
$headers[] = "Host: {$this->host}";
$headers[] = "User-Agent: {$this->user_agent}";
$headers[] = "Accept: {$this->accept}";
if ($this->use_gzip) {
$headers[] = "Accept-encoding: {$this->accept_encoding}";
}
$headers[] = "Accept-language: {$this->accept_language}";
if ($this->referer) {
$headers[] = "Referer: {$this->referer}";
}
if ($this->cookies) {
$cookie = 'Cookie: ';
foreach ($this->cookies as $key => $value) {
$cookie .= "$key=$value; ";
}
$headers[] = $cookie;
}
if ($this->username && $this->password) {
$headers[] = 'Authorization: BASIC '.base64_encode($this->username.':'.$this->password);
}
if ($this->postdata) {
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Content-Length: '.strlen($this->postdata);
}
$request = implode("\r\n", $headers)."\r\n\r\n".$this->postdata;
return $request;
}
function getStatus() {
return $this->status;
}
function getContent() {
return $this->content;
}
function getHeaders() {
return $this->headers;
}
function getHeader($header) {
$header = strtolower($header);
if (isset($this->headers[$header])) {
return $this->headers[$header];
} else {
return false;
}
}
function getError() {
return $this->errormsg;
}
function getCookies() {
return $this->cookies;
}
function getRequestURL() {
$url = 'http://'.$this->host;
if ($this->port != 80) {
$url .= ':'.$this->port;
}
$url .= $this->path;
return $url;
}
function setUserAgent($string) {
$this->user_agent = $string;
}
function setAuthorization($username, $password) {
$this->username = $username;
$this->password = $password;
}
function setCookies($array) {
$this->cookies = $array;
}
function useGzip($boolean) {
$this->use_gzip = $boolean;
}
function setPersistCookies($boolean) {
$this->persist_cookies = $boolean;
}
function setPersistReferers($boolean) {
$this->persist_referers = $boolean;
}
function setHandleRedirects($boolean) {
$this->handle_redirects = $boolean;
}
function setMaxRedirects($num) {
$this->max_redirects = $num;
}
function setHeadersOnly($boolean) {
$this->headers_only = $boolean;
}
function setDebug($boolean) {
$this->debug = $boolean;
}
function quickGet($url) {
$bits = parse_url($url);
$host = $bits['host'];
$port = isset($bits['port']) ? $bits['port'] : 80;
$path = isset($bits['path']) ? $bits['path'] : '/';
if (isset($bits['query'])) {
$path .= '?'.$bits['query'];
}
$client = new HttpClient($host, $port);
if (!$client->get($path)) {
return false;
} else {
return $client->getContent();
}
}
function quickPost($url, $data) {
$bits = parse_url($url);
$host = $bits['host'];
$port = isset($bits['port']) ? $bits['port'] : 80;
$path = isset($bits['path']) ? $bits['path'] : '/';
$client = new HttpClient($host, $port);
if (!$client->post($path, $data)) {
return false;
} else {
return $client->getContent();
}
}
function debug($msg, $object = false) {
if ($this->debug) {
print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;"><strong>HttpClient Debug:</strong> '.$msg;
if ($object) {
ob_start();
print_r($object);
$content = htmlentities(ob_get_contents());
ob_end_clean();
print '<pre>'.$content.'</pre>';
}
print '</div>';
}
}
}
?>

View File

@@ -0,0 +1,167 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: luofei614 <weibo.com/luofei614>
// +----------------------------------------------------------------------
// | 修改者: anuo (本权限类在原3.2.3的基础上修改过来的)
// +----------------------------------------------------------------------
namespace longbingcore\printer;
class Printer
{
/*
* 飞鹅 打印/检查打印机状态
* */
public static function FeiePrintMsg($config , $orderInfo , $apiname = 'Open_printMsg',$time=1)
{
$content = array(
'user' => $config['user'],
'stime' => $_SERVER['REQUEST_TIME'],
'sig' => sha1($config['user'] . $config['ukey'] . $_SERVER['REQUEST_TIME']),
'apiname' => $apiname,
'sn' => $config['sn'],
'content' => $orderInfo,
'times' => $time
);
return self::getStream($content);
}
/*
* @param array $index 订单ID
* 根据订单索引,去查询订单是否打印成功
*
* */
public static function FeieQueryOrderState($config , $index){
$msgInfo = array(
'user' => $config['user'],
'stime' => $_SERVER['REQUEST_TIME'],
'sig' => sha1($config['user'] . $config['ukey'] . $_SERVER['REQUEST_TIME']),
'apiname'=>'Open_queryOrderState',
'orderid'=>$index
);
return self::getStream($msgInfo);
}
/**
* Request stream.
* @param array $msgInfo
* @return array
*/
protected static function getStream($msgInfo)
{
$client = new HttpClient('api.feieyun.cn',80);
if(!$client->post('/Api/Open/',$msgInfo))
return ['code' => false];
else
return json_decode($client->getContent(),true);
}
// 飞鹅 over
// 易联云
/**
* 打印接口
* @param int $partner 用户ID
* @param string $machine_code 打印机终端号
* @param string $content 打印内容
* @param string $apiKey API密钥
* @param string $msign 打印机密钥
*/
public static function action_print($partner,$machine_code,$content,$apiKey,$msign)
{
$param = array(
"partner"=>$partner,
'machine_code'=>$machine_code,
'time'=>time(),
);
//获取签名
$param['sign'] = self::generateSign($param,$apiKey,$msign);
$param['content'] = $content;
$str = self::getStr($param);
return json_decode(self::sendCmd('http://open.10ss.net:8888',$str) , true);
}
/**
* 生成签名sign
* @param array $params 参数
* @param string $apiKey API密钥
* @param string $msign 打印机密钥
* @return string sign
*/
protected static function generateSign($params, $apiKey,$msign)
{
//所有请求参数按照字母先后顺序排
ksort($params);
//定义字符串开始所包括的字符串
$stringToBeSigned = $apiKey;
//把所有参数名和参数值串在一起
foreach ($params as $k => $v)
{
$stringToBeSigned .= urldecode($k.$v);
}
unset($k, $v);
//定义字符串结尾所包括的字符串
$stringToBeSigned .= $msign;
//使用MD5进行加密再转化成大写
return strtoupper(md5($stringToBeSigned));
}
/**
* 生成字符串参数
* @param array $param 参数
* @return string 参数字符串
*/
protected static function getStr($param)
{
$str = '';
foreach ($param as $key => $value) {
$str=$str.$key.'='.$value.'&';
}
$str = rtrim($str,'&');
return $str;
}
/**
* 发起请求
* @param string $url 请求地址
* @param string $data 请求数据包
* @return string 请求返回数据
*/
protected static function sendCmd($url,$data)
{
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);
}
curl_close($curl); // 关键CURL会话
return $tmpInfo; // 返回数据
}
}