This commit is contained in:
cun-nan 2024-05-31 09:01:18 +08:00
parent fe84e7d8c7
commit 4cb73620e1
24 changed files with 0 additions and 780 deletions

View File

@ -1,306 +0,0 @@
package com.fuint.business.printer.controller;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.internal.util.codec.Base64;
import com.fuint.business.printer.entity.Test;
import com.fuint.business.printer.service.PrinterService;
import com.fuint.business.printer.template.*;
import com.fuint.business.printer.unit.Methods;
import com.fuint.business.printer.vo.OrderPayment;
import com.fuint.common.dto.AccountInfo;
import com.fuint.common.util.Constants;
import com.fuint.common.util.RedisUtil;
import com.fuint.common.util.TokenUtil;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
//import com.yly.print_sdk_librar.RequestMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/business/printer")
//@EnableWebMvc
@Slf4j
public class printerController extends BaseController {
@Resource
PrinterService printerService;
@GetMapping("callback")
public Map<String, String> callback(Map<String , String> map){
log.info("------print/callback-------");
Map<String, String> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("message","ok");
return objectObjectHashMap;
}
@GetMapping("push")
public Map<String, String> push( Map<String , String> map){
log.info("------接收GET回调-------");
Map<String, String> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("message","ok");
return objectObjectHashMap;
}
@PostMapping("push")
public Map<String, String> PostPush(
@RequestParam("ciphertext") String ciphertext,
@RequestParam("nonce") String nonce,
@RequestParam("tag") String tag,
@RequestParam("additional_data") String additional_data,
@RequestParam("iv") String iv,
@RequestParam("signature") String signature){
log.info("12222222222222222222222222");
return printerService.PostPush(ciphertext,nonce,tag,additional_data,iv,signature);
}
@GetMapping("returnCode")
public ResponseObject returnCode(String payAmount) {
return getSuccessResult(printerService.returnCode(payAmount));
}
//
// public static final String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuiQQsY9lbZhtRoZIaBVN\n" +
// "XKuAme47UCUv3RG2AYJXKQ/TyXBBY0JULSf4637Ug7JqGR1ohHFpY9XotG5J/7Ne\n" +
// "safQApAz8QHKOa6+jqS0edafegjE7P0KqfQWvjUIbhAl5iXmiGLA/R6zLXxk8+PU\n" +
// "toRu1rmzMPCsbGRG0uHOZ0nYez1LSM4k4k8o4/dl7NDr2CqXrfYQCEvGAAJay/bg\n" +
// "h1B05cqFNuhQGvv55EX377QVZX7V0ZipIZhlzoh8pA0ZpzW/KndZ+pRYw6cPLvey\n" +
// "qymRnTlCSQUDYQRlxaDppPbZcU1Krb9tZUmhJp+R9l73tJX8yN5wzbq6HATZbD1v\n" +
// "BwIDAQAB";
// @GetMapping("getToken")
// public ResponseObject getToken(){
//// Methods.()
// //初始化控制器类
// Methods m=Methods.getInstance();
// //初始化终端信息
// m.init("1038835098", "1595cb28ea30e98908e6334e735f4b8a");
// //获取token
// m.getFreedomToken();
// System.out.println("123123123123123");
// return getSuccessResult("");
// }
private static final String APP_KEY = "1073000501";
private static final String APP_SECRET = "e0292ae66d9d21feaa895370d3a875aa";
// 授权
@GetMapping("scancodemodel")
public ResponseObject scancodemodel(){
AccountInfo nowAccountInfo = TokenUtil.getNowAccountInfo();
String token = printerService.getToken(nowAccountInfo.getStoreId());
// String token = printerService.getToken(138);
return getSuccessResult(token);
}
// 发起网络支付
@GetMapping("printIndex")
public ResponseObject printIndex(BigDecimal payAmount) {
// Methods m=Methods.getInstance();
// m.init("26845ee50f13467cacbaa099489a2fc152abd6f5");
//
// m.init(APP_KEY, APP_SECRET);
//
//// m.addPrinter("4004872261", "628023865417");
// Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
// HashMap<String, String> objectObjectHashMap = new HashMap<>();
//// <JSON>{"order_payment":{"pay_amount":"支付金额","pay_amount_source":3},"processed_state":0}</JSON>
//
//
// Map<String, Object> orderPaymentMap = new HashMap<>();
// orderPaymentMap.put("pay_amount", payAmount);
//// orderPaymentMap.put("pay_amount", 0.01);
// orderPaymentMap.put("pay_amount_source", 3);
//
// Map<String, Integer> processedStateMap = new HashMap<>();
// processedStateMap.put("processed_state", 0);
//
// Map<String, Object> jsonDataMap = new HashMap<>();
// jsonDataMap.put("order_payment", orderPaymentMap);
// jsonDataMap.putAll(processedStateMap);
//
// Gson gson = new GsonBuilder().setPrettyPrinting().create();
// String json = gson.toJson(jsonDataMap);
//
// String a = "<JSON>"+json+"</JSON>";
// log.info("json:{}",a);
//
// String url=m.print("4004872261",a, System.currentTimeMillis()+random6.toString());
//
// // 使用 Fastjson JSON 字符串解析为 JSONObject
// JSONObject jsonObject = JSON.parseObject(url);
//
// // JSONObject 转换为 Map
// Map<String, Object> map = jsonObject.getInnerMap();
// //获取支付参数
// log.info("url:{}",url);
// return getSuccessResult(map.get("error_description"));
String s = printerService.printIndex(payAmount, TokenUtil.getNowAccountInfo().getStoreId());
return getSuccessResult(s);
}
@GetMapping("doGet")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Methods m=Methods.getInstance();
// m.init(APP_KEY,APP_SECRET);
m.init("26845ee50f13467cacbaa099489a2fc152abd6f5");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//测试数据
List<Test> testList = new ArrayList<Test>();
Test t1 = new Test("麻辣牛肉", 23.00, 1,"1");
Test t2 = new Test("麻辣牛肉", 23.00, 2,"2");
Test t3 = new Test("精品千层肚", 24.00, 3,"3");
Test t4 = new Test("麻辣牛肉", 23.00, 2,"1");
Test t5 = new Test("极品鲜毛肚", 26.00, 2,"1");
Test t6 = new Test("极品鲜毛肚", 26.00, 1,"2");
Test t7 = new Test("极品鲜毛肚", 26.00, 3,"2");
Test t8 = new Test("极品鲜毛肚", 26.00, 1,"1");
Test t9 = new Test("极品鲜毛肚", 26.00, 2,"3");
testList.add(t1);
testList.add(t2);
testList.add(t3);
testList.add(t4);
testList.add(t5);
testList.add(t6);
testList.add(t7);
testList.add(t8);
testList.add(t9);
Prient.setTestList(testList);
//关键代码自己的程序发送请求
//初始化控制器类
// Methods m=Methods.getInstance();
//初始化终端信息
m.init(APP_KEY, APP_SECRET);
//获取token
// m.getFreedomToken();
//刷新token
// m.refreshToken();
//添加授权
m.addPrinter("4004872261", "628023865417");
//打印
//终端编号 打印内容 订单号
//生成6位随机数
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
String url=m.print("4004872261", Prient.print(), "Z"+System.currentTimeMillis()+random6.toString());
response.sendRedirect(url);
out.flush();
out.close();
}
@PostMapping("getHandover")
public void getHandover(@RequestBody Map<String, Object> handover) {
// Methods m=Methods.getInstance();
// m.init("26845ee50f13467cacbaa099489a2fc152abd6f5");
//
// m.init(APP_KEY, APP_SECRET);
// Handover.setHandover(handover);
// // m.addPrinter("4004872261", "628023865417");
// Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
// String url=m.print("4004872261", Handover.printHandover(), "Z"+System.currentTimeMillis()+random6.toString());
printerService.getHandover(handover,TokenUtil.getNowAccountInfo().getStoreId());
}
/**
* 收银台商品打印
* @param oilOrder
*/
@PostMapping("printOilOrderReport")
public void oilOrderReport(@RequestBody Map<String, Object> oilOrder) {
// log.info("handover:{}",handover.toString());
// Methods m=Methods.getInstance();
// m.init("26845ee50f13467cacbaa099489a2fc152abd6f5");
//
// m.init(APP_KEY, APP_SECRET);
// PrintOilOrderReport.setHandover(handover);
// Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
// String url=m.print("4004872261", PrintOilOrderReport.printOilOrder(), "Z"+System.currentTimeMillis()+random6.toString());
printerService.oilOrderReport(oilOrder,TokenUtil.getNowAccountInfo().getStoreId());
}
@PostMapping("rePrintOilOrderReport")
public void reOilOrderReport(@RequestBody Map<String, Object> oilOrder) {
printerService.reOilOrderReport(oilOrder,TokenUtil.getNowAccountInfo().getStoreId());
}
/**
* 余额充值订单
* @param cardValue
*/
@PostMapping("printCardValueReport")
public void printCardValueReport(@RequestBody Map<String, Object> cardValue) {
// log.info("handover:{}",cardValue.toString());
// Methods m=Methods.getInstance();
// m.init("26845ee50f13467cacbaa099489a2fc152abd6f5");
//
// m.init(APP_KEY, APP_SECRET);
// PrintCardValueReport.setHandover(cardValue);
// Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
// String url=m.print("4004872261", PrintCardValueReport.printOilOrder(), "Z"+System.currentTimeMillis()+random6.toString());
printerService.printCardValueReport(cardValue, TokenUtil.getNowAccountInfo().getStoreId());
}
/**
* 油卡充值订单
* @param fuelDiesel
*/
@PostMapping("printFuelDieselReport")
public void printFuelDieselReport(@RequestBody Map<String, Object> fuelDiesel) {
// log.info("handover:{}",fuelDiesel.toString());
// Methods m=Methods.getInstance();
// m.init("26845ee50f13467cacbaa099489a2fc152abd6f5");
//
// m.init(APP_KEY, APP_SECRET);
// PrintFuelDieselReport.setHandover(fuelDiesel);
// Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
// String url=m.print("4004872261", PrintFuelDieselReport.printFeulOrder(), "Z"+System.currentTimeMillis()+random6.toString());
printerService.printFuelDieselReport(fuelDiesel,TokenUtil.getNowAccountInfo().getStoreId());
}
@PostMapping("printIntegralReport")
public void printIntegralReport(@RequestBody Map<String, Object> integral) {
printerService.printIntegralReport(integral,TokenUtil.getNowAccountInfo().getStoreId());
}
}

