You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
338 lines
12 KiB
338 lines
12 KiB
<?php
|
|
|
|
use think\Db;
|
|
|
|
header("Content-type: text/html; charset=utf-8");
|
|
include 'httpclient.php';
|
|
|
|
define('USER', '13161443713@163.com'); //*必填*:飞鹅云后台注册账号
|
|
define('UKEY', 'XsaHzgePdyWTfcMX'); //*必填*: 飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】
|
|
define('SN', '550510805'); //*必填*:打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
|
|
|
|
//以下参数不需要修改
|
|
define('IP', 'api.feieyun.cn'); //接口IP或域名
|
|
define('PORT', 80); //接口IP端口
|
|
define('PATH', '/Api/Open/'); //接口路径
|
|
|
|
//拼凑订单内容时可参考如下格式
|
|
//根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
|
|
$content = '<CB>测试打印</CB><BR>';
|
|
$content .= '名称 单价 数量 金额<BR>';
|
|
$content .= '--------------------------------<BR>';
|
|
$content .= '饭 10.0 10 100.0<BR>';
|
|
$content .= '炒饭 10.0 10 100.0<BR>';
|
|
$content .= '蛋炒饭 10.0 10 100.0<BR>';
|
|
$content .= '鸡蛋炒饭 10.0 10 100.0<BR>';
|
|
$content .= '西红柿炒饭 10.0 10 100.0<BR>';
|
|
$content .= '西红柿蛋炒饭 10.0 10 100.0<BR>';
|
|
$content .= '西红柿鸡蛋炒饭 10.0 10 100.0<BR>';
|
|
$content .= '--------------------------------<BR>';
|
|
$content .= '备注:加辣<BR>';
|
|
$content .= '合计:xx.0元<BR>';
|
|
$content .= '送货地点:广西南宁五象新区<BR>';
|
|
$content .= '联系电话:13888888888888<BR>';
|
|
$content .= '订餐时间:2014-08-08 08:08:08<BR>';
|
|
$content .= '<QR>http://www.feieyun.com</QR>';//把二维码字符串用标签套上即可自动生成二维码
|
|
//标签说明:
|
|
$content = "<DIRECTION>1</DIRECTION>";//设定打印时出纸和打印字体的方向,n 0 或 1,每次设备重启后都会初始化为 0 值设置,1:正向出纸,0:反向出纸,
|
|
$content .= "<TEXT x='9' y='10' font='12' w='1' h='2' r='0'>#001 五号桌 1/3</TEXT><TEXT x='80' y='80' font='12' w='2' h='2' r='0'>可乐鸡翅</TEXT><TEXT x='9' y='180' font='12' w='1' h='1' r='0'>张三先生 13800138000</TEXT>";//40mm宽度标签纸打印例子,打开注释调用标签打印接口打印
|
|
|
|
/**
|
|
* [打印订单接口 Open_printMsg]
|
|
* @param [string] $sn [打印机编号sn]
|
|
* @param [string] $content [打印内容]
|
|
* @param [string] $times [打印联数]
|
|
* @return [string] [接口返回值]
|
|
*/
|
|
function printMsg($sn, $content, $times = 1)
|
|
{
|
|
$time = time(); //请求时间
|
|
$msgInfo = array(
|
|
'user' => USER,
|
|
'stime' => $time,
|
|
'sig' => signature($time),
|
|
'apiname' => 'Open_printMsg',
|
|
'sn' => $sn,
|
|
'content' => $content,
|
|
'times' => $times//打印次数
|
|
);
|
|
$client = new HttpClient(IP, PORT);
|
|
if (!$client->post(PATH, $msgInfo)) {
|
|
echo 'error';
|
|
} else {
|
|
//服务器返回的JSON字符串,建议要当做日志记录起来
|
|
$result = $client->getContent();
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* [批量添加打印机接口 Open_printerAddlist]
|
|
* @param [string] $printerContent [打印机的sn#key]
|
|
* @return [string] [接口返回值]
|
|
*/
|
|
function printerAddlist($printerContent)
|
|
{
|
|
$time = time(); //请求时间
|
|
$msgInfo = array(
|
|
'user' => USER,
|
|
'stime' => $time,
|
|
'sig' => signature($time),
|
|
'apiname' => 'Open_printerAddlist',
|
|
'printerContent' => $printerContent
|
|
);
|
|
$client = new HttpClient(IP, PORT);
|
|
if (!$client->post(PATH, $msgInfo)) {
|
|
echo 'error';
|
|
} else {
|
|
$result = $client->getContent();
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* [批量删除打印机 Open_printerDelList]
|
|
* @param [string] $snlist [打印机编号,多台打印机请用减号“-”连接起来]
|
|
* @return [string] [接口返回值]
|
|
*/
|
|
function printerDelList($snlist)
|
|
{
|
|
$time = time(); //请求时间
|
|
$msgInfo = array(
|
|
'user' => USER,
|
|
'stime' => $time,
|
|
'sig' => signature($time),
|
|
'apiname' => 'Open_printerDelList',
|
|
'snlist' => $snlist
|
|
);
|
|
$client = new HttpClient(IP, PORT);
|
|
if (!$client->post(PATH, $msgInfo)) {
|
|
echo 'error';
|
|
} else {
|
|
$result = $client->getContent();
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* [查询订单是否打印成功接口 Open_queryOrderState]
|
|
* @param [string] $orderid [调用打印机接口成功后,服务器返回的JSON中的编号 例如:123456789_20190919163739_95385649]
|
|
* @return [string] [接口返回值]
|
|
*/
|
|
function queryOrderState($orderid)
|
|
{
|
|
$time = time(); //请求时间
|
|
$msgInfo = array(
|
|
'user' => USER,
|
|
'stime' => $time,
|
|
'sig' => signature($time),
|
|
'apiname' => 'Open_queryOrderState',
|
|
'orderid' => $orderid
|
|
);
|
|
$client = new HttpClient(IP, PORT);
|
|
if (!$client->post(PATH, $msgInfo)) {
|
|
echo 'error';
|
|
} else {
|
|
$result = $client->getContent();
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* [signature 生成签名]
|
|
* @param [string] $time [当前UNIX时间戳,10位,精确到秒]
|
|
* @return [string] [接口返回值]
|
|
*/
|
|
function signature($time)
|
|
{
|
|
return sha1(USER . UKEY . $time);//公共参数,请求公钥
|
|
}
|
|
|
|
function FeiePrint($order_num)
|
|
{
|
|
//$data = DB::table('ims_cjdc_order_main')
|
|
// ->alias('m')
|
|
// ->join(['ims_cjdc_order'=>'o'],'o.order_main_id = m.id','inner')
|
|
// ->join(['ims_cjdc_order_goods'=>'g'],'o.id = g.order_id','inner')
|
|
// ->join(['ims_cjdc_feprint'=>'f'],'m.market_id = f.market_id','inner')
|
|
// ->join(['ims_cjdc_store'=>'s'],'s.id = o.store_id','inner')
|
|
// //->distinct(true)
|
|
// ->where('m.order_num',$order_num)
|
|
// ->field("o.note as o_note,g.name,g.number,g.money,g.good_unit,m.delivery_time as ps_time,m.address,m.note,m.name as user_name,m.dada_fee,m.money as m_money,m.yhq_money2,m.box_money,f.sn,m.tel,m.order_num,g.id,g.spec,s.name as shopname")
|
|
// ->order('s.id')
|
|
// ->select();
|
|
|
|
$data = Db::table('ims_cjdc_order_main as m')
|
|
->join('ims_cjdc_order as o','o.order_main_id', '=', 'm.id','inner')
|
|
->join('ims_cjdc_order_goods as g','o.id','=', 'g.order_id','inner')
|
|
->join('ims_cjdc_feprint as f','m.market_id','=', 'f.market_id','inner')
|
|
->join('ims_cjdc_store as s','s.id','=', 'o.store_id','inner')
|
|
->where('m.global_order_id', $order_num)
|
|
->selectRaw("o.note as o_note,g.name,g.number,g.money,g.good_unit,m.delivery_time as ps_time,m.address,m.note,m.name as user_name,m.dada_fee,m.money as m_money,m.yhq_money2,m.box_money,f.sn,m.tel,m.order_num,g.id,g.spec,s.name as shopname")
|
|
->orderBy('s.id')
|
|
->get()
|
|
->toArray();
|
|
|
|
// $order_goods = Db::table('ims_cjdc_order_goods')->where('order_id', $order_main['id'])->field("name,number,money")->select();
|
|
// $sn = Db::table('ims_cjdc_feprint')->where('market_id', $order_main['market_id'])->value("sn");
|
|
$content = PrintFormat($data, 14, 6, 3, 6);
|
|
$res = printMsg($data[0]['sn'], $content, 1);
|
|
return ($res);
|
|
}
|
|
|
|
function PrintFormat($arr, $A, $B, $C, $D)
|
|
{
|
|
$orderInfo = '<CB>链街生活</CB><BR>';
|
|
$orderInfo .= '名称 单价 数量 金额<BR>';
|
|
$orderInfo .= '--------------------------------<BR>';
|
|
$shopname = "";
|
|
$shopnum = 0;
|
|
foreach ($arr as $k5 => $v5) {
|
|
if ($shopname != $v5['shopname']) {
|
|
if ($shopname != "") {
|
|
$orderInfo .= ' <BR>';
|
|
}
|
|
$shopnum++;
|
|
$orderInfo .= "<C>(" . $shopnum . ")" .$v5['shopname'] . '</C><BR>';
|
|
$shopname = $v5['shopname'];
|
|
}
|
|
$name = $v5['name'];
|
|
if(!empty($v5['spec'])) {
|
|
$name .= "(规格:". $v5['spec'].")";
|
|
}elseif (!empty($v5['good_unit'])){
|
|
$name .= "(规格:". $v5['good_unit'].")";
|
|
}
|
|
$price = $v5['money'];
|
|
$num = $v5['number'];
|
|
$prices = sprintf("%.2f",$v5['money']*$v5['number']);
|
|
$kw3 = '';
|
|
$kw1 = '';
|
|
$kw2 = '';
|
|
$kw4 = '';
|
|
$str = $name;
|
|
$blankNum = $A;//名称控制为14个字节
|
|
$lan = mb_strlen($str,'utf-8');
|
|
$m = 0;
|
|
$j=1;
|
|
$blankNum++;
|
|
$result = array();
|
|
if(strlen($price) < $B){
|
|
$k1 = $B - strlen($price);
|
|
for($q=0;$q<$k1;$q++){
|
|
$kw1 .= ' ';
|
|
}
|
|
$price = $kw1.$price;
|
|
}
|
|
if(strlen($num) < $C){
|
|
$k2 = $C - strlen($num);
|
|
for($q=0;$q<$k2;$q++){
|
|
$kw2 .= ' ';
|
|
}
|
|
$num = $kw2.$num;
|
|
}
|
|
if(strlen($prices) < $D){
|
|
$k3 = $D - strlen($prices);
|
|
for($q=0;$q<$k3;$q++){
|
|
$kw4 .= ' ';
|
|
}
|
|
$prices = $kw4.$prices;
|
|
}
|
|
for ($i=0;$i<$lan;$i++){
|
|
$new = mb_substr($str,$m,$j,'utf-8');
|
|
$j++;
|
|
if(mb_strwidth($new,'utf-8')<$blankNum) {
|
|
if($m+$j>$lan) {
|
|
$m = $m+$j;
|
|
$tail = $new;
|
|
$lenght = iconv("UTF-8", "GBK//IGNORE", $new);
|
|
$k = $A - strlen($lenght);
|
|
for($q=0;$q<$k;$q++){
|
|
$kw3 .= ' ';
|
|
}
|
|
if($m==$j){
|
|
$tail .= $kw3.' '.$price.' '.$num.' '.$prices;
|
|
}else{
|
|
$tail .= $kw3.'<BR>';
|
|
}
|
|
break;
|
|
}else{
|
|
$next_new = mb_substr($str,$m,$j,'utf-8');
|
|
if(mb_strwidth($next_new,'utf-8')<$blankNum) continue;
|
|
else{
|
|
$m = $i+1;
|
|
$result[] = $new;
|
|
$j=1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$head = '';
|
|
foreach ($result as $key=>$value) {
|
|
if($key < 1){
|
|
$v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
|
|
$v_lenght = strlen($v_lenght);
|
|
if($v_lenght == 13) $value = $value." ";
|
|
$head .= $value.' '.$price.' '.$num.' '.$prices;
|
|
}else{
|
|
$head .= $value.'<BR>';
|
|
}
|
|
}
|
|
$orderInfo .= $head.$tail;
|
|
if(!empty($v5['o_note'])){
|
|
$orderInfo .= '备注:'.$v5['o_note'].'<BR>';
|
|
}
|
|
@$nums += $prices;
|
|
}
|
|
// $time = date('Y-m-d H:i:s', time());
|
|
$orderInfo .= '--------------------------------<BR>';
|
|
if ($arr[0]['box_money'] > 0) {
|
|
$kw5 = '';
|
|
$len = 24 - strlen($arr[0]['box_money']);
|
|
for ($q = 0; $q < $len; $q++) {
|
|
$kw5 .= ' ';
|
|
}
|
|
$orderInfo .= '包装费:' . $kw5 . $arr[0]['box_money'] . '<BR>';
|
|
}
|
|
if($arr[0]['dada_fee'] > 0){
|
|
$kw5 = '';
|
|
$len = 24 - strlen($arr[0]['dada_fee']);
|
|
for ($q = 0; $q < $len; $q++) {
|
|
$kw5 .= ' ';
|
|
}
|
|
$orderInfo .= '配送费:'.$kw5.$arr[0]['dada_fee'].'<BR>';
|
|
}
|
|
if($arr[0]['yhq_money2'] > 0){
|
|
$yhq_money2 = sprintf("%.2f",$arr[0]['yhq_money2']);
|
|
$kw6 = '';
|
|
$len = 25 - strlen($yhq_money2);
|
|
for ($q = 0; $q < $len; $q++) {
|
|
$kw6 .= ' ';
|
|
}
|
|
$orderInfo .= '红包:'.$kw6.'-'.$yhq_money2.'<BR>';
|
|
}
|
|
$total = '合计:'.$arr[0]['m_money'];
|
|
$user_name = $arr[0]['user_name'];
|
|
if(strlen($user_name)>18){
|
|
$user_name=substr($user_name,0,18).'...';
|
|
}
|
|
$str = $user_name . $total;
|
|
$kw5 = '';
|
|
$lenght = iconv("UTF-8", "GBK//IGNORE", $str);
|
|
$total_len = 32 - strlen($lenght);
|
|
for ($q = 0; $q < $total_len; $q++) {
|
|
$kw5 .= ' ';
|
|
}
|
|
$total_str = $user_name.$kw5.$total;
|
|
$orderInfo .= $total_str.'<BR>';
|
|
$orderInfo .= '送货地点:' . $arr[0]['address'] . '<BR>';
|
|
$tel = substr_replace( $arr[0]['tel'], '****', 3, 4);
|
|
$orderInfo .= '联系电话:' . $tel . '<BR>';
|
|
$orderInfo .= '配送时间:' . $arr[0]['ps_time'] . '<BR>';
|
|
if(!empty($arr[0]['note'])){
|
|
$orderInfo .= '备注:'.$arr[0]['note'].'<BR><BR>';
|
|
}
|
|
//$orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
|
|
return $orderInfo;
|
|
}
|
|
|
|
?>
|