package com.zcloud.util; import com.alibaba.fastjson.JSONObject; import com.zcloud.controller.corp.CorpInfoController; import com.zcloud.entity.EmployeeData; import com.zcloud.entity.PageData; import com.zcloud.service.corp.CorpInfoService; 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.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class PLSUtil { @Autowired public static CorpPlsInfoService corpplsinfoService; @Autowired public static UsersService usersService; // 修改 getToken 方法以返回布尔值 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"); OkHttpClient client = new OkHttpClient(); MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(JSON, loginPayload.toJSONString()); Request request = new Request.Builder() .url(loginUrl) .post(body) .build(); Response response = client.newCall(request).execute(); 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"; OkHttpClient client = new OkHttpClient(); MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(JSON, employeeData.toString()); Request request = new Request.Builder() .url(url) .post(body) .addHeader("Authorization", "Bearer " + token) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) { throw new IOException(String.valueOf(response)); } } else { throw new Exception("无法获取有效的Token"); } } }