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.

284 lines
11 KiB

package com.eco.plugin.xx.hqctb.function;
import com.eco.plugin.xx.hqctb.config.PluginSimpleConfig;
import com.eco.plugin.xx.hqctb.utils.FRUtils;
import com.eco.plugin.xx.hqctb.utils.HttpUtils;
import com.eco.plugin.xx.hqctb.utils.JDBCUtils;
import com.eco.plugin.xx.hqctb.utils.Utils;
import com.fr.json.JSONObject;
import com.fr.script.AbstractFunction;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class AuthFunction extends AbstractFunction {
@Override
public Object run(Object[] objects) {
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
String tokenid = login(psc);
if(Utils.isNullStr(tokenid)){
FRUtils.FRLogError("登陆失败");
return "FAIL";
}
JSONObject pullJsonObj = new JSONObject();
pullJsonObj.put("tokenId", tokenid);
pullTaskAndFinishForJsonObj(psc,pullJsonObj,tokenid);
logout(psc,pullJsonObj);
return "SUCCESS";
}
//获取token
private static String login(PluginSimpleConfig psc) {
JSONObject loginJsonObj = new JSONObject();
loginJsonObj.put("systemCode", psc.getSystemcode());
loginJsonObj.put("integrationKey", psc.getIntegrationKey());
loginJsonObj.put("force", true);
loginJsonObj.put("timestamp", Integer.valueOf(Long.toString(System.currentTimeMillis() / 1000L)));
String url = null;
try {
url = psc.getApiurl()+"?method=login&request="+ URLEncoder.encode(loginJsonObj.toString(),"Utf-8");
} catch (UnsupportedEncodingException e) {
FRUtils.FRLogError("url编码异常");
return "";
}
String result = HttpUtils.httpGet(url,null,null);
if(Utils.isNullStr(result)){
return "";
}
JSONObject json = new JSONObject(result);
Boolean isLoginSuccess = json.getBoolean("success");
if(isLoginSuccess){
return json.getString("tokenId");
}
return "";
}
//获取token
private static void logout(PluginSimpleConfig psc,JSONObject param) {
param.put("timestamp", Integer.valueOf(Long.toString(System.currentTimeMillis() / 1000L)));
String url = null;
try {
url = psc.getApiurl()+"?method=logout&request="+ URLEncoder.encode(param.toString(),"Utf-8");
} catch (UnsupportedEncodingException e) {
FRUtils.FRLogError("url编码异常");
return ;
}
String result = HttpUtils.httpGet(url,null,null);
if(Utils.isNullStr(result)){
FRUtils.FRLogError("登出异常:");
return ;
}
JSONObject json = new JSONObject(result);
Boolean isLoginSuccess = json.getBoolean("success");
if (!isLoginSuccess) {
FRUtils.FRLogInfo("登出失败!");
}
}
//操作用户
private static void operateUser(JSONObject pullResObject) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
String effectOn = pullResObject.getString("effectOn");
JSONObject orgJsonObj = pullResObject.getJSONObject("data");
StringBuilder deleteSQL;
if ("DELETED".equals(effectOn)) {
conn = JDBCUtils.getConnect();
conn.setAutoCommit(false);
deleteSQL = new StringBuilder();
deleteSQL.append("delete from user_info where username = ?");
ps = conn.prepareStatement(deleteSQL.toString());
ps.setString(1, orgJsonObj.getString("username"));
ps.executeUpdate();
conn.commit();
} else {
conn = JDBCUtils.getConnect();
conn.setAutoCommit(false);
deleteSQL = new StringBuilder();
deleteSQL.append("replace into user_info ");
deleteSQL.append("(_user, _organization, username, password, fullname, ");
deleteSQL.append(" is_disabled, is_locked, create_at, update_at, is_system, ");
deleteSQL.append(" is_public, is_master, email, company_code, ");
deleteSQL.append(" depart_code, effect_sign, job_name, mobile) ");
deleteSQL.append(" values ");
deleteSQL.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
ps = conn.prepareStatement(deleteSQL.toString());
ps.setString(1, orgJsonObj.getString("_user"));
ps.setString(2, orgJsonObj.getString("_organization"));
ps.setString(3, orgJsonObj.getString("username"));
ps.setString(4, orgJsonObj.getString("password"));
ps.setString(5, orgJsonObj.getString("fullname"));
ps.setString(6, orgJsonObj.getString("isDisabled"));
ps.setString(7, orgJsonObj.getString("isLocked"));
ps.setString(8, orgJsonObj.getString("createAt"));
ps.setString(9, orgJsonObj.getString("updateAt"));
ps.setString(10, orgJsonObj.getString("isSystem"));
ps.setString(11, orgJsonObj.getString("isPublic"));
ps.setString(12, orgJsonObj.getString("isMaster"));
ps.setString(13, orgJsonObj.getString("email"));
ps.setString(14, orgJsonObj.getString("companyCode"));
ps.setString(15, orgJsonObj.getString("departCode"));
ps.setString(16, orgJsonObj.getString("effectSign"));
ps.setString(17, orgJsonObj.getString("jobName"));
ps.setString(18, orgJsonObj.getString("mobile"));
ps.executeUpdate();
conn.commit();
}
release(conn,ps);
}
//操作机构
private static void operateDept(JSONObject pullResObject) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
String effectOn = pullResObject.getString("effectOn");
JSONObject orgJsonObj = pullResObject.getJSONObject("data");
StringBuilder deleteSQL;
if ("DELETED".equals(effectOn)) {
conn = JDBCUtils.getConnect();
conn.setAutoCommit(false);
deleteSQL = new StringBuilder();
deleteSQL.append("delete from dept_info where code = ?");
ps = conn.prepareStatement(deleteSQL.toString());
ps.setString(1, orgJsonObj.getString("code"));
ps.executeUpdate();
conn.commit();
} else {
conn = JDBCUtils.getConnect();
conn.setAutoCommit(false);
deleteSQL = new StringBuilder();
deleteSQL.append("replace into dept_info ");
deleteSQL.append("(_parent, _organization, code, name, fullname, ");
deleteSQL.append(" description, sequence, is_disabled, create_at, update_at, ");
deleteSQL.append(" company_code, effect_sign, org_type, parent_code) ");
deleteSQL.append(" values ");
deleteSQL.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
ps = conn.prepareStatement(deleteSQL.toString());
ps.setString(1, orgJsonObj.getString("_parent"));
ps.setString(2, orgJsonObj.getString("_organization"));
ps.setString(3, orgJsonObj.getString("code"));
ps.setString(4, orgJsonObj.getString("name"));
ps.setString(5, orgJsonObj.getString("fullname"));
ps.setString(6, orgJsonObj.getString("description"));
ps.setString(7, orgJsonObj.getString("sequence"));
ps.setString(8, orgJsonObj.getString("isDisabled"));
ps.setString(9, orgJsonObj.getString("createAt"));
ps.setString(10, orgJsonObj.getString("updateAt"));
ps.setString(11, orgJsonObj.getString("companyCode"));
ps.setString(12, orgJsonObj.getString("effectSign"));
ps.setString(13, orgJsonObj.getString("orgType"));
ps.setString(14, orgJsonObj.getString("parentCode"));
ps.executeUpdate();
conn.commit();
}
release(conn,ps);
}
public static void pullTaskAndFinishForJsonObj(PluginSimpleConfig psc, JSONObject param, String tokenId) {
param.put("timestamp", Integer.valueOf(Long.toString(System.currentTimeMillis() / 1000L)));
String url = null;
try {
url = psc.getApiurl()+"?method=pullTask&request="+ URLEncoder.encode(param.toString(),"Utf-8");
} catch (UnsupportedEncodingException e) {
FRUtils.FRLogError("url编码异常");
return ;
}
String pullRes = HttpUtils.httpGet(url,null,null);
if(Utils.isNullStr(pullRes)){
return ;
}
JSONObject pullResObject = new JSONObject(pullRes);
boolean isPullSuccess = pullResObject.getBoolean("success");
if(!isPullSuccess){
return ;
}
String taskId = pullResObject.getString("taskId");
String objectType = pullResObject.getString("objectType");
String guid = pullResObject.getString("id");
JSONObject orgJsonObj;
try{
if ("TARGET_ACCOUNT".equals(objectType)) {
operateUser(pullResObject);
} else if ("TARGET_ORGANIZATION".equals(objectType)) {
operateDept(pullResObject);
}
}catch (Exception e){
FRUtils.FRLogError("操作数据库异常"+e.getMessage());
return ;
}
orgJsonObj = new JSONObject();
orgJsonObj.put("tokenId", tokenId);
orgJsonObj.put("taskId", taskId);
orgJsonObj.put("guid", guid);
orgJsonObj.put("success", true);
orgJsonObj.put("timestamp", Integer.valueOf(Long.toString(System.currentTimeMillis() / 1000L)));
String finishUrl = null;
try {
finishUrl = psc.getApiurl()+"?method=pullFinish&request="+ URLEncoder.encode(orgJsonObj.toString(),"Utf-8");
} catch (UnsupportedEncodingException e) {
FRUtils.FRLogError("url编码异常");
return ;
}
String pullFinishRes = HttpUtils.httpGet(finishUrl,null,null);
JSONObject pullFinishResObject = new JSONObject(pullFinishRes);
boolean isPullFinish = pullFinishResObject.getBoolean("success");
if (isPullFinish) {
pullTaskAndFinishForJsonObj(psc,param,tokenId);
}
return ;
}
private static void release(Connection conn, PreparedStatement ps) {
try {
if(ps!=null) {
ps.close();
}
} catch (SQLException e) {
FRUtils.FRLogError("关闭ps异常");
} finally {
ps=null;
}
try {
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
FRUtils.FRLogError("关闭conn异常");
} finally {
conn=null;
}
}
}