| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package com.kingdee.eas.custom.webbeisen.utils;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.kingdee.bos.Context;
- import com.kingdee.bos.util.BOSUuid;
- import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
- import com.kingdee.shr.base.syssetting.util.MetaDataUtil;
- import com.kingdee.util.StringUtils;
- import com.kingdee.eas.custom.webbeisen.utils.Helper;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.net.URLEncoder;
- import java.util.*;
- public class BeiSenUtils {
- /**
- * 构造函数,通过传入 APP_KEY 和 APP_SECRET 初始化对象
- *
- * @param app_key 北森应用的 APP_KEY
- * @param app_secret 北森应用的 APP_SECRET
- */
- // 北森应用的 APP_KEY
- public String APP_KEY = null;
- // 北森应用的 APP_SECRET
- public String APP_SECRET = null;
- public Properties propt = new Properties();
- public Context context=null;
- public Helper helper = null;
- public BeiSenUtils(String app_key, String app_secret, Context context) {
- this.context = context;
- helper = new Helper(context);
- // 加载配置文件
- try {
- propt.load(new FileInputStream(System.getProperty("EAS_HOME") + "/server/properties/beiSen/BeiSenConfig.properties"));
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- this.APP_KEY = app_key;
- this.APP_SECRET = app_secret;
- }
- /**
- * 构造函数,从配置文件中读取 APP_KEY 和 APP_SECRET 初始化对象
- */
- public BeiSenUtils(Context context) {
- this.context = context;
- helper = new Helper(context);
- try {
- // 创建 Properties 对象用于读取配置文件
- // 加载配置文件
- propt.load(new FileInputStream(System.getProperty("EAS_HOME") + "/server/properties/beiSen/BeiSenConfig.properties"));
- // 从配置文件中获取 APP_KEY
- this.APP_KEY = propt.getProperty("APP_KEY");
- // 从配置文件中获取 APP_SECRET
- this.APP_SECRET = propt.getProperty("APP_SECRET");
- } catch (FileNotFoundException e) {
- // 若文件未找到,抛出运行时异常
- throw new RuntimeException(e);
- } catch (IOException e) {
- // 若读取文件时发生 IO 异常,抛出运行时异常
- throw new RuntimeException(e);
- }
- }
- /**
- * 获取访问令牌
- *
- * @return 访问令牌字符串
- * @throws FileNotFoundException 文件未找到异常
- * @throws IOException IO 异常
- * @throws URISyntaxException URI 语法异常
- */
- public String getAccessToken() throws FileNotFoundException, IOException, URISyntaxException {
- String tenantAccessToken = null;
- // 创建请求头的 Map
- Map<String, String> header = new HashMap<String, String>();
- // 设置请求头的 Content-Type
- header.put("Content-Type", "application/x-www-form-urlencoded");
- // 创建请求体的 JSONObject
- JSONObject requestBody = new JSONObject();
- // 设置请求体的 grant_type
- requestBody.put("grant_type", "client_credentials");
- // 设置请求体的 app_key
- requestBody.put("app_key", APP_KEY);
- // 设置请求体的 app_secret
- requestBody.put("app_secret", APP_SECRET);
- // 调用 Helper 类的 getURLEncoded 方法发送请求并获取响应的 JSONObject
- JSONObject responseJson = helper.getURLEncoded(propt.getProperty("ACCESSTOKEN_URL"), header, requestBody, "POST");
- // 从响应的 JSONObject 中获取访问令牌
- tenantAccessToken = responseJson.getString("access_token");
- return tenantAccessToken;
- }
- public JSONArray getStaffIds(JSONObject requestBody) throws IOException, URISyntaxException {
- // 获取访问令牌
- String token = getAccessToken();
- Map<String, String> header = new HashMap<String, String>();
- // 设置请求头的 Content-Type
- header.put("Content-Type", "application/json");
- // 设置请求头的 Authorization
- header.put("Authorization", "Bearer " + token);
- requestBody.put("batchId", "");
- JSONArray jsonArray = new JSONArray();
- do {
- JSONObject reData = helper.getURL(propt.getProperty("GETSTAFFIDS"), header, requestBody, "POST"
- , null, "获取待入职员工Id", "北森");
- Integer code = reData.getInteger("code");
- if (code==200){
- JSONObject data = reData.getJSONObject("data");
- requestBody.put("batchId",data.getString("nextBatchId"));
- JSONArray items = data.getJSONArray("items");
- for (int i = 0; i < items.size(); i++) {
- jsonArray.add(items.get(i));
- }
- }else {
- break;
- }
- }while (requestBody.get("batchId")!=null&&requestBody.get("batchId").equals(""));
- return jsonArray;
- }
- public JSONArray getStaffInfos(JSONArray requestBody,List<String> targetFields ) throws IOException, URISyntaxException {
- String token = getAccessToken();
- Map<String, String> header = new HashMap<String, String>();
- // 设置请求头的 Content-Type
- header.put("Content-Type", "application/json");
- // 设置请求头的 Authorization
- header.put("Authorization", "Bearer " + token);
- List<JSONArray> jsonArrays = splitJsonArray(requestBody);
- JSONArray jsonArray = new JSONArray();
- for (int j = 0; j < jsonArrays.size(); j++) {
- JSONObject reData = helper.getURL(propt.getProperty("GETSTAFFINFOS"), header, jsonArrays.get(j), "POST");
- Integer code = reData.getInteger("code");
- if (code==200){
- JSONArray data = reData.getJSONArray("data");
- for (int i = 0; i < data.size(); i++) {
- jsonArray.add(data.get(i));
- }
- }
- }
- String convertedJson = JsonFormatConverter.convertDataFields(jsonArray.toJSONString(), targetFields);
- return JSONArray.parseArray(convertedJson);
- }
- /**
- * 将一个FastJSON的JSONArray按每50个元素分割成多个子数组
- *
- * @param requestBody 原始JSONArray
- * @return 包含所有子JSONArray的列表
- */
- public static List<JSONArray> splitJsonArray(JSONArray requestBody) {
- List<JSONArray> result = new ArrayList<JSONArray>();
- int totalElements = requestBody.size(); // FastJSON使用size()方法
- int batchSize = 50;
- for (int i = 0; i < totalElements; i += batchSize) {
- JSONArray batch = new JSONArray();
- int endIndex = Math.min(i + batchSize, totalElements);
- for (int j = i; j < endIndex; j++) {
- batch.add(requestBody.get(j)); // FastJSON使用add()方法添加元素
- }
- result.add(batch);
- }
- return result;
- }
- public JSONObject createOrUpdateByOrg(JSONObject requestBody, AdminOrgUnitInfo adminOrgUnitInfo) throws FileNotFoundException, IOException
- , URISyntaxException {
- String originalId = adminOrgUnitInfo.getString("originalId");
- String orgid = adminOrgUnitInfo.getId().toString();
- // 用于存储响应数据的 JSONArray
- JSONArray responseData = new JSONArray();
- // 用于存储响应的 JSONObject
- JSONObject responseJson = null;
- // 获取访问令牌
- String token = getAccessToken();
- // 若访问令牌不为空且开始时间和结束时间不为空
- if (!StringUtils.isEmpty(token)) {
- // 创建请求头的 Map
- Map<String, String> header = new HashMap<String, String>();
- // 设置请求头的 Content-Type
- header.put("Content-Type", "application/json");
- // 设置请求头的 Authorization
- header.put("Authorization", "Bearer " + token);
- if (originalId!=null&&!originalId.equals("")){
- // 调用 Helper 类的 getURL 方法发送请求并获取响应的 JSONObject
- header.put("originalId",orgid);
- String departmentsput = propt.getProperty("DEPARTMENTSPUT");
- departmentsput+="?originalId="+URLEncoder.encode(orgid, "UTF-8");
- responseJson = helper.getURL(departmentsput,header, requestBody, "PUT",orgid,"更新","北森");
- System.out.println("url:"+departmentsput);
- System.out.println("requestBody:"+requestBody);
- }else {
- String departmentspost = propt.getProperty("DEPARTMENTSPOST");
- responseJson = helper.getURL(departmentspost, header, requestBody, "POST",orgid,"创建","北森");
- System.out.println("url:"+propt.getProperty("DEPARTMENTSPOST"));
- System.out.println("requestBody:"+requestBody);
- }
- }
- return responseJson;
- }
- public JSONObject setDisableAndEnable(String businessId,String url,String method) throws IOException, URISyntaxException {
- // 获取访问令牌
- String token = getAccessToken();
- Map<String, String> header = new HashMap<String, String>();
- // 设置请求头的 Content-Type
- header.put("Content-Type", "application/json");
- // 设置请求头的 Authorization
- header.put("Authorization", "Bearer " + token);
- JSONObject responseJson = helper.getURL(url, header, new JSONObject(), method,businessId,"启用/禁用/删除","北森");
- return responseJson;
- }
- public static void main(String[] args) {
- MetaDataUtil.getEntityObjectByBosType(BOSUuid.read("/1aEqNuoTWaCOnKAj1sSTHSuYS4=").getType()).getAlias();
- }
- }
|