第一步:检查准备
public String businessLicenseCheck(String businessLicenseUrl){
String url = "https://bizlicense.market.alicloudapi.com/rest/160601/ocr/ocr_business_license.json";
String method = "POST";
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + userSystemConstant.getAliAppCode());
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
String bodys = "{\"image\":\""+businessLicenseUrl+"\"}";
try {
String response = HttpsRequestUtil.httpsRequest(url, method, headers, bodys);
//获取response的body
if(response.equals("Invalid Result - invalid business license")){
return null;
}
return response;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
实体类
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CompanyAuthVO implements Serializable {
private static final long serialVersionUID = 3584743219477515646L;
@ApiModelProperty(value = "id")
@NotNull(message = "id不能为空", groups = {Update.class})
private Integer id;
@ApiModelProperty(value = "用户id", hidden = true)
private Integer userAccountId;
@ApiModelProperty(value = "企业名称")
@NotBlank(message = "企业名称", groups = {Insert.class, Update.class})
private String companyName;
@ApiModelProperty(value = "统一社会信用代码")
@NotBlank(message = "统一社会信用代码", groups = {Insert.class, Update.class})
private String creditCode;
@ApiModelProperty(value = "营业执照")
@NotBlank(message = "营业执照", groups = {Insert.class, Update.class})
private String licenseImg;
@ApiModelProperty(value = "企业认证状态,0未通过,1已通过")
private Integer authStatus;
}
第二步:请求资源检查
public static String httpsRequest(String url, String method, Map<String, String> headerMap, String bodyData)
throws Exception {
HttpsURLConnection conn = null;
OutputStream out = null;
String rsp = null;
byte[] byteArray = bodyData.getBytes("utf-8");
try {
URL uri = new URL(url);
conn = (HttpsURLConnection) uri.openConnection();
// 忽略证书验证--Begin
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
// 忽略证书验证--End
conn.setRequestMethod(method);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Host", uri.getHost());
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
if (headerMap != null) {
Iterator<Map.Entry<String, String>> it = headerMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
out = conn.getOutputStream();
out.write(byteArray);
out.close();
if (conn.getResponseCode() == 200) {
rsp = getStreamAsString(conn.getInputStream(), "utf-8");
} else {
// rsp = getStreamAsString(conn.getErrorStream(), "utf-8");
InputStream errorStream = conn.getErrorStream();
if (errorStream != null) {
rsp = getStreamAsString(errorStream, "utf-8");
} else {
// 处理没有错误流的情况,例如返回默认错误信息
rsp = "Invalid Result - invalid business license";
}
}
} catch (Exception e) {
if (null != out) {
out.close();
}
e.printStackTrace();
}
return rsp;
}
数据流处理
private static String getStreamAsString(InputStream stream, String charset) throws IOException {
try {
Reader reader = new InputStreamReader(stream, charset);
StringBuilder response = new StringBuilder();
final char[] buff = new char[1024];
int read = 0;
while ((read = reader.read(buff)) > 0) {
response.append(buff, 0, read);
}
return response.toString();
} finally {
if (stream != null) {
stream.close();
}
}
}
最后:对照用户填写信息
public ResultBody companyCheck(CompanyAuthVO companyAuthVO) {
String businessRes = qccEntAuthUtil.businessLicenseCheck(companyAuthVO.getLicenseImg());
if (businessRes == null) {
ResultBody resultBody = ResultBody.error(ResultEnum.BUSINESS_LICENSE_CHECK_ANALYSIS_ERROR);
return resultBody;
}
//将字符串转化成json对象
JSONObject businessResJson = JSONObject.parseObject(businessRes);
System.out.println(businessResJson.toString());
if (businessResJson.getString("success").equals("false")) {
return ResultBody.error(ResultEnum.BUSINESS_LICENSE_CHECK_ANALYSIS_ERROR);
} else {
if (businessResJson.getString("name").equals(this.convert(companyAuthVO.getCompanyName()))
&& businessResJson.getString("reg_num").equals(companyAuthVO.getCreditCode())) {
String check2Res = qccEntAuthUtil.check2(companyAuthVO.getCompanyName(), companyAuthVO.getCreditCode());
if (check2Res != null) {
String status = qccEntAuthUtil.FormartJson(check2Res, "Status");
if (status.equals("200")) {
Integer verifyCode = qccEntAuthUtil.resultFormatJson(check2Res);
if (verifyCode.equals(1)) {
return ResultBody.success();
}
String returnString = "";
if (verifyCode.equals(0)) {
returnString = "统一社会信用代码有误";
} else if (verifyCode.equals(2)) {
returnString = "企业名称不一致";
} else if (verifyCode.equals(3)) {
returnString = "法定代表人名称不一致";
}
return ResultBody.error(returnString);
}
String errorString = "";
if (status.equals("201")) {
errorString = "查询无结果";
} else if (status.equals("202")) {
errorString = "查询参数错误,请检查";
} else if (status.equals("205")) {
errorString = "等待处理中";
} else if (status.equals("207")) {
errorString = "请求数据的条目数超过上限(5000)";
} else if (status.equals("208")) {
errorString = "此接口不支持此公司类型查询";
} else if (status.equals("209")) {
errorString = "企业数量超过上限";
} else if (status.equals("213")) {
errorString = "参数长度不能小于2";
} else if (status.equals("215")) {
errorString = "不支持的查询关键字";
} else if (status.equals("218")) {
errorString = "该企业暂不支持空壳扫描";
} else if (status.equals("219")) {
errorString = "该企业暂不支持准入尽调";
} else if (status.equals("105")) {
errorString = "接口已下线停用";
} else if (status.equals("110")) {
errorString = "当前相同查询连续出错,请等2小时后重试";
} else {
errorString = "认证报错,请联系管理员";
}
return ResultBody.error(errorString);
} else {
return ResultBody.error("认证报错");
}
} else {
return ResultBody.error(ResultEnum.BUSINESS_LICENSE_CHECK_PARAMETER_ERROR);
}
}
}