优化打印机管理

This commit is contained in:
Jack 2024-11-22 00:20:40 +08:00
parent 50643690f1
commit 6a440d95bc
5 changed files with 139 additions and 80 deletions

View File

@ -1,43 +0,0 @@
package com.suisung.mall.common.pojo.res;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 飞鹅云打印机接口响应结果
*
* @author 潘军杰
* @since 2024-10-30 10:10:02
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FeiePrinterApiDataRes implements Serializable {
private static final long serialVersionUID = 2L;
@ApiModelProperty(value = "成功信息数组")
private String[] ok;
@ApiModelProperty(value = "失败信息数组")
private String[] no;
@ApiModelProperty(value = "失败指导信息数组")
private NoGuide[] noGuide;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class NoGuide implements Serializable {
private static final long serialVersionUID = 3L;
@ApiModelProperty(value = "设备编号标识")
private String sn;
@ApiModelProperty(value = "指导图片路径")
private String imgUrl;
}
}

View File

@ -1,6 +1,5 @@
package com.suisung.mall.common.pojo.res; package com.suisung.mall.common.pojo.res;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
@ -20,23 +19,72 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
public class FeiePrinterApiRes<T> implements Serializable { public class FeiePrinterApiRes<T> implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "返回码正确返回0【注意结果正确与否的判断请用此返回参数】 ,错误返回非零。")
private Integer ret;
@ApiModelProperty(value = "结果提示信息正确返回”ok”如果有错误返回错误信息。")
private String msg;
@ApiModelProperty(value = "服务器程序执行时间,单位:毫秒。")
private Long serverExecutedTime;
@ApiModelProperty("数据类型和内容详看私有返回参数data如果有错误返回null。")
private T data;
public FeiePrinterApiRes fail(String msg) { public FeiePrinterApiRes fail(String msg) {
this.ret = 1001; this.ret = 1001;
this.msg = msg; this.msg = msg;
this.serverExecutedTime = System.currentTimeMillis()/1000; this.serverExecutedTime = System.currentTimeMillis() / 1000;
return this; return this;
} }
@ApiModelProperty(value = "返回码正确返回0【注意结果正确与否的判断请用此返回参数】 ,错误返回非零。") // @Data
private Integer ret; // @NoArgsConstructor
// @AllArgsConstructor
public class FeiePrinterApiData implements Serializable {
private static final long serialVersionUID = 2L;
@ApiModelProperty(value = "结果提示信息正确返回”ok”如果有错误返回错误信息。") @ApiModelProperty(value = "成功信息数组")
private String msg; private String[] ok;
@ApiModelProperty(value = "服务器程序执行时间,单位:毫秒。") @ApiModelProperty(value = "失败信息数组")
private Long serverExecutedTime; private String[] no;
@ApiModelProperty("数据类型和内容详看私有返回参数data如果有错误返回null。") @ApiModelProperty(value = "失败指导信息数组")
private T data; private FeiePrinterApiDataNoGuide[] noGuide;
public String[] getOk() {
return ok;
}
public void setOk(String[] ok) {
this.ok = ok;
}
public FeiePrinterApiDataNoGuide[] getNoGuide() {
return noGuide;
}
public void setNoGuide(FeiePrinterApiDataNoGuide[] noGuide) {
this.noGuide = noGuide;
}
public String[] getNo() {
return no;
}
public void setNo(String[] no) {
this.no = no;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FeiePrinterApiDataNoGuide implements Serializable {
private static final long serialVersionUID = 3L;
@ApiModelProperty(value = "设备编号标识")
private String sn;
@ApiModelProperty(value = "指导图片路径")
private String imgUrl;
}
}
} }

View File

