WebClientRest.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. package com.cnd3b.common.restful;
  2. import com.cnd3b.restcontroller.publicmethod.users.Users;
  3. import com.cnd3b.utility.Encryption;
  4. import com.alibaba.fastjson.JSONException;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.cnd3b.common.D3BReturnObject_Err;
  7. import com.cnd3b.common.data.Row;
  8. import com.cnd3b.common.data.Rows;
  9. import com.cnd3b.common.data.SQLFactory;
  10. import com.cnd3b.common.data.db.DBConnect;
  11. import com.cnd3b.common.data.db.DataPool;
  12. import com.cnd3b.common.parameter.parameter;
  13. import com.cnd3b.restcontroller.system.system.uploadExcelData;
  14. import com.cnd3b.utility.Sms;
  15. import com.cnd3b.utility.sysmsg;
  16. import com.cnd3b.utility.wechatpay.apppay.POJO.APPQueryOrderRequest;
  17. import com.cnd3b.utility.wechatpay.apppay.POJO.AppWechatOrder;
  18. import com.cnd3b.utility.wechatpay.apppay.apppay;
  19. import com.cnd3b.utility.wechatpay.jsapipay.POJO.JSQueryOrderRequest;
  20. import com.cnd3b.utility.wechatpay.jsapipay.POJO.JSWechatOrder;
  21. import com.cnd3b.utility.wechatpay.jsapipay.jsapipay;
  22. import com.cnd3b.utility.wechatpay.nativepay.nativepay;
  23. import org.dom4j.dom.DOMElement;
  24. import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
  25. import org.glassfish.jersey.media.multipart.FormDataParam;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.ws.rs.*;
  28. import javax.ws.rs.core.Context;
  29. import javax.ws.rs.core.MediaType;
  30. import java.io.InputStream;
  31. import java.lang.reflect.Constructor;
  32. import java.lang.reflect.InvocationTargetException;
  33. import java.lang.reflect.Method;
  34. import java.text.SimpleDateFormat;
  35. import java.util.ArrayList;
  36. import java.util.Arrays;
  37. import java.util.Calendar;
  38. import java.util.Random;
  39. @Path("webclientrest")
  40. public class WebClientRest {
  41. private static String[] nocheckphonenumber = {"13732579910"};
  42. @POST
  43. public String method(@Context HttpServletRequest request, String RequestContent) {
  44. /**
  45. * 验证请求正文是否为规范的SONObject格式
  46. */
  47. JSONObject requestcontent = null;
  48. try {
  49. requestcontent = JSONObject.parseObject(RequestContent);
  50. } catch (Exception e) {
  51. return new D3BReturnObject_Err().setErrMsg("请求正文格式错误,必须为JSONObject格式").toString();
  52. }
  53. /**
  54. * 验证请求正文中是否包含必填的键值
  55. */
  56. String[] mustkeys = {"classname", "method", "content"};
  57. for (String mustkey : mustkeys) {
  58. if (!requestcontent.containsKey(mustkey)) {
  59. return new D3BReturnObject_Err().setErrMsg("json缺少KEY" + mustkey).toString();
  60. }
  61. }
  62. /**
  63. * 验证请求正文中的content是否为规范的SONObject格式
  64. */
  65. JSONObject content = new JSONObject();
  66. try {
  67. content = requestcontent.getJSONObject("content");
  68. } catch (Exception e) {
  69. return new D3BReturnObject_Err().setErrMsg("正文content格式错误,必须为JSONObject格式").toString();
  70. }
  71. /**
  72. * 验证正文中的token是否有效
  73. */
  74. String accesstoken = "";
  75. String className = requestcontent.getString("classname");
  76. if (!className.contains("publicmethod")) {
  77. if (!requestcontent.containsKey("accesstoken")) {
  78. return new D3BReturnObject_Err().setErrMsg("登陆状态已过期,请重新登陆!").toString();
  79. }
  80. accesstoken = requestcontent.getString("accesstoken");
  81. /**
  82. * 盘点当前账号是否存在有效的token
  83. */
  84. if (!parameter.tokenlist.containsKey(accesstoken) && !istokeninuserlist(accesstoken)) {
  85. //如果缓存中不存在,则在账号列表中进行查询,查到结果后,将tokne存入缓存
  86. return new D3BReturnObject_Err().setErrMsg("登陆状态已过期,请重新登陆!").toString();
  87. }
  88. parameter.requesttime.put(accesstoken, Calendar.getInstance().getTime());
  89. }
  90. String methodName = requestcontent.getString("method");
  91. if (content.isEmpty()) {
  92. content = new JSONObject();
  93. }
  94. content.put("$classname", className);
  95. content.put("$method", methodName);
  96. content.put("$accesstoken", accesstoken);
  97. content.put("$requestHost", request.getScheme() + "://" + request.getHeader("Host"));
  98. String key = className + "." + methodName;
  99. String result;
  100. Object obj = null;
  101. try {
  102. boolean getdatafromdbanyway = content.containsKey("getdatafromdbanyway")
  103. && content.getBoolean("getdatafromdbanyway");
  104. content.remove("getdatafromdbanyway");
  105. Object data = null;
  106. if (!getdatafromdbanyway) {
  107. data = DataPool.get(content.toString());
  108. }
  109. if (data != null) {
  110. result = data.toString();
  111. saveCallMethodMsg(key, false, 0L);
  112. } else {
  113. long starttimes = Calendar.getInstance().getTimeInMillis();
  114. /**
  115. * 执行请求方法
  116. */
  117. Class clz = Class.forName("com.cnd3b.restcontroller." + className);
  118. Constructor cla = clz.getDeclaredConstructor(JSONObject.class);
  119. obj = cla.newInstance(content);
  120. Method method = obj.getClass().getDeclaredMethod(methodName);
  121. result = (String) method.invoke(obj);
  122. long endtimes = Calendar.getInstance().getTimeInMillis();
  123. saveCallMethodMsg(key, true, endtimes - starttimes);
  124. }
  125. } catch (ClassNotFoundException e) {
  126. e.printStackTrace();
  127. result = new D3BReturnObject_Err().setErrMsg("找不到指定的类" + className).toString();
  128. } catch (InstantiationException e) {
  129. e.printStackTrace();
  130. result = new D3BReturnObject_Err().setErrMsg("类" + className + "实例化异常").toString();
  131. } catch (IllegalAccessException e) {
  132. e.printStackTrace();
  133. result = new D3BReturnObject_Err().setErrMsg("类" + className + "安全权限异常,可能该类为非public类").toString();
  134. } catch (NoSuchMethodException e) {
  135. e.printStackTrace();
  136. result = new D3BReturnObject_Err().setErrMsg("找不到指定的类" + className + "的" + methodName + "方法").toString();
  137. } catch (IllegalArgumentException e) {
  138. e.printStackTrace();
  139. result = new D3BReturnObject_Err().setErrMsg("类" + className + "的" + methodName + "方法参数不合法").toString();
  140. } catch (InvocationTargetException e) {
  141. Throwable targetException = e.getTargetException();
  142. D3BReturnObject_Err d3BReturnObject_err = new D3BReturnObject_Err();
  143. d3BReturnObject_err.setErrMsg(targetException.getMessage());
  144. result = d3BReturnObject_err.toString();
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. result = new D3BReturnObject_Err().setErrMsg("发生未知异常" + e.getMessage()).toString();
  148. } finally {
  149. if (obj != null) {
  150. try {
  151. obj.getClass().getMethod("p2ServerSystemPaoSetClose").invoke(obj);
  152. } catch (Exception e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. }
  157. return result;
  158. }
  159. public boolean istokeninuserlist(String accesstoken) {
  160. DBConnect dbConnect = new DBConnect();
  161. SQLFactory factory = new SQLFactory(this, "持久化账号列表查询");
  162. factory.addParameter("accesstoken", accesstoken);
  163. Rows rows = dbConnect.runSqlQuery(factory.getSQL());
  164. if (rows.isEmpty()) {
  165. return false;
  166. } else {
  167. for (Row row : rows) {
  168. long userid = row.getLong("userid");
  169. String token = row.getString("token");
  170. row.put("logintime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
  171. parameter.userIdList.put(userid, row);
  172. parameter.tokenlist.put(token, userid);
  173. }
  174. return true;
  175. }
  176. }
  177. /**
  178. * 记录请求数
  179. *
  180. * @param key
  181. * @param fromdb
  182. * @param time
  183. */
  184. private void saveCallMethodMsg(String key, boolean fromdb, long time) {
  185. long callmethodTimes = parameter.callmethodTimes.containsKey(key) ? parameter.callmethodTimes.get(key) : 0L;
  186. //更新请求总数
  187. parameter.callmethodTimes.put(key, callmethodTimes + 1L);
  188. //最新请求时间
  189. parameter.lastcallmethodtime.put(key, Calendar.getInstance().getTime());
  190. //从缓存获取的次数
  191. long callmethod_fromcacheTimes = parameter.callmethod_fromcacheTimes.containsKey(key) ? parameter.callmethod_fromcacheTimes.get(key) : 0L;
  192. if (!fromdb) {
  193. /**
  194. * 方法请求从缓存获取次数
  195. */
  196. parameter.callmethod_fromcacheTimes.put(key, callmethod_fromcacheTimes + 1L);
  197. } else {
  198. /**
  199. * 方法请求查询最新耗时
  200. */
  201. parameter.callmethodLastTimeLong.put(key, time);
  202. long totaltimes = callmethodTimes - callmethod_fromcacheTimes;
  203. /**
  204. * 方法请求查询平均时间
  205. */
  206. long callmethodTimeLong = parameter.callmethodTimeLong.containsKey(key) ? parameter.callmethodTimeLong.get(key) : 0L;
  207. parameter.callmethodTimeLong.put(key, (callmethodTimeLong * totaltimes + time) / (totaltimes + 1));
  208. }
  209. }
  210. /**
  211. * 获取短信登陆验证码
  212. *
  213. * @param RequestContent
  214. * @return
  215. */
  216. @Path("getpassword")
  217. @POST
  218. public String getPassWord(@Context HttpServletRequest request, String RequestContent) {
  219. /**
  220. * 验证请求正文是否为规范的SONObject格式
  221. */
  222. JSONObject requestcontent = null;
  223. try {
  224. requestcontent = JSONObject.parseObject(RequestContent);
  225. } catch (Exception e) {
  226. return new D3BReturnObject_Err().setErrMsg("请求正文格式错误,必须为JSONObject格式").toString();
  227. }
  228. /**
  229. * 验证请求正文中是否包含必填的键值
  230. */
  231. String[] mustkeys = {"phonenumber"};
  232. for (String mustkey : mustkeys) {
  233. if (!requestcontent.containsKey(mustkey)) {
  234. return new D3BReturnObject_Err().setErrMsg("json缺少KEY" + mustkey).toString();
  235. }
  236. }
  237. String phonenumber = requestcontent.getString("phonenumber");
  238. String client = "";
  239. if (requestcontent.containsKey("client")) {
  240. client = requestcontent.getString("client");
  241. }
  242. // DBConnect dbConnect = new DBConnect();
  243. // if (dbConnect.runSqlQuery("select * from tenterprise_users where fphonenumber='" + phonenumber + "'").isEmpty()) {
  244. // JSONObject object = new JSONObject();
  245. // object.put("code", 0);
  246. // object.put("msg", "当前手机号未注册!");
  247. // return object.toString();
  248. // }
  249. String password = createPassWord();
  250. parameter.phonenumber_password.put(phonenumber, password);
  251. Calendar calendar = Calendar.getInstance();
  252. calendar.add(Calendar.MINUTE, 5);
  253. parameter.phonenumber_date.put(phonenumber, calendar.getTime());
  254. if (parameter.isdebug()) {
  255. JSONObject object = new JSONObject();
  256. object.put("code", 1);
  257. object.put("msg", "手机验证码为:" + password);
  258. return object.toString();
  259. } else {
  260. JSONObject object = new JSONObject();
  261. Sms sms = new Sms();
  262. sms.sendOutMsg(phonenumber, password);
  263. object.put("code", 1);
  264. object.put("msg", "手机验证码已发送,请注意查收!");
  265. return object.toString();
  266. }
  267. }
  268. /**
  269. * 创建验证码
  270. *
  271. * @return
  272. */
  273. public String createPassWord() {
  274. String allChar = "1234567890";
  275. StringBuffer sb = new StringBuffer();
  276. Random random = new Random();
  277. for (int i = 0; i < 6; i++) {
  278. sb.append(allChar.charAt(random.nextInt(allChar.length())));
  279. }
  280. if (parameter.phonenumber_password.containsValue(sb.toString())) {
  281. return createPassWord();
  282. } else {
  283. return sb.toString();
  284. }
  285. }
  286. @Path("login")
  287. @POST
  288. public String login(String RequestContent) {
  289. /**
  290. * 验证请求正文是否为规范的SONObject格式
  291. */
  292. JSONObject requestcontent = null;
  293. try {
  294. requestcontent = JSONObject.parseObject(RequestContent);
  295. } catch (Exception e) {
  296. return new D3BReturnObject_Err().setErrMsg("请求正文格式错误,必须为JSONObject格式").toString();
  297. }
  298. /**
  299. * 验证请求正文中是否包含必填的键值
  300. */
  301. String[] mustkeys = {"phonenumber", "password"};
  302. for (String mustkey : mustkeys) {
  303. if (!requestcontent.containsKey(mustkey)) {
  304. return new D3BReturnObject_Err().setErrMsg("json缺少KEY" + mustkey).toString();
  305. }
  306. }
  307. String phonenumber = requestcontent.getString("phonenumber");
  308. String password = requestcontent.getString("password");
  309. int resultcode;
  310. DBConnect connect = new DBConnect();
  311. if (!parameter.phonenumber_password.containsKey(phonenumber)) {
  312. resultcode = 1;//没有获取验证码
  313. } else if (parameter.phonenumber_date.get(phonenumber).before(Calendar.getInstance().getTime())) {
  314. resultcode = 2;//验证码已失效
  315. } else {
  316. //系统验证码
  317. String syspassword = parameter.phonenumber_password.get(phonenumber);
  318. if (password.equals(new Encryption().Encode_MD5(syspassword))) {
  319. resultcode = 0;//验证码正确
  320. } else {
  321. resultcode = 3;//验证码错误
  322. }
  323. }
  324. int isnewregister = 0;
  325. if (resultcode == 0 || Arrays.asList(nocheckphonenumber).contains(phonenumber)) {
  326. //登陆时,如果验证码验证成功,但是手机号未注册过,则自动进行注册!
  327. if (connect.runSqlQuery("select * from tenterprise_users where fphonenumber='" + phonenumber + "'").isEmpty()) {
  328. Users users = new Users(requestcontent);
  329. users.register_usersByLogin(phonenumber);
  330. isnewregister = 1;
  331. }
  332. SQLFactory factory = new SQLFactory(this, "手机账号列表查询");
  333. factory.addParameter("fphonenumber", phonenumber);
  334. Rows rows = connect.runSqlQuery(factory.getSQL());
  335. ArrayList<String> tokensaveList = new ArrayList<>();
  336. for (Row row : rows) {
  337. long userid = row.getLong("userid");
  338. String usertoken = new Encryption().Encode_MD5(phonenumber + password + userid + Calendar.getInstance().getTimeInMillis());
  339. row.put("token", usertoken);
  340. row.put("logintime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
  341. row.put("isnewregister", isnewregister);
  342. parameter.userIdList.put(userid, row);
  343. parameter.tokenlist.put(usertoken, userid);
  344. tokensaveList.add("update tenterprise_users set accesstoken='" + usertoken + "' where tenterprise_userid=" + userid);
  345. }
  346. if (!tokensaveList.isEmpty()) {
  347. /**
  348. * 账号登录态持久化
  349. */
  350. connect.runSqlUpdate(tokensaveList);
  351. }
  352. JSONObject object = new JSONObject();
  353. object.put("code", 1);
  354. object.put("msg", "成功");
  355. object.put("webclienturl", "");
  356. object.put("servicehotline", "");
  357. object.put("account_list", rows.toJsonArray());
  358. //登录成功,将验证码失效掉;
  359. Calendar calendar = Calendar.getInstance();
  360. calendar.add(Calendar.MINUTE, -5);
  361. parameter.phonenumber_date.put(phonenumber, calendar.getTime());
  362. return object.toString();
  363. } else {
  364. String msg = "";
  365. if (resultcode == 1) {
  366. msg = "请先获取所填手机号验证码!";
  367. } else if (resultcode == 2) {
  368. msg = "验证码已失效,请重新获取!";
  369. } else if (resultcode == 3) {
  370. msg = "无效的验证码!";
  371. }
  372. JSONObject object = new JSONObject();
  373. object.put("code", 0);
  374. object.put("msg", msg);
  375. return object.toString();
  376. }
  377. }
  378. @Path("logout")
  379. @POST
  380. public String logout(String RequestContent) {
  381. /**
  382. * 验证请求正文是否为规范的SONObject格式
  383. */
  384. JSONObject requestcontent = null;
  385. try {
  386. requestcontent = JSONObject.parseObject(RequestContent);
  387. } catch (Exception e) {
  388. return new D3BReturnObject_Err().setErrMsg("请求正文格式错误,必须为JSONObject格式").toString();
  389. }
  390. /**
  391. * 验证请求正文中是否包含必填的键值
  392. */
  393. String[] mustkeys = {"accesstoken"};
  394. for (String mustkey : mustkeys) {
  395. if (!requestcontent.containsKey(mustkey)) {
  396. return new D3BReturnObject_Err().setErrMsg("json缺少KEY" + mustkey).toString();
  397. }
  398. }
  399. /**
  400. * 验证正文中的token是否有效
  401. */
  402. String accesstoken = requestcontent.getString("accesstoken");
  403. parameter.tokenlist.remove(accesstoken);
  404. new DBConnect().runSqlUpdate("update tenterprise_users set accesstoken=null where accesstoken='" + accesstoken + "'");
  405. JSONObject object = new JSONObject();
  406. object.put("status", "ok");
  407. return object.toString();
  408. }
  409. /**
  410. * 数据导入
  411. *
  412. * @param uploadfileInputStream
  413. * @param uploadfile
  414. * @param userid
  415. * @param accesstoken
  416. * @param ftype
  417. * @return
  418. */
  419. @Path("uploadexcel")
  420. @POST
  421. @Consumes({"multipart/form-data"})
  422. @Produces({"application/json"})
  423. public String upLoadDoc(@FormDataParam("uploadfile") InputStream uploadfileInputStream,
  424. @FormDataParam("uploadfile") FormDataContentDisposition uploadfile,
  425. @FormDataParam("userid") String userid,
  426. @FormDataParam("accesstoken") String accesstoken, @FormDataParam("ftype") String ftype) {
  427. /**
  428. * 验证正文中的token是否有效
  429. */
  430. if (!parameter.tokenlist.containsKey(accesstoken)) {
  431. return new D3BReturnObject_Err().setErrMsg("登陆状态已过期,请重新登陆!").toString();
  432. }
  433. JSONObject content = new JSONObject();
  434. content.put("userid", userid);
  435. uploadExcelData uploadExcelData = new uploadExcelData(content);
  436. return uploadExcelData.upLoadExcel(uploadfileInputStream, uploadfile, ftype);
  437. }
  438. /**
  439. * 微信支付回调接口
  440. *
  441. * @param RequestContent
  442. * @return
  443. */
  444. @POST
  445. @Path("wechatpay/returnorderstateapp")
  446. public String wechatpayapp(String RequestContent) {
  447. APPQueryOrderRequest queryOrderRequest = new APPQueryOrderRequest();
  448. AppWechatOrder order = queryOrderRequest.getWechatOrder(RequestContent);
  449. apppay apppay = new apppay();
  450. boolean success = apppay.updateLocalOrder(order);
  451. DOMElement request = new DOMElement("xml");
  452. request.addElement("return_code").addText(success ? "SUCCESS" : "FAIL");
  453. request.addElement("return_msg").addText(success ? "OK" : "校验失败");
  454. return request.asXML();
  455. }
  456. /**
  457. * 微信网页支付回调接口
  458. *
  459. * @param RequestContent
  460. * @return
  461. */
  462. @POST
  463. @Path("wechatpay/returnorderstatenative")
  464. public String wechatpaynative(String RequestContent) {
  465. nativepay nativepay = new nativepay();
  466. JSONObject returnobject = JSONObject.parseObject(RequestContent);
  467. boolean success = nativepay.updateLocalOrder_callback(returnobject);
  468. JSONObject request = new JSONObject();
  469. request.put("code", success ? "SUCCESS" : "FAIL");
  470. request.put("message", success ? "成功" : "校验失败");
  471. return request.toString();
  472. }
  473. /**
  474. * 微信网页支付回调接口
  475. *
  476. * @param RequestContent
  477. * @return
  478. */
  479. @POST
  480. @Path("wechatpay/returnorderstatejs")
  481. public String wechatpayjs(String RequestContent) {
  482. JSQueryOrderRequest queryOrderRequest = new JSQueryOrderRequest();
  483. JSWechatOrder order = queryOrderRequest.getWechatOrder(RequestContent);
  484. jsapipay jsapipay = new jsapipay();
  485. boolean success = jsapipay.updateLocalOrder(order);
  486. DOMElement request = new DOMElement("xml");
  487. request.addElement("return_code").addText(success ? "SUCCESS" : "FAIL");
  488. request.addElement("return_msg").addText(success ? "OK" : "校验失败");
  489. return request.asXML();
  490. }
  491. @Path("sysmsg")
  492. @GET
  493. @Produces(MediaType.TEXT_HTML)
  494. @Consumes(MediaType.TEXT_HTML)
  495. public String sysmsg() throws JSONException {
  496. return new sysmsg().getCustMsg();
  497. }
  498. }