管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-09-28 81e92195c3fb7f207f6b306a49b52591d24b92e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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);
    }
}