zy-acs-common/src/main/java/com/zy/acs/common/utils/QrCodeCodecSupport.java
@@ -1,5 +1,7 @@ package com.zy.acs.common.utils; import com.zy.acs.framework.common.Cools; import java.math.BigInteger; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -22,6 +24,16 @@ public static int qrCodeBytes() { return config.bytes; } public static String normalize(String qrCode) { if (Cools.isEmpty(qrCode)) { throw new IllegalArgumentException("qrCode must not be empty"); } if (config.mode == Mode.NUMERIC) { return normalizeNumericValue(qrCode, config.bytes, config.displayLength); } return normalizeStringValue(qrCode, config.bytes, config.charset); } public static String decode(byte[] bytes, int pos) { @@ -48,10 +60,7 @@ if (value.isEmpty()) { value = "0"; } BigInteger numeric = new BigInteger(value); if (numeric.signum() < 0) { throw new IllegalArgumentException("qrCode must not be negative: " + qrCode); } BigInteger numeric = parseUnsignedNumericValue(value, qrCode); byte[] raw = numeric.toByteArray(); if (raw.length > 1 && raw[0] == 0) { raw = Arrays.copyOfRange(raw, 1, raw.length); @@ -65,15 +74,40 @@ } private static byte[] toFixedStringBytes(String qrCode, int size, Charset charset) { String value = qrCode == null ? "" : qrCode; byte[] raw = value.getBytes(charset); String value = normalizeStringValue(qrCode, size, charset); return value.getBytes(charset); } private static String normalizeNumericValue(String qrCode, int size, int displayLength) { BigInteger numeric = parseUnsignedNumericValue(qrCode, qrCode); byte[] raw = numeric.toByteArray(); if (raw.length > 1 && raw[0] == 0) { raw = Arrays.copyOfRange(raw, 1, raw.length); } if (raw.length > size) { throw new IllegalArgumentException("qrCode exceeds configured byte size: " + qrCode); } byte[] result = new byte[size]; Arrays.fill(result, (byte) '0'); System.arraycopy(raw, 0, result, size - raw.length, raw.length); return result; return Utils.zeroFill(numeric.toString(), displayLength); } private static String normalizeStringValue(String qrCode, int size, Charset charset) { String value = qrCode == null ? "" : qrCode.trim(); byte[] raw = value.getBytes(charset); if (raw.length != size) { throw new IllegalArgumentException("qrCode byte length must be " + size + ": " + qrCode); } return value; } private static BigInteger parseUnsignedNumericValue(String value, String originalValue) { if (!value.chars().allMatch(Character::isDigit)) { throw new IllegalArgumentException("qrCode must be numeric: " + originalValue); } BigInteger numeric = new BigInteger(value); if (numeric.signum() < 0) { throw new IllegalArgumentException("qrCode must not be negative: " + originalValue); } return numeric; } private static String stripPadding(String value) { zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/CodeController.java
@@ -3,9 +3,8 @@ import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.zy.acs.common.constant.CommonConstant; import com.zy.acs.common.utils.GsonUtils; import com.zy.acs.common.utils.Utils; import com.zy.acs.common.utils.QrCodeCodecSupport; import com.zy.acs.framework.common.Cools; import com.zy.acs.framework.common.R; import com.zy.acs.framework.exception.CoolException; @@ -124,7 +123,7 @@ @OperationLog("Create Code") @PostMapping("/code/save") public R save(@RequestBody Code code) { code.setData(Utils.zeroFill(code.getData(), CommonConstant.QR_CODE_LEN)); code.setData(QrCodeCodecSupport.normalize(code.getData())); code.setUuid("code".concat(code.getData())); code.setCreateBy(getLoginUserId()); code.setCreateTime(new Date()); @@ -145,7 +144,7 @@ boolean cornerChanged = origin != null && code.getCorner() != null && !Objects.equals(origin.getCorner(), code.getCorner()); code.setData(Utils.zeroFill(code.getData(), CommonConstant.QR_CODE_LEN)); code.setData(QrCodeCodecSupport.normalize(code.getData())); code.setUpdateBy(getLoginUserId()); code.setUpdateTime(new Date()); if (!codeService.updateById(code)) { @@ -212,10 +211,10 @@ Date now = new Date(); Long userId = getLoginUserId(); for (Map<String, Object> map : list) { Code code = Cools.convert(map, Code.class); code.setData(QrCodeCodecSupport.normalize(code.getData())); if (null != codeService.getCacheByData(code.getData())) { continue; } code.setData(Utils.zeroFill(code.getData(), CommonConstant.QR_CODE_LEN)); code.setUuid("code".concat(code.getData())); // code.setCorner(0); code.setScale(GsonUtils.toJson(Cools.add("x", 1).add("y", 1))); zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/LocController.java
@@ -2,7 +2,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.zy.acs.common.constant.CommonConstant; import com.zy.acs.common.utils.Utils; import com.zy.acs.framework.common.Cools; import com.zy.acs.framework.common.R; @@ -277,8 +276,7 @@ if (Cools.isEmpty(data)) { return null; } String codeData = Utils.zeroFill(data, CommonConstant.QR_CODE_LEN); Code code = codeService.getCacheByData(codeData); Code code = codeService.getCacheByData(data); if (code == null) { throw new CoolException("code " + data + " not exist"); } zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/MapController.java
@@ -3,6 +3,7 @@ import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zy.acs.common.utils.GsonUtils; import com.zy.acs.common.utils.QrCodeCodecSupport; import com.zy.acs.framework.common.Cools; import com.zy.acs.framework.common.R; import com.zy.acs.framework.common.SnowflakeIdWorker; @@ -251,6 +252,7 @@ List<Code> codes = codeService.list(); List<Code> codeArr = param.getCodeArr(); for (Code code : codeArr) { code.setData(QrCodeCodecSupport.normalize(code.getData())); Code one = codeService.getCacheByData(code.getData()); if (one == null) { if (!codeService.save(code)) { @@ -262,7 +264,7 @@ throw new BusinessException(code.getData()+" update fail![Code]"); } } codes.removeIf(next -> code.getData().equals(next.getData())); codes.removeIf(next -> Objects.equals(next.getId(), one == null ? code.getId() : one.getId())); } for (Code code : codes) { if (!codeService.removeById(code.getId())) { zy-acs-manager/src/main/java/com/zy/acs/manager/manager/controller/RouteController.java
@@ -2,7 +2,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.zy.acs.common.constant.CommonConstant; import com.zy.acs.common.utils.QrCodeCodecSupport; import com.zy.acs.common.utils.Utils; import com.zy.acs.framework.common.Cools; import com.zy.acs.framework.common.R; @@ -221,8 +221,8 @@ } RouteExcel excelDto = Cools.convert(one, RouteExcel.class); Code code0 = codeService.getCacheByData(Utils.zeroFill(excelDto.getStartCode(), CommonConstant.QR_CODE_LEN)); Code code1 = codeService.getCacheByData(Utils.zeroFill(excelDto.getEndCode(), CommonConstant.QR_CODE_LEN)); Code code0 = codeService.getCacheByData(excelDto.getStartCode()); Code code1 = codeService.getCacheByData(excelDto.getEndCode()); if (null == code0 || null == code1) { continue; } zy-acs-manager/src/main/java/com/zy/acs/manager/manager/service/impl/CodeServiceImpl.java
@@ -3,6 +3,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zy.acs.common.utils.CodeUtils; import com.zy.acs.common.utils.QrCodeCodecSupport; import com.zy.acs.framework.common.Cools; import com.zy.acs.manager.common.domain.CodeExcel; import com.zy.acs.manager.common.exception.BusinessException; @@ -46,7 +47,11 @@ @Override public Code selectByData(String data) { return this.getOne(new LambdaQueryWrapper<Code>().eq(Code::getData, data)); if (Cools.isEmpty(data)) { return null; } data = QrCodeCodecSupport.normalize(data); return this.getOne(new LambdaQueryWrapper<Code>().eq(Code::getData, data), false); } @Override @@ -66,9 +71,10 @@ @Override public Code getCacheByData(String data) { if (data == null) { if (Cools.isEmpty(data)) { return null; } data = QrCodeCodecSupport.normalize(data); Code code = CODE_DATA_CACHE.get(data); if (code == null) { code = this.selectByData(data); @@ -177,17 +183,18 @@ @Override public void importExecute(List<CodeExcel> excelList) { for (CodeExcel excel : excelList) { String codeData = QrCodeCodecSupport.normalize(excel.getCode()); Code code = this.selectByData(excel.getCode()); if (null == code) { code = new Code(); code.setUuid("code" + excel.getCode()); code.setData(excel.getCode()); code.setUuid("code" + codeData); code.setData(codeData); code.setX(excel.getX()); code.setY(excel.getY()); code.setScale(CodeUtils.DEFAULT_SCALE); code.setMemo(excel.getMemo()); if (!save(code)) { throw new BusinessException(excel.getCode() + "保存失败"); throw new BusinessException(codeData + "保存失败"); } } } @@ -327,8 +334,9 @@ if (code == null || code.getId() == null || code.getData() == null) { return; } CODE_ID_CACHE.put(code.getId(), this.copyCode(code)); CODE_DATA_CACHE.put(code.getData(), this.copyCode(code)); Code cachedCode = this.copyCode(code); CODE_ID_CACHE.put(code.getId(), cachedCode); CODE_DATA_CACHE.put(code.getData(), this.copyCode(cachedCode)); } private Code copyCode(Code code) {