对接打印机

This commit is contained in:
mi9688
2024-12-10 17:51:32 +08:00
parent 81e5429ee9
commit 5e76aa6305
12 changed files with 346 additions and 50 deletions

View File

@ -22,9 +22,10 @@ public class MqttTest {
*/
@GetMapping("/publish")
public String test(){
pubMqttClient.publish(1,true,"/topic/test","mqtt测试主题发布消息");
pubMqttClient.publish(1,true,"/10086/155/cmd/down","mqtt测试主题发布消息");
return "test";
}
/**
* 测试创建mqtt客户端
*/

View File

@ -0,0 +1,33 @@
package com.fastbee.data.controller.printer;
import com.fastbee.common.core.domain.AjaxResultPro;
import com.fastbee.data.controller.printer.yilianyun.YiLianYunPrintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/printer")
public class PrinterController {
@Autowired
private YiLianYunPrintService printerService;
/**
* 文本内容打印
*/
@PostMapping("/text/do")
public AjaxResultPro textPrint(@RequestBody String content){
printerService.textPrint(content);
return AjaxResultPro.success();
}
/**
* 图片打印
*/
@PostMapping("/image/do")
public AjaxResultPro imagePrint(@RequestBody String imageUrl){
printerService.imagePrint(imageUrl);
return AjaxResultPro.success();
}
}

View File

@ -0,0 +1,70 @@
package com.fastbee.data.controller.printer.yilianyun;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Component
public class YiLianYunAuthorizationService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private String clientId = "1044721086";
private String clientSecret = "dfb761fb75768df0066f9614dc507531";
private String grantType = "client_credentials";
private String sign;
private String scope = "all";
private Long timestamp;
private String id = "550e8400-e29b-41d4-a716-446655440000";
private String endpoint = "https://open-api.10ss.net/v2/oauth/oauth";
public String getAccessToken() {
String cacheToken = stringRedisTemplate.opsForValue().get("yilianyun:user:token");
if (!Objects.isNull(cacheToken)){
System.err.println("缓存中获取token:"+cacheToken);
return cacheToken;
}
String token = getAuth();
// System.err.println("从服务器获取token:"+token);
// return token;
stringRedisTemplate.opsForValue().set("yilianyun:user:token",token,3600*24*20, TimeUnit.MILLISECONDS);
return stringRedisTemplate.opsForValue().get("yilianyun:access_token");
}
public String getAuth() {
timestamp = System.currentTimeMillis();
sign=YiLianYunUtil.getSign(timestamp);
//构建请求体
Map<String,Object> param= new HashMap<>();
param.put("client_id",clientId);
param.put("grant_type",grantType);
param.put("sign",sign);
param.put("scope",scope);
param.put("timestamp",timestamp);
param.put("id",id);
YiLianYunBaseService yiLianYunBaseService = new YiLianYunBaseService();
JSONObject resp = yiLianYunBaseService.baseRequest(endpoint, param);
System.err.println(resp);
return resp.getStr("access_token");
}
public static void main(String[] args) {
YiLianYunPrintService yiLianYunPrintService = new YiLianYunPrintService();
// yiLianYunPrintService.print("<FS2>您好</FS2>");
}
}

View File

@ -0,0 +1,26 @@
package com.fastbee.data.controller.printer.yilianyun;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fastbee.common.exception.ServiceException;
import java.util.Map;
public class YiLianYunBaseService {
/**
* 基本请求
* @param body 请求负载
* @return 请求结果 data
*/
public JSONObject baseRequest(String url, Map<String,Object> body){
String jsonStr = JSONUtil.toJsonStr(body);
String respStr = HttpUtil.post(url, jsonStr);
JSONObject resp = JSONUtil.parseObj(respStr);
System.err.println(resp);
if(!resp.get("error").toString().equals("0")){
throw new ServiceException(resp.get("error_description").toString());
}
return resp.getJSONObject("body");
}
}

View File

@ -0,0 +1,57 @@
package com.fastbee.data.controller.printer.yilianyun;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class YiLianYunPrintService {
@Autowired
private YiLianYunAuthorizationService authorizationService;
private String endpoint = "https://open-api.10ss.net/v2/print/index";
/**
* 文本打印
* @param content 打印内容
*/
public void textPrint(String content){
YiLianYunBaseService yiLianYunBaseService = new YiLianYunBaseService();
//构建请求参数
// 创建一个 HashMap 来存储属性
Map<String, Object> attributes = new HashMap<>();
// 添加属性到 HashMap
Long timestamp = System.currentTimeMillis();
attributes.put("client_id", YiLianYunUtil.clientId);
attributes.put("sign", YiLianYunUtil.getSign(timestamp));
attributes.put("timestamp", timestamp);
attributes.put("id", "550e8400-e29b-41d4-a716-446655440000");
attributes.put("access_token", authorizationService.getAccessToken());
attributes.put("machine_code", "4004899747");
attributes.put("origin_id", "123");
attributes.put("content", content);
JSONObject resp = yiLianYunBaseService.baseRequest(endpoint, attributes);
}
/**
* 图片打印
*/
public void imagePrint(String pictureUrl){
YiLianYunBaseService yiLianYunBaseService = new YiLianYunBaseService();
//构建请求参数
// 创建一个 HashMap 来存储属性
Map<String, Object> attributes = new HashMap<>();
// 添加属性到 HashMap
Long timestamp = System.currentTimeMillis();
attributes.put("client_id", YiLianYunUtil.clientId);
attributes.put("sign", YiLianYunUtil.getSign(timestamp));
attributes.put("timestamp", timestamp);
attributes.put("id", "550e8400-e29b-41d4-a716-446655440000");
attributes.put("access_token", authorizationService.getAccessToken());
attributes.put("machine_code", "4004899747");
attributes.put("origin_id", "123");
attributes.put("picture_url", pictureUrl);
JSONObject resp = yiLianYunBaseService.baseRequest(endpoint, attributes);
}
}

View File

@ -0,0 +1,12 @@
package com.fastbee.data.controller.printer.yilianyun;
import cn.hutool.crypto.digest.DigestUtil;
public class YiLianYunUtil {
public static String clientId = "1044721086";
public static String clientSecret = "dfb761fb75768df0066f9614dc507531";
public static String getSign(Long timestamp) {
String param = clientId + timestamp + clientSecret;
return DigestUtil.md5Hex(param).toLowerCase();
}
}