forked from integrated_whb/integrated_whb
377 lines
13 KiB
Java
377 lines
13 KiB
Java
package com.zcloud.util;
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.zcloud.entity.Camera;
|
||
import com.zcloud.entity.Card;
|
||
import com.zcloud.entity.EmployeeData;
|
||
import com.zcloud.entity.PageData;
|
||
import com.zcloud.service.corp.CorpPlsInfoService;
|
||
import com.zcloud.service.system.UsersService;
|
||
import okhttp3.*;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
||
import java.io.IOException;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
public class PLSUtil {
|
||
|
||
@Autowired
|
||
public static CorpPlsInfoService corpplsinfoService;
|
||
@Autowired
|
||
public static UsersService usersService;
|
||
|
||
/**
|
||
* 验证登录
|
||
* @param pd
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static boolean getToken(PageData pd) throws Exception {
|
||
boolean tokenUpdated = false;
|
||
pd = corpplsinfoService.findById(pd);
|
||
String token = pd.getString("TOKEN");
|
||
String expireTime = pd.getString("EXPIRE_TIME");
|
||
String userName = pd.getString("ACCOUNT");
|
||
String passWord = pd.getString("PASSWORD");
|
||
String loginUrl = pd.getString("POST_URL") + "/auth/login";
|
||
|
||
if (token == null || expireTime == null || expireTime.trim().isEmpty() || DateUtil.compareDate(DateUtil.getTime(), expireTime)) {
|
||
JSONObject loginPayload = new JSONObject();
|
||
loginPayload.put("username", userName);
|
||
loginPayload.put("password", passWord);
|
||
loginPayload.put("isPresentationMode", "2");
|
||
|
||
Response response = sendPostHttpRequest(loginPayload.toJSONString(), loginUrl,null);
|
||
if (response.isSuccessful()) {
|
||
String responseBody = response.body().string();
|
||
JSONObject responseJSON = new JSONObject(responseBody.isEmpty());
|
||
JSONObject data = responseJSON.getJSONObject("data");
|
||
token = data.getString("access_token");
|
||
String newExpireTime = DateUtil.getAfterDayDate("1");
|
||
pd.put("TOKEN", token);
|
||
pd.put("EXPIRE_TIME", newExpireTime);
|
||
corpplsinfoService.edit(pd);
|
||
tokenUpdated = true;
|
||
} else {
|
||
System.out.println("登录失败" + response.code());
|
||
}
|
||
} else {
|
||
tokenUpdated = true;
|
||
}
|
||
return tokenUpdated;
|
||
}
|
||
|
||
/**
|
||
* 新增员工信息
|
||
* @param pd
|
||
* @throws Exception
|
||
*/
|
||
public static void saveUser(PageData pd) throws Exception {
|
||
if (getToken(pd)) {
|
||
PageData user = usersService.findByUsername(pd);
|
||
EmployeeData employeeData = new EmployeeData();
|
||
|
||
if (user == null) {
|
||
employeeData.setName(user.getString("NAME"));
|
||
employeeData.setAvatar(user.getString("USERAVATARPREFIX") + user.getString("USERAVATARURL"));
|
||
employeeData.setCardNo(user.getString("CARDNO"));
|
||
employeeData.setSex(user.getString("SEX"));
|
||
employeeData.setPhone(user.getString("PHONE"));
|
||
}
|
||
|
||
pd = corpplsinfoService.findById(pd);
|
||
String token = pd.getString("TOKEN");
|
||
String url = pd.getString("POST_URL") + "/deploy/psnmgmt/insertPsnInfo";
|
||
Response response = sendPostHttpRequest(url,employeeData.toString(),token);
|
||
if (!response.isSuccessful()) {
|
||
throw new IOException("请求失败:" + response);
|
||
}
|
||
} else {
|
||
throw new Exception("无法获取有效的Token");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改人员卡
|
||
* @param pd
|
||
* @throws Exception
|
||
*/
|
||
public static void updateUserCardInfo(PageData pd) throws Exception {
|
||
Card card = new Card();
|
||
if (getToken(pd)){
|
||
card.setCardNo(pd.getString("CARDNO"));
|
||
card.setPsnId(pd.getString("PsnId"));
|
||
card.setCardType(pd.getString("CARDTYPE"));
|
||
card.setCardStatus(pd.getString("CARDSTATUS"));
|
||
card.setRemark(pd.getString("REMARK"));
|
||
|
||
pd = corpplsinfoService.findById(pd);
|
||
String token = pd.getString("TOKEN");
|
||
String url = pd.getString("POST_URL") + "/deploy/card";
|
||
|
||
Response response = sendPutHttpRequest(url, card.toString(), token);
|
||
if (!response.isSuccessful()) {
|
||
throw new IOException("请求失败:" + response);
|
||
}
|
||
}else {
|
||
throw new Exception("无法获取有效的Token");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 根据人员卡卡号获取人员信息
|
||
* @param pd
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static JSONObject cardIdGetUserInfo(PageData pd) throws Exception {
|
||
JSONObject result = null;
|
||
if (getToken(pd)){
|
||
pd = corpplsinfoService.findById(pd);
|
||
String token = pd.getString("TOKEN");
|
||
String cardId = pd.getString("CARDNO");
|
||
String url = pd.getString("POST_URL") + "/deploy/psnmgt/feign-client/getPsnInfoByCardId/" + cardId;
|
||
|
||
Response response = sendGetHttpRequest(url, null, token);
|
||
if (response.isSuccessful()){
|
||
String responseBody = response.body().string();
|
||
result = new JSONObject(responseBody.isEmpty());
|
||
}else {
|
||
throw new IOException("请求失败:" + response);
|
||
}
|
||
}else {
|
||
throw new Exception("无法获取有效的Token");
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 添加摄像头
|
||
* @param pd
|
||
* @throws Exception
|
||
*/
|
||
public static void saveCamera(PageData pd) throws Exception {
|
||
Camera camera = new Camera();
|
||
if (getToken(pd)) {
|
||
camera.setCamName(pd.getString("CAMNAME"));
|
||
camera.setCamNo(pd.getString("CAMNO"));
|
||
camera.setCamIp(pd.getString("CAMIP"));
|
||
camera.setPort(pd.getString("PORT"));
|
||
camera.setType(pd.getString("TYPE"));
|
||
camera.setCamUserName(pd.getString("CAMUSERNAME"));
|
||
camera.setCamPassword(pd.getString("CAMPASSWORD"));
|
||
camera.setLon(pd.getString("LON"));
|
||
camera.setAlt(pd.getString("ALT"));
|
||
camera.setChannel(pd.getString("CHANNEL"));
|
||
camera.setIndexcode(pd.getString("INDEXCODE"));
|
||
|
||
pd = corpplsinfoService.findById(pd);
|
||
String token = pd.getString("TOKEN");
|
||
String url = pd.getString("POST_URL") + "/device/camera";
|
||
|
||
Response response = sendPostHttpRequest(url, String.valueOf(camera), token);
|
||
if (!response.isSuccessful()){
|
||
throw new IOException("请求失败:" + response);
|
||
}
|
||
}else {
|
||
throw new Exception("无法获取有效的Token");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改摄像头基本信息
|
||
* @param pd
|
||
* @throws Exception
|
||
*/
|
||
public static void updateCamera(PageData pd) throws Exception {
|
||
Camera camera = new Camera();
|
||
if (getToken(pd)){
|
||
camera.setCamName(pd.getString("CAMNAME"));
|
||
camera.setCamNo(pd.getString("CAMNO"));
|
||
camera.setCamIp(pd.getString("CAMIP"));
|
||
camera.setPort(pd.getString("PORT"));
|
||
camera.setType(pd.getString("TYPE"));
|
||
camera.setCamUserName(pd.getString("CAMUSERNAME"));
|
||
camera.setCamPassword(pd.getString("CAMPASSWORD"));
|
||
camera.setLon(pd.getString("LON"));
|
||
camera.setAlt(pd.getString("ALT"));
|
||
camera.setChannel(pd.getString("CHANNEL"));
|
||
camera.setIndexcode(pd.getString("INDEXCODE"));
|
||
|
||
pd = corpplsinfoService.findById(pd);
|
||
String token = pd.getString("TOKEN");
|
||
String url = pd.getString("POST_URL") + "/device/camera";
|
||
Response response = sendPutHttpRequest(url, String.valueOf(camera), token);
|
||
if (!response.isSuccessful()){
|
||
throw new IOException("请求失败:" + response);
|
||
}
|
||
}else {
|
||
throw new Exception("无法获取有效的Token");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取电子围栏列表
|
||
* @param pd
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static JSONObject getAListOfElectronicFences(PageData pd) throws Exception {
|
||
if (getToken(pd)) {
|
||
pd = corpplsinfoService.findById(pd);
|
||
String token = pd.getString("TOKEN");
|
||
String url = pd.getString("POST_URL") + "/region/api/electronicFence/getAListOfElectronicFences";
|
||
|
||
Response response = sendGetHttpRequest(url, null, token);
|
||
if (response.isSuccessful()){
|
||
String responseBody = response.body().string();
|
||
return new JSONObject(responseBody.isEmpty());
|
||
}else {
|
||
throw new IOException("请求失败:" + response);
|
||
}
|
||
}else {
|
||
throw new Exception("无法获取有效的Token");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 告警统计
|
||
* @param pd
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static JSONObject getAlarmStatistics(PageData pd) throws Exception {
|
||
JSONObject result = null;
|
||
if (getToken(pd)){
|
||
pd = corpplsinfoService.findById(pd);
|
||
String token = pd.getString("TOKEN");
|
||
Map<String,String> queryParams = new HashMap<>();
|
||
queryParams.put("type",pd.getString("week"));
|
||
String url = pd.getString("POST_URL") + "/region/alarm/alarmStatistics";
|
||
|
||
Response response = sendGetHttpRequest(url, queryParams, token);
|
||
if (response.isSuccessful()){
|
||
String responseBody = response.body().string();
|
||
return new JSONObject(responseBody.isEmpty());
|
||
}else {
|
||
throw new IOException("请求失败:" + response);
|
||
}
|
||
}else {
|
||
throw new Exception("无法获取有效的Token");
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 发送post类型请求
|
||
* @param url
|
||
* @param jsonPayload
|
||
* @param token
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
private static Response sendPostHttpRequest(String url, String jsonPayload, String token) throws IOException {
|
||
OkHttpClient client = new OkHttpClient();
|
||
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||
RequestBody body = RequestBody.create(JSON, jsonPayload);
|
||
Request.Builder builder = new Request.Builder() // 构建一个新的Request对象。
|
||
.url(url)// 设置请求的URL。
|
||
.post(body);// 设置请求的方法为POST,并设置请求体。
|
||
|
||
//构建请求头
|
||
if (token != null && !token.isEmpty()) {// 检查token是否存在且不为空。
|
||
builder.addHeader("Authorization", "Bearer " + token);
|
||
}
|
||
|
||
// 构建请求
|
||
Request request = builder.build();
|
||
//发起请求并响应数据
|
||
return client.newCall(request).execute();
|
||
}
|
||
|
||
/**
|
||
* 发送PUT类型请求
|
||
* @param url
|
||
* @param jsonPayload
|
||
* @param token
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
private static Response sendPutHttpRequest(String url, String jsonPayload, String token)throws IOException{
|
||
OkHttpClient client = new OkHttpClient();
|
||
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||
RequestBody body = RequestBody.create(JSON, jsonPayload);
|
||
Request.Builder builder = new Request.Builder()
|
||
.url(url)
|
||
.put(body);
|
||
if (token != null && !token.isEmpty()) {
|
||
builder.addHeader("Authorization", "Bearer " + token);
|
||
}
|
||
Request request = builder.build();
|
||
|
||
return client.newCall(request).execute();
|
||
}
|
||
|
||
/**
|
||
* 发送get类型请求
|
||
* @param url
|
||
* @param queryParams
|
||
* @param token
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
private static Response sendGetHttpRequest(String url, Map<String, String> queryParams, String token) throws IOException {
|
||
OkHttpClient client = new OkHttpClient();
|
||
|
||
HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder();
|
||
//封装请求参数
|
||
if (queryParams != null) {
|
||
for (Map.Entry<String, String> param : queryParams.entrySet()) {
|
||
httpBuilder.addQueryParameter(param.getKey(), param.getValue());
|
||
}
|
||
}
|
||
//设置请求的url
|
||
Request.Builder requestBuilder = new Request.Builder()
|
||
.url(httpBuilder.build());
|
||
|
||
if (token != null && !token.isEmpty()) {
|
||
requestBuilder.addHeader("Authorization", "Bearer " + token);
|
||
}
|
||
|
||
Request request = requestBuilder.build();
|
||
//发送请求并返回响应数据
|
||
return client.newCall(request).execute();
|
||
}
|
||
|
||
/**
|
||
* 发送delete类型请求
|
||
* @param url
|
||
* @param camIds
|
||
* @param token
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
private static Response sendDeleteHttpRequest(String url, List<String> camIds, String token) throws IOException {
|
||
OkHttpClient client = new OkHttpClient();
|
||
//将要删除的Id列表转换为逗号分隔的字符串
|
||
String joinedIds = String.join(",", camIds);
|
||
//构建完整的url
|
||
url = url + "/" + joinedIds;
|
||
|
||
Request.Builder builderBuilder = new Request.Builder()
|
||
.url(url)
|
||
.delete();
|
||
|
||
if (token != null && !token.isEmpty()) {
|
||
builderBuilder.addHeader("Authorization", "Bearer " + token);
|
||
}
|
||
|
||
Request request = builderBuilder.build();
|
||
|
||
return client.newCall(request).execute();
|
||
}
|
||
}
|