ERPDocking.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package utility;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import common.YosException;
  5. import utility.tools.WebRequest;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.PrintWriter;
  10. import java.net.CookieHandler;
  11. import java.net.CookieManager;
  12. import java.net.HttpURLConnection;
  13. import java.net.URL;
  14. import java.nio.charset.StandardCharsets;
  15. import java.util.HashMap;
  16. public class ERPDocking {
  17. public static boolean loginstatus = false;
  18. public ERPDocking() {
  19. // try {
  20. // login();
  21. // } catch (YosException e) {
  22. // System.err.println(e.getMessage());
  23. // }
  24. }
  25. /*
  26. crm登陆
  27. */
  28. // private void login() throws YosException {
  29. // if (!loginstatus) {
  30. // /*
  31. // 自动保存cookie
  32. // */
  33. // CookieManager manager = new CookieManager();
  34. // CookieHandler.setDefault(manager);
  35. // String s = new WebRequest().doPost("username=yosAdmin&password=yosAdmin123", "http://crm.meida.com/dmsService/ext/partner/login");
  36. // JSONObject object = JSONObject.parseObject(s);
  37. // if (object.getIntValue("code") == 1) {
  38. // loginstatus = true;
  39. // } else {
  40. // loginstatus = false;
  41. // }
  42. // }
  43. // }
  44. /**
  45. * crm账号批量查询
  46. *
  47. * @param fagentNum 经销商编号,可为空
  48. * @param fmobile 手机号,可为空
  49. * @param fupdateFlag 同步状态标识【0/1/2/99 : 未获取/已获取/获取中/待更新】
  50. * @param pageSize 每页数量
  51. * @param pageNumber 页码
  52. * @return crm账号列表
  53. * @throws YosException
  54. */
  55. public JSONArray getAccount(String fagentNum, String fmobile, int fupdateFlag, int pageSize, int pageNumber) throws YosException {
  56. JSONObject object = new JSONObject();
  57. object.put("fagentNum", fagentNum);
  58. object.put("fmobile", fmobile);
  59. object.put("fupdateFlag", fupdateFlag);//同步状态标识【0/1/2/99 : 未获取/已获取/获取中/待更新】
  60. object.put("pageSize", pageSize);
  61. object.put("pageNumber", pageNumber);
  62. HashMap<String, String> map = new HashMap<>();
  63. map.put("content-Type", "application/json");
  64. String result = new WebRequest().doPost(object.toString(), "http://crm.meida.com/dmsService/ext/partner/userYos/querySynUserYosList", map);
  65. JSONObject resultobject = JSONObject.parseObject(result);
  66. if (resultobject.getIntValue("code") == 20001) {
  67. loginstatus = false;
  68. throw new YosException("crm不在登陆状态");
  69. } else {
  70. return resultobject.getJSONObject("data").getJSONArray("rows");
  71. }
  72. }
  73. /**
  74. * crm账号同步反馈
  75. *
  76. * @param id crm账号id
  77. * @param succ 同步是否成功
  78. * @param errmsg 错误原因
  79. * @return
  80. * @throws YosException
  81. */
  82. public boolean feedback(long id, boolean succ, String errmsg) throws YosException {
  83. JSONArray array = new JSONArray();
  84. JSONObject object = new JSONObject();
  85. object.put("id", id);
  86. object.put("fupdateFlag", succ ? 1 : 4);
  87. object.put("fupdateWarnLog", errmsg);
  88. array.add(object);
  89. HashMap<String, String> map = new HashMap<>();
  90. map.put("content-Type", "application/json");
  91. System.out.println("feedback:"+array.toString());
  92. String result = new WebRequest().doPost(array.toString(), "http://crm.meida.com/dmsService/ext/partner/userYos/syncFupdateFlags", map);
  93. JSONObject resultobject = JSONObject.parseObject(result);
  94. return resultobject.getIntValue("code") == 1;
  95. }
  96. /**
  97. * crm账号同步反馈
  98. *
  99. * @param id crm账号id
  100. * @param succ 同步是否成功
  101. * @param errmsg 错误原因
  102. * @return
  103. * @throws YosException
  104. */
  105. public boolean feedback1(long id, boolean succ, String errmsg) throws YosException {
  106. JSONArray array = new JSONArray();
  107. JSONObject object = new JSONObject();
  108. object.put("id", id);
  109. object.put("fupdateFlag", succ ? 99 : 4);
  110. object.put("fupdateWarnLog", errmsg);
  111. array.add(object);
  112. HashMap<String, String> map = new HashMap<>();
  113. map.put("content-Type", "application/json");
  114. System.out.println("feedback1:"+array.toString());
  115. String result = new WebRequest().doPost(array.toString(), "http://crm.meida.com/dmsService/ext/partner/userYos/syncFupdateFlags", map);
  116. JSONObject resultobject = JSONObject.parseObject(result);
  117. return resultobject.getIntValue("code") == 1;
  118. }
  119. /**
  120. * 获取指定的账号信息
  121. *
  122. * @param id crm账号id
  123. * @return
  124. * @throws YosException
  125. */
  126. public JSONObject getSingleAccount(long id) throws YosException {
  127. String result = doGet("http://crm.meida.com/dmsService/ext/partner/userYos/getSynUserYos?id=" + id);
  128. JSONObject resultobject = JSONObject.parseObject(result);
  129. if (resultobject.getIntValue("code") == 20001) {
  130. loginstatus = false;
  131. throw new YosException("crm不在登陆状态");
  132. } else {
  133. return resultobject.getJSONObject("data");
  134. }
  135. }
  136. /**
  137. * 更新账号有效期
  138. *
  139. * @param id crm账号id
  140. * @param fexpireTime 账号到期时间
  141. * @return
  142. * @throws YosException
  143. */
  144. public boolean updateAccount(long id, String fexpireTime) throws YosException {
  145. JSONObject object = new JSONObject();
  146. object.put("id", id);
  147. object.put("fexpireTime", fexpireTime);
  148. HashMap<String, String> map = new HashMap<>();
  149. map.put("content-Type", "application/json");
  150. String result = new WebRequest().doPost(object.toString(), "http://crm.meida.com/dmsService/ext/partner/userYos/editSynUserYos", map);
  151. JSONObject resultobject = JSONObject.parseObject(result);
  152. if (resultobject.getIntValue("code") == 20001) {
  153. loginstatus = false;
  154. throw new YosException("crm不在登陆状态");
  155. } else {
  156. return resultobject.getIntValue("code") == 1;
  157. }
  158. }
  159. private String doGet(String url) {
  160. PrintWriter out = null;
  161. BufferedReader in = null;
  162. StringBuilder result = new StringBuilder();
  163. HttpURLConnection conn = null;
  164. try {
  165. URL realUrl = new URL(url);
  166. // 打开和URL之间的连接
  167. conn = (HttpURLConnection) realUrl.openConnection();
  168. conn.setRequestProperty("accept", "*/*");
  169. conn.setRequestProperty("connection", "Keep-Alive");
  170. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  171. conn.setConnectTimeout(8000);
  172. in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
  173. String line;
  174. while ((line = in.readLine()) != null) {
  175. result.append(line);
  176. }
  177. } catch (Exception e) {
  178. e.printStackTrace();
  179. System.err.println("[POST请求]向地址:" + url + " 发送数据:发生错误!");
  180. } finally {// 使用finally块来关闭输出流、输入流
  181. if (out != null) {
  182. out.close();
  183. out = null;
  184. }
  185. if (in != null) {
  186. try {
  187. in.close();
  188. } catch (IOException e) {
  189. e.printStackTrace();
  190. }
  191. in = null;
  192. }
  193. if (conn != null) {
  194. conn.disconnect();
  195. conn = null;
  196. }
  197. }
  198. return result.toString();
  199. }
  200. }