BaseController.java 16 KB

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