2024-01-11 15:17:27 +08:00
|
|
|
package com.zcloud.util;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
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;
|
2024-01-11 18:02:48 +08:00
|
|
|
import java.util.Map;
|
2024-01-11 15:17:27 +08:00
|
|
|
|
|
|
|
public class PLSUtil {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public static CorpPlsInfoService corpplsinfoService;
|
|
|
|
@Autowired
|
|
|
|
public static UsersService usersService;
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2024-01-11 18:02:48 +08:00
|
|
|
Response response = sendPostHttpRequest(loginPayload.toJSONString(), loginUrl,null);
|
2024-01-11 15:17:27 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
2024-01-11 18:02:48 +08:00
|
|
|
Response response = sendPostHttpRequest(url,employeeData.toString(),token);
|
2024-01-11 15:17:27 +08:00
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
throw new IOException(String.valueOf(response));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Exception("无法获取有效的Token");
|
|
|
|
}
|
|
|
|
}
|
2024-01-11 18:02:48 +08:00
|
|
|
|
|
|
|
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()
|
|
|
|
.url(url)
|
|
|
|
.post(body);
|
|
|
|
|
|
|
|
if (token != null && !token.isEmpty()) {
|
|
|
|
builder.addHeader("Authorization", "Bearer " + token);
|
|
|
|
}
|
|
|
|
|
|
|
|
Request request = builder.build();
|
|
|
|
return client.newCall(request).execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2024-01-11 15:17:27 +08:00
|
|
|
}
|