BeiSenUtils.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.kingdee.eas.custom.webbeisen.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.kingdee.bos.Context;
  6. import com.kingdee.bos.util.BOSUuid;
  7. import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
  8. import com.kingdee.shr.base.syssetting.util.MetaDataUtil;
  9. import com.kingdee.util.StringUtils;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.net.URISyntaxException;
  14. import java.net.URLEncoder;
  15. import java.util.*;
  16. public class BeiSenUtils {
  17. /**
  18. * 构造函数,通过传入 APP_KEY 和 APP_SECRET 初始化对象
  19. *
  20. * @param app_key 北森应用的 APP_KEY
  21. * @param app_secret 北森应用的 APP_SECRET
  22. */
  23. // 北森应用的 APP_KEY
  24. public String APP_KEY = null;
  25. // 北森应用的 APP_SECRET
  26. public String APP_SECRET = null;
  27. public Properties propt = new Properties();
  28. public Context context=null;
  29. public Helper helper = null;
  30. public BeiSenUtils(String app_key, String app_secret, Context context) {
  31. this.context = context;
  32. helper = new Helper(context);
  33. // 加载配置文件
  34. try {
  35. propt.load(new FileInputStream(System.getProperty("EAS_HOME") + "/server/properties/beiSen/BeiSenConfig.properties"));
  36. } catch (IOException e) {
  37. throw new RuntimeException(e);
  38. }
  39. this.APP_KEY = app_key;
  40. this.APP_SECRET = app_secret;
  41. }
  42. /**
  43. * 构造函数,从配置文件中读取 APP_KEY 和 APP_SECRET 初始化对象
  44. */
  45. public BeiSenUtils(Context context) {
  46. this.context = context;
  47. helper = new Helper(context);
  48. try {
  49. // 创建 Properties 对象用于读取配置文件
  50. // 加载配置文件
  51. propt.load(new FileInputStream(System.getProperty("EAS_HOME") + "/server/properties/beiSen/BeiSenConfig.properties"));
  52. // 从配置文件中获取 APP_KEY
  53. this.APP_KEY = propt.getProperty("APP_KEY");
  54. // 从配置文件中获取 APP_SECRET
  55. this.APP_SECRET = propt.getProperty("APP_SECRET");
  56. } catch (FileNotFoundException e) {
  57. // 若文件未找到,抛出运行时异常
  58. throw new RuntimeException(e);
  59. } catch (IOException e) {
  60. // 若读取文件时发生 IO 异常,抛出运行时异常
  61. throw new RuntimeException(e);
  62. }
  63. }
  64. /**
  65. * 获取访问令牌
  66. *
  67. * @return 访问令牌字符串
  68. * @throws FileNotFoundException 文件未找到异常
  69. * @throws IOException IO 异常
  70. * @throws URISyntaxException URI 语法异常
  71. */
  72. public String getAccessToken() throws FileNotFoundException, IOException, URISyntaxException {
  73. String tenantAccessToken = null;
  74. // 创建请求头的 Map
  75. Map<String, String> header = new HashMap<String, String>();
  76. // 设置请求头的 Content-Type
  77. header.put("Content-Type", "application/x-www-form-urlencoded");
  78. // 创建请求体的 JSONObject
  79. JSONObject requestBody = new JSONObject();
  80. // 设置请求体的 grant_type
  81. requestBody.put("grant_type", "client_credentials");
  82. // 设置请求体的 app_key
  83. requestBody.put("app_key", APP_KEY);
  84. // 设置请求体的 app_secret
  85. requestBody.put("app_secret", APP_SECRET);
  86. // 调用 Helper 类的 getURLEncoded 方法发送请求并获取响应的 JSONObject
  87. JSONObject responseJson = helper.getURLEncoded(propt.getProperty("ACCESSTOKEN_URL"), header, requestBody, "POST");
  88. // 从响应的 JSONObject 中获取访问令牌
  89. tenantAccessToken = responseJson.getString("access_token");
  90. return tenantAccessToken;
  91. }
  92. public JSONArray getStaffIds(JSONObject requestBody) throws IOException, URISyntaxException {
  93. // 获取访问令牌
  94. String token = getAccessToken();
  95. Map<String, String> header = new HashMap<String, String>();
  96. // 设置请求头的 Content-Type
  97. header.put("Content-Type", "application/json");
  98. // 设置请求头的 Authorization
  99. header.put("Authorization", "Bearer " + token);
  100. requestBody.put("batchId", "");
  101. JSONArray jsonArray = new JSONArray();
  102. do {
  103. JSONObject reData = helper.getURL(propt.getProperty("GETSTAFFIDS"), header, requestBody, "POST"
  104. , null, "获取待入职员工Id", "北森");
  105. Integer code = reData.getInteger("code");
  106. if (code==200){
  107. JSONObject data = reData.getJSONObject("data");
  108. requestBody.put("batchId",data.getString("nextBatchId"));
  109. JSONArray items = data.getJSONArray("items");
  110. for (int i = 0; i < items.size(); i++) {
  111. jsonArray.add(items.get(i));
  112. }
  113. }else {
  114. break;
  115. }
  116. }while (requestBody.get("batchId")!=null&&requestBody.get("batchId").equals(""));
  117. return jsonArray;
  118. }
  119. public JSONArray getStaffInfos(JSONArray requestBody,List<String> targetFields ) throws IOException, URISyntaxException {
  120. String token = getAccessToken();
  121. Map<String, String> header = new HashMap<String, String>();
  122. // 设置请求头的 Content-Type
  123. header.put("Content-Type", "application/json");
  124. // 设置请求头的 Authorization
  125. header.put("Authorization", "Bearer " + token);
  126. List<JSONArray> jsonArrays = splitJsonArray(requestBody);
  127. JSONArray jsonArray = new JSONArray();
  128. for (int j = 0; j < jsonArrays.size(); j++) {
  129. JSONObject reData = helper.getURL(propt.getProperty("GETSTAFFINFOS"), header, jsonArrays.get(j), "POST");
  130. Integer code = reData.getInteger("code");
  131. if (code==200){
  132. JSONArray data = reData.getJSONArray("data");
  133. for (int i = 0; i < data.size(); i++) {
  134. jsonArray.add(data.get(i));
  135. }
  136. }
  137. }
  138. String convertedJson = JsonFormatConverter.convertDataFields(jsonArray.toJSONString(), targetFields);
  139. return JSONArray.parseArray(convertedJson);
  140. }
  141. /**
  142. * 将一个FastJSON的JSONArray按每50个元素分割成多个子数组
  143. *
  144. * @param requestBody 原始JSONArray
  145. * @return 包含所有子JSONArray的列表
  146. */
  147. public static List<JSONArray> splitJsonArray(JSONArray requestBody) {
  148. List<JSONArray> result = new ArrayList<JSONArray>();
  149. int totalElements = requestBody.size(); // FastJSON使用size()方法
  150. int batchSize = 50;
  151. for (int i = 0; i < totalElements; i += batchSize) {
  152. JSONArray batch = new JSONArray();
  153. int endIndex = Math.min(i + batchSize, totalElements);
  154. for (int j = i; j < endIndex; j++) {
  155. batch.add(requestBody.get(j)); // FastJSON使用add()方法添加元素
  156. }
  157. result.add(batch);
  158. }
  159. return result;
  160. }
  161. public JSONObject createOrUpdateByOrg(JSONObject requestBody, AdminOrgUnitInfo adminOrgUnitInfo) throws FileNotFoundException, IOException
  162. , URISyntaxException {
  163. String originalId = adminOrgUnitInfo.getString("originalId");
  164. String orgid = adminOrgUnitInfo.getId().toString();
  165. // 用于存储响应数据的 JSONArray
  166. JSONArray responseData = new JSONArray();
  167. // 用于存储响应的 JSONObject
  168. JSONObject responseJson = null;
  169. // 获取访问令牌
  170. String token = getAccessToken();
  171. // 若访问令牌不为空且开始时间和结束时间不为空
  172. if (!StringUtils.isEmpty(token)) {
  173. // 创建请求头的 Map
  174. Map<String, String> header = new HashMap<String, String>();
  175. // 设置请求头的 Content-Type
  176. header.put("Content-Type", "application/json");
  177. // 设置请求头的 Authorization
  178. header.put("Authorization", "Bearer " + token);
  179. if (originalId!=null&&!originalId.equals("")){
  180. // 调用 Helper 类的 getURL 方法发送请求并获取响应的 JSONObject
  181. header.put("originalId",orgid);
  182. String departmentsput = propt.getProperty("DEPARTMENTSPUT");
  183. departmentsput+="?originalId="+URLEncoder.encode(orgid, "UTF-8");
  184. responseJson = helper.getURL(departmentsput,header, requestBody, "PUT",orgid,"更新","北森");
  185. System.out.println("url:"+departmentsput);
  186. System.out.println("requestBody:"+requestBody);
  187. }else {
  188. String departmentspost = propt.getProperty("DEPARTMENTSPOST");
  189. responseJson = helper.getURL(departmentspost, header, requestBody, "POST",orgid,"创建","北森");
  190. System.out.println("url:"+propt.getProperty("DEPARTMENTSPOST"));
  191. System.out.println("requestBody:"+requestBody);
  192. }
  193. }
  194. return responseJson;
  195. }
  196. public JSONObject setDisableAndEnable(String businessId,String url,String method) throws IOException, URISyntaxException {
  197. // 获取访问令牌
  198. String token = getAccessToken();
  199. Map<String, String> header = new HashMap<String, String>();
  200. // 设置请求头的 Content-Type
  201. header.put("Content-Type", "application/json");
  202. // 设置请求头的 Authorization
  203. header.put("Authorization", "Bearer " + token);
  204. JSONObject responseJson = helper.getURL(url, header, new JSONObject(), method,businessId,"启用/禁用/删除","北森");
  205. return responseJson;
  206. }
  207. public static void main(String[] args) {
  208. MetaDataUtil.getEntityObjectByBosType(BOSUuid.read("/1aEqNuoTWaCOnKAj1sSTHSuYS4=").getType()).getAlias();
  209. }
  210. }