WebClientRest.java 46 KB

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