You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
381 lines
15 KiB
381 lines
15 KiB
3 years ago
|
package com.fr.plugin.utils;
|
||
|
|
||
|
import com.fr.base.FRContext;
|
||
|
import com.fr.base.ServerConfig;
|
||
|
import com.fr.json.JSONArray;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.plugin.beans.MyDepBean;
|
||
|
import com.fr.plugin.beans.MyUserBean;
|
||
|
import com.fr.plugin.config.YituanPri;
|
||
|
import com.fr.plugin.web.hander.LoginUserModel;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.third.org.apache.commons.lang3.RandomStringUtils;
|
||
|
import com.fr.third.org.apache.commons.lang3.time.DateFormatUtils;
|
||
|
|
||
|
import java.io.*;
|
||
|
import java.net.HttpURLConnection;
|
||
|
import java.net.URL;
|
||
|
import java.net.URLEncoder;
|
||
|
import java.nio.charset.StandardCharsets;
|
||
|
import java.util.*;
|
||
|
|
||
|
public class WebUtils {
|
||
|
public static String post(String path, Map<String, Object> param) {
|
||
|
String var3 = getParam(param,ServerConfig.getInstance().getServerCharset());
|
||
|
PrintWriter var4 = null;
|
||
|
BufferedReader var5 = null;
|
||
|
String var6 = "";
|
||
|
|
||
|
try {
|
||
|
URL var7 = new URL(path);
|
||
|
HttpURLConnection var8 = (HttpURLConnection) var7.openConnection();
|
||
|
var8.setRequestProperty("accept", "*/*");
|
||
|
var8.setRequestProperty("connection", "Keep-Alive");
|
||
|
var8.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||
|
// var8.setRequestProperty("Accept-Charset", "UTF-8");
|
||
|
var8.setRequestMethod("POST");
|
||
|
var8.setDoOutput(true);
|
||
|
var8.setDoInput(true);
|
||
|
var4 = new PrintWriter(new OutputStreamWriter(var8.getOutputStream(), StandardCharsets.UTF_8));
|
||
|
var4.print(var3);
|
||
|
var4.flush();
|
||
|
|
||
|
String var9;
|
||
|
for (var5 = new BufferedReader(new InputStreamReader(var8.getInputStream(), "UTF-8")); (var9 = var5.readLine()) != null; var6 = var6 + var9) {
|
||
|
;
|
||
|
}
|
||
|
} catch (Exception var18) {
|
||
|
var18.printStackTrace();
|
||
|
} finally {
|
||
|
try {
|
||
|
if (var4 != null) {
|
||
|
var4.close();
|
||
|
}
|
||
|
|
||
|
if (var5 != null) {
|
||
|
var5.close();
|
||
|
}
|
||
|
} catch (Exception var17) {
|
||
|
;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
return var6;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 发送json 模式的请求
|
||
|
*
|
||
|
* @param url
|
||
|
* @param param
|
||
|
* @return
|
||
|
*/
|
||
|
public static String sendJsonPost(String url, String param) {
|
||
|
StringBuilder sb = new StringBuilder();
|
||
|
PrintWriter out = null;
|
||
|
BufferedReader in = null;
|
||
|
HttpURLConnection conn = null;
|
||
|
try {
|
||
|
URL realUrl = new URL(url);
|
||
|
// 打开和URL之间的连接
|
||
|
conn = (HttpURLConnection) realUrl.openConnection();
|
||
|
// 设置通用的请求属性
|
||
|
conn.setRequestProperty("accept", "*/*");
|
||
|
conn.setRequestMethod("POST");
|
||
|
// conn.setRequestProperty("connection", "Keep-Alive");
|
||
|
String property = System.getProperty("os.name");
|
||
|
String serverCharset="utf-8";
|
||
|
// if (property.contains("windows")) {
|
||
|
// serverCharset="GBK";
|
||
|
// }
|
||
|
conn.setRequestProperty("Content-Type", "application/json; charset="+serverCharset);
|
||
|
|
||
|
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||
|
// 发送POST请求必须设置如下两行
|
||
|
conn.setDoOutput(true);
|
||
|
conn.setDoInput(true);
|
||
|
// 获取URLConnection对象对应的输出流
|
||
|
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
|
||
|
// 发送请求参数
|
||
|
if (StringUtils.isNotBlank(param)) {
|
||
|
out.print(param);
|
||
|
}
|
||
|
// flush输出流的缓冲
|
||
|
out.flush();
|
||
|
// 定义BufferedReader输入流来读取URL的响应
|
||
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||
|
String line;
|
||
|
sb = new StringBuilder();
|
||
|
while ((line = in.readLine()) != null) {
|
||
|
sb.append(line);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
if (conn != null) {
|
||
|
InputStream errorStream = ( conn).getErrorStream();
|
||
|
in = new BufferedReader(new InputStreamReader(errorStream));
|
||
|
sb = new StringBuilder();
|
||
|
String line;
|
||
|
try {
|
||
|
while ((line = in.readLine()) != null) {
|
||
|
sb.append(line);
|
||
|
}
|
||
|
} catch (Exception ee) {
|
||
|
|
||
|
}
|
||
|
System.out.println("错误响应:=======》" + sb);
|
||
|
}
|
||
|
System.out.println("发送 POST 请求出现异常!" + e);
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
// 使用finally块来关闭输出流、输入流
|
||
|
finally {
|
||
|
try {
|
||
|
if (null != in) {
|
||
|
in.close();
|
||
|
}
|
||
|
if (out != null) {
|
||
|
out.close();
|
||
|
}
|
||
|
} catch (IOException ex) {
|
||
|
ex.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
return sb.toString();
|
||
|
}
|
||
|
|
||
|
private static String getParam(Map<String, Object> var0,String enc) {
|
||
|
String var1 = "";
|
||
|
Set var2 = var0.keySet();
|
||
|
Iterator var3 = var2.iterator();
|
||
|
|
||
|
while (var3.hasNext()) {
|
||
|
String var4 = (String) var3.next();
|
||
|
String var5 = var0.get(var4) + "";
|
||
|
|
||
|
try {
|
||
|
var1 = var1 + (var1.length() == 0 ? "" : "&") + URLEncoder.encode(var4, enc) + "=" + URLEncoder.encode(var5, enc);
|
||
|
} catch (Exception var7) {
|
||
|
;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return var1;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
public static String getSyncDepOrUserAccessToken(String eid, String secret) {
|
||
|
Map<String, Object> parms = new HashMap<String, Object>();
|
||
|
parms.put("eid", eid);
|
||
|
parms.put("secret", secret);
|
||
|
parms.put("scope", "resGroupSecret");
|
||
|
|
||
|
return getAccessToken(parms);
|
||
|
}
|
||
|
|
||
|
public static LoginUserModel getUserInfo(String appid, String acc, String tick) throws Exception {
|
||
|
String cloudHost = YituanPri.getInstance().getCloudHost();
|
||
|
String var2 = cloudHost + "/gateway/ticket/user/acquirecontext?accessToken=" + acc;
|
||
|
JSONObject params = new JSONObject();
|
||
|
params.put("appid", appid);
|
||
|
params.put("ticket", tick);
|
||
|
String resp = sendJsonPost(var2, params.toString());
|
||
|
JSONObject jsonObject = new JSONObject(resp);
|
||
|
if (jsonObject.getBoolean("success")) {
|
||
|
JSONObject data = jsonObject.getJSONObject("data");
|
||
|
LoginUserModel model = new LoginUserModel();
|
||
|
model.setAppid(data.getString("appid"));
|
||
|
model.setUsername(data.getString("username"));
|
||
|
model.setUserid(data.getString("userid"));
|
||
|
model.setJobNo(data.getString("jobNo"));
|
||
|
model.setOpenid(data.getString("openid"));
|
||
|
return model;
|
||
|
} else {
|
||
|
FRContext.getLogger().info("登陆用户失败,响应:" + resp);
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 根据不同的scope获取accessToken
|
||
|
* resGroupSecret 组织人员通讯录读取密钥、组织人员通讯录同步密钥、签到数据密钥、时间助手密钥、
|
||
|
* 生态圈同步密钥
|
||
|
*
|
||
|
* @param appId
|
||
|
* @param secret
|
||
|
* @return
|
||
|
*/
|
||
|
public static String getAppAccessToken(String appId, String secret) {
|
||
|
Map<String, Object> parms = new HashMap<String, Object>();
|
||
|
parms.put("appId", appId);
|
||
|
parms.put("secret", secret);
|
||
|
parms.put("scope", "app");
|
||
|
|
||
|
return getAccessToken(parms);
|
||
|
}
|
||
|
|
||
|
public static String getAccessToken(Map<String, Object> parms) {
|
||
|
String cloudHost = YituanPri.getInstance().getCloudHost();
|
||
|
String url = cloudHost + "/gateway/oauth2/token/getAccessToken";
|
||
|
parms.put("timestamp", System.currentTimeMillis() + "");
|
||
|
String content = post(url, parms);
|
||
|
FineLoggerFactory.getLogger().error("获取ticket:{}", content);
|
||
|
JSONObject object = new JSONObject(content);
|
||
|
Boolean success = object.getBoolean("success");
|
||
|
if (success) {
|
||
|
JSONObject data = object.getJSONObject("data");
|
||
|
return data.getString("accessToken");
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public static List<MyUserBean> getUserByPage(String eid, String sect, int page) {
|
||
|
String cloudHost = YituanPri.getInstance().getCloudHost();
|
||
|
String url = cloudHost +"/gateway/openimport/open/person/getall?accessToken=" + getSyncDepOrUserAccessToken(eid, sect);
|
||
|
Map<String, Object> params = new HashMap<>();
|
||
|
params.put("nonce", RandomStringUtils.randomAlphabetic(8));
|
||
|
params.put("eid", eid);
|
||
|
JSONObject object = new JSONObject();
|
||
|
object.put("begin", page * 1000);
|
||
|
object.put("count", 1000);
|
||
|
params.put("data", object.toString());
|
||
|
String post = post(url, params);
|
||
|
JSONObject json = new JSONObject(post);
|
||
|
List<MyUserBean> list = new ArrayList<>();
|
||
|
if (json.getBoolean("success")) {
|
||
|
JSONArray deps = json.getJSONArray("data");
|
||
|
for (int i = 0; i < deps.size(); i++) {
|
||
|
JSONObject userJson = deps.getJSONObject(i);
|
||
|
/*
|
||
|
* 人员
|
||
|
"openId":String, //人员的openid
|
||
|
"name":String, //姓名
|
||
|
"photoUrl":String, //头像URL
|
||
|
"phone":String, //手机号码
|
||
|
"isHidePhone":String, //是否在通讯录中隐藏手机号码,0: 不隐藏; 1: 隐藏,默认为0
|
||
|
"email":String, //邮箱
|
||
|
"department":String, //组织长名称
|
||
|
"jobNo":String, //企业工号
|
||
|
"jobTitle":String, //职位
|
||
|
"gender":int, //性别,0: 不确定; 1: 男; 2: 女
|
||
|
"status":int, //状态 0: 注销,1: 正常,2: 禁用
|
||
|
"orgUserType":int //是否部门负责人 0:否, 1:是
|
||
|
*/
|
||
|
String id = userJson.getString("openId");
|
||
|
String photoUrl = userJson.getString("photoUrl");
|
||
|
String name = userJson.getString("name");
|
||
|
String jobNo = userJson.getString("jobNo");
|
||
|
String phone = userJson.getString("phone");
|
||
|
String depName = userJson.getString("department");
|
||
|
int status = userJson.getInt("status");
|
||
|
MyUserBean entity = new MyUserBean();
|
||
|
entity.setPhone(phone);
|
||
|
entity.setPhotoUrl(photoUrl);
|
||
|
entity.setStatus(status);
|
||
|
entity.setOpenId(id);
|
||
|
entity.setName(name);
|
||
|
entity.setDepartment(depName);
|
||
|
entity.setJobNo(jobNo);
|
||
|
list.add(entity);
|
||
|
}
|
||
|
} else {
|
||
|
FRContext.getLogger().info("同步用户失败,响应:" + post);
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* sect 组织人员通讯录读取密钥
|
||
|
*
|
||
|
* @param eid
|
||
|
* @param sect
|
||
|
*/
|
||
|
public static List<MyDepBean> getAllDep(String eid, String sect) {
|
||
|
String cloudHost = YituanPri.getInstance().getCloudHost();
|
||
|
String url = cloudHost +"/gateway/openimport/open/dept/getall?accessToken=" + getSyncDepOrUserAccessToken(eid, sect);
|
||
|
Map<String, Object> params = new HashMap<>();
|
||
|
params.put("nonce", RandomStringUtils.randomAlphabetic(8));
|
||
|
params.put("eid", eid);
|
||
|
String post = post(url, params);
|
||
|
JSONObject json = new JSONObject(post);
|
||
|
List<MyDepBean> list = new ArrayList<>();
|
||
|
if (json.getBoolean("success")) {
|
||
|
JSONArray deps = json.getJSONArray("data");
|
||
|
for (int i = 0; i < deps.size(); i++) {
|
||
|
JSONObject depJson = deps.getJSONObject(i);
|
||
|
/**
|
||
|
* { //组织列表
|
||
|
* "id":String, //组织的id
|
||
|
* "parentId":String, //组织父Id
|
||
|
* "name":String, //组织名称
|
||
|
* "department":String, //组织长名称
|
||
|
* "weights":int //排序码
|
||
|
* }
|
||
|
*/
|
||
|
String id = depJson.getString("id");
|
||
|
String pid = depJson.getString("parentId");
|
||
|
String name = depJson.getString("name");
|
||
|
String depName = depJson.getString("department");
|
||
|
int order = depJson.getInt("weights");
|
||
|
MyDepBean depBean = new MyDepBean();
|
||
|
depBean.setDepName(depName);
|
||
|
depBean.setId(id);
|
||
|
depBean.setOrder(order);
|
||
|
depBean.setParentId(pid);
|
||
|
depBean.setName(name);
|
||
|
list.add(depBean);
|
||
|
}
|
||
|
} else {
|
||
|
FRContext.getLogger().info("同步部门失败,响应:" + post);
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
public static JSONArray getUserLastSyn(int begin, String lastSyncUserTime) {
|
||
|
YituanPri myConfig = YituanPri.getInstance();
|
||
|
String eid = myConfig.getEid();
|
||
|
String sect = myConfig.getReadKey();
|
||
|
String cloudHost = myConfig.getCloudHost();
|
||
|
String url = cloudHost +"/gateway/openimport/open/person/getAtTime?accessToken=" + getSyncDepOrUserAccessToken(eid, sect);
|
||
|
Map<String, Object> params = new HashMap<>();
|
||
|
params.put("eid", eid);
|
||
|
params.put("time", lastSyncUserTime);
|
||
|
params.put("begin", begin);
|
||
|
String post = post(url, params);
|
||
|
// FineLoggerFactory.getLogger().info("同步的用户数据:{}",post);
|
||
|
JSONObject json = new JSONObject(post);
|
||
|
if (json.getBoolean("success")) {
|
||
|
return json.getJSONArray("data");
|
||
|
}
|
||
|
return JSONArray.create();
|
||
|
}
|
||
|
|
||
|
public static JSONArray getDepLastSyn() {
|
||
|
YituanPri myConfig = YituanPri.getInstance();
|
||
|
String eid = myConfig.getEid();
|
||
|
String sect = myConfig.getReadKey();
|
||
|
String cloudHost = myConfig.getCloudHost();
|
||
|
String url = cloudHost+"/gateway/openimport/open/dept/getAtTime?accessToken=" + getSyncDepOrUserAccessToken(eid, sect);
|
||
|
String lastSyncUserTime = myConfig.getLastSyncDepTime();//格式:“2014-08-02 01:40:38”
|
||
|
String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
|
||
|
if (StringUtils.isBlank(lastSyncUserTime)) {
|
||
|
lastSyncUserTime = format;
|
||
|
}
|
||
|
myConfig.setLastSyncDepTime(format);
|
||
|
Map<String, Object> params = new HashMap<>();
|
||
|
params.put("eid", eid);
|
||
|
params.put("time", lastSyncUserTime);
|
||
|
String post = post(url, params);
|
||
|
FineLoggerFactory.getLogger().info("同步的部门数据:{}", post);
|
||
|
JSONObject json = new JSONObject(post);
|
||
|
if (json.getBoolean("success")) {
|
||
|
JSONArray users = json.getJSONArray("data");
|
||
|
return users;
|
||
|
}
|
||
|
return JSONArray.create();
|
||
|
}
|
||
|
|
||
|
}
|