350 lines
14 KiB
Java
350 lines
14 KiB
Java
|
|
package com.zhgd.xmgl.util;
|
|||
|
|
|
|||
|
|
import com.alibaba.fastjson.JSONArray;
|
|||
|
|
import com.alibaba.fastjson.JSONObject;
|
|||
|
|
import com.zhgd.jeecg.common.execption.OpenAlertException;
|
|||
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|||
|
|
import org.apache.commons.lang3.StringUtils;
|
|||
|
|
import org.apache.http.HttpEntity;
|
|||
|
|
import org.apache.http.HttpResponse;
|
|||
|
|
import org.apache.http.client.methods.HttpPost;
|
|||
|
|
import org.apache.http.entity.StringEntity;
|
|||
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|||
|
|
import org.apache.http.impl.client.HttpClients;
|
|||
|
|
import org.apache.http.util.EntityUtils;
|
|||
|
|
|
|||
|
|
import java.io.IOException;
|
|||
|
|
import java.text.SimpleDateFormat;
|
|||
|
|
import java.util.Date;
|
|||
|
|
import java.util.UUID;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @program: wisdomSite
|
|||
|
|
* @description: 芊熠人脸设备
|
|||
|
|
* @author: Mr.Peng
|
|||
|
|
* @create: 2020-11-24 11:54
|
|||
|
|
**/
|
|||
|
|
|
|||
|
|
public class QYUfaceUtil {
|
|||
|
|
|
|||
|
|
private static String url = "http://qy-rgs.com/api";
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 添加设备
|
|||
|
|
*
|
|||
|
|
* @param appId
|
|||
|
|
* @param appSecret
|
|||
|
|
* @param type 设备类型(1:未知,2:人脸识别,3:人证比对)
|
|||
|
|
* @param serial 设备序列号
|
|||
|
|
* @param name 设备名称
|
|||
|
|
* @param inoutFlag 出入标识(1:入,0:出)
|
|||
|
|
* @param groupId 设备组 ID(为空时不属于任何组)
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String addDevice(String appId, String appSecret, Integer type, String serial, String name,
|
|||
|
|
Integer inoutFlag, String groupId) {
|
|||
|
|
JSONObject body = new JSONObject();
|
|||
|
|
body.put("type", type);
|
|||
|
|
body.put("serial", serial);
|
|||
|
|
body.put("name", name);
|
|||
|
|
body.put("inoutFlag", inoutFlag);
|
|||
|
|
body.put("groupId", groupId);
|
|||
|
|
JSONObject json = createPostBody("DEVICE_CREATE", appId, appSecret, body);
|
|||
|
|
String result = sendHttpPostWithJson(url, json.toJSONString());
|
|||
|
|
if (StringUtils.isNotEmpty(result)) {
|
|||
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|||
|
|
if ("SUCCESS".equals(resultJson.getString("code"))) {
|
|||
|
|
JSONObject jsonObject = JSONObject.parseObject(resultJson.getString("body"));
|
|||
|
|
return jsonObject.getString("deviceId");
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(resultJson.getString("msg"));
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(MessageUtil.get("failErr"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 更新设备
|
|||
|
|
*
|
|||
|
|
* @param appId
|
|||
|
|
* @param appSecret
|
|||
|
|
* @param deviceId 设备id
|
|||
|
|
* @param type 设备类型(1:未知,2:人脸识别,3:人证比对)
|
|||
|
|
* @param serial 设备序列号
|
|||
|
|
* @param name 设备名称
|
|||
|
|
* @param inoutFlag 出入标识(1:入,0:出)
|
|||
|
|
* @param groupId 设备组 ID(为空时不属于任何组)
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static Boolean updateDevice(String appId, String appSecret, String deviceId, Integer type, String serial,
|
|||
|
|
String name, Integer inoutFlag, String groupId) {
|
|||
|
|
JSONObject body = new JSONObject();
|
|||
|
|
body.put("deviceId", deviceId);
|
|||
|
|
body.put("type", type);
|
|||
|
|
body.put("serial", serial);
|
|||
|
|
body.put("name", name);
|
|||
|
|
body.put("inoutFlag", inoutFlag);
|
|||
|
|
body.put("groupId", groupId);
|
|||
|
|
JSONObject json = createPostBody("DEVICE_UPDATE", appId, appSecret, body);
|
|||
|
|
String result = sendHttpPostWithJson(url, json.toJSONString());
|
|||
|
|
if (StringUtils.isNotEmpty(result)) {
|
|||
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|||
|
|
if ("SUCCESS".equals(resultJson.getString("code"))) {
|
|||
|
|
return true;
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(resultJson.getString("msg"));
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(MessageUtil.get("failErr"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 删除设备
|
|||
|
|
*
|
|||
|
|
* @param appId
|
|||
|
|
* @param appSecret
|
|||
|
|
* @param deviceId 设备id
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static Boolean deleteDevice(String appId, String appSecret, String deviceId) {
|
|||
|
|
JSONObject body = new JSONObject();
|
|||
|
|
body.put("deviceId", deviceId);
|
|||
|
|
JSONObject json = createPostBody("DEVICE_DELETE", appId, appSecret, body);
|
|||
|
|
String result = sendHttpPostWithJson(url, json.toJSONString());
|
|||
|
|
if (StringUtils.isNotEmpty(result)) {
|
|||
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|||
|
|
if ("SUCCESS".equals(resultJson.getString("code"))) {
|
|||
|
|
return true;
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(resultJson.getString("msg"));
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(MessageUtil.get("failErr"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
*
|
|||
|
|
* @param appId
|
|||
|
|
* @param appSecret
|
|||
|
|
* @param id 回调记录Id
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String getUfaceUrl(String appId, String appSecret, String id) {
|
|||
|
|
String img="";
|
|||
|
|
JSONObject body = new JSONObject();
|
|||
|
|
body.put("id", id);
|
|||
|
|
JSONObject json = createPostBody("RECOGNITION_PICTURE", appId, appSecret, body);
|
|||
|
|
String result = sendHttpPostWithJson(url, json.toJSONString());
|
|||
|
|
//log.info("芊熠人脸识别图片:"+result);
|
|||
|
|
if (StringUtils.isNotEmpty(result)) {
|
|||
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|||
|
|
if ("SUCCESS".equals(resultJson.getString("code"))) {
|
|||
|
|
JSONObject jsonObject = JSONObject.parseObject(resultJson.getString("body"));
|
|||
|
|
img = jsonObject.getString("snapPic");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return img;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 添加人员
|
|||
|
|
*
|
|||
|
|
* @param type 人员类型,1:白名单,2:黑名单,3:访客
|
|||
|
|
* @param code 不能为空且必须为唯一。
|
|||
|
|
* @param name 人员名称
|
|||
|
|
* @param groupId 人员组 ID(为空时不属于任何组)
|
|||
|
|
* @param phone 电话
|
|||
|
|
* @param idNum 证件号
|
|||
|
|
* @param icNum IC 卡号
|
|||
|
|
* @param ethnic IC 卡号
|
|||
|
|
* @param gender 1:男 2:女 3:未知
|
|||
|
|
*
|
|||
|
|
* @param birthday 出生日期(yyyy-MM-dd)
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static JSONObject addOrUpdatePerson(String appId, String appSecret, Integer type, String code, String name,
|
|||
|
|
String groupId, String phone, String idNum, String icNum, String ethnic,
|
|||
|
|
Integer gender, String birthday, String[] deviceIds, String face) {
|
|||
|
|
JSONObject body = new JSONObject();
|
|||
|
|
JSONObject resultJsonStr = new JSONObject();
|
|||
|
|
body.put("type", type);
|
|||
|
|
body.put("code", code);
|
|||
|
|
body.put("name", name);
|
|||
|
|
body.put("groupId", groupId);
|
|||
|
|
body.put("phone", phone);
|
|||
|
|
body.put("idNum", idNum);
|
|||
|
|
body.put("icNum", icNum);
|
|||
|
|
body.put("ethnic", ethnic);
|
|||
|
|
body.put("gender", gender);
|
|||
|
|
body.put("birthday", birthday);
|
|||
|
|
String[] str2 = new String[1];
|
|||
|
|
str2[0]=Base64Util.getFileToBase64(face);
|
|||
|
|
body.put("faces", str2);
|
|||
|
|
JSONObject json = createPostBody("PERSON_CREATE_OR_UPDATE", appId, appSecret, body);
|
|||
|
|
String result = sendHttpPostWithJson(url, json.toJSONString());
|
|||
|
|
if (StringUtils.isNotEmpty(result)) {
|
|||
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|||
|
|
if ("SUCCESS".equals(resultJson.getString("code"))) {
|
|||
|
|
JSONObject jsonObject = JSONObject.parseObject(resultJson.getString("body"));
|
|||
|
|
String resultStrEmpower = addEmpower(appId, appSecret, jsonObject.getString("personId"), deviceIds);
|
|||
|
|
if(StringUtils.isNotEmpty(resultStrEmpower)){
|
|||
|
|
JSONObject jsonObjectEmpower = JSONObject.parseObject(resultStrEmpower);
|
|||
|
|
if(!"SUCCESS".equals(jsonObjectEmpower.getString("code"))){
|
|||
|
|
throw new OpenAlertException(jsonObjectEmpower.getString("msg"));
|
|||
|
|
}else{
|
|||
|
|
return resultJsonStr;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(resultJson.getString("msg"));
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(MessageUtil.get("failErr"));
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 添加授权
|
|||
|
|
*
|
|||
|
|
* @param appId
|
|||
|
|
* @param appSecret
|
|||
|
|
* @param personId 人员 ID
|
|||
|
|
* @param deviceIds 设备 ID 数组
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String addEmpower(String appId, String appSecret, String personId, String[] deviceIds) {
|
|||
|
|
JSONObject body = new JSONObject();
|
|||
|
|
body.put("personId", personId);
|
|||
|
|
body.put("deviceIds", deviceIds);
|
|||
|
|
body.put("passMonday", true);
|
|||
|
|
body.put("passTuesday", true);
|
|||
|
|
body.put("passWednesday", true);
|
|||
|
|
body.put("passThursday", true);
|
|||
|
|
body.put("passFriday", true);
|
|||
|
|
body.put("passSaturday", true);
|
|||
|
|
body.put("passSunday", true);
|
|||
|
|
JSONObject json = createPostBody("PERSON_GRANT_DEVICE", appId, appSecret, body);
|
|||
|
|
String result = sendHttpPostWithJson(url, json.toJSONString());
|
|||
|
|
if (StringUtils.isNotEmpty(result)) {
|
|||
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|||
|
|
if ("SUCCESS".equals(resultJson.getString("code"))) {
|
|||
|
|
return result;
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(resultJson.getString("msg"));
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(MessageUtil.get("failErr"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static Boolean deleteNewPerson(String appId, String appSecret, String idCard) {
|
|||
|
|
JSONObject body = new JSONObject();
|
|||
|
|
body.put("currentPage", 0);
|
|||
|
|
body.put("pageSize", 50);
|
|||
|
|
JSONObject condition = new JSONObject();
|
|||
|
|
condition.put("search", idCard);
|
|||
|
|
body.put("condition", condition);
|
|||
|
|
JSONObject json = createPostBody("PERSON_LIST", appId, appSecret, body);
|
|||
|
|
String result = sendHttpPostWithJson(url, json.toJSONString());
|
|||
|
|
if (StringUtils.isNotEmpty(result)) {
|
|||
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|||
|
|
if ("SUCCESS".equals(resultJson.getString("code"))) {
|
|||
|
|
JSONObject jsonObject = JSONObject.parseObject(resultJson.getString("body"));
|
|||
|
|
JSONArray array=(JSONArray) jsonObject.get("persons");
|
|||
|
|
if(array!=null&&array.size()>0){
|
|||
|
|
JSONObject obj=(JSONObject) array.get(0);
|
|||
|
|
return deletePerson(appId,appSecret,obj.getString("id"));
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(resultJson.getString("msg"));
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(MessageUtil.get("failErr"));
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 删除人员
|
|||
|
|
*
|
|||
|
|
* @param personId 人员 ID
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static Boolean deletePerson(String appId, String appSecret, String personId) {
|
|||
|
|
JSONObject body = new JSONObject();
|
|||
|
|
body.put("personId", personId);
|
|||
|
|
JSONObject json = createPostBody("PERSON_DELETE", appId, appSecret, body);
|
|||
|
|
String result = sendHttpPostWithJson(url, json.toJSONString());
|
|||
|
|
if (StringUtils.isNotEmpty(result)) {
|
|||
|
|
JSONObject resultJson = JSONObject.parseObject(result);
|
|||
|
|
if ("SUCCESS".equals(resultJson.getString("code"))) {
|
|||
|
|
return true;
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(resultJson.getString("msg"));
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
throw new OpenAlertException(MessageUtil.get("failErr"));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static JSONObject createPostBody(String type, String appId, String appSecret, JSONObject body) {
|
|||
|
|
JSONObject jsonObject = new JSONObject();
|
|||
|
|
jsonObject.put("type", type);
|
|||
|
|
jsonObject.put("appId", appId);
|
|||
|
|
String requestId = UUID.randomUUID().toString().replace("-", "");
|
|||
|
|
jsonObject.put("requestId", requestId);
|
|||
|
|
String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
|||
|
|
jsonObject.put("timestamp", timestamp);
|
|||
|
|
String sign = DigestUtils.md5Hex(appId + requestId + type + appSecret + timestamp).toLowerCase();
|
|||
|
|
jsonObject.put("sign", sign);
|
|||
|
|
jsonObject.put("body", body);
|
|||
|
|
return jsonObject;
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* 发起post Json请求
|
|||
|
|
*
|
|||
|
|
* @param url
|
|||
|
|
* @param json
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static String sendHttpPostWithJson(String url, String json) {
|
|||
|
|
CloseableHttpClient httpClient = null;
|
|||
|
|
try {
|
|||
|
|
httpClient = HttpClients.createDefault();
|
|||
|
|
HttpPost httpPost = new HttpPost(url);
|
|||
|
|
StringEntity requestEntity = new StringEntity(json, "utf-8");
|
|||
|
|
requestEntity.setContentEncoding("UTF-8");
|
|||
|
|
httpPost.setHeader("Content-type", "application/json");
|
|||
|
|
//设置token请求参数
|
|||
|
|
// JSONObject jsonObject = buildTokenParam();
|
|||
|
|
//获取token设置在请求头
|
|||
|
|
// String token = getToken("https://iam.cn-north-1.myhuaweicloud.com/v3/auth/tokens", jsonObject.toJSONString(), 30000, 30000, "application/json");
|
|||
|
|
// log.info("=======>token:" + token);
|
|||
|
|
// httpPost.setHeader("X-Auth-Token", token);
|
|||
|
|
httpPost.setEntity(requestEntity);
|
|||
|
|
HttpResponse resp = httpClient.execute(httpPost);
|
|||
|
|
if (resp.getStatusLine().getStatusCode() == 200) {
|
|||
|
|
HttpEntity he = resp.getEntity();
|
|||
|
|
return EntityUtils.toString(he, "UTF-8");
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
} finally {
|
|||
|
|
try {
|
|||
|
|
httpClient.close();
|
|||
|
|
} catch (IOException e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|