Commit 7bb493d6 by suxiaochun

境外码

parent 853b03cd
package io.office.modules.manage.controller;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.office.common.utils.IPUtils;
import io.office.common.utils.PageUtils;
import io.office.common.utils.R;
import io.office.modules.app.annotation.Login;
import io.office.modules.manage.entity.SearchgtinlogEntity;
import io.office.modules.manage.service.SearchgtinlogService;
import io.office.modules.manage.utils.SearchLimitUtil;
import io.office.modules.manage.vo.request.VbgOutFirmSearchVo;
import io.office.modules.manage.vo.request.VbgOutOtherKeyVo;
import io.office.modules.manage.vo.request.VbgOutSearchGLNVo;
import io.office.modules.manage.vo.request.VbgOutVerifiedVo;
import io.office.modules.sys.service.SysCaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.DatatypeConverter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping(value = "/vbgOutSearch")
@Slf4j
public class GLNOutController {
private static String CLIENTGLN = "6901234506556";
private static String KEY = "2mI+lM0U5AmGOr6dWDR735hcS4qLah7NoFI3LYnYtoo=";
private static String HOST = "http://192.168.100.163:8088";
private static JSONArray COUNTRYS;
@Autowired
private SysCaptchaService sysCaptchaService;
@Autowired
private SearchgtinlogService searchgtinlogService;
@Autowired
private SearchLimitUtil searchLimitUtil;
/**
* 境外条码查询--验证产品--通过GTIN查询
* 通过条码查询产品信息
*
* @param vo
* @return
*/
@Login
@RequestMapping("/api/verified")
public R verified(@RequestBody VbgOutVerifiedVo vo, HttpServletRequest request) {
if (StringUtils.isBlank(vo.getGtin())) {
return R.error("请输入GTIN");
}
if (!sysCaptchaService.validate(vo.getUuid(), vo.getCaptcha())) {
return R.error("验证码不正确");
}
SearchgtinlogEntity searchgtinlogEntity = new SearchgtinlogEntity();
searchgtinlogEntity.setCreatedate(new Date());
searchgtinlogEntity.setIp(IPUtils.getIpAddr(request));
searchgtinlogEntity.setClassStr("境外条码查询验证产品");
searchgtinlogEntity.setKeyword(vo.getGtin());
searchgtinlogEntity.setSearchsource(0);
//校验是否开启
String searchLimitStr = searchLimitUtil.checkSearchLimit(request, searchgtinlogEntity.getClassStr());
if (StringUtils.isNotBlank(searchLimitStr)) {
return R.error(searchLimitStr);
}
//新增查询日志
searchgtinlogService.save(searchgtinlogEntity);
String gtin = vo.getGtin();
String data = "/api/ANCC/Verified?gtin=" + gtin + "&clientGln=" + CLIENTGLN;
String url = HOST + "/api/ANCC/Verified";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("gtin", gtin);
paramMap.put("clientGln", CLIENTGLN);
paramMap.put("mac", getMac(KEY, data));
String result = HttpUtil.get(url, paramMap);
log.info("/verified==>{}==>{}", paramMap, result);
JSONArray objects = JSONArray.parseArray(result);
return R.ok().put("data", objects);
}
/**
* 境外条码查询--验证其他GS1标识
* 通过其他key查询企业信息
*
* @return
*/
@Login
@RequestMapping("/api/otherKey")
public R otherKey(@RequestBody VbgOutOtherKeyVo vo, HttpServletRequest request) {
if (StringUtils.isBlank(vo.getKeyType())) {
return R.error("标识类型不能为空");
}
if (StringUtils.isBlank(vo.getKeyValue())) {
return R.error("请输入GS1码");
}
if (!sysCaptchaService.validate(vo.getUuid(), vo.getCaptcha())) {
return R.error("验证码不正确");
}
SearchgtinlogEntity searchgtinlogEntity = new SearchgtinlogEntity();
searchgtinlogEntity.setCreatedate(new Date());
searchgtinlogEntity.setIp(IPUtils.getIpAddr(request));
searchgtinlogEntity.setClassStr("境外条码查询验证其他GS1标识");
searchgtinlogEntity.setKeyword(vo.getKeyValue());
searchgtinlogEntity.setSearchsource(0);
//校验是否开启
String searchLimitStr = searchLimitUtil.checkSearchLimit(request, searchgtinlogEntity.getClassStr());
if (StringUtils.isNotBlank(searchLimitStr)) {
return R.error(searchLimitStr);
}
//新增查询日志
searchgtinlogService.save(searchgtinlogEntity);
String keyType = vo.getKeyType();
String keyValue = vo.getKeyValue();
String data = "/api/ANCC/OtherKey?keyType=" + keyType + "&keyValue=" + keyValue + "&clientGln=" + CLIENTGLN;
String url = HOST + "/api/ANCC/OtherKey";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("keyType", keyType);
paramMap.put("keyValue", keyValue);
paramMap.put("clientGln", CLIENTGLN);
paramMap.put("mac", getMac(KEY, data));
String result = HttpUtil.get(url, paramMap);
log.info("/otherKey==>{}==>{}", paramMap, result);
if (JSONUtil.isJsonArray(result)) {
//gtin单独处理
if ("gtin".equals(keyType.toLowerCase())) {
JSONArray arrays = JSONArray.parseArray(result);
JSONArray r = new JSONArray();
for (Object array : arrays) {
JSONObject o = new JSONObject();
o.put("gs1Licence", array);
o.put("gtin", keyValue);
r.add(o);
}
return R.ok().put("data", r);
}
return R.ok().put("data", JSONArray.parseArray(result));
} else if (JSONUtil.isJsonObj(result)) {
return R.ok().put("data", JSONObject.parseObject(result));
}
return R.error("参数有误");
}
/**
* 境外条码查询--验证企业--通过企业名称查询
* 通过其他key查询企业信息
*
* @return
*/
@Login
@RequestMapping("/api/firmSearch")
public R firmSearch(@RequestBody VbgOutFirmSearchVo vo, HttpServletRequest request) {
if (StringUtils.isBlank(vo.getLicenseeName())) {
return R.error("企业名称不能为空");
}
if (!sysCaptchaService.validate(vo.getUuid(), vo.getCaptcha())) {
return R.error("验证码不正确");
}
SearchgtinlogEntity searchgtinlogEntity = new SearchgtinlogEntity();
searchgtinlogEntity.setCreatedate(new Date());
searchgtinlogEntity.setIp(IPUtils.getIpAddr(request));
searchgtinlogEntity.setClassStr("境外条码查询验证企业");
searchgtinlogEntity.setKeyword(vo.getLicenseeName());
searchgtinlogEntity.setSearchsource(0);
//校验是否开启
String searchLimitStr = searchLimitUtil.checkSearchLimit(request, searchgtinlogEntity.getClassStr());
if (StringUtils.isNotBlank(searchLimitStr)) {
return R.error(searchLimitStr);
}
//新增查询日志
searchgtinlogService.save(searchgtinlogEntity);
if (vo.getPagesize() == null || vo.getPageindex() == null) {
vo.setPageindex(1);
vo.setPagesize(10);
}
//国家编号
String countryCode = StringUtils.isBlank(vo.getCountryCode()) ? "" : vo.getCountryCode();
//街道地址
String streetAddress = StringUtils.isBlank(vo.getStreetAddress()) ? "" : vo.getStreetAddress();
//邮政编码
String postalCode = StringUtils.isBlank(vo.getPostalCode()) ? "" : vo.getPostalCode();
//城市名称
String addressLocality = StringUtils.isBlank(vo.getAddressLocality()) ? "" : vo.getAddressLocality();
String data = "/api/ANCC/FirmSearch?" +
"addressLocality=" + addressLocality +
"&countryCode=" + countryCode +
"&licenseeName=" + vo.getLicenseeName() +
"&postalCode=" + postalCode +
"&streetAddress=" + streetAddress +
"&size=100" +
"&clientGln=" + CLIENTGLN;
String url = HOST + "/api/ANCC/FirmSearch";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("licenseeName", vo.getLicenseeName());
paramMap.put("addressLocality", addressLocality);
paramMap.put("countryCode", countryCode);
paramMap.put("postalCode", postalCode);
paramMap.put("streetAddress", streetAddress);
paramMap.put("size", 100);
paramMap.put("clientGln", CLIENTGLN);
paramMap.put("mac", getMac(KEY, data));
paramMap.put("pageindex", vo.getPageindex());
paramMap.put("pagesize", vo.getPagesize());
String result = HttpUtil.get(url, paramMap);
log.info("/firmSearch==>{}==>{}", paramMap, result);
JSONObject jsonObject = JSONObject.parseObject(result);
if (jsonObject != null && "0".equals(jsonObject.getString("code"))) {
Integer totalRecords = jsonObject.getInteger("totalRecords");
JSONArray licences = JSONArray.parseArray(jsonObject.getString("licences"));
return R.ok().put("data", new PageUtils(licences, totalRecords, vo.getPagesize(), vo.getPageindex()));
}
return R.ok().put("data", new PageUtils(new ArrayList<>(), 0, vo.getPagesize(), vo.getPageindex()));
}
/**
* 境外条码查询--验证位置实体--通过GLN查询
* 查询GLN信息
*
* @return
*/
@Login
@RequestMapping("/api/searchGLN")
public R searchGLN(@RequestBody VbgOutSearchGLNVo vo, HttpServletRequest request) {
if (StringUtils.isBlank(vo.getGln())) {
return R.error("请输入GLN");
}
if (!sysCaptchaService.validate(vo.getUuid(), vo.getCaptcha())) {
return R.error("验证码不正确");
}
SearchgtinlogEntity searchgtinlogEntity = new SearchgtinlogEntity();
searchgtinlogEntity.setCreatedate(new Date());
searchgtinlogEntity.setIp(IPUtils.getIpAddr(request));
searchgtinlogEntity.setClassStr("境外条码查询验证位置实体");
searchgtinlogEntity.setKeyword(vo.getGln());
searchgtinlogEntity.setSearchsource(0);
//校验是否开启
String searchLimitStr = searchLimitUtil.checkSearchLimit(request, searchgtinlogEntity.getClassStr());
if (StringUtils.isNotBlank(searchLimitStr)) {
return R.error(searchLimitStr);
}
//新增查询日志
searchgtinlogService.save(searchgtinlogEntity);
String gln = vo.getGln();
String data = "/api/ANCC/GLN?glncode=" + gln + "&clientGln=" + CLIENTGLN;
String url = HOST + "/api/ANCC/GLN";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("glncode", gln);
paramMap.put("clientGln", CLIENTGLN);
paramMap.put("mac", getMac(KEY, data));
String result = HttpUtil.get(url, paramMap);
log.info("/searchGLN==>{}==>{}", paramMap, result);
JSONArray objects = JSONArray.parseArray(result);
return R.ok().put("data", objects);
}
/**
* 国家字典
*
* @return
*/
@Login
@RequestMapping("/api/searchCountry")
public R searchCountry() {
if (COUNTRYS == null) {
String url = "http://219.232.114.68:8088/api/importer/v1/CodeApi?name=ISO3166Code";
String result = HttpUtil.get(url);
log.info("/searchCountry==>{}", result);
COUNTRYS = JSONArray.parseArray(result);
}
return R.ok().put("data", COUNTRYS);
}
/**
* 公共方法
*
* @param key
* @param data
* @return
*/
public static String getMac(String key, String data) {
Mac m;
byte[] secretByte;
SecretKey macKey;
String resultMac = "";
byte[] digest;
try {
m = Mac.getInstance("HmacSHA256");
secretByte = DatatypeConverter.parseBase64Binary(key);
byte[] dataBytes = data.getBytes("ASCII");
macKey = new SecretKeySpec(secretByte, "HMACSHA256");
m.init(macKey);
digest = m.doFinal(dataBytes);
resultMac = toHex(digest);
resultMac = resultMac.toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
return resultMac;
}
/*
* change byte to 16
*/
public static String toHex(byte[] buffer) {
StringBuffer sb = new StringBuffer(buffer.length * 2);
StringBuffer s = new StringBuffer(buffer.length);
for (int i = 0; i < buffer.length; i++) {
sb.append(Character.forDigit((buffer[i] & 240) >> 4, 16));
sb.append(Character.forDigit(buffer[i] & 15, 16));
s.append(buffer[i]);
}
return sb.toString();
}
}
package io.office.modules.manage.vo.request;
import lombok.Data;
/**
* 验证码公共字段
*/
@Data
public class VbgOutBo {
private String captcha;
private String uuid;
}
package io.office.modules.manage.vo.request;
import lombok.Data;
import java.io.Serializable;
@Data
public class VbgOutFirmSearchVo extends VbgOutBo implements Serializable {
private static final long serialVersionUID = 1L;
//必填.企业名称
private String licenseeName;
//国家编号
private String countryCode ;
//街道地址
private String streetAddress ;
//邮政编码
private String postalCode ;
//城市名称
private String addressLocality ;
//不用前端传
// private int size = 100;
//页码默认1
private Integer pageindex;
//每页数量默认10
private Integer pagesize;
}
package io.office.modules.manage.vo.request;
import lombok.Data;
import java.io.Serializable;
@Data
public class VbgOutOtherKeyVo extends VbgOutBo implements Serializable {
private static final long serialVersionUID = 1L;
private String keyType;
private String keyValue;
}
package io.office.modules.manage.vo.request;
import lombok.Data;
import java.io.Serializable;
@Data
public class VbgOutSearchGLNVo extends VbgOutBo implements Serializable {
private static final long serialVersionUID = 1L;
private String gln;
}
package io.office.modules.manage.vo.request;
import lombok.Data;
import java.io.Serializable;
@Data
public class VbgOutVerifiedVo extends VbgOutBo implements Serializable {
private static final long serialVersionUID = 1L;
private String gtin;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment