/**
* Copyright (C) 2018-2022
* All rights reserved, Designed By www.yixiang.co
* 注意:
* 本软件为www.yixiang.co开发研制
*/
package co.yixiang.print;
import co.yixiang.constant.ShopConstants;
import co.yixiang.utils.DateUtils;
import co.yixiang.utils.RedisUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@SuppressWarnings("static-access")
public class PrintUtil4 {
public static final String URL = "http://api.feieyun.cn/Api/Open/";//不需要修改
public static final String USER = RedisUtil.get(ShopConstants.YSHOP_FEI_E_USER);//*必填*:账号名
public static final String UKEY = RedisUtil.get(ShopConstants.YSHOP_FEI_E_UKEY);//*必填*: 飞鹅云后台注册账号后生成的UKEY 【备注:这不是填打印机的KEY】
public static final String SN = "918502791";//*必填*:打印机编号,必须要在管理后台里添加打印机或调用API接口添加之后,才能调用API
/**
* 保存文件到磁盘
*
* @param path 保存路径
* @param content 打印内容
* writeFile("E:/retlog.txt", result);
*/
public void writeFile(String path, String content) {
content = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date()) + ",保存的订单日志信息为: " + content;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path, true);
fos.write(content.getBytes());
fos.write("\r<BR>".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
try {
System.out.println(new String(addPrintEquip("918502791", "nvz8kw5n", "测试", "").getBytes("ISO-8859-1"), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 添加打印机
*
* @param sn 编号
* @param key 密钥
* @param remark 备注
* @param phone 流量卡号码
* @return 返回打印结果
*/
public static String addPrintEquip(String sn, String key, String remark, String phone) {
String printerContent = sn + " # " + key + " # " + remark + " # " + phone;
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("user", USER));
String STIME = String.valueOf(System.currentTimeMillis() / 1000);
nvps.add(new BasicNameValuePair("stime", STIME));
nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
nvps.add(new BasicNameValuePair("apiname", "Open_printerAddlist"));
nvps.add(new BasicNameValuePair("printerContent", printerContent));// 固定值,不需要修改
return sendHttpRequest(nvps);
}
/**
* 编辑打印机信息
*
* @param sn 编号
* @param remark 备注
* @param phone 电话
* @return 结果
*/
public static String editPrintEquip(String sn, String remark, String phone) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("user", USER));
String STIME = String.valueOf(System.currentTimeMillis() / 1000);
nvps.add(new BasicNameValuePair("stime", STIME));
nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
nvps.add(new BasicNameValuePair("apiname", "Open_printerEdit"));
nvps.add(new BasicNameValuePair("sn", sn));
nvps.add(new BasicNameValuePair("name", remark));
nvps.add(new BasicNameValuePair("phonenum", phone));
return sendHttpRequest(nvps);
}
/**
* 打印订单数据
*
* @param sn 打印机编号
* @param contentTitle 打印标题
* @param orderList 订单数据
* @param order
* @return
*/
public static String printOrder(String sn, String contentTitle, List<OrderPrint> orderList, PrintOrderDataVO order, int lian) {
String content = getPrintContent(contentTitle, orderList, 14, 6, 3, 6, order, lian + 1);//orderList为数组 b1代表名称列占用(14个字节) b2单价列(6个字节) b3数量列(3个字节) b4金额列(6个字节)-->这里的字节数可按自己需求自由改写,14+6+3+6再加上代码写的3个空格就是32了,58mm打印机一行总占32字节
// List<NameValuePair> nvps = new ArrayList<NameValuePair>();
// nvps.add(new BasicNameValuePair("user", USER));
// String STIME = String.valueOf(System.currentTimeMillis() / 1000);
// nvps.add(new BasicNameValuePair("stime", STIME));
// nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
// nvps.add(new BasicNameValuePair("apiname", "Open_printMsg"));// 固定值,不需要修改
// nvps.add(new BasicNameValuePair("sn", sn));
// nvps.add(new BasicNameValuePair("content", content));
// nvps.add(new BasicNameValuePair("times", "1"));// 打印联数
// return sendHttpRequest(nvps);
return print(sn, content);
}
/**
* 打印预养订单
*
* @param orderList 订单数据
* @param title 标题
* @param mobile 联系方式
* @param name 用户姓名
* @param payPrice 实际支付
* @return
*/
public static String printOrderByAdvance(List<OrderPrint> orderList, String title, String mobile, String name, String payPrice) {
String content = getOrderByAdvance(orderList, 14, 6, 3, 6, title, mobile, name, payPrice);//orderList为数组 b1代表名称列占用(14个字节) b2单价列(6个字节) b3数量列(3个字节) b4金额列(6个字节)-->这里的字节数可按自己需求自由改写,14+6+3+6再加上代码写的3个空格就是32了,58mm打印机一行总占32字节
// List<NameValuePair> nvps = new ArrayList<NameValuePair>();
// nvps.add(new BasicNameValuePair("user", USER));
// String STIME = String.valueOf(System.currentTimeMillis() / 1000);
// nvps.add(new BasicNameValuePair("stime", STIME));
// nvps.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
// nvps.add(new BasicNameValuePair("apiname", "Open_printMsg"));// 固定值,不需要修改
// nvps.add(new BasicNameValuePair("sn", SN));
// nvps.add(new BasicNameValuePair("content", content));
// nvps.add(new BasicNameValuePair("times", "1"));// 打印联数
// return sendHttpRequest(nvps);
return print(SN, content);
}
/**
* 打印订单数据
*
* @param contentTitle 打印标题
* @param orderList 订单数据
* @return
*/
public static String printContentByStore(String contentTitle, List<OrderPrint> orderList, int b1, int b3, PrintStoreOrderVO printStoreOrder, int lian) {
String content = getPrintContentByStore(contentTitle, orderList, b1, b3, printStoreOrder, lian + 1);//orderList为数组 b1