package com.qxueyou.scc.base.util;
|
|
import java.util.List;
|
import java.util.Map;
|
|
import com.qxueyou.scc.base.dao.AbstractRedisTemplate;
|
import com.qxueyou.scc.base.dao.CommonRedisTemplate;
|
import com.qxueyou.scc.base.model.UserInfoWrapper;
|
import com.qxueyou.scc.base.service.impl.RedisCacheBean;
|
import com.qxueyou.scc.sys.utils.SpringContextHolder;
|
import org.apache.commons.lang3.StringUtils;
|
|
import com.qxueyou.scc.base.model.Constants;
|
import com.qxueyou.scc.base.model.FilePathConstants;
|
import com.qxueyou.scc.sys.model.ResponseResult;
|
import com.qxueyou.scc.sys.model.SysMenu;
|
import com.qxueyou.scc.user.model.User;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.cache.RedisCache;
|
import org.springframework.data.redis.cache.RedisCacheManager;
|
import org.springframework.data.redis.core.RedisAccessor;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Repository;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpSession;
|
|
|
/**
|
* ¿Í»§¶ËÐÅÏ¢´¦Àí¹¤¾ßÀà
|
* @author µÂ»¢ 2014-12-29
|
*
|
*/
|
public final class ClientUtils {
|
|
/*
|
* Ï̱߳äÁ¿: Óû§ÐÅÏ¢
|
*/
|
private static ThreadLocal<UserInfoWrapper> threadLocalUserInfo = new ThreadLocal<UserInfoWrapper>();
|
private static UserInfoWrapper wrapper = threadLocalUserInfo.get();
|
|
private static RedisTemplate redisTemplate = (RedisTemplate)SpringContextHolder.getBean("redisTemplate");
|
|
/** µÃµ½Óû§ÐÅÏ¢*/
|
public static UserInfoWrapper getUserInfo() {
|
// UserInfoWrapper wrapper = threadLocalUserInfo.get();
|
// if(wrapper == null) {
|
wrapper = (UserInfoWrapper)redisTemplate.opsForValue().get(UserInfoWrapper.SESSION_USER_INFO_KEY);
|
if (wrapper == null) {
|
wrapper = new UserInfoWrapper();
|
User user = new User();
|
//ĬÈÏÖµ£¬ÉÏ´«¿ÉÒÔ×÷ΪĬÈÏÉÏ´«Õß
|
user.setUserId(Constants.VISITOR_USER_ID);
|
user.setName(Constants.VISITOR_USER_ID);
|
wrapper.setUser(user);
|
setUserInfo(wrapper);
|
}
|
// }
|
// else{
|
// redisTemplate.opsForValue().set(UserInfoWrapper.SESSION_USER_INFO_KEY,wrapper);
|
// }
|
|
return wrapper;
|
}
|
|
|
/** ÉèÖÃÓû§ÐÅÏ¢*/
|
public static void setUserInfo(UserInfoWrapper wrapper) {
|
threadLocalUserInfo.remove();
|
threadLocalUserInfo.set(wrapper);
|
}
|
|
/** µÃµ½userId*/
|
public static String getUserId(){
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
HttpSession session = request.getSession();
|
String info = getUserInfo().getInfo(UserInfoWrapper.INF_USER_ID);
|
if(info.equals("visitor_001")){
|
info= (String) session.getAttribute(UserInfoWrapper.INF_USER_ID);
|
}
|
return info;
|
}
|
|
/** µÃµ½userName*/
|
public static String getUserName(){
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
HttpSession session = request.getSession();
|
String info = getUserInfo().getInfo(UserInfoWrapper.INF_USER_NAME);
|
// if(info!=null){
|
// info= (String) session.getAttribute(UserInfoWrapper.INF_USER_NAME);
|
// }
|
return info;
|
}
|
|
/** µÃµ½Óû§Í·Ïñ*/
|
public static String getUserImg(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_USER_HEAD_IMG);
|
// return session.getAttribute();
|
}
|
|
/** µÃµ½ÊÖ»úºÅ*/
|
public static String getUserMobilePhone(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_USER_MOBILE_PHONE);
|
}
|
|
/** µÃµ½ÕʺÅ*/
|
public static String getUserAccount(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_USER_ACCOUNT);
|
}
|
|
/** µÃµ½classId*/
|
public static String getClassId(){
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
HttpSession session = request.getSession();
|
String info = getUserInfo().getInfo(UserInfoWrapper.INF_CLASS_ID);
|
// if(info!=null){
|
// info= (String) session.getAttribute(UserInfoWrapper.INF_CLASS_ID);
|
// }
|
return info;
|
// return getUserInfo().getInfo(UserInfoWrapper.);
|
}
|
|
/** µÃµ½className*/
|
public static String getClassName(){
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
HttpSession session = request.getSession();
|
String info = getUserInfo().getInfo(UserInfoWrapper.INF_CLASS_NAME);
|
// if(info!=null){
|
// info= (String) session.getAttribute(UserInfoWrapper.INF_CLASS_NAME);
|
// }
|
return info;
|
// System.out.println("CLassName"+session.getAttribute("className"));
|
// return (String) session.getAttribute("className");
|
}
|
|
/** */
|
public static String getClassCharger(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_CLASS_CHARGER);
|
}
|
|
/** µÃµ½¿ÆÄ¿ID*/
|
public static String getCourseId(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_COURSE_ID);
|
}
|
|
/** µÃµ½¿ÆÄ¿Name*/
|
public static String getCourseName(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_COURSE_NAME);
|
}
|
|
/** µÃµ½»ú¹¹ID*/
|
public static String getOrgId(){
|
return "1";
|
}
|
|
/** µÃµ½×îÉϲã»ú¹¹ID*/
|
public static String getTopOrgId(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_TOP_ORG_ID);
|
}
|
|
/** µÃµ½»ú¹¹CODE*/
|
public static String getOrgCode(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_ORG_CODE);
|
}
|
|
/** µÃµ½»ú¹¹NAME*/
|
public static String getOrgName(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_ORG_NAME);
|
}
|
|
/** µÃµ½»ú¹¹SHORTNAME*/
|
public static String getOrgShortName(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_ORG_SHORT_NAME);
|
}
|
|
/** µÃµ½»ú¹¹LOGO*/
|
public static String getOrgLogoPath(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_ORG_LOGO_PATH);
|
}
|
|
/** µÃµ½µ±Ç°½ÇÉ«*/
|
public static String getCurrentRole(){
|
return getUserInfo().getCurrentRole();
|
}
|
|
|
/** µÃµ½µ±Ç°½ÇÉ«±àÂë*/
|
public static String getCurrentRoleEName(){
|
return getUserInfo().getInfo(UserInfoWrapper.INF_ROLE_ENAME);
|
}
|
|
/** µÃµ½µ±Ç°½ÇÉ«ID*/
|
public static String getCurrentRoleId(){
|
return getUserInfo().getCurrentRoleId();
|
}
|
|
/** µÃµ½µ±Ç°½Çɫֵ*/
|
public static String getCurrentRoleValue(){
|
return getUserInfo().getCurrentRoleValue();
|
}
|
|
/** µÃµ½µ±Ç°½ÇÉ«ÐÅÏ¢*/
|
public static List<Map<String,Object>> getRoleInfo(String role){
|
return getUserInfo().getRoleInfo(role);
|
}
|
|
/** »ñÈ¡µ±Ç°¾ÍÊÇ ²Ëµ¥ÐÅÏ¢*/
|
public static List<SysMenu> getMenuInfo(){
|
return getUserInfo().getMenus();
|
}
|
|
/** */
|
public static Boolean getButtonPrivilegeEdit(){
|
return getUserInfo().getButtonPrivilegeEdit();
|
}
|
|
/** µ±Ç°µÇ¼IP*/
|
public static String getCurrentLoginIp(){
|
return getUserInfo().getCurrentLoginIp();
|
}
|
|
/**
|
* »ñÈ¡Óû§»ù±¾ÐÅÏ¢
|
* @return
|
*/
|
public static ResponseResult getUserBaseInfo(User user){
|
ResponseResult userInfo = new ResponseResult();
|
// Óû§
|
userInfo.setUserId(user.getUserId());
|
userInfo.setUserName(user.getName());
|
userInfo.setImgPath(StringUtils.isNotBlank(user.getImgPath()) ? user.getImgPath() : FilePathConstants.USER_DEFAULT_IMG );
|
userInfo.setMobilePhone(user.getMobilePhone());
|
return userInfo;
|
}
|
|
/**
|
* ÊÇ·ñΪ¹ÜÀíÔ±
|
* @return
|
*/
|
public static boolean isAdmin(){
|
HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
HttpSession session = request.getSession();
|
Object admin =session.getAttribute("admin");
|
// if(Boolean.valueOf((Boolean) admin)){
|
// System.out.println(Boolean.valueOf((Boolean) admin));
|
// return Boolean.valueOf((Boolean) admin);
|
// }else{
|
// return false;
|
// }
|
return true;
|
// System.out.println(Boolean.valueOf((Boolean) admin));
|
// return Boolean.valueOf((Boolean) admin);
|
}
|
}
|