BaseWebCtl.java 15 KB

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