BeiSenUtils.java 9.4 KB

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