123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- package com.xt.jygl.common;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.xml.ws.WebServiceContext;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.propertyeditors.CustomBooleanEditor;
- import org.springframework.beans.propertyeditors.CustomDateEditor;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.data.domain.Sort;
- import org.springframework.data.domain.Sort.Direction;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.WebDataBinder;
- import org.springframework.web.bind.annotation.InitBinder;
- import com.cxfws.config.utils.CxfwsUtils;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.liandi.encrypt.Des;
- import com.xt.jygl.ftp.Ftp;
- import com.xt.jygl.gkjygl.gkjyr.jyrjbxx.entity.GkjyrEntity;
- import com.xt.jygl.gkjygl.gkjyr.jyrjbxx.service.GkjyrService;
- import com.xt.jygl.gkjygl.gkjyr.jyrjbxx.vo.GkjyrVo;
- import com.xtframe.core.base.ctl.BaseCtl;
- import com.xtframe.core.exception.BizException;
- import com.xtframe.core.support.SecurityMgr;
- import com.xtframe.core.utils.CoreConstants;
- import com.xtframe.core.webService.AuthHandler;
- import com.xtframe.sec.common.BaseVo;
- import com.xtframe.sec.common.PredicateModel;
- import com.xtframe.sec.common.PredicateModel.JoinType;
- import com.xtframe.sec.common.PredicateModel.Operator;
- import com.xtframe.sec.common.QueryService;
- import com.xtframe.sec.common.SimplePageRequest;
- import com.xtframe.sec.menu.entity.MenuEntity;
- import com.xtframe.sec.role.entity.RoleEntity;
- import com.xtframe.sec.user.entity.UserEntity;
- import com.xtframe.sec.utils.SecUtils;
- import com.xtframe.util.DateTime;
- import net.sf.json.JsonConfig;
- import rtx.RTXSvrApi;
- public class BaseWebCtl extends BaseCtl {
- /**
- * 安全服务接口
- */
- @Autowired
- protected SecurityMgr securityMgr;
- @Autowired
- private GkjyrService gkjyrService;
- @Autowired
- protected QueryService query;
- @Resource
- private WebServiceContext wscontexts;
- public static final String NOLOGIN = "NO LOGIN";
- public static final String ALGORITHM = "SHA-256";
- public static JsonConfig config = new JsonConfig();
- static {
- config.setExcludes(new String[] { "createUser", "updateUser" });
- }
- @InitBinder
- protected void initBinder(WebDataBinder binder) {
- binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(DateTime.DATE_FORMAT_DATETIME), true));
- binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(DateTime.DATE_FORMAT_DATEONLY), true));
- binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor(CoreConstants.CODE_TY_SF_YES, CoreConstants.CODE_TY_SF_NO, true));
- }
- /**
- * 是否经营人
- *
- * @return
- */
- public boolean isJyr() {
- if (SecUtils.getCurrUser() == null) {
- throw new BizException("不好,访问者没登陆");
- }
- return SecUtils.getCurrUser().getSsjyr() == null ? false : true;
- }
- /**
- * 根据当前登陆人所属经营人
- */
- public GkjyrVo getSsjyr() {
- if (SecUtils.getCurrUser() == null) {
- return null;
- }
- if (SecUtils.getCurrUser().getSsjyr() == null) {
- return null;
- }
- GkjyrEntity jyr = gkjyrService.findOne(SecUtils.getCurrUser().getSsjyr().getId());
- GkjyrVo vo = new GkjyrVo();
- if (jyr != null) {
- BeanUtils.copyProperties(jyr, vo);
- vo.setSzdzw(null != jyr.getSzd() ? jyr.getSzd().getText() : "");
- vo.setSzgkzw(null != jyr.getSzgk() ? jyr.getSzgk().getGkmc() : "");
- vo.setSzgqzw(null != jyr.getSzgq() ? jyr.getSzgq().getGqmc() : "");
- vo.setSzd(null != jyr.getSzd() ? jyr.getSzd().getId() : "");
- vo.setSzgk(null != jyr.getSzgk() ? jyr.getSzgk().getId() : "");
- vo.setSzgq(null != jyr.getSzgq() ? jyr.getSzgq().getId() : "");
- }
- return vo;
- }
- /**
- * 对象转json
- *
- * @param list
- * @return
- */
- protected String convertToJsonData(Object list) {
- try {
- ObjectMapper mapper = new ObjectMapper();
- mapper.setDateFormat(new SimpleDateFormat(DateTime.DATE_FORMAT_DATEONLY));
- String liststr = mapper.writeValueAsString(list);
- return liststr;
- } catch (JsonProcessingException jsone) {
- throw new BizException(jsone);
- }
- }
- /**
- * 对象转json字符串
- *
- * @param value
- * @return
- * @throws JsonProcessingException
- * @author 袁晓冬
- */
- public String Object2JsonStr(Object value) {
- ObjectMapper mapper = new ObjectMapper();
- try {
- return mapper.writeValueAsString(value);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- throw new BizException(e);
- }
- }
- /**
- * entity转vo时,vo中可能需要的一些其他共通字段 ,比如新增人的账号和姓名
- *
- * @param vo
- * @param creid
- */
- public void voSet(BaseVo vo, String createUserId, String updateUserId) {
- UserEntity createUser = securityMgr.userService().findById(createUserId);
- if (createUser != null) {
- vo.setCreateUserCode(createUser.getUname());
- vo.setCreateUserId(createUser.getId());
- vo.setCreateUserName(createUser.getName());
- }
- UserEntity updateUser = securityMgr.userService().findById(updateUserId);
- if (updateUser != null) {
- vo.setUpdateUserCode(updateUser.getUname());
- vo.setUpdateUserId(updateUser.getId());
- vo.setUpdateUserName(updateUser.getName());
- }
- }
- /**
- * 生成PageRequest对象
- *
- * @return
- */
- public PageRequest toPageRequest(SimplePageRequest page) {
- if (page.getPage() < 1) {
- page.setPage(1);
- }
- if (page.getRows() < 10) {
- page.setRows(10);
- }
- Sort newsort = new Sort(Direction.fromString(page.getOrder()), new String[] { page.getSort() });
- // 页码从0开始
- PageRequest pageable = new PageRequest(page.getPage() - 1, page.getRows(), newsort);
- return pageable;
- }
- public boolean isGET(HttpServletRequest request) {
- if (null != request && null != request.getMethod()) {
- return "GET".equalsIgnoreCase(request.getMethod());
- }
- return false;
- }
- public boolean isPOST(HttpServletRequest request) {
- if (null != request && null != request.getMethod()) {
- return "POST".equalsIgnoreCase(request.getMethod());
- }
- return false;
- }
- public void putPageInfo(Model model, SimplePageRequest page, Page<?> list) {
- model.addAttribute("page", page);
- model.addAttribute("pagesize", list.getContent().size());
- model.addAttribute("totalpage", list.getTotalPages());
- model.addAttribute("totalsize", list.getTotalElements());
- model.addAttribute("sort", list.getSort());
- }
- /**
- * 判断PredicateModel的value是否为空,只添加非空数据
- *
- * @param filterList
- * @param model
- */
- protected void addNotEmptyModel(List<PredicateModel> filterList, PredicateModel model) {
- Object value = model.getValue();
- if (null == value || "".equals(value)) {
- return;
- }
- if (null == filterList) {
- throw new BizException("List<PredicateModel> filterList can't be null!");
- }
- filterList.add(model);
- }
- /**
- * 判断PredicateModel的value是否为空,只添加非空数据
- *
- * @param filterList
- * @param model
- */
- protected void addNotEmptyModel(List<PredicateModel> filterList, String fieldName, Object value, Operator operator) {
- if (null == value || "".equals(value) || "null".equals(value)) {
- return;
- }
- if (null == filterList) {
- throw new BizException("List<PredicateModel> filterList can't be null!");
- }
- filterList.add(new PredicateModel(fieldName, value, operator));
- }
- /**
- * 判断PredicateModel的value是否为空,只添加非空数据
- *
- * @param filterList
- * @param model
- */
- protected void addNotEmptyModel(List<PredicateModel> filterList, String fieldName, Object value, Operator operator, JoinType type) {
- if (null == value || "".equals(value) || "null".equals(value)) {
- return;
- }
- if (null == filterList) {
- throw new BizException("List<PredicateModel> filterList can't be null!");
- }
- filterList.add(new PredicateModel(fieldName, value, operator, type));
- }
- /**
- * List参数判断
- *
- * @param filterList
- * @param fieldName
- * @param values
- * @param index
- * @param operator
- */
- protected void addNotEmptyModel(List<PredicateModel> filterList, String fieldName, List<?> values, int index, Operator operator) {
- if (values.size() >= index + 1) {
- addNotEmptyModel(filterList, fieldName, values.get(index), operator);
- }
- }
- public void getMenuPath(HttpServletRequest request, Model model, String menuId) {
- if (menuId == null) {
- menuId = (String) request.getSession().getAttribute("menuId");
- } else {
- request.getSession().setAttribute("menuId", menuId);
- }
- if (menuId != null) {
- MenuEntity m = securityMgr.menuService().findMenu(menuId);
- MenuEntity pm = securityMgr.menuService().findMenu(m.getPid());
- MenuEntity ppm = securityMgr.menuService().findMenu(pm.getPid());
- String path = " 当前位置:" + (ppm == null ? "" : ppm.getName()) + ">>" + (pm == null ? "" : pm.getName()) + ">>" + (m == null ? "" : m.getName());
- model.addAttribute("menuPath", path);
- } else {
- model.addAttribute("menuPath", "");
- }
- }
- /**
- * SHA-256加密
- *
- * @param orignal
- * @return
- */
- public static String SHA256Encrypt(String orignal) {
- MessageDigest md = null;
- try {
- md = MessageDigest.getInstance(ALGORITHM);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- if (null != md) {
- byte[] origBytes = orignal.getBytes();
- md.update(origBytes);
- byte[] digestRes = md.digest();
- String digestStr = getDigestStr(digestRes);
- return digestStr;
- }
- return null;
- }
- public static String destoSHA256Encrypt(String orignal) {
- if (StringUtils.isNotBlank(orignal)) {
- System.out.println("destoSHA256Encrypt===" + Des.decrypt(orignal));
- return SHA256Encrypt(Des.decrypt(orignal));
- }
- return orignal;
- }
- private static String getDigestStr(byte[] origBytes) {
- String tempStr = null;
- StringBuilder stb = new StringBuilder();
- for (int i = 0; i < origBytes.length; i++) {
- // System.out.println("and by bit: " + (origBytes[i] & 0xff));
- // System.out.println("no and: " + origBytes[i]);
- // System.out.println("---------------------------------------------");
- // 这里按位与是为了把字节转整时候取其正确的整数,java中一个int是4个字节
- // 如果origBytes[i]最高位为1,则转为int时,int的前三个字节都被1填充了
- tempStr = Integer.toHexString(origBytes[i] & 0xff);
- if (tempStr.length() == 1) {
- stb.append("0");
- }
- stb.append(tempStr);
- }
- return stb.toString();
- }
- /**
- * WebService登录用户
- *
- * @return
- */
- public static UserEntity getWebServiceUser() {
- if (AuthHandler.getCurrentUser() != null) {
- return AuthHandler.getCurrentUser();
- }
- return null;
- }
- /**
- * 只有盐城,盐城内河,镇江三个地市需要用webservice同步
- */
- public boolean needWS() {
- String needSzd = "90019,90027,90011,90012";
- if (needSzd.contains(GlobalData.CITY_CODE)) {
- return true;
- }
- return false;
- }
- // rtx提醒
- public void rtxSend(String userids, String role, String msg) {
- if (StringUtils.isEmpty(userids)) {
- List<RoleEntity> rolelist = securityMgr.roleService().findByCode(role);
- RoleEntity roleEntity = new RoleEntity();
- if (rolelist != null && rolelist.size() > 0) {
- roleEntity = rolelist.get(0);
- List<UserEntity> ulist = securityMgr.userService().findByRole(roleEntity.getId());
- if (ulist != null && ulist.size() > 0) {
- for (UserEntity u : ulist) {
- inserttx(u.getId(), msg);
- }
- }
- }
- } else {
- if (!StringUtils.isEmpty(userids)) {
- String[] users = userids.split(",");
- for (String userid : users) {
- inserttx(userid, msg);
- }
- }
- }
- }
- /**
- * 更改当前处理人的限时提醒状态
- */
- // 参数顺序 限时天数,系统用户id,步骤,rtx提醒内荣,办理提醒内同,业务key,模块(PC)
- public void inserttx(String userid, String msg) {
- // 发送rtx提醒
- try {
- UserEntity u = (UserEntity) securityMgr.userService().findById(userid);
- if (u != null) {
- try {
- String type = "0";
- String delayTime = "0";
- int iRet = -1;
- RTXSvrApi RtxsvrapiObj = new RTXSvrApi();
- if (RtxsvrapiObj.Init()) {
- iRet = RtxsvrapiObj.sendNotify(u.getUname(), "", msg, type, delayTime);
- if (iRet == 0) {
- System.out.println("发送成功");
- } else {
- System.out.println("发送失败");
- }
- }
- RtxsvrapiObj.UnInit();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- public boolean getTbStatus(String uStr ,String szd) {
- boolean flag = false;
- try {
- String str="";
- // 判断省市区别
- if (GlobalData.DEPLOY_MODE.equals(Constants.DEPLOY_MODE_CITY)) {
- str = GlobalData.SJYH_URL +GlobalData.DSJC + "_";
- } else {
- str = GlobalData.SJYH_URL + szd + "_";
- }
- URL url = new URL(str + uStr);
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- con.connect();
- BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
- StringBuffer buffer = new StringBuffer();
- String tmp = null;
- while ((tmp = br.readLine()) != null) {
- buffer.append(tmp);
- }
- String result = buffer.toString();
- if (result.indexOf("true") > 0) {
- flag = true;
- }
- } catch (MalformedURLException e) {
- flag = false;
- e.printStackTrace();
- } catch (IOException e) {
- flag = false;
- e.printStackTrace();
- }
- return flag;
- }
- public Ftp getftp() {
- // ftp 参数设置
- Ftp f = new Ftp();
- f.setIpAddr(GlobalData.FTP_IP);
- f.setPort(Integer.parseInt(GlobalData.FTP_PORT));
- f.setUserName(GlobalData.FTP_QT_USER);
- f.setPwd(GlobalData.FTP_QT_USER);
- f.setPath(GlobalData.FTP_UPLOAD_PATH);
- return f;
- }
-
- /**
- * 校验用户信息
- * @param userName
- * @param pwd
- * @return
- */
- public boolean checkUser(){
- String userName = CxfwsUtils.userNameFromHeaderInfo(wscontexts);
- String pwd = CxfwsUtils.passWordFromHeaderInfo(wscontexts);
- if ( StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(pwd)){
- UserEntity user = securityMgr.userService().findByUname(userName);
- if (user != null) {
- if (pwd.equals(user.getPassword())) {
- AuthHandler.setCurrentUser(user);
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- }
|