/* * Copyright (C), 2018-2021 * Project: starter * FileName: WechatTableDataModel * Author: xxx * Date: 2021/12/8 19:09 */ package com.fr.plugin.xxx.data; import com.fanruan.api.i18n.I18nKit; import com.fanruan.api.log.LogKit; import com.fr.json.JSONArray; import com.fr.json.JSONObject; import com.fr.log.FineLoggerFactory; import com.fr.plugin.context.PluginContexts; import com.fr.plugin.xxx.kit.HttpKit; import com.fr.plugin.xxx.util.RSAUtils; import com.fr.stable.ParameterProvider; import com.fr.third.org.apache.commons.codec.binary.Base64; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** *
* * * @author xxx * @since 1.0.0 */ public class UsersTableDataModel extends WebTableDataModel { public static final String TOTAL_CAGE_SUM = "totalCageSum"; public static final String FREE_PRE = "freePre"; public static final String NAME = "name"; public static final String FREE_CAGE_SUM = "freeCageSum"; public static final String FEED_ANIMAL_SUM = "feedAnimalSum"; private static final long serialVersionUID = 1561845127687781184L; public UsersTableDataModel(ParameterProvider[] parameters) { super(parameters); // this.columnNames = new String[]{TOTAL_CAGE_SUM, FREE_PRE, NAME, FREE_CAGE_SUM, FEED_ANIMAL_SUM}; this.columnNames = new String[]{"总笼位", "空置率", "名称", "空笼位", "在笼猴子数"}; } @Override protected void init() { if (!PluginContexts.currentContext().isAvailable()) { LogKit.error(I18nKit.getLocText("Plugin-xxx_Licence_Expired")); return; } if (this.valueList != null) { return; } this.valueList = new ArrayList(); // 域名 this.host = this.parameters[0].getValue().toString(); // 应用标识 String apiKey = this.parameters[1].getValue().toString(); // 公钥 String publicKey = this.parameters[2].getValue().toString(); try { JSONArray usersInfo = getUserInfo(apiKey, publicKey); addFRUserInfo2List(usersInfo); } catch (Exception e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); } } /** * 人员获取 * * @param apiKey * @param publicKey * @return * @throws IOException */ private JSONArray getUserInfo(String apiKey, String publicKey) throws Exception { Map header = new HashMap<>(); header.put("API_KEY", apiKey); String response = HttpKit.get(this.host, new HashMap<>(), header); LogKit.info("xxx-UsersTableDataModel-getUserInfo-response:{}", response); byte[] bytes = Base64.decodeBase64(response); byte[] decodedData = RSAUtils.decryptByPublicKey(bytes, publicKey); String decodedDataStr = new String(decodedData); LogKit.info("xxx-UsersTableDataModel-getUserInfo-decodedData:{}", decodedDataStr); return new JSONArray(decodedDataStr); } private void addFRUserInfo2List(JSONArray usersInfo) { ArrayList frUserInfo; for (Object o : usersInfo) { JSONObject userInfo = (JSONObject) o; frUserInfo = new ArrayList<>(); frUserInfo.add(userInfo.getString(TOTAL_CAGE_SUM)); frUserInfo.add(userInfo.getString(FREE_PRE)); frUserInfo.add(userInfo.getString(NAME)); frUserInfo.add(userInfo.getString(FREE_CAGE_SUM)); frUserInfo.add(userInfo.getString(FEED_ANIMAL_SUM)); this.valueList.add(frUserInfo.toArray()); } } }