@ -24,15 +24,12 @@ import lombok.NoArgsConstructor;
public class SFExpressApiRes { public class SFExpressApiRes {
@ApiModelProperty(value = "错误码") @ApiModelProperty(value = "错误码")
private Integer error_code; private Integer error_code;
@ApiModelProperty(value = "错误信息") @ApiModelProperty(value = "错误信息")
private String error_msg; private String error_msg;
@ApiModelProperty(value = "响应结果")
private Object result;
@ApiModelProperty(value = "错误数据") @ApiModelProperty(value = "错误数据")
public Object error_data; public Object error_data;
@ApiModelProperty(value = "响应结果")
private Object result;
public SFExpressApiRes fail(Integer errorCode, String errorMsg) { public SFExpressApiRes fail(Integer errorCode, String errorMsg) {
return new SFExpressApiRes(errorCode, errorMsg, null, null); return new SFExpressApiRes(errorCode, errorMsg, null, null);

View File

@ -115,6 +115,7 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
Integer userId = user.getId() == null ? 0 : user.getId(); Integer userId = user.getId() == null ? 0 : user.getId();
record.setStore_id(Integer.parseInt(user.getStore_id())); record.setStore_id(Integer.parseInt(user.getStore_id()));
// 默认不向厂家绑定打印机 // 默认不向厂家绑定打印机
record.setStatus(ConstantError.Disable2); record.setStatus(ConstantError.Disable2);
record.setFlag(record.getStatus()); record.setFlag(record.getStatus());
@ -242,12 +243,12 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
if (existRecord2 == null || existRecord2.getPrinter_id() <= 0) { if (existRecord2 == null || existRecord2.getPrinter_id() <= 0) {
// 打印机从未加入到厂家到情况往厂商添加打印机 // 打印机从未加入到厂家到情况往厂商添加打印机
// 格式"922441475#r6ZXPvHH#核销柜台"; // 格式"922441475#r6ZXPvHH#核销柜台";
boolean success = feieUtil.addPrinter(String.format("%s#%s#%s", record.getPrinter_sn(), record.getPrinter_key(), record.getPrinter_name())); Pair<Boolean,String> retPair = feieUtil.addPrinter(String.format("%s#%s#%s", record.getPrinter_sn(), record.getPrinter_key(), record.getPrinter_name()));
if (success) { if (retPair.getFirst()) {
updateWrapper.set("flag", ConstantError.Enable); updateWrapper.set("flag", ConstantError.Enable);
updateWrapper.set("status",ConstantError.Enable); updateWrapper.set("status",ConstantError.Enable);
} else { } else {
msg = msg + ",但打印机绑定未成功,请检查打印机编号和密钥是否正确。"; msg = msg + ",但打印机绑定未成功,请检查打印机编号和密钥是否填写正确。";
updateWrapper.set("flag", ConstantError.Disable2); updateWrapper.set("flag", ConstantError.Disable2);
updateWrapper.set("status",ConstantError.Disable2); updateWrapper.set("status",ConstantError.Disable2);
} }
@ -275,8 +276,8 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
// 判断有没有权限 // 判断有没有权限
UserDto user = getCurrentUser(); UserDto user = getCurrentUser();
if (!user.isStore()) { if (user==null || !user.isStore()) {
return CommonResult.failed("用户信息异常!"); return CommonResult.failed("无权操作!");
} }
ShopStorePrinter record = getById(printerId); ShopStorePrinter record = getById(printerId);
@ -284,18 +285,18 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
return CommonResult.failed("打票机不存在,无法操作!"); return CommonResult.failed("打票机不存在,无法操作!");
} }
boolean success;
if (ConstantError.Enable.equals(status)) { if (ConstantError.Enable.equals(status)) {
// 向厂家新增打印机 // 向厂家新增打印机
// 格式"922441475#r6ZXPvHH#核销柜台"; // 格式"922441475#r6ZXPvHH#核销柜台";
success = feieUtil.addPrinter(String.format("%s#%s#%s", record.getPrinter_sn(), record.getPrinter_key(), record.getPrinter_name())); Pair<Boolean,String> retPair = feieUtil.addPrinter(String.format("%s#%s#%s", record.getPrinter_sn(), record.getPrinter_key(), record.getPrinter_name()));
if (!success) { if (!retPair.getFirst()) {
return CommonResult.failed("打印机绑定未成功,操作失败。"); return CommonResult.failed(retPair.getSecond());
} }
} else { } else {
status = ConstantError.Disable2; status = ConstantError.Disable2;
// 向厂家解绑打印机 // 向厂家解绑打印机
success = feieUtil.delPrinter(record.getPrinter_sn()); boolean success = feieUtil.delPrinter(record.getPrinter_sn());
if (!success) { if (!success) {
return CommonResult.failed("打印机解绑定未成功,操作失败。"); return CommonResult.failed("打印机解绑定未成功,操作失败。");
} }
@ -323,8 +324,8 @@ public class ShopStorePrinterServiceImpl extends BaseServiceImpl<ShopStorePrinte
// 判断有没有权限 // 判断有没有权限
UserDto user = getCurrentUser(); UserDto user = getCurrentUser();
if (!user.isStore()) { if (user==null || !user.isStore()) {
return CommonResult.failed("用户信息异常!"); return CommonResult.failed("无权操作!");
} }
ShopStorePrinter record = getById(printer_id); ShopStorePrinter record = getById(printer_id);

View File

@ -1,8 +1,11 @@
package com.suisung.mall.shop.store.utis; package com.suisung.mall.shop.store.utis;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.suisung.mall.common.pojo.res.FeiePrinterApiDataRes; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.suisung.mall.common.pojo.res.FeiePrinterApiRes; import com.suisung.mall.common.pojo.res.FeiePrinterApiRes;
import com.suisung.mall.common.pojo.vo.ShopStoreOrderProductPrintVO; import com.suisung.mall.common.pojo.vo.ShopStoreOrderProductPrintVO;
import com.suisung.mall.common.utils.JsonUtil; import com.suisung.mall.common.utils.JsonUtil;
@ -91,18 +94,25 @@ public class FeieUtil {
* 每次最多添加100台 * 每次最多添加100台
* @return * @return
*/ */
public boolean addPrinter(String printerInfo) { public Pair<Boolean,String> addPrinter(String printerInfo) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("printerContent", printerInfo)); nvps.add(new BasicNameValuePair("printerContent", printerInfo));
FeiePrinterApiRes<FeiePrinterApiDataRes> reps = sendHttpPost("Open_printerAddlist", nvps); JSONObject reps = sendHttpPost2("Open_printerAddlist", nvps);
logger.info("飞鹅添加打印机返回数据:{}", reps); if (reps == null) {
if (reps != null && reps.getRet().equals(0)) { return Pair.of(false, "打印机未返回数据,操作失败");
// && reps.getData()!=null && reps.getData().getOk()!=null &&reps.getData().getOk().length>0
return true;
} }
logger.error("飞鹅添加打印机操作失败:{}", reps.getMsg()); if (ObjUtil.equals(reps.get("ret"), 0) && ObjUtil.isNotEmpty(reps.get("data"))) {
return false; if (ObjUtil.isNotEmpty(reps.getByPath("data.ok"))) {
return Pair.of(true, "打印机完成绑定,操作成功!");
} else if (ObjUtil.isNotEmpty(reps.getByPath("data.no"))) {
logger.error("飞鹅添加打印机操作失败:{}", reps.getByPath("data.no"));
return Pair.of(false, reps.getByPath("data.no.0") + ",操作失败!");
}
}
logger.error("飞鹅添加打印机操作失败:{}", reps);
return Pair.of(false, reps.getByPath("msg") + ",操作失败!");
} }
/** /**
@ -115,7 +125,7 @@ public class FeieUtil {
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("snlist", snList)); nvps.add(new BasicNameValuePair("snlist", snList));
FeiePrinterApiRes<FeiePrinterApiDataRes> reps = sendHttpPost("Open_printerDelList", nvps); FeiePrinterApiRes reps = sendHttpPost("Open_printerDelList", nvps);
logger.info("飞鹅删除打印机返回数据:{}", reps); logger.info("飞鹅删除打印机返回数据:{}", reps);
if (reps != null && reps.getRet().equals(0)) { if (reps != null && reps.getRet().equals(0)) {
return true; return true;
@ -265,6 +275,52 @@ public class FeieUtil {
return null; return null;
} }
private JSONObject sendHttpPost2(String apiName, List<NameValuePair> paramPair) {
if (StrUtil.isBlank(apiName) || CollectionUtil.isEmpty(paramPair)) {
logger.info("缺少必要参数!");
return null;
}
//通过POST请求发送打印信息到服务器
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(30000)//读取超时
.setConnectTimeout(30000)//连接超时
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
HttpPost post = new HttpPost(URL);
paramPair.add(new BasicNameValuePair("user", USER));
String STIME = String.valueOf(System.currentTimeMillis() / 1000);
paramPair.add(new BasicNameValuePair("stime", STIME));
paramPair.add(new BasicNameValuePair("sig", signature(USER, UKEY, STIME)));
paramPair.add(new BasicNameValuePair("apiname", apiName));
CloseableHttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(paramPair, "utf-8"));
response = httpClient.execute(post);
int stateCode = response.getStatusLine().getStatusCode();
if (stateCode == 200) {
HttpEntity httpentity = response.getEntity();
if (httpentity != null) {
String result = EntityUtils.toString(httpentity);
logger.debug("飞鹅云返回到信息:{}", result);
return JSONUtil.parseObj(result);
}
}
} catch (Exception e) {
logger.error("发送飞鹅接口请求失败:{}", e.getMessage());
} finally {
close(response, post, httpClient);
}
return null;
}
/** /**
* 生成商品的打印字符串 * 生成商品的打印字符串
* *