package com.bonus.sys; import com.bonus.core.UUIDHelper; import com.bonus.sys.beans.ResourcesBean; import com.bonus.sys.beans.SysDataDictBean; import com.bonus.sys.beans.SysDictTypeBean; import com.bonus.sys.service.ResourcesService; import com.bonus.sys.service.SysDataDictService; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.List; import java.util.regex.Pattern; public class BaseController { protected Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired public ResourcesService resourcesService; @Autowired public SysDataDictService dataDictService; /** * 得到ModelAndView */ public ModelAndView getModelAndView() { return new ModelAndView(); } public AjaxRes getAjaxRes() { return new AjaxRes(); } /** * 得到32位的uuid * @return */ public String get32UUID(){ return UUIDHelper.get32UUID(); } /** * 得到PageData */ public PageData getPageData() { return new PageData(this.getRequest()); } /** * @Author 无畏 * @Date 2019-06-12 * @function 公用的获取数据字典列表 * @param typeId 数据字典所属类型 * @return 数据字典list集合 */ public List getDictListByTypeId(Integer typeId) { SysDictTypeBean o = new SysDictTypeBean(); o.setId(typeId); return dataDictService.findListByTypeId(o); } /** * @Author 无畏 * @Date 2019-06-12 * @function 公用的获取数据字典列表 * @param typeId 数据字典所属类型 * @return 数据字典list集合 */ public SysDataDictBean getDataDictById(SysDataDictBean data) { return dataDictService.findOneById(data); } /** * 得到request对象 */ public HttpServletRequest getRequest() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); return request; } /** * 资源的权限(URl级别) * * @param type * 资源类别(优化速度) * @return */ protected boolean doSecurityIntercept(String type) { try { String servletPath = getRequest().getServletPath(); servletPath = StringUtils.substringBeforeLast(servletPath, ".");// 去掉后面的后缀 int userId = UserShiroHelper.getCurrentUser().getId(); List authorized = resourcesService.resAuthorized(userId, type); for (ResourcesBean r : authorized) { if (r != null && StringUtils.isNotBlank(r.getUrl())) { if (StringUtils.equals(r.getUrl(), servletPath)) { return true; } } } } catch (Exception e) { logger.error(e.toString(), e); } return false; } /** * 资源的权限(URl级别,拥有第一级资源权限,这资源才能访问) * * @param type * 资源类别(优化速度) * @param url * 第一级资源 * @return */ protected boolean doSecurityIntercept(String type, String url) { try { int userId = UserShiroHelper.getCurrentUser().getId(); List authorized = resourcesService.resAuthorized( userId, type); logger.debug("authorized=" + authorized + ",type=" + type + ",userId=" + userId + ",url=" + url); for (ResourcesBean r : authorized) { if (r != null && StringUtils.isNotBlank(r.getUrl())) { if (StringUtils.equals(r.getUrl(), url)) { return true; } } } } catch (Exception e) { logger.error(e.toString(), e); } return false; } // 判断字符串是否为数字 public static boolean isInteger(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); } public String toChangeString(String ids) { String idsss = ""; String[] idss = ids.split(","); for(String id: idss) { boolean isInt = isInteger(id); if(isInt) { idsss += id+","; } } return idsss; } //获取本地IP地址 public String getIpAddress() { InetAddress addr = null; String ipAddress = ""; try { addr = InetAddress.getLocalHost(); System.out.println("Local HostAddress:"+addr.getHostAddress()); //String hostname = addr.getHostName(); ipAddress = addr.getHostAddress(); //System.out.println("Local host name: "+hostname); } catch (UnknownHostException e) { e.printStackTrace(); } return ipAddress; } }