package utility; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import common.YosException; import utility.tools.WebRequest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.CookieHandler; import java.net.CookieManager; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.HashMap; public class ERPDocking { public static boolean loginstatus = false; public ERPDocking() { // try { // login(); // } catch (YosException e) { // System.err.println(e.getMessage()); // } } /* crm登陆 */ // private void login() throws YosException { // if (!loginstatus) { // /* // 自动保存cookie // */ // CookieManager manager = new CookieManager(); // CookieHandler.setDefault(manager); // String s = new WebRequest().doPost("username=yosAdmin&password=yosAdmin123", "http://crm.meida.com/dmsService/ext/partner/login"); // JSONObject object = JSONObject.parseObject(s); // if (object.getIntValue("code") == 1) { // loginstatus = true; // } else { // loginstatus = false; // } // } // } /** * crm账号批量查询 * * @param fagentNum 经销商编号,可为空 * @param fmobile 手机号,可为空 * @param fupdateFlag 同步状态标识【0/1/2/99 : 未获取/已获取/获取中/待更新】 * @param pageSize 每页数量 * @param pageNumber 页码 * @return crm账号列表 * @throws YosException */ public JSONArray getAccount(String fagentNum, String fmobile, int fupdateFlag, int pageSize, int pageNumber) throws YosException { JSONObject object = new JSONObject(); object.put("fagentNum", fagentNum); object.put("fmobile", fmobile); object.put("fupdateFlag", fupdateFlag);//同步状态标识【0/1/2/99 : 未获取/已获取/获取中/待更新】 object.put("pageSize", pageSize); object.put("pageNumber", pageNumber); HashMap map = new HashMap<>(); map.put("content-Type", "application/json"); String result = new WebRequest().doPost(object.toString(), "http://crm.meida.com/dmsService/ext/partner/userYos/querySynUserYosList", map); JSONObject resultobject = JSONObject.parseObject(result); if (resultobject.getIntValue("code") == 20001) { loginstatus = false; throw new YosException("crm不在登陆状态"); } else { return resultobject.getJSONObject("data").getJSONArray("rows"); } } /** * crm账号同步反馈 * * @param id crm账号id * @param succ 同步是否成功 * @param errmsg 错误原因 * @return * @throws YosException */ public boolean feedback(long id, boolean succ, String errmsg) throws YosException { JSONArray array = new JSONArray(); JSONObject object = new JSONObject(); object.put("id", id); object.put("fupdateFlag", succ ? 1 : 4); object.put("fupdateWarnLog", errmsg); array.add(object); HashMap map = new HashMap<>(); map.put("content-Type", "application/json"); System.out.println("feedback:"+array.toString()); String result = new WebRequest().doPost(array.toString(), "http://crm.meida.com/dmsService/ext/partner/userYos/syncFupdateFlags", map); JSONObject resultobject = JSONObject.parseObject(result); return resultobject.getIntValue("code") == 1; } /** * crm账号同步反馈 * * @param id crm账号id * @param succ 同步是否成功 * @param errmsg 错误原因 * @return * @throws YosException */ public boolean feedback1(long id, boolean succ, String errmsg) throws YosException { JSONArray array = new JSONArray(); JSONObject object = new JSONObject(); object.put("id", id); object.put("fupdateFlag", succ ? 99 : 4); object.put("fupdateWarnLog", errmsg); array.add(object); HashMap map = new HashMap<>(); map.put("content-Type", "application/json"); System.out.println("feedback1:"+array.toString()); String result = new WebRequest().doPost(array.toString(), "http://crm.meida.com/dmsService/ext/partner/userYos/syncFupdateFlags", map); JSONObject resultobject = JSONObject.parseObject(result); return resultobject.getIntValue("code") == 1; } /** * 获取指定的账号信息 * * @param id crm账号id * @return * @throws YosException */ public JSONObject getSingleAccount(long id) throws YosException { String result = doGet("http://crm.meida.com/dmsService/ext/partner/userYos/getSynUserYos?id=" + id); JSONObject resultobject = JSONObject.parseObject(result); if (resultobject.getIntValue("code") == 20001) { loginstatus = false; throw new YosException("crm不在登陆状态"); } else { return resultobject.getJSONObject("data"); } } /** * 更新账号有效期 * * @param id crm账号id * @param fexpireTime 账号到期时间 * @return * @throws YosException */ public boolean updateAccount(long id, String fexpireTime) throws YosException { JSONObject object = new JSONObject(); object.put("id", id); object.put("fexpireTime", fexpireTime); HashMap map = new HashMap<>(); map.put("content-Type", "application/json"); String result = new WebRequest().doPost(object.toString(), "http://crm.meida.com/dmsService/ext/partner/userYos/editSynUserYos", map); JSONObject resultobject = JSONObject.parseObject(result); if (resultobject.getIntValue("code") == 20001) { loginstatus = false; throw new YosException("crm不在登陆状态"); } else { return resultobject.getIntValue("code") == 1; } } private String doGet(String url) { PrintWriter out = null; BufferedReader in = null; StringBuilder result = new StringBuilder(); HttpURLConnection conn = null; try { URL realUrl = new URL(url); // 打开和URL之间的连接 conn = (HttpURLConnection) realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setConnectTimeout(8000); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); System.err.println("[POST请求]向地址:" + url + " 发送数据:发生错误!"); } finally {// 使用finally块来关闭输出流、输入流 if (out != null) { out.close(); out = null; } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } in = null; } if (conn != null) { conn.disconnect(); conn = null; } } return result.toString(); } }