package cn.ksource.core.web;
|
|
import java.io.PrintWriter;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpSession;
|
|
import org.apache.commons.lang.StringUtils;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.servlet.ModelAndView;
|
|
import cn.ksource.core.util.ConvertUtil;
|
import cn.ksource.web.SysConstants;
|
|
/**
|
* Web处理常用工具类
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Dec 12, 2013 6:21:37 PM
|
*/
|
public class WebUtil {
|
|
/**
|
* 获取系统URL
|
* @param request
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Jan 15, 2014 3:12:19 PM
|
*/
|
public static String getFullPath(HttpServletRequest request){
|
return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
|
}
|
|
/**
|
* 获取当前登录人ID
|
* @param request
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Dec 12, 2013 6:22:40 PM
|
*/
|
public static String getLoginedUserId(HttpServletRequest request){
|
return getLoginUser(request).getLoginUser().get("ID").toString();
|
}
|
|
|
/**
|
* 获取用户id统一入口
|
* @param request
|
* @return
|
* @author chenlong
|
*/
|
public static String getUserId(HttpServletRequest request){
|
return getLoginUser(request).getLoginUser().get("ID").toString();
|
}
|
/**
|
* 获取用户name统一入口
|
* @param request
|
* @return
|
* @author chenlong
|
*/
|
public static String getUserName(HttpServletRequest request){
|
return getLoginUser(request).getLoginUser().get("ZSXM").toString();
|
}
|
|
/**
|
* 返回Controller类的RequestMapping+ViewName
|
* @param controller
|
* @param viewName
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Jan 25, 2014 3:40:42 PM
|
*/
|
public static String getViewName(Object controller,String viewName){
|
String controllerViewName = controller.getClass().getAnnotation(RequestMapping.class).value()[0];
|
return controllerViewName+viewName;
|
}
|
|
public static ModelAndView getView(Object controller,String viewName){
|
String controllerViewName = controller.getClass().getAnnotation(RequestMapping.class).value()[0];
|
return new ModelAndView(controllerViewName+viewName);
|
}
|
|
|
|
/**
|
* 判断一个请求是否为AJAX请求
|
* @param request
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Jan 16, 2014 6:38:05 PM
|
*/
|
public static boolean isAjaxRequest(HttpServletRequest request){
|
String requestType = request.getHeader("X-Requested-With");
|
if (StringUtils.isNotBlank(requestType) && requestType.equalsIgnoreCase("XMLHttpRequest")) {
|
return true;
|
}
|
return false;
|
}
|
|
|
/**
|
* 获取Sql排序
|
* @param request
|
* @param defaultOrderBy
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Jan 15, 2014 10:13:00 AM
|
*/
|
public static String getOrderBySql(HttpServletRequest request,String defaultOrderBy){
|
String order = request.getParameter("order");
|
String sort = request.getParameter("sort");
|
if (StringUtils.isBlank(order)) {
|
return defaultOrderBy;
|
}
|
|
String[] orders = order.split(",");
|
String[] sorts = sort.split(",");
|
String orderby = " order by ";
|
for (int i = 0; i < sorts.length; i++) {
|
orderby += sorts[i] + " " + orders[i] + ",";
|
}
|
return StringUtils.removeEnd(orderby, ",");
|
}
|
/**
|
* 获取Sql排序
|
* @param request
|
* @param defaultOrderBy
|
* @param replace 替代默认值
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Jan 15, 2014 10:13:00 AM
|
*/
|
public static String getOrderBySql(HttpServletRequest request,String defaultOrderBy,Map<String,String> replace){
|
String order = request.getParameter("order");
|
String sort = request.getParameter("sort");
|
|
if (StringUtils.isBlank(order)) {
|
return defaultOrderBy;
|
}
|
if (replace == null) {
|
replace = new HashMap();
|
}
|
|
String[] orders = order.split(",");
|
String[] sorts = sort.split(",");
|
String orderby = " order by ";
|
for (int i = 0; i < sorts.length; i++) {
|
String key = sorts[i];
|
if (replace.containsKey(key)) {
|
key = replace.get(key);
|
}
|
orderby += key + " " + orders[i] + ",";
|
}
|
return StringUtils.removeEnd(orderby, ",");
|
}
|
|
|
/**
|
* 取得当前登陆人信息
|
* @param request
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Dec 28, 2013 5:59:40 PM
|
*/
|
public static LoginUser getLoginUser(HttpServletRequest request){
|
return (LoginUser)request.getSession().getAttribute(SysConstants.LOGIN_INFO_KEY);
|
}
|
|
|
|
|
|
|
|
|
/**
|
* 是否具有指定的权限
|
* @param funcid
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Jan 13, 2014 5:36:37 PM
|
*/
|
public static boolean hasPower(HttpServletRequest request,String funcid){
|
LoginUser loginUser = getLoginUser(request);
|
return loginUser.getPermissionMap().containsKey(funcid);
|
}
|
|
|
/**
|
* 获取真实路径
|
* @param request
|
* @param path
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Dec 28, 2013 11:13:42 AM
|
*/
|
public static String getRealPath(HttpServletRequest request,String path){
|
return request.getSession().getServletContext().getRealPath(path);
|
}
|
|
|
/**
|
* 以utf-8编码向客户写入信息
|
* @param str
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Sep 18, 2013 12:51:36 AM
|
*/
|
public static void write(HttpServletResponse response, String str){
|
try {
|
response.setContentType("text/html");
|
response.setCharacterEncoding("utf-8");
|
PrintWriter writer =response.getWriter();
|
writer.print(str);
|
writer.flush();
|
writer.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* 界面统一跳转
|
* @param message 展示的信息
|
* @param autoScript 自动执行的脚本
|
* @param sysInfo 成功或者失败
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Dec 10, 2013 10:15:26 AM
|
*/
|
public static ModelAndView goSysInfoPage(HttpServletRequest request,String message,String autoScript,SysInfo sysInfo){
|
ModelAndView view = new ModelAndView("redirect:/business/sysinfo.html");
|
HttpSession session = request.getSession();
|
session.setAttribute("message", message);
|
session.setAttribute("autoScript", autoScript);
|
session.setAttribute("sysInfo", sysInfo.toString());
|
return view;
|
}
|
|
/**
|
* 新界面统一跳转
|
* @param message 展示的信息
|
* @param autoScript 自动执行的脚本
|
* @param sysInfo 成功或者失败
|
* @version V1.0.0sysInfoPage
|
* @author jxl
|
* @date Dec 10, 2013 10:15:26 AM
|
*/
|
public static ModelAndView sysInfoPage(HttpServletRequest request,String message,String autoScript,SysInfo sysInfo,String url){
|
ModelAndView view = new ModelAndView("/business/pages/include/sysInfoPage");
|
HttpSession session = request.getSession();
|
session.setAttribute("message", message);
|
session.setAttribute("autoScript", autoScript);
|
session.setAttribute("sysInfo", sysInfo.toString());
|
session.setAttribute("outurlput", url);
|
session.setAttribute("msg", null);
|
return view;
|
}
|
|
/**
|
* 新界面统一跳转
|
* @param message 展示的信息
|
* @param autoScript 自动执行的脚本
|
* @param sysInfo 成功或者失败
|
* @version V1.0.0
|
* @author jxl
|
* @date Dec 10, 2013 10:15:26 AM
|
*/
|
public static ModelAndView sysInfoPage(HttpServletRequest request,String message,String autoScript,SysInfo sysInfo,String url,SysInfoMsg msg){
|
ModelAndView view = new ModelAndView("/business/pages/include/sysInfoPage");
|
HttpSession session = request.getSession();
|
session.setAttribute("message", message);
|
session.setAttribute("autoScript", autoScript);
|
session.setAttribute("sysInfo", sysInfo.toString());
|
session.setAttribute("outurlput", url);
|
session.setAttribute("msg", msg);
|
return view;
|
}
|
|
/**
|
* 得到不同的用户桌面所对应的客户信息
|
*/
|
public static List getCustomers(HttpServletRequest request) {
|
Map cusPro = (Map)request.getSession().getAttribute(SysConstants.CUSTOMER_PROJECT);
|
List customers = (List)cusPro.get("customers");
|
return customers;
|
}
|
|
/**
|
* 取得当前登陆人信息(用户中心)
|
* @param request
|
* @return
|
* @version V1.0.0
|
* @author 江小磊
|
*/
|
public static WebLoginUser getWebLoginUser(HttpServletRequest request){
|
return (WebLoginUser)request.getSession().getAttribute(SysConstants.WEBSITE_LOGIN_INFO_KEY);
|
}
|
|
|
/**
|
* 取得当前用户中心当前登录人的ID
|
*/
|
public static String getWebLoginUserId(HttpServletRequest request){
|
WebLoginUser loginUser = getWebLoginUser(request);
|
return loginUser.getUserId();
|
}
|
|
/**
|
* 取得当前用户中心当前登录人的客户ID
|
*/
|
public static String getWebLoginUserCusId(HttpServletRequest request) {
|
WebLoginUser loginUser = getWebLoginUser(request);
|
return loginUser.getCustomerId();
|
}
|
|
//===================手机微信工程师session存放信息
|
/**
|
* 获取手机微信工程师session信息
|
*/
|
public static Map getEngineerMsg(HttpServletRequest request) {
|
Map userMap = new HashMap();
|
Object obj = request.getSession().getAttribute(SysConstants.WYW_ENGINEER_LOGIN_INFO_KEY);
|
if(null!=obj) {
|
userMap = (Map)obj;
|
}
|
return userMap;
|
}
|
|
|
/**
|
* 获取手机微信工程师openId
|
*/
|
public static String getEngineerOpenId(HttpServletRequest request) {
|
Map userMap = new HashMap();
|
Object obj = request.getSession().getAttribute(SysConstants.WYW_ENGINEER_LOGIN_INFO_KEY);
|
if(null!=obj) {
|
userMap = (Map)obj;
|
}
|
String openId = new String();
|
if(userMap.size()>0) {
|
openId = ConvertUtil.obj2StrBlank(userMap.get("OPEN_ID"));
|
}
|
return openId;
|
}
|
|
|
/**
|
* 清空手机微信工程师session信息
|
*/
|
public static void removeEngineerMsg(HttpServletRequest request) {
|
HttpSession session = request.getSession();
|
session.removeAttribute(SysConstants.WYW_ENGINEER_LOGIN_INFO_KEY);
|
session.invalidate();
|
}
|
|
/**
|
* 获取手机微信工程师桌面类型
|
*/
|
public static String getEngineerDeskType(HttpServletRequest request) {
|
Map userMap = new HashMap();
|
Object obj = request.getSession().getAttribute(SysConstants.WYW_ENGINEER_LOGIN_INFO_KEY);
|
if(null!=obj) {
|
userMap = (Map)obj;
|
}
|
String deskType = new String();
|
if(userMap.size()>0) {
|
deskType = ConvertUtil.obj2StrBlank(userMap.get("deskTopType"));
|
}
|
return deskType;
|
}
|
|
/**
|
* 获取手机微信工程师userId
|
*/
|
public static String getEngineerUserId(HttpServletRequest request) {
|
Map userMap = new HashMap();
|
Object obj = request.getSession().getAttribute(SysConstants.WYW_ENGINEER_LOGIN_INFO_KEY);
|
if(null!=obj) {
|
userMap = (Map)obj;
|
}
|
String userId = new String();
|
if(userMap.size()>0) {
|
userId = ConvertUtil.obj2StrBlank(userMap.get("ID"));
|
}
|
return userId;
|
}
|
|
/**
|
* 取得当前用户微运维登录者的信息
|
*/
|
public static Map getWywLoginUser(HttpServletRequest request) {
|
return (Map)request.getSession().getAttribute(SysConstants.WYW_LOGIN_INFO_KEY);
|
}
|
|
/**
|
* 取得当前用户微运维当前登录人的ID
|
*/
|
public static String getUserWywId(HttpServletRequest request){
|
Map user = getWywLoginUser(request);
|
String userId = ConvertUtil.obj2StrBlank(user.get("ID"));
|
return userId;
|
}
|
|
/**
|
* 取得当前用户微运维当前登录的客户ID
|
*/
|
public static String getUserWywCusId(HttpServletRequest request) {
|
Map user = getWywLoginUser(request);
|
String cusId = ConvertUtil.obj2StrBlank(user.get("CUSTOMER_ID"));
|
return cusId;
|
}
|
|
/**
|
* 取得当前用户微运维当前登录人的openId
|
*/
|
public static String getUserWywOpenId(HttpServletRequest request) {
|
Map user = getWywLoginUser(request);
|
String openId = ConvertUtil.obj2StrBlank(user.get("WECHAT_OPEN_ID"));
|
return openId;
|
}
|
|
|
/**
|
* 清空手机微信用户session信息
|
*/
|
public static void removeCustomerMsg(HttpServletRequest request) {
|
HttpSession session = request.getSession();
|
session.removeAttribute(SysConstants.WYW_LOGIN_INFO_KEY);
|
session.invalidate();
|
}
|
|
/**
|
* 取得当前微运维工程师的信息
|
*/
|
public static Map getWywEngineerLoginUser(HttpServletRequest request) {
|
return (Map)request.getSession().getAttribute(SysConstants.WYW_ENGINEER_LOGIN_INFO_KEY);
|
}
|
|
//===================手机微信工程师session存放信息
|
|
}
|