基础类
<?php
namespace app\common\lib\printman;
use think\facade\Log;
class Basic
{
#APPID
public $AppId;
#密钥
public $AppSecret;
#API地址
public $ApiUrl;
#打印机ID
public $PrinterId;
public function __construct($AppId, $AppSecret, $PrinterId)
{
$this->AppId = $AppId;
$this->AppSecret = $AppSecret;
$this->ApiUrl = "https://iot-app-prod.fengdada.cn/mk/api";
$this->PrinterId = $PrinterId;
}
public function encode($BizData, $nonce) {
// global $AppSecret;
$jsonBytes = mb_convert_encoding($BizData , 'utf-8');
$bizData = strval($jsonBytes);
$sign_ori = $bizData . $nonce . $this->AppSecret;
$md5_hash = md5($sign_ori, true);
$sign = base64_encode($md5_hash);
return $sign;
}
public function generate_verification_code() {
$verification_code = "";
for ($i = 0; $i < 6; $i++) {
$verification_code .= strval(rand(0, 9));
}
return $verification_code;
}
public function requests_post($url, $data, $headers) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_CAINFO, "cacert-2023-01-10.pem");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function DoMyPost($URL, $json_BizData) {
$ts = round(microtime(true) * 1000);
$nonce = $this->generate_verification_code();
$sign = $this->encode($json_BizData, $nonce);
$data = array(
"bizData" => $json_BizData,
"nonce" => $nonce,
"appId" => $this->AppId,
"timestamp" => strval($ts)
);
$headers = array(
'Content-Type: application/json',
'sign:'.$sign
);
// $response = $this->requests_post($URL, json_encode($data), $headers);
$response = $this->curlRequest($URL, "POST",json_encode($data),true,false, $headers);
Log::write("打印机日志:".print_r($response,true),'printman');
if(empty($response))
{
echo "Error: no response received";
}else{
return $response;
}
}
// 云打印验证码
public function PrintCaptcha() {
$URL = $this->ApiUrl."/print/captcha";
$BizData = array('printerId' => $this->PrinterId);
$json_BizData = json_encode($BizData);
return $this->DoMyPost($URL, $json_BizData);
}
// 云打印机绑定
public function PrintBind($VerificationCode) {
$URL = $this->ApiUrl."/printer/bind";
$BizData = array('printerId' => $this->PrinterId, 'captcha' => $VerificationCode);
$json_BizData = json_encode($BizData);
return $this->DoMyPost($URL, $json_BizData);
}
// 云打印
public function CloudPrint( $ShareCode, $PrintDataList) {
$URL = $this->ApiUrl."/print";
$BizData = array('printerId' => $this->PrinterId, 'shareCode' => $ShareCode, 'printData' => $PrintDataList);
$json_BizData = json_encode($BizData);
return $this->DoMyPost($URL, $json_BizData);
}
// 云打印状态查询
public function QueryPrintStatus($ShareCode) {
$URL = $this->ApiUrl."/printer/status/query";
$BizData = array('printerId' => $this->PrinterId, 'shareCode' => $ShareCode);
$json_BizData = json_encode($BizData);
return $this->DoMyPost($URL, $json_BizData);
}
//云打印解绑//0标识解绑失败,1标识解绑成功
public function unbind($ShareCode){
$URL = $this->ApiUrl."/printer/unbind";
$BizData = array('printerId' => $this->PrinterId, 'shareCode' => $ShareCode);
$json_BizData = json_encode($BizData);
return $this->DoMyPost($URL, $json_BizData);
}
/**
* @Author: 小破孩嫩
* @Email: 3584685883@qq.com
* @Time: 2021/4/1 10:39
* @param string $url url地址
* @param string $method 请求方法,默认为 'GET',可选值为 'GET' 或 'POST'
* @param mixed $data 要发送的数据,如果是 POST 请求则为数据内容,否则为 null
* @param array $headers 自定义请求头信息
* @param int $timeout 超时时间,默认为 30 秒
* @param bool $verifySSL 是否验证 SSL 证书,默认为 true
* @param bool $flbg 返回值是否转成数组,默认不转
* @param bool $headercontent 是否获取请求的header值内容,默认不获取
* @return array|bool|mixed|string
* @Description:curl请求
*/
protected function curlRequest($url, $method = 'GET', $data = null, $flbg = false, $verifySSL = true, $headers = [], $headerContent = false, $timeout = 30)
{
// 初始化 cURL 会话
$ch = curl_init();
// 设置要请求的 URL
curl_setopt($ch, CURLOPT_URL, $url);
// 设置获取的信息以字符串形式返回,而不是直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 设置超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
// 设置请求方法
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
// 设置请求头
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
// 设置是否验证 SSL 证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verifySSL);
// 执行 cURL 会话并获取响应
$response = curl_exec($ch);
// 获取 HTTP 响应码
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 如果 cURL 执行出错
if (curl_errno($ch)) {
// 输出错误信息
echo 'Curl error: ' . curl_error($ch);
// 关闭 cURL 会话并返回 false
curl_close($ch);
return false;
} // 如果 HTTP 响应码大于等于 400(表示错误)
elseif ($httpCode >= 400) {
// 输出错误信息
echo "HTTP error: $httpCode";
// 关闭 cURL 会话并返回 false
curl_close($ch);
return false;
}
// 处理是否获取请求头内容
if ($headerContent && $httpCode == 200) {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
curl_close($ch);
return [$headers, $body];
}
// 关闭 cURL 会话
curl_close($ch);
// 处理是否将响应转换为数组
if ($flbg) {
$response = json_decode($response, true);
}
// 返回响应内容
return $response;
}
/**
* @Author:小破孩
* @Email:3584685883@qq.com
* @Time:2025/1/10 14:02
* @param $code
* @return string
* @Description:打印机错误码
*/
public function errorCode($code){
$errorCodes = [
200 => "success 成功",
500 => "sys fail 系统异常",
2001 => "sign check fail 签名失败",
2002 => "not find partner 查无合作伙伴",
2003 => "illegal access 非法访问",
3001 => "param check error 参数错误",
3002 => "please input params 请输入参数",
40001 => "please input APPID 请输入appid",
40002 => "biz exception 业务异常",
40003 => "printer xxx is offline 打印机离线",
40004 => "printer xxx is not auth 打印机未授权",
40005 => "shareCode is error 分享码错误",
40006 => "printer xxx after 5 minutes reprinting 请5分钟后重试",
40007 => "printer xxx captcha error 验证码错误",
40008 => "printer xxx captcha expired 验证码过期",
40009 => "printer xxx bind fail 绑定失败",
40023 => "lip not close 盖子未闭合",
40023 => "sticker 粘纸",
40023 => "sticker and lip not close 粘纸并且盖子未闭合",
40023 => "no page 缺纸",
40023 => "no page and lip not close 缺纸并且盖子未闭合",
40023 => "temperature too high 温度过高",
40023 => "temperature too high and lip not close 温度过高且盖子未闭合",
40023 => "temperature too high and sticker 温度过高且粘纸",
40023 => "temperature too high and lip not close and sticker 温度过高且粘纸,盖子未闭合",
40023 => "command error 指令错误"
];
return $errorCodes[$code];
}
}
模板
<?php
namespace app\common\lib\printman\template;
use app\common\lib\printman\Template;
class Temp1 implements Template
{
protected $id;
public function __construct($id = "")
{
$this->id = $id;
}
/**
* @Author:小破孩
* @Email:3584685883@qq.com
* @Time:2025/1/10 10:51
* @return string
* @Description:打印模板1
*/
public function temp($order = ""){
if(empty($order)){
return "没有订单信息";
}
$height = ceil(self::getTempHight($order)/10+50);
$i = 40;
$template = "";
$template .= "SIZE 72 mm, ".$height." mm\r\n";
$template .= "CODEPAGE 437\r\n";
$template .= "DENSITY 8\r\n";
$template .= "CLS \r\n";
$template .= "CODEPAGE 936\r\n";
$template .= "DIRECTION 0\r\n";
$template .= "TEXT 220,0,\"4\",0,1,1,\""."订单详情"."\"\r\n"; //小票标题
$template .= "TEXT 220,".$i.",\"4\",0,1,1,\"".""."\"\r\n"; //换行
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"订单编号:".$order['o_uuid']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"打印时间:".date("Y-m-d H:i:s")."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"申 请 人:".$order['o_address_name']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"联系方式:".$order['o_address_tel_default']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"收货地址:".mb_substr($order['o_address_info'],0,16)."\"\r\n";
if(mb_strlen($order['o_address_info']) > 16){
$template .= "TEXT 40,";
$template .= $i+=30;
$template .=",\"0\",0,1,1,\" ".mb_substr($order['o_address_info'],16)."\"\r\n";
}
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"申请时间:".$order['o_create_time']."\"\r\n";
// $template .= "TEXT 40,";
// $template .= $i+=30;
// $template .= ",\"0\",0,1,1,\"审核时间:".date("Y-m-d H:i:s",$order['o_pay_receipt_allow_time'])."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"配 送 员:".$order['salesmaninfo']['sm_name']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"配送电话:".$order['salesmaninfo']['sm_phone']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"出货仓库:".$order['warehouse_name']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"商品总数:".$order['goods_total_num']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"商品总价:".$order['o_real_price']."\"\r\n";
$template .= "BAR 20,";
$template .= $i+=28;
$template .= ",720,2\r\n";
$template .= "TEXT 40,";
$template .= $i+=16;
$template .= ",\"0\",0,1,1,\"商品 数量 单价 金额\"\r\n";
$template .= "BAR 20,";
$template .= $i+=28;
$template .= ",720,2\r\n";
foreach ($order['order_list'] as $kk => $vv){
if(!empty($vv['oi_issendgoods'])){
$firstNamaText = "赠品:";
}else{
$firstNamaText = "商品:";
}
$knum = $kk+=1;
$template .= "TEXT 30,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"$knum.". mb_substr($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')',0,26)."\"\r\n";
if(mb_strlen($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')') > 26){
$template .= "TEXT 30,";
$template .= $i+=30;
$template .=",\"0\",0,1,1,\" ".mb_substr($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')',26)."\"\r\n";
}
$template .= "TEXT 65,";
$template .= $i+=30;
if(!empty($vv['oi_issendgoods'])){
$template .=",\"0\",0,1,1,\"".$vv['oi_sku_info']['num'].'件'.' '."0.00".' '."0.00"."\"\r\n";
}else{
$template .=",\"0\",0,1,1,\"".$vv['oi_sku_info']['num'].'件'.' '.sprintf("%.2f",$vv['oi_sku_info']['sgcs_price']/$vv['oi_sku_info']['num']).' '.$vv['oi_real_price']."\"\r\n";
}
}
$template .= "BAR 20,";
$template .= $i+=28;
$template .= ",720,2\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"数量总计:".$order['goods_total_num'].'件'."\"\r\n";
$template .= "PRINT 1,1";
$instacneStr = new \app\common\lib\data\Str();
$data = [
'waybillPrinterData' => $instacneStr->gzipAndBase64Encode($template),
'printType' => 'tspl',
'id' => $this->id
];
return [$data];
}
protected function getTempHight($order){
$i = 50;
$template = "";
$template .= "SIZE 72 mm, 90 mm\r\n";
$template .= "CODEPAGE 437\r\n";
$template .= "DENSITY 8\r\n";
$template .= "CLS \r\n";
$template .= "CODEPAGE 936\r\n";
$template .= "DIRECTION 0\r\n";
$template .= "TEXT 220,0,\"4\",0,1,1,\""."订单详情"."\"\r\n"; //小票标题
$template .= "TEXT 220,".$i.",\"4\",0,1,1,\"".""."\"\r\n"; //换行
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"订单编号:".$order['o_uuid']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"打印时间:".date("Y-m-d H:i:s")."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"申 请 人:".$order['o_address_name']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"联系方式:".$order['o_address_tel_default']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"收货地址:".mb_substr($order['o_address_info'],0,16)."\"\r\n";
if(mb_strlen($order['o_address_info']) > 16){
$template .= "TEXT 40,";
$template .= $i+=30;
$template .=",\"0\",0,1,1,\" ".mb_substr($order['o_address_info'],16)."\"\r\n";
}
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"申请时间:".$order['o_create_time']."\"\r\n";
// if(empty($order['o_help'])){
// $template .= "TEXT 40,";
// $template .= $i+=30;
// $template .= ",\"0\",0,1,1,\"审核时间:".date("Y-m-d H:i:s",$order['o_pay_receipt_allow_time'])."\"\r\n";
// }
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"配送员姓名:".$order['salesmaninfo']['sm_name']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"配送员电话:".$order['salesmaninfo']['sm_phone']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"出货仓库:".$order['warehouse_name']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"商品总数:".$order['goods_total_num']."\"\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"商品总价:".$order['o_real_price']."\"\r\n";
$template .= "BAR 20,";
$template .= $i+=28;
$template .= ",720,2\r\n";
$template .= "TEXT 40,";
$template .= $i+=16;
$template .= ",\"0\",0,1,1,\"商品 数量 单价 金额\"\r\n";
$template .= "BAR 20,";
$template .= $i+=28;
$template .= ",720,2\r\n";
// foreach ($order as $key => $val){
foreach ($order['order_list'] as $kk => $vv){
if(!empty($vv['oi_issendgoods'])){
$firstNamaText = "赠品:";
}else{
$firstNamaText = "商品:";
}
$knum = $kk+=1;
$template .= "TEXT 30,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"$knum.". mb_substr($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')',0,26)."\"\r\n";
if(mb_strlen($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')') > 26){
$template .= "TEXT 30,";
$template .= $i+=30;
$template .=",\"0\",0,1,1,\" ".mb_substr($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')',26)."\"\r\n";
}
$template .= "TEXT 65,";
$template .= $i+=30;
$template .=",\"0\",0,1,1,\"".$vv['oi_sku_info']['num'].'件'.' '.$vv['oi_sku_info']['sgcs_price'].' '.$vv['oi_sku_info']['sgcs_price']*$vv['oi_sku_info']['num']."\"\r\n";
}
$template .= "BAR 20,";
$template .= $i+=28;
$template .= ",720,2\r\n";
$template .= "TEXT 40,";
$template .= $i+=30;
$template .= ",\"0\",0,1,1,\"数量总计:".$order['goods_total_num']."\"\r\n";
// }
$template .= "PRINT 1,1";
return $i;
}
}
评论