package com.lf.server.service.sys;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.lf.server.entity.all.StaticData;
|
import com.lf.server.helper.RestHelper;
|
import com.lf.server.helper.SpringContextHelper;
|
import com.lf.server.helper.StringHelper;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* 通用服务类
|
* @author WWW
|
*/
|
@Service("commonService")
|
public class CommonService {
|
@Autowired
|
private RedisService redisService;
|
|
/**
|
* 获取请求
|
*
|
* @return HttpServletRequest
|
*/
|
public HttpServletRequest getRequest() {
|
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
}
|
|
/**
|
* 获取响应
|
*
|
* @return HttpServletResponse
|
*/
|
public HttpServletResponse getResponse() {
|
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
|
}
|
|
/**
|
* 获取请求Uri
|
*
|
* @return Uri
|
*/
|
public String getRequestUri() {
|
return this.getRequest().getRequestURI();
|
}
|
|
/**
|
* 获取最终的URL
|
*
|
* @param mainUrl 主Url
|
* @return 最终Url
|
*/
|
public String getLastUrl(String mainUrl) {
|
String queryString = this.getRequest().getQueryString();
|
if (StringHelper.isNull(queryString)) {
|
return mainUrl;
|
}
|
|
return String.format("%s?%s", mainUrl, queryString);
|
}
|
|
/**
|
* 是/否为开发环境
|
*
|
* @return 是/否
|
*/
|
public boolean isDevProfile() {
|
String profile = SpringContextHelper.getActiveProfile();
|
|
return "dev".equals(profile);
|
}
|
|
/**
|
* 检查令牌
|
*
|
* @param token 令牌
|
* @return 是/否有效
|
* @throws IOException
|
*/
|
public boolean checkToken(String token) throws IOException {
|
// http://a4.petrochina/sysservice/token/check?token=1d016abc-cf17-4801-9b5b-b43304e5bfe1
|
String url = "http://a4.petrochina/sysservice/" + "token/check?token=" + token;
|
|
String json = isDevProfile() ? RestHelper.get(url) : RestHelper.getForRest(url);
|
if (StringHelper.isEmpty(json)) {
|
return false;
|
}
|
|
JSONObject jo = JSONObject.parseObject(json);
|
Object code = jo.get("code");
|
|
return null != code && "200".equals(code.toString());
|
}
|
|
/**
|
* 设置缓存
|
*
|
* @param db 数据库
|
* @param tag 标识
|
* @param obj 对象
|
* @param timeout 缓存分钟数
|
*/
|
private void setCache(String db, String tag, Object obj, int timeout) {
|
String key = StaticData.CACHE_PREFIX + ":" + db + ":" + tag;
|
redisService.put(key, obj, timeout, TimeUnit.MINUTES);
|
}
|
|
/**
|
* 获取缓存
|
*
|
* @param db 数据库
|
* @param tag 标识
|
* @return 对象
|
*/
|
private Object getCache(String db, String tag) {
|
String key = StaticData.CACHE_PREFIX + ":" + db + ":" + tag;
|
if (redisService.hasKey(key)) {
|
return redisService.get(key);
|
}
|
|
return null;
|
}
|
|
/**
|
* 清空缓存
|
*/
|
public void clearCache() {
|
redisService.clearKeys(StaticData.CACHE_PREFIX);
|
}
|
}
|