import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
@RestController
@Slf4j
public class BaseController {
@Resource
public HttpServletRequest request;
@Resource
public HttpServletResponse response;
@Autowired
public ApplicationContext applicationContext;
@Autowired
private RedisTemplateUtil<String, String> redisConfig;
/**
* 获取登录token
*
* @return
*/
public String getToken() {
String token = request.getHeader("token");
if (StringUtils.isEmpty(token)) {
token = request.getParameter("token");
}
if (StringUtils.isEmpty(token)) {
// throw new NoveControllerException(ResultEnum.NO_TOKEN.getStrCode(), ResultEnum.NO_TOKEN.getDesc());
throw new RuntimeException(LoginEnum.NO_TOKEN.getDesc());
}
return token;
}
/**
* 得到登录用户信息
* @return
*/
public UserInfoDto getLoginUserInfo(){
//测试redis
// cacheObject("cp",111,60L,TimeUnit.SECONDS);
// String str = getCacheNew("cp");
String token = getToken();
String redisKey = RedisKeyEnum.UPMS_TOKEN_PREFIX.getKey();
redisKey = redisKey.concat(token);
String redisValue = getCacheNew(redisKey);
if(!StringUtils.isBlank(redisValue))
{
return JSONObject.parseObject(redisValue, UserInfoDto.class);
}
else
throw new RuntimeException(LoginEnum.LOGIN_OUT.getDesc());
}
/**
* 得到登录用户ID
* @return
*/
public Integer getLoginUserId(){
return getLoginUserInfo().getUserId();
}
/**
*
* @param * @param
* @return java.util.List<java.lang.Long>
* 获取公司列表
*/
public List<Long> getCompanyIds(){
String companyIds = getLoginUserInfo().getCompanyIds();
List<Long> companyIdList = NumberUtil.toLongList(companyIds, ",");
return companyIdList;
}
/**
*
* @param * @param
* @return java.util.List<java.lang.Long>
* 获取仓库列表
*/
public List<Long> getWarehouseIds(){
String storeIds = getLoginUserInfo().getStoreIds();
List<Long> warehouseIdList = NumberUtil.toLongList(storeIds, ",");
return warehouseIdList;
}
private String getCacheNew(String key) {
RedisTemplate<String, String> stringStringRedisTemplate = redisConfig.redisTemplate();
ValueOperations<String, String> stringOperations = stringStringRedisTemplate.opsForValue();
try {
String strObject = stringOperations.get(key);
return strObject;
} catch (Exception e) {
log.error("连接redis获取值出现错误." + key, e);
return null;
}
}
private void cacheObject(String key, Object obj, Long timeout, TimeUnit unit) {
ValueOperations<String, String> stringOperations = redisConfig.redisTemplate().opsForValue();
log.info("存入redis的数据{}", JSON.toJSONString(obj));
if(obj instanceof String){
stringOperations.set(key, obj.toString(), timeout, unit);
}else{
String jsonString = JSON.toJSONString(obj);
stringOperations.set(key, jsonString, timeout, unit);
}
}
}