View File

@ -1,474 +0,0 @@
package com.fuint.business.printer.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.internal.util.codec.Base64;
import com.fuint.business.printer.entity.PrintDeviceInfo;
import com.fuint.business.printer.mapper.PrintDeviceInfoMapper;
import com.fuint.business.printer.service.PrintDeviceInfoService;
import com.fuint.business.printer.service.PrinterService;
import com.fuint.business.printer.template.*;
import com.fuint.business.printer.unit.Methods;
import com.fuint.business.printer.vo.OrderPayment;
import com.fuint.common.util.RedisUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yly.print_sdk_library.RequestMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class PrinterServiceImpl implements PrinterService {
private static String code = new String();
public static final String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuiQQsY9lbZhtRoZIaBVN\n" +
"XKuAme47UCUv3RG2AYJXKQ/TyXBBY0JULSf4637Ug7JqGR1ohHFpY9XotG5J/7Ne\n" +
"safQApAz8QHKOa6+jqS0edafegjE7P0KqfQWvjUIbhAl5iXmiGLA/R6zLXxk8+PU\n" +
"toRu1rmzMPCsbGRG0uHOZ0nYez1LSM4k4k8o4/dl7NDr2CqXrfYQCEvGAAJay/bg\n" +
"h1B05cqFNuhQGvv55EX377QVZX7V0ZipIZhlzoh8pA0ZpzW/KndZ+pRYw6cPLvey\n" +
"qymRnTlCSQUDYQRlxaDppPbZcU1Krb9tZUmhJp+R9l73tJX8yN5wzbq6HATZbD1v\n" +
"BwIDAQAB";
@Resource
PrintDeviceInfoService printDeviceInfoService;
@Resource
PrintDeviceInfoMapper printDeviceInfoMapper;
@Override
public Map<String, String> PostPush(String ciphertext, String nonce, String tag, String additional_data, String iv, String signature) {
System.out.println("PostMapping"+ciphertext);
code = new String();
Map<String, String> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("message","ok");
verifySign(ciphertext,signature);
try {
String decrypte = decrypte(ciphertext, iv, tag);
// 使用 Fastjson JSON 字符串解析为 JSONObject
JSONObject jsonObject = JSON.parseObject(decrypte);
// JSONObject 转换为 Map
Map<String, Object> map = jsonObject.getInnerMap();
//获取支付参数
Map<String, Object> orderPaymentMap = (Map<String, Object>) map.get("order_payment");
OrderPayment orderPayment = new OrderPayment();
orderPayment.setOrderPayment(orderPaymentMap.get("pay_amount").toString());
orderPayment.setScannedCode(orderPaymentMap.get("scanned_code").toString());
RedisUtil.set(orderPaymentMap.get("scanned_code").toString(), orderPaymentMap.get("pay_amount").toString(), 100);
code = orderPaymentMap.get("scanned_code").toString();
log.info("--获取打印回调--{}",jsonObject);
} catch (Exception e) {
throw new RuntimeException(e);
}
return objectObjectHashMap;
}
@Override
public String returnCode(String payAmount) {
String thisCode = code;
code = new String();
if (ObjectUtil.isEmpty(thisCode)) {
return "300";
}
// if (RedisUtil.get(thisCode)) {
return thisCode;
// }else {
// return getSuccessResult("301");
// }
}
@Override
public String getToken(Integer storeId) {
PrintDeviceInfo infoByStoreId = printDeviceInfoMapper.getInfoByStoreId(storeId);
if (ObjectUtil.isEmpty(infoByStoreId)){
return "error";
}
RequestMethod.getInstance().init(infoByStoreId.getAppKey(),infoByStoreId.getAppSecret());
try {
// String resJson = RequestMethod.getInstance().scanCodeModel_msign("4004872261", "628023865417");
String resJson = RequestMethod.getInstance().scanCodeModel_msign(infoByStoreId.getMachineCode(), infoByStoreId.getMsign());
log.info("--yilianyunAccessToken--{}",resJson);
JSONObject jsonObject = JSONObject.parseObject(resJson);
String errorDescription = jsonObject.getString("error_description"); // success
if ("success".equals(errorDescription)) {
JSONObject body = jsonObject.getJSONObject("body");
String accessToken = body.getString("access_token");
RedisUtil.set("scancodemodel+"+storeId,accessToken,60*60*24);
return accessToken;
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return "error";
}
@Override
public String printIndex(BigDecimal payAmount,Integer storeId){
Methods m=Methods.getInstance();
Object accessToken = RedisUtil.get("scancodemodel+" + storeId);
if (ObjectUtil.isNotEmpty(accessToken)) {
m.init(accessToken.toString());
} else {
String token = getToken(storeId);
if ("error".equals(token)) {
throw new RuntimeException("获取参数失败,无法打印");
}else {
m.init(token);
}
}
PrintDeviceInfo infoByStoreId = printDeviceInfoMapper.getInfoByStoreId(storeId);
m.init(infoByStoreId.getAppKey(), infoByStoreId.getAppSecret());
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
// HashMap<String, String> objectObjectHashMap = new HashMap<>();
Map<String, Object> orderPaymentMap = new HashMap<>();
orderPaymentMap.put("pay_amount", payAmount);
// orderPaymentMap.put("pay_amount", 0.01);
orderPaymentMap.put("pay_amount_source", 3);
Map<String, Integer> processedStateMap = new HashMap<>();
processedStateMap.put("processed_state", 0);
Map<String, Object> jsonDataMap = new HashMap<>();
jsonDataMap.put("order_payment", orderPaymentMap);
jsonDataMap.putAll(processedStateMap);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(jsonDataMap);
String jsonData = "<JSON>"+json+"</JSON>";
log.info("json:{}",jsonData);
String url=m.print(infoByStoreId.getMachineCode(),jsonData, System.currentTimeMillis()+random6.toString());
// 使用 Fastjson JSON 字符串解析为 JSONObject
JSONObject jsonObject = JSON.parseObject(url);
// JSONObject 转换为 Map
Map<String, Object> map = jsonObject.getInnerMap();
//获取支付参数
log.info("url:{}",url);
if (ObjectUtil.isNotEmpty(url)) {
String error = jsonObject.getString("error");
if ("18".equals(error)) {
getToken(storeId);
printIndex(payAmount,storeId);
}
}
return map.get("error_description").toString();
}
// 打印交接班
public void getHandover(Map<String, Object> handover,Integer storeId) {
PrintDeviceInfo infoByStoreId = printDeviceInfoMapper.getInfoByStoreId(storeId);
Methods m=Methods.getInstance();
Object accessToken = RedisUtil.get("scancodemodel+" + storeId);
if (ObjectUtil.isNotEmpty(accessToken)) {
m.init(accessToken.toString());
} else {
String token = getToken(storeId);
if ("error".equals(token)) {
throw new RuntimeException("获取参数失败,无法打印");
}else {
m.init(token);
}
}
m.init(infoByStoreId.getAppKey(), infoByStoreId.getAppSecret());
Handover.setHandover(handover);
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
String url=m.print(infoByStoreId.getMachineCode(), Handover.printHandover(), "Z"+System.currentTimeMillis()+random6.toString());
}
public void oilOrderReport(Map<String, Object> oilOrder,Integer storeId) {
PrintDeviceInfo infoByStoreId = printDeviceInfoMapper.getInfoByStoreId(storeId);
Methods m=Methods.getInstance();
Object accessToken = RedisUtil.get("scancodemodel+" + storeId);
if (ObjectUtil.isNotEmpty(accessToken)) {
m.init(accessToken.toString());
} else {
String token = getToken(storeId);
if ("error".equals(token)) {
throw new RuntimeException("获取参数失败,无法打印");
}else {
m.init(token);
} }
m.init(infoByStoreId.getAppKey(), infoByStoreId.getAppSecret());
PrintOilOrderReport.setHandover(oilOrder);
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
String url=m.print(infoByStoreId.getMachineCode(), PrintOilOrderReport.printOilOrder(), "Z"+System.currentTimeMillis()+random6.toString());
if (ObjectUtil.isNotEmpty(url)) {
// 使用 Fastjson JSON 字符串解析为 JSONObject
JSONObject jsonObject = JSON.parseObject(url);
String error = jsonObject.getString("error");
if ("18".equals(error)) {
getToken(storeId);
reOilOrderReport(oilOrder,storeId);
}
}
}
public void reOilOrderReport(Map<String, Object> oilOrder,Integer storeId) {
PrintDeviceInfo infoByStoreId = printDeviceInfoMapper.getInfoByStoreId(storeId);
Methods m=Methods.getInstance();
Object accessToken = RedisUtil.get("scancodemodel+" + storeId);
if (ObjectUtil.isNotEmpty(accessToken)) {
m.init(accessToken.toString());
} else {
String token = getToken(storeId);
if ("error".equals(token)) {
throw new RuntimeException("获取参数失败,无法打印");
}else {
m.init(token);
}
}
m.init(infoByStoreId.getAppKey(), infoByStoreId.getAppSecret());
RePrintOilOrderReport.setHandover(oilOrder);
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
String url=m.print(infoByStoreId.getMachineCode(), RePrintOilOrderReport.rePrintOilOrder(), "Z"+System.currentTimeMillis()+random6.toString());
if (ObjectUtil.isNotEmpty(url)) {
// 使用 Fastjson JSON 字符串解析为 JSONObject
JSONObject jsonObject = JSON.parseObject(url);
String error = jsonObject.getString("error");
if ("18".equals(error)) {
getToken(storeId);
reOilOrderReport(oilOrder,storeId);
}
}
}
public void printCardValueReport(Map<String, Object> cardValue,Integer storeId) {
PrintDeviceInfo infoByStoreId = printDeviceInfoMapper.getInfoByStoreId(storeId);
log.info("handover:{}",cardValue.toString());
Methods m=Methods.getInstance();
Object accessToken = RedisUtil.get("scancodemodel+" + storeId);
if (ObjectUtil.isNotEmpty(accessToken)) {
m.init(accessToken.toString());
} else {
String token = getToken(storeId);
if ("error".equals(token)) {
throw new RuntimeException("获取参数失败,无法打印");
}else {
m.init(token);
}
}
m.init(infoByStoreId.getAppKey(), infoByStoreId.getAppSecret());
PrintCardValueReport.setHandover(cardValue);
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
String url=m.print(infoByStoreId.getMachineCode(), PrintCardValueReport.printOilOrder(), "Z"+System.currentTimeMillis()+random6.toString());
if (ObjectUtil.isNotEmpty(url)) {
// 使用 Fastjson JSON 字符串解析为 JSONObject
JSONObject jsonObject = JSON.parseObject(url);
String error = jsonObject.getString("error");
if ("18".equals(error)) {
getToken(storeId);
reOilOrderReport(cardValue,storeId);
}
}
}
public void printFuelDieselReport(Map<String, Object> fuelDiesel,Integer storeId) {
PrintDeviceInfo infoByStoreId = printDeviceInfoMapper.getInfoByStoreId(storeId);
log.info("handover:{}",fuelDiesel.toString());
Methods m=Methods.getInstance();
Object accessToken = RedisUtil.get("scancodemodel+" + storeId);
if (ObjectUtil.isNotEmpty(accessToken)) {
m.init(accessToken.toString());
} else {
String token = getToken(storeId);
if ("error".equals(token)) {
throw new RuntimeException("获取参数失败,无法打印");
}else {
m.init(token);
}
}
m.init(infoByStoreId.getAppKey(), infoByStoreId.getAppSecret());
PrintFuelDieselReport.setHandover(fuelDiesel);
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
String url=m.print(infoByStoreId.getMachineCode(), PrintFuelDieselReport.printFeulOrder(), "Z"+System.currentTimeMillis()+random6.toString());
if (ObjectUtil.isNotEmpty(url)) {
// 使用 Fastjson JSON 字符串解析为 JSONObject
JSONObject jsonObject = JSON.parseObject(url);
String error = jsonObject.getString("error");
if ("18".equals(error)) {
getToken(storeId);
reOilOrderReport(fuelDiesel,storeId);
}
}
}
public void printIntegralReport(Map<String, Object> integral,Integer storeId) {
PrintDeviceInfo infoByStoreId = printDeviceInfoMapper.getInfoByStoreId(storeId);
log.info("handover:{}",integral.toString());
Methods m=Methods.getInstance();
Object accessToken = RedisUtil.get("scancodemodel+" + storeId);
if (ObjectUtil.isNotEmpty(accessToken)) {
m.init(accessToken.toString());
} else {
String token = getToken(storeId);
if ("error".equals(token)) {
throw new RuntimeException("获取参数失败,无法打印");
}else {
m.init(token);
}
}
m.init(infoByStoreId.getAppKey(), infoByStoreId.getAppSecret());
PrintIntegralReport.setHandover(integral);
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
String url=m.print(infoByStoreId.getMachineCode(), PrintIntegralReport.printIntegral(), "Z"+System.currentTimeMillis()+random6.toString());
if (ObjectUtil.isNotEmpty(url)) {
// 使用 Fastjson JSON 字符串解析为 JSONObject
JSONObject jsonObject = JSON.parseObject(url);
String error = jsonObject.getString("error");
if ("18".equals(error)) {
getToken(storeId);
reOilOrderReport(integral,storeId);
}
}
}
public void printTagReport(Map<String, Object> integral,Integer storeId) {
PrintDeviceInfo infoByStoreId = printDeviceInfoMapper.getInfoByStoreId(storeId);
log.info("handover:{}",integral.toString());
Methods m=Methods.getInstance();
Object accessToken = RedisUtil.get("scancodemodel+" + storeId);
if (ObjectUtil.isNotEmpty(accessToken)) {
m.init(accessToken.toString());
} else {
String token = getToken(storeId);
if ("error".equals(token)) {
throw new RuntimeException("获取参数失败,无法打印");
}else {
m.init(token);
}
}
m.init(infoByStoreId.getAppKey(), infoByStoreId.getAppSecret());
// PrintIntegralReport.setHandover(integral);
StringBuilder sb=new StringBuilder();
sb.append("<center>收款码打印\r\n</center>");
sb.append("------------------------------------\r\n");
sb.append("油枪:¥"+integral.get("gunName")+"\r\n");
sb.append("升数:¥"+integral.get("oilNum")+"\r\n");
sb.append("价格:¥"+integral.get("payAmount")+"\r\n");
sb.append("------------------------------------\r\n");
sb.append("<center>谢谢惠顾,欢迎下次光临!</center>");
Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
String url=m.print(infoByStoreId.getMachineCode(), sb.toString(), "Z"+System.currentTimeMillis()+random6.toString());
if (ObjectUtil.isNotEmpty(url)) {
// 使用 Fastjson JSON 字符串解析为 JSONObject
JSONObject jsonObject = JSON.parseObject(url);
String error = jsonObject.getString("error");
if ("18".equals(error)) {
getToken(storeId);
reOilOrderReport(integral,storeId);
}
}
}
public void verifySign(String ciphertext, String signature) {
// 交易报文中ciphertext与signature
byte[] signatureByte = Base64.decodeBase64String(signature);
byte[] publicKeyByte = Base64.decodeBase64String(publicKey);
try {
System.out.println("验签结果:" + verify(ciphertext.getBytes(), signatureByte, publicKeyByte));
boolean verify = verify(ciphertext.getBytes(), signatureByte, publicKeyByte);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}
/**
* 第二步解析参数
*/
public String decrypte(String ciphertext,
String iv,
String tag) throws Exception {
// 交易报文中ciphertext与tag和iv
// String ciphertext = "";
// String tag = "";
// String iv = "";
// 应用设置 -> K8推送 -> 设置 -> 接口加签名方式密钥 -> 应用公钥
String key = "e0292ae66d9d21feaa895370d3a875aa";
try {
String plaintext = decrypte(Base64.decodeBase64String(ciphertext), key.getBytes(), iv.getBytes(), Base64.decodeBase64String(tag));
System.out.println(plaintext);
return plaintext;
} catch (Exception e) {
throw new Exception(e);
}
}
private static boolean verify(byte[] data, byte[] signature, byte[] publicKey) throws GeneralSecurityException {
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(pubKey);
sig.update(data);
return sig.verify(signature);
}
/**
* AES-GCM-256对称解密
*/
private static String decrypte(byte[] encryptedBytes, byte[] keyBytes, byte[] ivBytes, byte[] tagBytes) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, ivBytes);
cipher.init(Cipher.DECRYPT_MODE, secretKey, gcmParameterSpec);
cipher.updateAAD("transaction".getBytes(StandardCharsets.UTF_8));
cipher.update(encryptedBytes);
byte[] decryptedBytes = cipher.doFinal(tagBytes);
String decryptedString = new String(decryptedBytes, StandardCharsets.UTF_8);
byte[] content = Base64.decodeBase64String(decryptedString);
return new String(content);
}
}