BaseWebCtl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. package com.xt.jygl.common;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.security.MessageDigest;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. import java.util.List;
  13. import javax.servlet.http.HttpServletRequest;
  14. import net.sf.json.JsonConfig;
  15. import org.apache.commons.lang.StringUtils;
  16. import org.springframework.beans.BeanUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.propertyeditors.CustomBooleanEditor;
  19. import org.springframework.beans.propertyeditors.CustomDateEditor;
  20. import org.springframework.data.domain.Page;
  21. import org.springframework.data.domain.PageRequest;
  22. import org.springframework.data.domain.Sort;
  23. import org.springframework.data.domain.Sort.Direction;
  24. import org.springframework.ui.Model;
  25. import org.springframework.web.bind.WebDataBinder;
  26. import org.springframework.web.bind.annotation.InitBinder;
  27. import rtx.RTXSvrApi;
  28. import com.fasterxml.jackson.core.JsonProcessingException;
  29. import com.fasterxml.jackson.databind.ObjectMapper;
  30. import com.liandi.encrypt.Des;
  31. import com.xt.jygl.ftp.Ftp;
  32. import com.xt.jygl.gkjcxx.gq.service.GqService;
  33. import com.xt.jygl.gkjygl.gkjyr.jyrjbxx.entity.GkjyrEntity;
  34. import com.xt.jygl.gkjygl.gkjyr.jyrjbxx.service.GkjyrService;
  35. import com.xt.jygl.gkjygl.gkjyr.jyrjbxx.vo.GkjyrVo;
  36. import com.xtframe.core.base.ctl.BaseCtl;
  37. import com.xtframe.core.exception.BizException;
  38. import com.xtframe.core.support.SecurityMgr;
  39. import com.xtframe.core.utils.CoreConstants;
  40. import com.xtframe.core.webService.AuthHandler;
  41. import com.xtframe.sec.common.BaseVo;
  42. import com.xtframe.sec.common.PredicateModel;
  43. import com.xtframe.sec.common.PredicateModel.JoinType;
  44. import com.xtframe.sec.common.PredicateModel.Operator;
  45. import com.xtframe.sec.common.QueryService;
  46. import com.xtframe.sec.common.SimplePageRequest;
  47. import com.xtframe.sec.menu.entity.MenuEntity;
  48. import com.xtframe.sec.role.entity.RoleEntity;
  49. import com.xtframe.sec.user.entity.UserEntity;
  50. import com.xtframe.sec.utils.SecUtils;
  51. import com.xtframe.util.DateTime;
  52. public class BaseWebCtl extends BaseCtl {
  53. /**
  54. * 安全服务接口
  55. */
  56. @Autowired
  57. protected SecurityMgr securityMgr;
  58. @Autowired
  59. private GkjyrService gkjyrService;
  60. @Autowired
  61. protected QueryService query;
  62. public static final String ALGORITHM = "SHA-256";
  63. public static JsonConfig config = new JsonConfig();
  64. static {
  65. config.setExcludes(new String[] { "createUser", "updateUser" });
  66. }
  67. @InitBinder
  68. protected void initBinder(WebDataBinder binder) {
  69. binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(DateTime.DATE_FORMAT_DATETIME), true));
  70. binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(DateTime.DATE_FORMAT_DATEONLY), true));
  71. binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor(CoreConstants.CODE_TY_SF_YES, CoreConstants.CODE_TY_SF_NO, true));
  72. }
  73. /**
  74. * 是否经营人
  75. *
  76. * @return
  77. */
  78. public boolean isJyr() {
  79. if (SecUtils.getCurrUser() == null) {
  80. throw new BizException("不好,访问者没登陆");
  81. }
  82. return SecUtils.getCurrUser().getSsjyr() == null ? false : true;
  83. }
  84. /**
  85. * 根据当前登陆人所属经营人
  86. */
  87. public GkjyrVo getSsjyr() {
  88. if (SecUtils.getCurrUser() == null) {
  89. return null;
  90. }
  91. if (SecUtils.getCurrUser().getSsjyr() == null) {
  92. return null;
  93. }
  94. GkjyrEntity jyr = gkjyrService.findOne(SecUtils.getCurrUser().getSsjyr().getId());
  95. GkjyrVo vo = new GkjyrVo();
  96. if (jyr != null) {
  97. BeanUtils.copyProperties(jyr, vo);
  98. vo.setSzdzw(null != jyr.getSzd() ? jyr.getSzd().getText() : "");
  99. vo.setSzgkzw(null != jyr.getSzgk() ? jyr.getSzgk().getGkmc() : "");
  100. vo.setSzgqzw(null != jyr.getSzgq() ? jyr.getSzgq().getGqmc() : "");
  101. vo.setSzd(null != jyr.getSzd() ? jyr.getSzd().getId() : "");
  102. vo.setSzgk(null != jyr.getSzgk() ? jyr.getSzgk().getId() : "");
  103. vo.setSzgq(null != jyr.getSzgq() ? jyr.getSzgq().getId() : "");
  104. }
  105. return vo;
  106. }
  107. /**
  108. * 对象转json
  109. *
  110. * @param list
  111. * @return
  112. */
  113. protected String convertToJsonData(Object list) {
  114. try {
  115. ObjectMapper mapper = new ObjectMapper();
  116. mapper.setDateFormat(new SimpleDateFormat(DateTime.DATE_FORMAT_DATEONLY));
  117. String liststr = mapper.writeValueAsString(list);
  118. return liststr;
  119. } catch (JsonProcessingException jsone) {
  120. throw new BizException(jsone);
  121. }
  122. }
  123. /**
  124. * 对象转json字符串
  125. *
  126. * @param value
  127. * @return
  128. * @throws JsonProcessingException
  129. * @author 袁晓冬
  130. */
  131. public String Object2JsonStr(Object value) {
  132. ObjectMapper mapper = new ObjectMapper();
  133. try {
  134. return mapper.writeValueAsString(value);
  135. } catch (JsonProcessingException e) {
  136. e.printStackTrace();
  137. throw new BizException(e);
  138. }
  139. }
  140. /**
  141. * entity转vo时,vo中可能需要的一些其他共通字段 ,比如新增人的账号和姓名
  142. *
  143. * @param vo
  144. * @param creid
  145. */
  146. public void voSet(BaseVo vo, String createUserId, String updateUserId) {
  147. UserEntity createUser = securityMgr.userService().findById(createUserId);
  148. if (createUser != null) {
  149. vo.setCreateUserCode(createUser.getUname());
  150. vo.setCreateUserId(createUser.getId());
  151. vo.setCreateUserName(createUser.getName());
  152. }
  153. UserEntity updateUser = securityMgr.userService().findById(updateUserId);
  154. if (updateUser != null) {
  155. vo.setUpdateUserCode(updateUser.getUname());
  156. vo.setUpdateUserId(updateUser.getId());
  157. vo.setUpdateUserName(updateUser.getName());
  158. }
  159. }
  160. /**
  161. * 生成PageRequest对象
  162. *
  163. * @return
  164. */
  165. public PageRequest toPageRequest(SimplePageRequest page) {
  166. if (page.getPage() < 1) {
  167. page.setPage(1);
  168. }
  169. if (page.getRows() < 10) {
  170. page.setRows(10);
  171. }
  172. Sort newsort = new Sort(Direction.fromString(page.getOrder()), new String[] { page.getSort() });
  173. // 页码从0开始
  174. PageRequest pageable = new PageRequest(page.getPage() - 1, page.getRows(), newsort);
  175. return pageable;
  176. }
  177. public boolean isGET(HttpServletRequest request) {
  178. if (null != request && null != request.getMethod()) {
  179. return "GET".equalsIgnoreCase(request.getMethod());
  180. }
  181. return false;
  182. }
  183. public boolean isPOST(HttpServletRequest request) {
  184. if (null != request && null != request.getMethod()) {
  185. return "POST".equalsIgnoreCase(request.getMethod());
  186. }
  187. return false;
  188. }
  189. public void putPageInfo(Model model, SimplePageRequest page, Page<?> list) {
  190. model.addAttribute("page", page);
  191. model.addAttribute("pagesize", list.getContent().size());
  192. model.addAttribute("totalpage", list.getTotalPages());
  193. model.addAttribute("totalsize", list.getTotalElements());
  194. model.addAttribute("sort", list.getSort());
  195. }
  196. /**
  197. * 判断PredicateModel的value是否为空,只添加非空数据
  198. *
  199. * @param filterList
  200. * @param model
  201. */
  202. protected void addNotEmptyModel(List<PredicateModel> filterList, PredicateModel model) {
  203. Object value = model.getValue();
  204. if (null == value || "".equals(value)) {
  205. return;
  206. }
  207. if (null == filterList) {
  208. throw new BizException("List<PredicateModel> filterList can't be null!");
  209. }
  210. filterList.add(model);
  211. }
  212. /**
  213. * 判断PredicateModel的value是否为空,只添加非空数据
  214. *
  215. * @param filterList
  216. * @param model
  217. */
  218. protected void addNotEmptyModel(List<PredicateModel> filterList, String fieldName, Object value, Operator operator) {
  219. if (null == value || "".equals(value) || "null".equals(value)) {
  220. return;
  221. }
  222. if (null == filterList) {
  223. throw new BizException("List<PredicateModel> filterList can't be null!");
  224. }
  225. filterList.add(new PredicateModel(fieldName, value, operator));
  226. }
  227. /**
  228. * 判断PredicateModel的value是否为空,只添加非空数据
  229. *
  230. * @param filterList
  231. * @param model
  232. */
  233. protected void addNotEmptyModel(List<PredicateModel> filterList, String fieldName, Object value, Operator operator, JoinType type) {
  234. if (null == value || "".equals(value) || "null".equals(value)) {
  235. return;
  236. }
  237. if (null == filterList) {
  238. throw new BizException("List<PredicateModel> filterList can't be null!");
  239. }
  240. filterList.add(new PredicateModel(fieldName, value, operator, type));
  241. }
  242. /**
  243. * List参数判断
  244. *
  245. * @param filterList
  246. * @param fieldName
  247. * @param values
  248. * @param index
  249. * @param operator
  250. */
  251. protected void addNotEmptyModel(List<PredicateModel> filterList, String fieldName, List<?> values, int index, Operator operator) {
  252. if (values.size() >= index + 1) {
  253. addNotEmptyModel(filterList, fieldName, values.get(index), operator);
  254. }
  255. }
  256. public void getMenuPath(HttpServletRequest request, Model model, String menuId) {
  257. if (menuId == null) {
  258. menuId = (String) request.getSession().getAttribute("menuId");
  259. } else {
  260. request.getSession().setAttribute("menuId", menuId);
  261. }
  262. if (menuId != null) {
  263. MenuEntity m = securityMgr.menuService().findMenu(menuId);
  264. MenuEntity pm = securityMgr.menuService().findMenu(m.getPid());
  265. MenuEntity ppm = securityMgr.menuService().findMenu(pm.getPid());
  266. String path = " 当前位置:" + (ppm == null ? "" : ppm.getName()) + ">>" + (pm == null ? "" : pm.getName()) + ">>" + (m == null ? "" : m.getName());
  267. model.addAttribute("menuPath", path);
  268. } else {
  269. model.addAttribute("menuPath", "");
  270. }
  271. }
  272. /**
  273. * SHA-256加密
  274. *
  275. * @param orignal
  276. * @return
  277. */
  278. public static String SHA256Encrypt(String orignal) {
  279. MessageDigest md = null;
  280. try {
  281. md = MessageDigest.getInstance(ALGORITHM);
  282. } catch (NoSuchAlgorithmException e) {
  283. e.printStackTrace();
  284. }
  285. if (null != md) {
  286. byte[] origBytes = orignal.getBytes();
  287. md.update(origBytes);
  288. byte[] digestRes = md.digest();
  289. String digestStr = getDigestStr(digestRes);
  290. return digestStr;
  291. }
  292. return null;
  293. }
  294. public static String destoSHA256Encrypt(String orignal) {
  295. if (StringUtils.isNotBlank(orignal)) {
  296. System.out.println("destoSHA256Encrypt===" + Des.decrypt(orignal));
  297. return SHA256Encrypt(Des.decrypt(orignal));
  298. }
  299. return orignal;
  300. }
  301. private static String getDigestStr(byte[] origBytes) {
  302. String tempStr = null;
  303. StringBuilder stb = new StringBuilder();
  304. for (int i = 0; i < origBytes.length; i++) {
  305. // System.out.println("and by bit: " + (origBytes[i] & 0xff));
  306. // System.out.println("no and: " + origBytes[i]);
  307. // System.out.println("---------------------------------------------");
  308. // 这里按位与是为了把字节转整时候取其正确的整数,java中一个int是4个字节
  309. // 如果origBytes[i]最高位为1,则转为int时,int的前三个字节都被1填充了
  310. tempStr = Integer.toHexString(origBytes[i] & 0xff);
  311. if (tempStr.length() == 1) {
  312. stb.append("0");
  313. }
  314. stb.append(tempStr);
  315. }
  316. return stb.toString();
  317. }
  318. /**
  319. * WebService登录用户
  320. *
  321. * @return
  322. */
  323. public static UserEntity getWebServiceUser() {
  324. if (AuthHandler.getCurrentUser() != null) {
  325. return AuthHandler.getCurrentUser();
  326. }
  327. return null;
  328. }
  329. /**
  330. * 只有盐城,盐城内河,镇江三个地市需要用webservice同步
  331. */
  332. public boolean needWS() {
  333. String needSzd = "90019,90027,90011,90012";
  334. if (needSzd.contains(GlobalData.CITY_CODE)) {
  335. return true;
  336. }
  337. return false;
  338. }
  339. // rtx提醒
  340. public void rtxSend(String userids, String role, String msg) {
  341. if (StringUtils.isEmpty(userids)) {
  342. List<RoleEntity> rolelist = securityMgr.roleService().findByCode(role);
  343. RoleEntity roleEntity = new RoleEntity();
  344. if (rolelist != null && rolelist.size() > 0) {
  345. roleEntity = rolelist.get(0);
  346. List<UserEntity> ulist = securityMgr.userService().findByRole(roleEntity.getId());
  347. if (ulist != null && ulist.size() > 0) {
  348. for (UserEntity u : ulist) {
  349. inserttx(u.getId(), msg);
  350. }
  351. }
  352. }
  353. } else {
  354. if (!StringUtils.isEmpty(userids)) {
  355. String[] users = userids.split(",");
  356. for (String userid : users) {
  357. inserttx(userid, msg);
  358. }
  359. }
  360. }
  361. }
  362. /**
  363. * 更改当前处理人的限时提醒状态
  364. */
  365. // 参数顺序 限时天数,系统用户id,步骤,rtx提醒内荣,办理提醒内同,业务key,模块(PC)
  366. public void inserttx(String userid, String msg) {
  367. // 发送rtx提醒
  368. try {
  369. UserEntity u = (UserEntity) securityMgr.userService().findById(userid);
  370. if (u != null) {
  371. try {
  372. String type = "0";
  373. String delayTime = "0";
  374. int iRet = -1;
  375. RTXSvrApi RtxsvrapiObj = new RTXSvrApi();
  376. if (RtxsvrapiObj.Init()) {
  377. iRet = RtxsvrapiObj.sendNotify(u.getUname(), "", msg, type, delayTime);
  378. if (iRet == 0) {
  379. System.out.println("发送成功");
  380. } else {
  381. System.out.println("发送失败");
  382. }
  383. }
  384. RtxsvrapiObj.UnInit();
  385. } catch (Throwable e) {
  386. e.printStackTrace();
  387. }
  388. }
  389. } catch (Throwable e) {
  390. e.printStackTrace();
  391. }
  392. }
  393. public boolean getTbStatus(String uStr) {
  394. boolean flag = false;
  395. try {
  396. String str = "http://10.1.30.118:9701/xtdsp/task/run";
  397. URL url = new URL(str + uStr);
  398. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  399. con.connect();
  400. BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
  401. StringBuffer buffer = new StringBuffer();
  402. String tmp = null;
  403. while ((tmp = br.readLine()) != null) {
  404. buffer.append(tmp);
  405. }
  406. String result = buffer.toString();
  407. if (result.indexOf("true") > 0) {
  408. flag = true;
  409. }
  410. } catch (MalformedURLException e) {
  411. flag = false;
  412. e.printStackTrace();
  413. } catch (IOException e) {
  414. flag = false;
  415. e.printStackTrace();
  416. }
  417. return flag;
  418. }
  419. public Ftp getftp() {
  420. // ftp 参数设置
  421. Ftp f = new Ftp();
  422. f.setIpAddr(GlobalData.FTP_IP);
  423. f.setPort(Integer.parseInt(GlobalData.FTP_PORT));
  424. f.setUserName(GlobalData.FTP_QT_USER);
  425. f.setPwd(GlobalData.FTP_QT_USER);
  426. f.setPath(GlobalData.FTP_UPLOAD_PATH);
  427. return f;
  428. }
  429. }