WebClientRest.java 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. package openapi.base.restful;
  2. import baseclass.tools.MessageDigestCust;
  3. import net.sf.json.JSONObject;
  4. import openapi.base.data.Row;
  5. import openapi.base.data.Rows;
  6. import openapi.base.data.db.DBConnect;
  7. import openapi.base.data.db.DataPool;
  8. import openapi.base.data.db.SQLiteJDBC;
  9. import openapi.base.parameter.ErrCode;
  10. import openapi.base.parameter.ErrModel;
  11. import openapi.base.parameter.parameter;
  12. import openapi.restcontroller.wechatapp.system.docManage;
  13. import openapi.tools.kuaidi100;
  14. import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
  15. import org.glassfish.jersey.media.multipart.FormDataParam;
  16. import p2.application.signature.P2User;
  17. import p2.p2server.P2Server;
  18. import p2.pao.PaoRemote;
  19. import p2.pao.PaoSetRemote;
  20. import p2.util.P2Exception;
  21. import javax.servlet.ServletOutputStream;
  22. import javax.servlet.http.HttpServletResponse;
  23. import javax.ws.rs.*;
  24. import javax.ws.rs.core.Context;
  25. import java.io.*;
  26. import java.lang.reflect.Constructor;
  27. import java.lang.reflect.InvocationTargetException;
  28. import java.lang.reflect.Method;
  29. import java.math.BigInteger;
  30. import java.net.URLEncoder;
  31. import java.security.MessageDigest;
  32. import java.security.NoSuchAlgorithmException;
  33. import java.util.*;
  34. @Path("webclientrest")
  35. public class WebClientRest {
  36. @POST
  37. public String method(String RequestContent) {
  38. if (parameter.isdebug) {
  39. new SQLiteJDBC().InsertLogMsg("请求内容", RequestContent, "info", true);
  40. }
  41. /**
  42. * 验证请求正文是否为规范的SONObject格式
  43. */
  44. JSONObject requestcontent = null;
  45. try {
  46. requestcontent = JSONObject.fromObject(RequestContent);
  47. } catch (Exception e) {
  48. return ErrModel.request_BasicJsonFormat().toString();
  49. }
  50. /**
  51. * 验证请求正文中是否包含必填的键值
  52. */
  53. String[] mustkeys = {"accesstoken", "classname", "method", "content"};
  54. for (String mustkey : mustkeys) {
  55. if (!requestcontent.containsKey(mustkey)) {
  56. return ErrModel.request_ContainsMustKey(mustkey).toString();
  57. }
  58. }
  59. /**
  60. * 验证请求正文中的content是否为规范的SONObject格式
  61. */
  62. JSONObject content = new JSONObject();
  63. try {
  64. content = JSONObject.fromObject(requestcontent.getJSONObject("content"));
  65. } catch (Exception e) {
  66. return ErrModel.request_ContentJsonFormat().toString();
  67. }
  68. /**
  69. * 验证正文中的token是否有效
  70. */
  71. String accesstoken = requestcontent.getString("accesstoken");
  72. if (!parameter.isdebug && !parameter.tokenlist.contains(accesstoken)) {
  73. return ErrModel.token_Validate().toString();
  74. }
  75. String result;
  76. String className = requestcontent.getString("classname");
  77. String methodName = requestcontent.getString("method");
  78. if (content.isNullObject()) {
  79. content = new JSONObject();
  80. }
  81. content.put("$classname", className);
  82. content.put("$method", methodName);
  83. content.put("$accesstoken", accesstoken);
  84. String key = className + "." + methodName;
  85. //
  86. try {
  87. boolean getdatafromdbanyway = content.containsKey("getdatafromdbanyway")
  88. && content.getBoolean("getdatafromdbanyway");
  89. content.remove("getdatafromdbanyway");
  90. Object data = null;
  91. if (!getdatafromdbanyway) {
  92. data = DataPool.get(content.toString());
  93. }
  94. if (data != null) {
  95. result = data.toString();
  96. saveCallMethodMsg(key, false, 0L);
  97. } else {
  98. Long starttimes = Calendar.getInstance().getTimeInMillis();
  99. /**
  100. * 执行请求方法
  101. */
  102. Class clz = Class.forName("openapi.restcontroller." + className);
  103. Constructor cla = clz.getDeclaredConstructor(JSONObject.class);
  104. Object obj = cla.newInstance(content);
  105. Method method = obj.getClass().getDeclaredMethod(methodName);
  106. result = (String) method.invoke(obj);
  107. Long endtimes = Calendar.getInstance().getTimeInMillis();
  108. saveCallMethodMsg(key, true, endtimes - starttimes);
  109. }
  110. } catch (ClassNotFoundException e) {
  111. e.printStackTrace();
  112. result = ErrModel.request_GetClass("找不到指定的类" + className).toString();
  113. } catch (InstantiationException e) {
  114. e.printStackTrace();
  115. result = ErrModel.request_GetClass("类" + className + "实例化异常").toString();
  116. } catch (IllegalAccessException e) {
  117. e.printStackTrace();
  118. result = ErrModel.request_GetClass("类" + className + "安全权限异常,可能该类为非public类").toString();
  119. } catch (NoSuchMethodException e) {
  120. e.printStackTrace();
  121. result = ErrModel.request_GetClass("找不到指定的类" + className + "的" + methodName + "方法").toString();
  122. } catch (IllegalArgumentException e) {
  123. e.printStackTrace();
  124. result = ErrModel.request_GetClass("类" + className + "的" + methodName + "方法参数不合法").toString();
  125. } catch (InvocationTargetException e) {
  126. e.printStackTrace();
  127. Throwable targetException = e.getTargetException();
  128. result = ErrModel.request_GetClass(targetException.getMessage()).toString();
  129. } catch (Exception e) {
  130. e.printStackTrace();
  131. result = ErrModel.request_GetClass("发生未知异常" + e.getMessage()).toString();
  132. }
  133. return result;
  134. }
  135. /**
  136. * 记录请求数
  137. *
  138. * @param key
  139. * @param fromdb
  140. * @param time
  141. */
  142. private void saveCallMethodMsg(String key, boolean fromdb, Long time) {
  143. Long callmethodTimes = parameter.callmethodTimes.containsKey(key) ? parameter.callmethodTimes.get(key) : 0L;
  144. //更新请求总数
  145. parameter.callmethodTimes.put(key, callmethodTimes + 1L);
  146. //最新请求时间
  147. parameter.lastcallmethodtime.put(key, Calendar.getInstance().getTime());
  148. //从缓存获取的次数
  149. Long callmethod_fromcacheTimes = parameter.callmethod_fromcacheTimes.containsKey(key) ? parameter.callmethod_fromcacheTimes.get(key) : 0L;
  150. if (!fromdb) {
  151. /**
  152. * 方法请求从缓存获取次数
  153. */
  154. parameter.callmethod_fromcacheTimes.put(key, callmethod_fromcacheTimes + 1L);
  155. } else {
  156. /**
  157. * 方法请求查询最新耗时
  158. */
  159. parameter.callmethodLastTimeLong.put(key, time);
  160. Long totaltimes = callmethodTimes - callmethod_fromcacheTimes;
  161. /**
  162. * 方法请求查询平均时间
  163. */
  164. Long callmethodTimeLong = parameter.callmethodTimeLong.containsKey(key) ? parameter.callmethodTimeLong.get(key) : 0L;
  165. parameter.callmethodTimeLong.put(key, (callmethodTimeLong * totaltimes + time) / (totaltimes + 1));
  166. }
  167. }
  168. @Path("getToken")
  169. @POST
  170. public String getToken(String RequestContent) {
  171. /**
  172. * 验证请求正文是否为规范的SONObject格式
  173. */
  174. JSONObject requestcontent = null;
  175. try {
  176. requestcontent = JSONObject.fromObject(RequestContent);
  177. } catch (Exception e) {
  178. return ErrModel.request_BasicJsonFormat().toString();
  179. }
  180. /**
  181. * 验证请求正文中是否包含必填的键值
  182. */
  183. String[] mustkeys = {"from_account"};
  184. for (String mustkey : mustkeys) {
  185. if (!requestcontent.containsKey(mustkey)) {
  186. return ErrModel.request_ContainsMustKey(mustkey).toString();
  187. }
  188. }
  189. String token = requestcontent.getString("from_account");
  190. parameter.tokenlist.add(token);
  191. JSONObject jsonObject = new JSONObject();
  192. jsonObject.put("token", token);
  193. return jsonObject.toString();
  194. }
  195. @Path("login")
  196. @POST
  197. public String login(String RequestContent) {
  198. /**
  199. * 验证请求正文是否为规范的SONObject格式
  200. */
  201. JSONObject requestcontent = null;
  202. try {
  203. requestcontent = JSONObject.fromObject(RequestContent);
  204. } catch (Exception e) {
  205. return ErrModel.request_BasicJsonFormat().toString();
  206. }
  207. /**
  208. * 验证请求正文中是否包含必填的键值
  209. */
  210. String[] mustkeys = {"username", "password", "from_account"};
  211. for (String mustkey : mustkeys) {
  212. if (!requestcontent.containsKey(mustkey)) {
  213. return ErrModel.request_ContainsMustKey(mustkey).toString();
  214. }
  215. }
  216. String username = requestcontent.getString("username");
  217. String password = requestcontent.getString("password");
  218. // 请求来源
  219. String from_account = requestcontent.getString("from_account");
  220. boolean result = true;
  221. PaoSetRemote userSet = null;
  222. try {
  223. userSet = P2Server.getP2Server().getPaoSet("pp_users",
  224. P2Server.getP2Server().getSystemUserInfo());
  225. userSet.setWhere("hrid='" + username + "'");
  226. userSet.reset();
  227. if (userSet.isEmpty()) {
  228. result = false;
  229. } else {
  230. byte[] bytes = userSet.getPao(0).getBytes("password");
  231. result = password
  232. .equals(new MessageDigestCust().Digest(P2Server.getP2Server().getP2Cipher().decData(bytes)));
  233. }
  234. } catch (Exception e) {
  235. e.printStackTrace();
  236. } finally {
  237. try {
  238. if (userSet != null) {
  239. userSet.clear();
  240. userSet.close();
  241. }
  242. } catch (Exception e) {
  243. e.printStackTrace();
  244. }
  245. }
  246. String token = "";
  247. if (result) {
  248. byte[] secretBytes = null;
  249. try {
  250. secretBytes = MessageDigest.getInstance("md5")
  251. .digest((username + password + Calendar.getInstance().getTimeInMillis()).getBytes());
  252. } catch (NoSuchAlgorithmException e) {
  253. throw new RuntimeException("没有这个md5算法!");
  254. }
  255. token = new BigInteger(1, secretBytes).toString(16);
  256. parameter.tokenlist.add(token);
  257. } else {
  258. return ErrModel.getToken().toString();
  259. }
  260. DBConnect connect = new DBConnect();
  261. // DBConnect connect = new DBConnect("DRP");
  262. // Rows rows = connect.runSqlQuery(
  263. // "select t1.fusertype,t2.fagentnum,t1.defsite as siteid,t1.fcansubmitagentorder,t1.fcanmodifyorderprice from pp_users t1 left join tagents_users t2 on t1.hrid=t2.hrid where t1.hrid='"
  264. // + username + "' and t1.status='ACTIVE'");
  265. Rows rows = connect.runSqlQuery(
  266. "select t1.fusertype,t2.fagentnum,t1.defsite as siteid,t1.fcansubmitagentorder,t1.fcanmodifyorderprice,isnull(t3.fisservice,0)as fisservice,isnull(t3.fisusestatementconfirm,0)as fisusestatementconfirm from pp_users t1 " +
  267. "left join tagents_users t2 on t1.hrid=t2.hrid " +
  268. "left join tagents t3 on t2.siteid=t3.siteid and t2.fagentnum=t3.fagentnum " +
  269. "where t1.hrid='" + username + "' and t1.status='ACTIVE'");
  270. if (rows.isEmpty()) {
  271. JSONObject object = new JSONObject();
  272. object.put("status", "error");
  273. object.put("errcode", ErrCode.gettokenfail[0]);
  274. object.put("msg", "账号已失效");
  275. return object.toString();
  276. }
  277. String fusertype = rows.get(0).getString("fusertype");
  278. String fisservice = rows.get(0).getString("fisservice");
  279. String fagentnum = rows.get(0).getString("fagentnum");
  280. String siteid = rows.get(0).getString("siteid");
  281. String fcansubmitagentorder = rows.get(0).getString("fcansubmitagentorder");
  282. String fcanmodifyorderprice = rows.get(0).getString("fcanmodifyorderprice");
  283. String fisusestatementconfirm = rows.get(0).getString("fisusestatementconfirm");
  284. parameter.siteidList.put(username.toUpperCase(), siteid.toUpperCase());
  285. parameter.usertypeList.put(username.toUpperCase(), fusertype.toUpperCase());
  286. Rows rows_moduleid = connect.runSqlQuery(
  287. "select distinct moduleid from twebclientappidauth t1 inner join pp_grpuser t2 on t1.groupname=t2.groupname where t2.hrid='"
  288. + username + "' and systemid='" + from_account + "'");
  289. JSONObject mobject = new JSONObject();
  290. for (Row row : rows_moduleid) {
  291. String moduleid = row.getString("moduleid");
  292. Rows rows_appid = connect.runSqlQuery(
  293. "select appid from twebclientappidauth t1 inner join pp_grpuser t2 on t1.groupname=t2.groupname where t2.hrid='"
  294. + username + "' and systemid='" + from_account + "' and moduleid='" + moduleid + "' order by t1.sequence");
  295. mobject.put(moduleid, rows_appid.toJsonArray("appid"));
  296. }
  297. JSONObject object = new JSONObject();
  298. object.put("status", "ok");
  299. object.put("token", token);
  300. JSONObject dataobject = new JSONObject();
  301. dataobject.put("fusertype", fusertype);
  302. dataobject.put("fisservice", fisservice);
  303. dataobject.put("fagentnum", fagentnum);
  304. dataobject.put("siteid", siteid);
  305. dataobject.put("fcansubmitagentorder", fcansubmitagentorder);
  306. dataobject.put("fcanmodifyorderprice", fcanmodifyorderprice);
  307. dataobject.put("fisusestatementconfirm", fisusestatementconfirm);
  308. dataobject.put("hrid", username.toUpperCase());
  309. dataobject.put("appids", mobject);
  310. object.put("data", dataobject);
  311. return object.toString();
  312. }
  313. @Path("method")
  314. @SuppressWarnings({"rawtypes", "unchecked"})
  315. @POST
  316. public String method2(String RequestContent) {
  317. if (parameter.isdebug) {
  318. new SQLiteJDBC().InsertLogMsg("请求内容", RequestContent, "info", true);
  319. }
  320. /**
  321. * 验证请求正文是否为规范的SONObject格式
  322. */
  323. JSONObject requestcontent = null;
  324. try {
  325. requestcontent = JSONObject.fromObject(RequestContent);
  326. } catch (Exception e) {
  327. return ErrModel.request_BasicJsonFormat().toString();
  328. }
  329. /**
  330. * 验证请求正文中是否包含必填的键值
  331. */
  332. String[] mustkeys = { "classname", "method", "content"};
  333. for (String mustkey : mustkeys) {
  334. if (!requestcontent.containsKey(mustkey)) {
  335. return ErrModel.request_ContainsMustKey(mustkey).toString();
  336. }
  337. }
  338. /**
  339. * 验证请求正文中的content是否为规范的SONObject格式
  340. */
  341. JSONObject content = new JSONObject();
  342. try {
  343. content = JSONObject.fromObject(requestcontent.getJSONObject("content"));
  344. } catch (Exception e) {
  345. return ErrModel.request_ContentJsonFormat().toString();
  346. }
  347. String result;
  348. String className = requestcontent.getString("classname");
  349. String methodName = requestcontent.getString("method");
  350. if (content.isNullObject()) {
  351. content = new JSONObject();
  352. }
  353. content.put("$classname", className);
  354. content.put("$method", methodName);
  355. String key = className + "." + methodName;
  356. //
  357. try {
  358. boolean getdatafromdbanyway = content.containsKey("getdatafromdbanyway")
  359. && content.getBoolean("getdatafromdbanyway");
  360. content.remove("getdatafromdbanyway");
  361. Object data = null;
  362. if (!getdatafromdbanyway) {
  363. data = DataPool.get(content.toString());
  364. }
  365. if (data != null) {
  366. result = data.toString();
  367. saveCallMethodMsg(key, false, 0L);
  368. } else {
  369. Long starttimes = Calendar.getInstance().getTimeInMillis();
  370. /**
  371. * 执行请求方法
  372. */
  373. Class clz = Class.forName("openapi.restcontroller." + className);
  374. Constructor cla = clz.getDeclaredConstructor(JSONObject.class);
  375. Object obj = cla.newInstance(content);
  376. Method method = obj.getClass().getDeclaredMethod(methodName);
  377. result = (String) method.invoke(obj);
  378. Long endtimes = Calendar.getInstance().getTimeInMillis();
  379. saveCallMethodMsg(key, true, endtimes - starttimes);
  380. }
  381. } catch (ClassNotFoundException e) {
  382. e.printStackTrace();
  383. result = ErrModel.request_GetClass("找不到指定的类" + className).toString();
  384. } catch (InstantiationException e) {
  385. e.printStackTrace();
  386. result = ErrModel.request_GetClass("类" + className + "实例化异常").toString();
  387. } catch (IllegalAccessException e) {
  388. e.printStackTrace();
  389. result = ErrModel.request_GetClass("类" + className + "安全权限异常,可能该类为非public类").toString();
  390. } catch (NoSuchMethodException e) {
  391. e.printStackTrace();
  392. result = ErrModel.request_GetClass("找不到指定的类" + className + "的" + methodName + "方法").toString();
  393. } catch (IllegalArgumentException e) {
  394. e.printStackTrace();
  395. result = ErrModel.request_GetClass("类" + className + "的" + methodName + "方法参数不合法").toString();
  396. } catch (InvocationTargetException e) {
  397. e.printStackTrace();
  398. Throwable targetException = e.getTargetException();
  399. result = ErrModel.request_GetClass(targetException.getMessage()).toString();
  400. } catch (Exception e) {
  401. e.printStackTrace();
  402. result = ErrModel.request_GetClass("发生未知异常" + e.getMessage()).toString();
  403. }
  404. return result;
  405. }
  406. @Path("logout")
  407. @POST
  408. public String logout(String RequestContent) {
  409. /**
  410. * 验证请求正文是否为规范的SONObject格式
  411. */
  412. JSONObject requestcontent = null;
  413. try {
  414. requestcontent = JSONObject.fromObject(RequestContent);
  415. } catch (Exception e) {
  416. return ErrModel.request_BasicJsonFormat().toString();
  417. }
  418. /**
  419. * 验证请求正文中是否包含必填的键值
  420. */
  421. String[] mustkeys = {"accesstoken"};
  422. for (String mustkey : mustkeys) {
  423. if (!requestcontent.containsKey(mustkey)) {
  424. return ErrModel.request_ContainsMustKey(mustkey).toString();
  425. }
  426. }
  427. /**
  428. * 验证正文中的token是否有效
  429. */
  430. String accesstoken = requestcontent.getString("accesstoken");
  431. parameter.tokenlist.remove(accesstoken);
  432. JSONObject object = new JSONObject();
  433. object.put("status", "ok");
  434. return object.toString();
  435. }
  436. @Path("login_jump")
  437. @POST
  438. public String login_jump(String RequestContent) {
  439. /**
  440. * 验证请求正文是否为规范的SONObject格式
  441. */
  442. JSONObject requestcontent = null;
  443. try {
  444. requestcontent = JSONObject.fromObject(RequestContent);
  445. } catch (Exception e) {
  446. return ErrModel.request_BasicJsonFormat().toString();
  447. }
  448. /**
  449. * 验证请求正文中是否包含必填的键值
  450. */
  451. String[] mustkeys = {"from_account", "accesstoken", "username"};
  452. for (String mustkey : mustkeys) {
  453. if (!requestcontent.containsKey(mustkey)) {
  454. return ErrModel.request_ContainsMustKey(mustkey).toString();
  455. }
  456. }
  457. /**
  458. * 验证正文中的token是否有效
  459. */
  460. String accesstoken = requestcontent.getString("accesstoken");
  461. if (!parameter.isdebug && !parameter.tokenlist.contains(accesstoken)) {
  462. return ErrModel.token_Validate().toString();
  463. }
  464. // 请求来源
  465. String from_account = requestcontent.getString("from_account");
  466. String username = requestcontent.getString("username");
  467. DBConnect connect = new DBConnect();
  468. Rows rows = connect.runSqlQuery(
  469. "select t1.fusertype,t2.fagentnum,t1.defsite as siteid from pp_users t1 left join tagents_users t2 on t1.hrid=t2.hrid where t1.hrid='"
  470. + username + "' and t1.status='ACTIVE'");
  471. if (rows.isEmpty()) {
  472. JSONObject object = new JSONObject();
  473. object.put("status", "error");
  474. object.put("errcode", ErrCode.gettokenfail[0]);
  475. object.put("msg", "账号已失效");
  476. return object.toString();
  477. }
  478. String fusertype = rows.get(0).getString("fusertype");
  479. String fagentnum = rows.get(0).getString("fagentnum");
  480. String siteid = rows.get(0).getString("siteid");
  481. parameter.siteidList.put(username.toUpperCase(), siteid.toUpperCase());
  482. parameter.usertypeList.put(username.toUpperCase(), fusertype.toUpperCase());
  483. Rows rows_moduleid = connect.runSqlQuery(
  484. "select distinct moduleid from twebclientappidauth t1 inner join pp_grpuser t2 on t1.groupname=t2.groupname where t2.hrid='"
  485. + username + "' and systemid='" + from_account + "'");
  486. JSONObject mobject = new JSONObject();
  487. for (Row row : rows_moduleid) {
  488. String moduleid = row.getString("moduleid");
  489. Rows rows_appid = connect.runSqlQuery(
  490. "select appid from twebclientappidauth t1 inner join pp_grpuser t2 on t1.groupname=t2.groupname where t2.hrid='"
  491. + username + "' and systemid='" + from_account + "' and moduleid='" + moduleid + "' order by t1.sequence");
  492. mobject.put(moduleid, rows_appid.toJsonArray("appid"));
  493. }
  494. String token = "";
  495. byte[] secretBytes = null;
  496. try {
  497. secretBytes = MessageDigest.getInstance("md5")
  498. .digest((username + accesstoken + Calendar.getInstance().getTimeInMillis()).getBytes());
  499. } catch (NoSuchAlgorithmException e) {
  500. throw new RuntimeException("没有这个md5算法!");
  501. }
  502. token = new BigInteger(1, secretBytes).toString(16);
  503. parameter.tokenlist.add(token);
  504. JSONObject object = new JSONObject();
  505. object.put("status", "ok");
  506. object.put("token", token);
  507. JSONObject dataobject = new JSONObject();
  508. dataobject.put("fusertype", fusertype);
  509. dataobject.put("fagentnum", fagentnum);
  510. dataobject.put("siteid", siteid);
  511. dataobject.put("hrid", username.toUpperCase());
  512. dataobject.put("appids", mobject);
  513. object.put("data", dataobject);
  514. return object.toString();
  515. }
  516. public static boolean FISUPLOADFOROBS=true;
  517. @Path("uploaddoc")
  518. @POST
  519. @Consumes({"multipart/form-data"})
  520. @Produces({"application/json"})
  521. public String upLoadDoc(@FormDataParam("uploadfile") InputStream uploadfileInputStream,
  522. @FormDataParam("uploadfile") FormDataContentDisposition uploadfile,
  523. @FormDataParam("ownertable") String ownertable, @FormDataParam("ownerid") String ownerid,
  524. @FormDataParam("hrid") String hrid, @FormDataParam("description") String description,
  525. @FormDataParam("accesstoken") String accesstoken, @FormDataParam("type") String type) {
  526. /**
  527. * 验证正文中的token是否有效
  528. */
  529. try{
  530. /**
  531. * 验证正文中的token是否有效
  532. */
  533. if (!parameter.isdebug && !parameter.tokenlist.contains(accesstoken)) {
  534. return ErrModel.token_Validate().toString();
  535. }
  536. docManage docManage = new docManage();
  537. String result=null;
  538. Set set=new HashSet();
  539. set.addAll(Arrays.asList("ordernode","hyordernode","hyworkorder","afterserviceorder"));
  540. if(set.contains(ownertable.toLowerCase())&&FISUPLOADFOROBS){
  541. result = docManage.upLoadDocForOBS(uploadfileInputStream, uploadfile, ownertable, ownerid, hrid, description, "",type);
  542. }else{
  543. result = docManage.upLoadDoc(uploadfileInputStream, uploadfile, ownertable, ownerid, hrid, description, "");
  544. }
  545. return result;
  546. }catch (Exception e){
  547. JSONObject returnObject=new JSONObject();
  548. returnObject.put("msg", "失败");
  549. returnObject.put("code", 0);
  550. returnObject.put("errcode", 0);
  551. returnObject.put("data", e.getMessage());
  552. return returnObject.toString();
  553. }
  554. }
  555. @Path("uploaddocurl")
  556. @POST
  557. @Consumes({"multipart/form-data"})
  558. @Produces({"application/json"})
  559. public String upLoadDocUrl(@FormDataParam("url") String urlstr,
  560. @FormDataParam("ownertable") String ownertable, @FormDataParam("ownerid") String ownerid,
  561. @FormDataParam("hrid") String hrid, @FormDataParam("description") String description,
  562. @FormDataParam("accesstoken") String accesstoken) {
  563. /**
  564. * 验证正文中的token是否有效
  565. */
  566. if (!parameter.isdebug && !parameter.tokenlist.contains(accesstoken)) {
  567. return ErrModel.token_Validate().toString();
  568. }
  569. docManage docManage = new docManage();
  570. String result = docManage.upLoadDocUrl(urlstr, ownertable, ownerid, hrid, description);
  571. return result;
  572. }
  573. @Path("uploaddocwechat")
  574. @POST
  575. @Consumes({"multipart/form-data"})
  576. @Produces({"application/json"})
  577. public String upLoadDocUrlWechat(@FormDataParam("uploadfile") InputStream uploadfileInputStream,
  578. @FormDataParam("uploadfile") FormDataContentDisposition uploadfile,
  579. @FormDataParam("ownertable") String ownertable, @FormDataParam("ownerid") String ownerid,
  580. @FormDataParam("hrid") String hrid, @FormDataParam("description") String description,
  581. @FormDataParam("accesstoken") String accesstoken) {
  582. /**
  583. * 验证正文中的token是否有效
  584. */
  585. if (!parameter.isdebug && !parameter.tokenlist.contains(accesstoken)) {
  586. return ErrModel.token_Validate().toString();
  587. }
  588. docManage docManage = new docManage();
  589. String result = docManage.upLoadDoc(uploadfileInputStream, uploadfile, ownertable, ownerid, hrid, description, "微信附件上传");
  590. return result;
  591. }
  592. @Path("wlcx")
  593. @GET
  594. public String wlcx(@QueryParam("com") String com, @QueryParam("num") String num) {
  595. String wl = kuaidi100.queryKuaiDi100(com, num);
  596. return wl;
  597. }
  598. @Path("housekeeperlogin")
  599. @POST
  600. public String housekeeper_login(String RequestContent) {
  601. /**
  602. * 验证请求正文是否为规范的SONObject格式
  603. */
  604. JSONObject requestcontent = null;
  605. try {
  606. requestcontent = JSONObject.fromObject(RequestContent);
  607. } catch (Exception e) {
  608. return ErrModel.request_BasicJsonFormat().toString();
  609. }
  610. /**
  611. * 验证请求正文中是否包含必填的键值
  612. */
  613. String[] mustkeys = {"username", "password", "from_account"};
  614. for (String mustkey : mustkeys) {
  615. if (!requestcontent.containsKey(mustkey)) {
  616. return ErrModel.request_ContainsMustKey(mustkey).toString();
  617. }
  618. }
  619. String username = requestcontent.getString("username");
  620. String password = requestcontent.getString("password");
  621. // System.out.println("username:"+username+" password:"+password);
  622. // 请求来源
  623. String from_account = requestcontent.getString("from_account");
  624. //如果from_account为1 ,则是前端web登录
  625. String usertype=null;
  626. if(from_account.equals("1")){
  627. boolean result = true;
  628. PaoSetRemote userSet = null;
  629. try {
  630. userSet = P2Server.getP2Server().getPaoSet("pp_users",
  631. P2Server.getP2Server().getSystemUserInfo());
  632. userSet.setWhere("hrid='" + username + "'");
  633. userSet.reset();
  634. if (userSet.isEmpty()) {
  635. JSONObject object = new JSONObject();
  636. object.put("status", "error");
  637. object.put("errcode", ErrCode.gettokenfail[0]);
  638. object.put("msg", "该帐号无登录权限!");
  639. return object.toString();
  640. } else {
  641. usertype=userSet.getPao(0).getString("FUSERTYPE");
  642. byte[] bytes = userSet.getPao(0).getBytes("password");
  643. result = password
  644. .equals(P2Server.getP2Server().getP2Cipher().decData(bytes));
  645. }
  646. } catch (Exception e) {
  647. e.printStackTrace();
  648. } finally {
  649. try {
  650. if (userSet != null) {
  651. userSet.clear();
  652. userSet.close();
  653. }
  654. } catch (Exception e) {
  655. e.printStackTrace();
  656. }
  657. }
  658. String token = "";
  659. if (result) {
  660. byte[] secretBytes = null;
  661. try {
  662. secretBytes = MessageDigest.getInstance("md5")
  663. .digest((username + password + Calendar.getInstance().getTimeInMillis()).getBytes());
  664. } catch (NoSuchAlgorithmException e) {
  665. throw new RuntimeException("没有这个md5算法!");
  666. }
  667. token = new BigInteger(1, secretBytes).toString(16);
  668. parameter.tokenlist.add(token);
  669. } else {
  670. return ErrModel.getToken().toString();
  671. }
  672. DBConnect connect = new DBConnect();
  673. // System.out.println("usertype:"+usertype);
  674. if(usertype.equals("经销商主账号")){//如果类型是经销商主账号
  675. // DBConnect connect = new DBConnect("DRP");
  676. Rows rows = connect.runSqlQuery(
  677. "select t1.fagentnum,t.defsite as siteid,t.hrid,t1.FAGENTSHORTNAME,t1.fisservice from pp_users t join TAGENTs t1 on t.hrid=t1.fagentnum " +
  678. "where t.hrid='"+username+"' and t.status='ACTIVE' and t1.fisservice=1");
  679. if (rows.isEmpty()) {
  680. JSONObject object = new JSONObject();
  681. object.put("status", "error");
  682. object.put("errcode", ErrCode.gettokenfail[0]);
  683. object.put("msg", "该帐号无登录权限!");
  684. return object.toString();
  685. }
  686. String fagentnum=rows.get(0).getString("fagentnum");
  687. String siteid=rows.get(0).getString("siteid");
  688. String fagentshortname=rows.get(0).getString("FAGENTSHORTNAME");
  689. boolean fisservice=rows.get(0).getBoolean("fisservice");
  690. Rows staffauthority = connect.runSqlQuery("select authoritymodule,authorityname from staffauthority where siteid='"+siteid+"' order by frownum");
  691. List list=new ArrayList();
  692. for (Row row : staffauthority) {
  693. list.add(row.getString("authoritymodule")+"_edit");
  694. }
  695. JSONObject object = new JSONObject();
  696. object.put("status", "ok");
  697. object.put("token", token);
  698. JSONObject dataobject = new JSONObject();
  699. dataobject.put("fusertype", usertype);
  700. dataobject.put("fagentnum", fagentnum);
  701. dataobject.put("siteid", siteid);
  702. dataobject.put("name", fagentshortname);
  703. dataobject.put("hrid", username.toUpperCase());
  704. dataobject.put("appids", list);
  705. dataobject.put("fisservice", fisservice);
  706. object.put("data", dataobject);
  707. return object.toString();
  708. }else if(usertype.equals("经销商员工账号")){//如果类型是经销商员工账号
  709. Rows rows = connect.runSqlQuery(
  710. "select t1.fagentnum,t.defsite as siteid,t.hrid,t1.staffid,t1.fname,'0' fisservice from pp_users t join staff t1 on t1.phone=t.hrid " +
  711. "where t.hrid='"+username+"' and t.status='ACTIVE'");
  712. if (rows.isEmpty()) {
  713. JSONObject object = new JSONObject();
  714. object.put("status", "error");
  715. object.put("errcode", ErrCode.gettokenfail[0]);
  716. object.put("msg", "该帐号无登录权限!");
  717. return object.toString();
  718. }
  719. String fagentnum=rows.get(0).getString("fagentnum");
  720. String siteid=rows.get(0).getString("siteid");
  721. String fname=rows.get(0).getString("fname");
  722. boolean fisservice=rows.get(0).getBoolean("fisservice");
  723. int staffid=rows.get(0).getInteger("staffid");
  724. Rows staffauthority = connect.runSqlQuery("select t.authoritymodule,t.authorityname,t.fisedit,t.fisquery from\n" +
  725. " staff_authority t\n" +
  726. " join staffauthority t1 on t.authoritymodule=t1.authoritymodule and t1.siteid='"+siteid+"'\n" +
  727. "where fparentid="+staffid+" order by t1.frownum");
  728. List list=new ArrayList();
  729. for (Row row : staffauthority) {
  730. if(row.getBoolean("fisedit")){
  731. list.add(row.getString("authoritymodule")+"_edit");
  732. }
  733. else if(row.getBoolean("fisquery")){
  734. list.add(row.getString("authoritymodule"));
  735. }
  736. }
  737. JSONObject object = new JSONObject();
  738. object.put("status", "ok");
  739. object.put("token", token);
  740. JSONObject dataobject = new JSONObject();
  741. dataobject.put("fusertype", usertype);
  742. dataobject.put("fagentnum", fagentnum);
  743. dataobject.put("siteid", siteid);
  744. dataobject.put("name", fname);
  745. dataobject.put("hrid", username.toUpperCase());
  746. dataobject.put("appids", list);
  747. dataobject.put("fisservice", fisservice);
  748. object.put("data", dataobject);
  749. return object.toString();
  750. }else{
  751. JSONObject object = new JSONObject();
  752. object.put("status", "error");
  753. object.put("errcode", ErrCode.gettokenfail[0]);
  754. object.put("msg", "该帐号无登录权限!");
  755. return object.toString();
  756. }
  757. }else if(from_account.equals("2")){//小程序登录
  758. DBConnect connect = new DBConnect();
  759. Rows rows = connect.runSqlQuery("select t.fagentnum,t.workphone,t.siteid,t.role,t.password from worker t " +
  760. "where fisused=1 and workphone='" + username + "' and t.role='服务组长'");
  761. if(!rows.isEmpty()){
  762. String realpassword = rows.get(0).getString("password");
  763. if(!realpassword.equals(password)){
  764. JSONObject object = new JSONObject();
  765. object.put("status", "error");
  766. object.put("errcode", ErrCode.gettokenfail[0]);
  767. object.put("msg", "密码错误!");
  768. return object.toString();
  769. }
  770. byte[] secretBytes = null;
  771. try {
  772. secretBytes = MessageDigest.getInstance("md5")
  773. .digest((username + password + Calendar.getInstance().getTimeInMillis()).getBytes());
  774. } catch (NoSuchAlgorithmException e) {
  775. throw new RuntimeException("没有这个md5算法!");
  776. }
  777. String token = new BigInteger(1, secretBytes).toString(16);
  778. parameter.tokenlist.add(token);
  779. String fagentnum=rows.get(0).getString("fagentnum");
  780. String siteid=rows.get(0).getString("siteid");
  781. String role=rows.get(0).getString("role");
  782. JSONObject object = new JSONObject();
  783. object.put("status", "ok");
  784. object.put("token", token);
  785. JSONObject dataobject = new JSONObject();
  786. dataobject.put("fusertype", usertype);
  787. dataobject.put("fagentnum", fagentnum);
  788. dataobject.put("siteid", siteid);
  789. dataobject.put("role", role);
  790. dataobject.put("hrid", username.toUpperCase());
  791. object.put("data", dataobject);
  792. return object.toString();
  793. }else{
  794. JSONObject object = new JSONObject();
  795. object.put("status", "error");
  796. object.put("errcode", ErrCode.gettokenfail[0]);
  797. object.put("msg", "该帐号无登录权限!");
  798. return object.toString();
  799. }
  800. }else{
  801. JSONObject object = new JSONObject();
  802. object.put("status", "error");
  803. object.put("errcode", ErrCode.gettokenfail[0]);
  804. object.put("msg", "from参数无效");
  805. return object.toString();
  806. }
  807. }
  808. public static final String FILE_PATH=P2Server.getP2Server().getConfig().getProperty("p2.attachment.defaultpath");
  809. public static final String FILE_URL="http://drp.idcgroup.com.cn:8082/samex/rest/webclientrest/download?docinfoid=";
  810. @Path("download")
  811. @GET
  812. public void download(@QueryParam("docinfoid") String docinfoid,@Context HttpServletResponse response){
  813. int id=Integer.valueOf(docinfoid);
  814. response.reset();
  815. File file=null;
  816. try {
  817. PaoSetRemote docinfos = P2Server.getP2Server().getPaoSet("docinfo", P2Server.getP2Server().getSystemUserInfo());
  818. docinfos.setWhere("docinfoid="+Long.valueOf(id));
  819. docinfos.reset();
  820. if(!docinfos.isEmpty()){
  821. PaoRemote docinfo = docinfos.getPao(0);
  822. String siteid = docinfo.getString("siteid");
  823. String serialnumber = docinfo.getString("serialnumber");
  824. String document = docinfo.getString("document");
  825. String ownertables = docinfo.getString("ownertable");
  826. String postfix = docinfo.getString("postfix");
  827. if(postfix.equalsIgnoreCase("pdf")){
  828. response.setContentType("application/pdf");
  829. response.setHeader("Content-Disposition", "attachment;fileName="+ URLEncoder.encode(document,"UTF-8"));
  830. }else if(postfix.equalsIgnoreCase("jpg")||
  831. postfix.equalsIgnoreCase("png")||
  832. postfix.equalsIgnoreCase("jpeg")
  833. ){
  834. response.setContentType("image/png");
  835. }else if(postfix.equalsIgnoreCase("mp4")){
  836. response.setContentType("video/mpeg4");
  837. }else{
  838. response.setContentType("multipart/form-data");
  839. response.setHeader("Content-Disposition", "attachment;fileName="+ URLEncoder.encode(document,"UTF-8"));
  840. }
  841. file = new File( FILE_PATH +"\\"+siteid+"\\"+ownertables+"\\"+serialnumber);
  842. }
  843. } catch (P2Exception | UnsupportedEncodingException e) {
  844. e.printStackTrace();
  845. }
  846. ServletOutputStream out;
  847. try {
  848. if(file!=null){
  849. FileInputStream inputStream = new FileInputStream(file);
  850. //3.通过response获取ServletOutputStream对象(out)
  851. out = response.getOutputStream();
  852. int b = 0;
  853. byte[] buffer = new byte[512];
  854. while (b != -1){
  855. b = inputStream.read(buffer);
  856. //4.写到输出流(out)中
  857. out.write(buffer,0,b);
  858. }
  859. inputStream.close();
  860. out.close();
  861. out.flush();
  862. }else{
  863. out = response.getOutputStream();
  864. out.write("null".getBytes());
  865. out.close();
  866. out.flush();
  867. }
  868. } catch (IOException e) {
  869. e.printStackTrace();
  870. }
  871. }
  872. @Path("find_staffauthority")
  873. @POST
  874. public String find_staffauthority(String RequestContent) {
  875. JSONObject requestcontent = null;
  876. try {
  877. requestcontent = JSONObject.fromObject(RequestContent);
  878. } catch (Exception e) {
  879. return ErrModel.request_BasicJsonFormat().toString();
  880. }
  881. String username = requestcontent.getString("username");
  882. DBConnect dbConnect=new DBConnect();
  883. Rows rows = dbConnect.runSqlQuery("select fusertype,defsite from pp_users where hrid='" + username + "' and status='ACTIVE'");
  884. if(!rows.isEmpty()){
  885. Row row_detail = rows.get(0);
  886. String fusertype = row_detail.getString("fusertype");
  887. String defsite = row_detail.getString("defsite");
  888. if(fusertype.equals("经销商主账号")){
  889. Rows staffauthority = dbConnect.runSqlQuery("select authoritymodule,authorityname from staffauthority where siteid='"+defsite+"' order by frownum");
  890. List list=new ArrayList();
  891. for (Row row : staffauthority) {
  892. list.add(row.getString("authoritymodule")+"_edit");
  893. }
  894. JSONObject object = new JSONObject();
  895. object.put("status", "ok");
  896. JSONObject dataobject = new JSONObject();
  897. dataobject.put("appids", list);
  898. object.put("data", dataobject);
  899. return object.toString();
  900. }else if(fusertype.equals("经销商员工账号")){
  901. Rows rows1 = dbConnect.runSqlQuery(
  902. "select t1.fagentnum,t.defsite as siteid,t.hrid,t1.staffid,t1.fname,'0' fisservice from pp_users t join staff t1 on t1.phone=t.hrid " +
  903. "where t.hrid='"+username+"' and t.status='ACTIVE'");
  904. if (rows1.isEmpty()) {
  905. JSONObject object = new JSONObject();
  906. object.put("status", "error");
  907. object.put("errcode", ErrCode.gettokenfail[0]);
  908. object.put("msg", "账号已失效");
  909. return object.toString();
  910. }
  911. String fagentnum=rows1.get(0).getString("fagentnum");
  912. String siteid=rows1.get(0).getString("siteid");
  913. String fname=rows1.get(0).getString("fname");
  914. boolean fisservice=rows1.get(0).getBoolean("fisservice");
  915. int staffid=rows1.get(0).getInteger("staffid");
  916. Rows staffauthority = dbConnect.runSqlQuery("select t.authoritymodule,t.authorityname,t.fisedit,t.fisquery from\n" +
  917. " staff_authority t\n" +
  918. " join staffauthority t1 on t.authoritymodule=t1.authoritymodule and t1.siteid='"+siteid+"'\n" +
  919. "where fparentid="+staffid+" order by t1.frownum");
  920. List list=new ArrayList();
  921. for (Row row : staffauthority) {
  922. if(row.getBoolean("fisedit")){
  923. list.add(row.getString("authoritymodule")+"_edit");
  924. }
  925. else if(row.getBoolean("fisquery")){
  926. list.add(row.getString("authoritymodule"));
  927. }
  928. }
  929. JSONObject object = new JSONObject();
  930. object.put("status", "ok");
  931. JSONObject dataobject = new JSONObject();
  932. dataobject.put("appids", list);
  933. object.put("data", dataobject);
  934. return object.toString();
  935. }
  936. }
  937. JSONObject object = new JSONObject();
  938. object.put("status", "error");
  939. object.put("errcode", ErrCode.gettokenfail[0]);
  940. object.put("msg", "账户不存在");
  941. return object.toString();
  942. }
  943. }