BaseController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. package com.yihu.mm.controller;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.yihu.wlyy.entity.IdEntity;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.json.JSONArray;
  6. import org.json.JSONObject;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.data.domain.Page;
  11. import javax.servlet.http.HttpServletRequest;
  12. import java.util.*;
  13. public class BaseController {
  14. private static Logger logger = LoggerFactory.getLogger(BaseController.class);
  15. @Autowired
  16. protected HttpServletRequest request;
  17. /**
  18. * 获取排序字段名称
  19. * @return
  20. */
  21. public String getSortName() {
  22. return request.getParameter("sortname");
  23. }
  24. /**
  25. * 获取排序方式ASC,DESC
  26. * @return
  27. */
  28. public String getSortOrder() {
  29. return request.getParameter("sortorder");
  30. }
  31. /**
  32. * 獲取髮送請求用戶的uid
  33. * @return
  34. */
  35. public String getUID() {
  36. try {
  37. String userAgent = request.getHeader("userAgent");
  38. if (StringUtils.isEmpty(userAgent)) {
  39. userAgent = request.getHeader("User-Agent");
  40. }
  41. JSONObject json = new JSONObject(userAgent);
  42. return json.getString("uid");
  43. } catch (Exception e) {
  44. return null;
  45. }
  46. }
  47. public String getOpenid() {
  48. try {
  49. String userAgent = request.getHeader("userAgent");
  50. if (StringUtils.isEmpty(userAgent)) {
  51. userAgent = request.getHeader("User-Agent");
  52. }
  53. JSONObject json = new JSONObject(userAgent);
  54. return json.getString("openid");
  55. } catch (Exception e) {
  56. return null;
  57. }
  58. }
  59. /**
  60. * 获取用户ID
  61. * @return
  62. */
  63. public long getId() {
  64. try {
  65. String userAgent = request.getHeader("userAgent");
  66. if (StringUtils.isEmpty(userAgent)) {
  67. userAgent = request.getHeader("User-Agent");
  68. }
  69. JSONObject json = new JSONObject(userAgent);
  70. return json.getLong("id");
  71. } catch (Exception e) {
  72. return 0;
  73. }
  74. }
  75. public String getIMEI() {
  76. try {
  77. String userAgent = request.getHeader("userAgent");
  78. if (StringUtils.isEmpty(userAgent)) {
  79. userAgent = request.getHeader("User-Agent");
  80. }
  81. JSONObject json = new JSONObject(userAgent);
  82. return json.getString("imei");
  83. } catch (Exception e) {
  84. return null;
  85. }
  86. }
  87. public String getToken() {
  88. try {
  89. String userAgent = request.getHeader("userAgent");
  90. if (StringUtils.isEmpty(userAgent)) {
  91. userAgent = request.getHeader("User-Agent");
  92. }
  93. JSONObject json = new JSONObject(userAgent);
  94. return json.getString("token");
  95. } catch (Exception e) {
  96. return null;
  97. }
  98. }
  99. public void error(Exception e) {
  100. logger.error(getClass().getName() + ":", e.getMessage());
  101. e.printStackTrace();
  102. }
  103. public void warn(Exception e) {
  104. logger.warn(getClass().getName() + ":", e.getMessage());
  105. e.printStackTrace();
  106. }
  107. /**
  108. * 返回接口处理结果
  109. *
  110. * @param code 结果码,成功为200
  111. * @param msg 结果提示信息
  112. * @return
  113. */
  114. public String error(int code, String msg) {
  115. try {
  116. Map<Object, Object> map = new HashMap<Object, Object>();
  117. ObjectMapper mapper = new ObjectMapper();
  118. map.put("status", code);
  119. map.put("msg", msg);
  120. return mapper.writeValueAsString(map);
  121. } catch (Exception e) {
  122. error(e);
  123. return null;
  124. }
  125. }
  126. /**
  127. * 接口处理成功
  128. * @param msg
  129. * @return
  130. */
  131. public String success(String msg) {
  132. try {
  133. Map<Object, Object> map = new HashMap<Object, Object>();
  134. ObjectMapper mapper = new ObjectMapper();
  135. map.put("status", 200);
  136. map.put("msg", msg);
  137. return mapper.writeValueAsString(map);
  138. } catch (Exception e) {
  139. error(e);
  140. return null;
  141. }
  142. }
  143. public String write(int code, String msg) {
  144. try {
  145. Map<Object, Object> map = new HashMap<Object, Object>();
  146. ObjectMapper mapper = new ObjectMapper();
  147. map.put("status", code);
  148. map.put("msg", msg);
  149. return mapper.writeValueAsString(map);
  150. } catch (Exception e) {
  151. error(e);
  152. return null;
  153. }
  154. }
  155. /**
  156. * 返回接口处理结果
  157. *
  158. *
  159. * @param code 结果码,成功为200
  160. * @param msg 结果提示信息
  161. * @return
  162. */
  163. public String write(int code, String msg, String key, List<?> list) {
  164. try {
  165. Map<Object, Object> map = new HashMap<Object, Object>();
  166. ObjectMapper mapper = new ObjectMapper();
  167. map.put("status", code);
  168. map.put("msg", msg);
  169. map.put(key, list);
  170. return mapper.writeValueAsString(map);
  171. } catch (Exception e) {
  172. error(e);
  173. return error(-1, "服务器异常,请稍候再试!");
  174. }
  175. }
  176. /**
  177. * 返回接口处理结果
  178. *
  179. *
  180. * @param code 结果码,成功为200
  181. * @param msg 结果提示信息
  182. * @param value 结果数据
  183. * @return
  184. */
  185. public String write(int code, String msg, String key, JSONObject value) {
  186. try {
  187. JSONObject json = new JSONObject();
  188. json.put("status", code);
  189. json.put("msg", msg);
  190. json.put(key, value);
  191. return json.toString();
  192. } catch (Exception e) {
  193. error(e);
  194. return error(-1, "服务器异常,请稍候再试!");
  195. }
  196. }
  197. /**
  198. * 返回接口处理结果
  199. *
  200. *
  201. * @param code 结果码,成功为200
  202. * @param msg 结果提示信息
  203. * @param value 结果数据
  204. * @return
  205. */
  206. public String write(int code, String msg, String key, JSONArray value) {
  207. try {
  208. JSONObject json = new JSONObject();
  209. json.put("status", code);
  210. json.put("msg", msg);
  211. json.put(key, value);
  212. return json.toString();
  213. } catch (Exception e) {
  214. error(e);
  215. return error(-1, "服务器异常,请稍候再试!");
  216. }
  217. }
  218. /**
  219. * 返回接口处理结果
  220. *
  221. *
  222. * @param code 结果码,成功为200
  223. * @param msg 结果提示信息
  224. * @param total 总数
  225. * @param value 结果数据
  226. * @return
  227. */
  228. public String write(int code, String msg, int total, String key, JSONArray value) {
  229. try {
  230. JSONObject json = new JSONObject();
  231. json.put("status", code);
  232. json.put("msg", msg);
  233. json.put("total", total);
  234. json.put(key, value);
  235. return json.toString();
  236. } catch (Exception e) {
  237. error(e);
  238. return error(-1, "服务器异常,请稍候再试!");
  239. }
  240. }
  241. /**
  242. * 返回接口处理结果
  243. *
  244. *
  245. * @param code 结果码,成功为200
  246. * @param msg 结果提示信息
  247. * @param value 结果数据
  248. * @return
  249. */
  250. public String write(int code, String msg, String key, Object value) {
  251. try {
  252. Map<Object, Object> map = new HashMap<Object, Object>();
  253. ObjectMapper mapper = new ObjectMapper();
  254. map.put("status", code);
  255. map.put("msg", msg);
  256. map.put(key, value);
  257. return mapper.writeValueAsString(map);
  258. } catch (Exception e) {
  259. error(e);
  260. return error(-1, "服务器异常,请稍候再试!");
  261. }
  262. }
  263. /**
  264. * 返回接口处理结果
  265. *
  266. *
  267. * @param code 结果码,成功为200
  268. * @param msg 结果提示信息
  269. * @return
  270. */
  271. public String write(int code, String msg, String key, Page<?> list) {
  272. try {
  273. Map<Object, Object> map = new HashMap<Object, Object>();
  274. ObjectMapper mapper = new ObjectMapper();
  275. map.put("status", code);
  276. map.put("msg", msg);
  277. // 是否为第一页
  278. map.put("isFirst", list.isFirst());
  279. // 是否为最后一页
  280. map.put("isLast", list.isLast());
  281. // 总条数
  282. map.put("total", list.getTotalElements());
  283. // 总页数
  284. map.put("totalPages", list.getTotalPages());
  285. map.put(key, list.getContent());
  286. return mapper.writeValueAsString(map);
  287. } catch (Exception e) {
  288. error(e);
  289. return error(-1, "服务器异常,请稍候再试!");
  290. }
  291. }
  292. /**
  293. * 返回接口处理结果
  294. *
  295. *
  296. * @param code 结果码,成功为200
  297. * @param msg 结果提示信息
  298. * @return
  299. */
  300. public String write(int code, String msg, String key, Page<?> page, JSONArray array) {
  301. try {
  302. JSONObject json = new JSONObject();
  303. json.put("status", code);
  304. json.put("msg", msg);
  305. // 是否为第一页
  306. json.put("isFirst", page.isFirst());
  307. // 是否为最后一页
  308. json.put("isLast", page.isLast());
  309. // 总条数
  310. json.put("total", page.getTotalElements());
  311. // 总页数
  312. json.put("totalPages", page.getTotalPages());
  313. json.put(key, array);
  314. return json.toString();
  315. } catch (Exception e) {
  316. error(e);
  317. return error(-1, "服务器异常,请稍候再试!");
  318. }
  319. }
  320. /**
  321. * 返回接口处理结果
  322. *
  323. *
  324. * @param code 结果码,成功为200
  325. * @param msg 结果提示信息
  326. * @param value 结果数据
  327. * @return
  328. */
  329. public String write(int code, String msg, String key, Map<?, ?> value) {
  330. try {
  331. Map<Object, Object> map = new HashMap<Object, Object>();
  332. ObjectMapper mapper = new ObjectMapper();
  333. map.put("status", code);
  334. map.put("msg", msg);
  335. map.put(key, value);
  336. return mapper.writeValueAsString(map);
  337. } catch (Exception e) {
  338. error(e);
  339. return error(-1, "服务器异常,请稍候再试!");
  340. }
  341. }
  342. /**
  343. * 返回接口处理结果
  344. *
  345. * @param code 结果码,成功为200
  346. * @param msg 结果提示信息
  347. * @param value 结果数据
  348. * @return
  349. */
  350. public String write(int code, String msg, String key, String value) {
  351. try {
  352. Map<Object, Object> map = new HashMap<Object, Object>();
  353. ObjectMapper mapper = new ObjectMapper();
  354. map.put("status", code);
  355. map.put("msg", msg);
  356. map.put(key, value);
  357. return mapper.writeValueAsString(map);
  358. } catch (Exception e) {
  359. error(e);
  360. return error(-1, "服务器异常,请稍候再试!");
  361. }
  362. }
  363. /**
  364. * 返回接口处理结果
  365. *
  366. *
  367. * @param code 结果码,成功为200
  368. * @param msg 结果提示信息
  369. * @return
  370. */
  371. public String write(int code, String msg, String key, IdEntity entity) {
  372. try {
  373. Map<Object, Object> map = new HashMap<Object, Object>();
  374. ObjectMapper mapper = new ObjectMapper();
  375. map.put("status", code);
  376. map.put("msg", msg);
  377. map.put(key, entity);
  378. return mapper.writeValueAsString(map);
  379. } catch (Exception e) {
  380. error(e);
  381. return error(-1, "服务器异常,请稍候再试!");
  382. }
  383. }
  384. /**
  385. * 返回接口处理结果
  386. *
  387. *
  388. * @param code 结果码,成功为200
  389. * @param msg 结果提示信息
  390. * @return
  391. */
  392. public String write(int code, String msg, boolean isFirst, boolean isLast, long total, int totalPages, String key, Object values) {
  393. try {
  394. JSONObject json = new JSONObject();
  395. json.put("status", code);
  396. json.put("msg", msg);
  397. // 是否为第一页
  398. json.put("isFirst", isFirst);
  399. // 是否为最后一页
  400. json.put("isLast", isLast);
  401. // 总条数
  402. json.put("total", total);
  403. // 总页数
  404. json.put("totalPages", totalPages);
  405. json.put(key, values);
  406. return json.toString();
  407. } catch (Exception e) {
  408. logger.error("BaseController:", e.getMessage());
  409. return error(-1, "服务器异常,请稍候再试!");
  410. }
  411. }
  412. public String trimEnd(String param, String trimChars) {
  413. if (param.endsWith(trimChars)) {
  414. param = param.substring(0, param.length() - trimChars.length());
  415. }
  416. return param;
  417. }
  418. /**
  419. * 无效用户消息返回
  420. * @param e
  421. * @param defaultCode
  422. * @param defaultMsg
  423. * @return
  424. */
  425. public String invalidUserException(Exception e, int defaultCode, String defaultMsg) {
  426. try {
  427. // if (e instanceof UndeclaredThrowableException) {
  428. // UndeclaredThrowableException ute = (UndeclaredThrowableException) e;
  429. // InvalidUserException iue = (InvalidUserException) ute.getUndeclaredThrowable();
  430. // if (iue != null) {
  431. // return error(iue.getCode(), iue.getMsg());
  432. // }
  433. // }
  434. return error(defaultCode, defaultMsg);
  435. } catch (Exception e2) {
  436. return null;
  437. }
  438. }
  439. /**
  440. * 返回表格列表数据
  441. * @param code 状态码:0成功,非0失败
  442. * @param errorMsg 错误消息
  443. * @param page 当前页码
  444. * @param rows 分页大小
  445. * @param list 查询的结果集
  446. * @return
  447. */
  448. public String write(int code, String errorMsg, int page, int rows, Page<?> list) {
  449. try {
  450. JSONObject object = new JSONObject();
  451. ObjectMapper mapper = new ObjectMapper();
  452. object.put("successFlg", code == 0);
  453. object.put("errorMsg", errorMsg);
  454. // 是否为第一页
  455. object.put("errorCode", code);
  456. // 是否为最后一页
  457. object.put("currPage", page);
  458. // 分页大小
  459. object.put("pageSize", rows);
  460. // 总条数
  461. object.put("totalCount", list.getTotalElements());
  462. // 总页数
  463. object.put("totalPage", list.getTotalPages());
  464. // 结果集
  465. object.put("detailModelList", list.getContent());
  466. return object.toString();
  467. } catch (Exception e) {
  468. error(e);
  469. return error(-1, "服务器异常,请稍候再试!");
  470. }
  471. }
  472. public String write(int code, String errorMsg, int page, int rows, List<?> list) {
  473. try {
  474. Map<Object, Object> map = new HashMap<Object, Object>();
  475. ObjectMapper mapper = new ObjectMapper();
  476. map.put("successFlg", code == 0);
  477. map.put("errorMsg", errorMsg);
  478. // 是否为第一页
  479. map.put("errorCode", code);
  480. // 是否为最后一页
  481. map.put("currPage", page);
  482. // 分页大小
  483. map.put("pageSize", rows);
  484. // 总条数
  485. map.put("totalCount", list.size());
  486. // 总页数
  487. map.put("totalPage", 1);
  488. // 结果集
  489. map.put("detailModelList", list);
  490. return mapper.writeValueAsString(map);
  491. } catch (Exception e) {
  492. error(e);
  493. return error(-1, "服务器异常,请稍候再试!");
  494. }
  495. }
  496. public String error(int code, String errorMsg, int page, int rows) {
  497. try {
  498. Map<Object, Object> map = new HashMap<Object, Object>();
  499. ObjectMapper mapper = new ObjectMapper();
  500. map.put("successFlg", code == 0);
  501. map.put("errorMsg", errorMsg);
  502. // 是否为第一页
  503. map.put("errorCode", code);
  504. // 是否为最后一页
  505. map.put("currPage", page);
  506. // 分页大小
  507. map.put("pageSize", rows);
  508. // 总条数
  509. map.put("totalCount", 0);
  510. // 总页数
  511. map.put("totalPage", 0);
  512. // 结果集
  513. map.put("detailModelList", null);
  514. return mapper.writeValueAsString(map);
  515. } catch (Exception e) {
  516. error(e);
  517. return error(-1, "服务器异常,请稍候再试!");
  518. }
  519. }
  520. //json串转集合
  521. public <T> Collection<T> jsonToEntities(String jsonDate, Collection<T> targets, Class<T> targetCls) {
  522. try {
  523. ObjectMapper objectMapper = new ObjectMapper();
  524. List modelList = objectMapper.readValue(jsonDate,List.class);
  525. Iterator ex = modelList.iterator();
  526. while(ex.hasNext()) {
  527. Object aModelList = ex.next();
  528. String objJsonData = objectMapper.writeValueAsString(aModelList);
  529. T model = objectMapper.readValue(objJsonData, targetCls);
  530. targets.add(model);
  531. }
  532. return targets;
  533. } catch (Exception var8) {
  534. var8.printStackTrace();
  535. return null;
  536. }
  537. }
  538. public String write(int code, String errorMsg, int page, int rows, long total, List<?> list) {
  539. try {
  540. Map<Object, Object> map = new HashMap<Object, Object>();
  541. ObjectMapper mapper = new ObjectMapper();
  542. map.put("successFlg", code == 0);
  543. map.put("errorMsg", errorMsg);
  544. // 是否为第一页
  545. map.put("errorCode", code);
  546. // 是否为最后一页
  547. map.put("currPage", page);
  548. // 分页大小
  549. map.put("pageSize", rows);
  550. // 总条数
  551. map.put("totalCount", total);
  552. // 总页数
  553. map.put("totalPage", 1);
  554. // 结果集
  555. map.put("detailModelList", list);
  556. return mapper.writeValueAsString(map);
  557. } catch (Exception e) {
  558. error(e);
  559. return error(-1, "服务器异常,请稍候再试!");
  560. }
  561. }
  562. }