zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
package cn.autoform.web.interceptor.spring;
 
import cn.autoform.bean.system.UserInfo;
import cn.autoform.util.Des1;
import cn.autoform.util.thread.Keys;
import cn.autoform.util.thread.ThreadData;
import cn.autoform.util.tool.ObjectUtil;
import cn.autoform.web.interceptor.filter.CloneRequestFilter;
import cn.autoform.web.service.login.LoginService;
import com.codingapi.tx.aop.bean.TxCompensateLocal;
import com.codingapi.tx.aop.bean.TxTransactionLocal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.*;
 
/**
 * 初始化线程参数
 * @author WangYX
 *
 */
@Service("initParamInterceptor")
@Slf4j
public class InitParamInterceptor implements HandlerInterceptor{
    
    /**
     * 初始化参数key
     */
    private static final List<Keys> PARAM_KEY = new ArrayList<>();
    
    /**
     * 访问对外接口参数
     */
    public static final Map<String,Keys> COOKIE_KEY = new HashMap<>();
 
    @Resource
    private LoginService loginService;
    
    static{
        // 表单id
        PARAM_KEY.add(Keys.FORM_ID);
        // 租户id
        PARAM_KEY.add(Keys.TENANT_ID);
        // 页码
        PARAM_KEY.add(Keys.PAGE_NUM);
        // 页条数
        PARAM_KEY.add(Keys.PAGE_SIZE);
        // 数据存储标识
        PARAM_KEY.add(Keys.IO_FLAG);
        // tenantId
        COOKIE_KEY.put("tenantId",Keys.TENANT_ID);
        // userName
        COOKIE_KEY.put("userName",Keys.USER_NAME);
        // openId
        COOKIE_KEY.put("openId",Keys.OPEN_ID);
        // comanyId
        COOKIE_KEY.put("companyId",Keys.COMPANY_ID);
        // serviceId
        COOKIE_KEY.put("serviceId",Keys.SERVICE_ID);
        // token
        COOKIE_KEY.put("token",Keys.TOKEN);
        // dataSourceId
        COOKIE_KEY.put("dataSourceId",Keys.DATA_SOURCE_ID);
        // 国际化
        COOKIE_KEY.put("Language", Keys.USER_LANGUAGE);
    
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)    throws Exception {
        final CloneRequestFilter.BodyReaderHttpServletRequestWrapper requestWrapper = new CloneRequestFilter.BodyReaderHttpServletRequestWrapper(request);
        Map<String,String[]> paramMap = requestWrapper.getParameterMap();
        PARAM_KEY.stream().forEach(key->{
            String[] val = paramMap.get(key.getKey());
            if(ObjectUtil.empty(val) || val.length == 0)
                return;
            ThreadData.set(key
                    , key.format(val[0]));
        });
        //取出cookie添加到线程中
        Cookie[] cookie =  request.getCookies();
        if(cookie!=null){
            for(Cookie cook : cookie){
                if(!ObjectUtil.empty(COOKIE_KEY.get(cook.getName()))){
                    String str = URLDecoder.decode(cook.getValue(),"UTF-8").replaceAll(" ","+");
                    ThreadData.set(COOKIE_KEY.get(cook.getName()),Des1.desDecrypt(str,Des1.SECRETKEY));
                    if(Objects.equals("Language", cook.getName())){
                        com.iemsoft.framework.cloud.core.thread.ThreadData.set(
                                com.iemsoft.framework.cloud.core.thread.Keys.USER_LANGUAGE, Des1.desDecrypt(str,Des1.SECRETKEY)
                        );
                    }
                }
            }
        }
        loginService.getUserInfo(new UserInfo.LogInInfo(ThreadData.get(Keys.TOKEN),ThreadData.get(Keys.OPEN_ID)));
        return true;
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // 清空线程参数
        TxCompensateLocal.clear();
        TxTransactionLocal.clear();
        ThreadData.clear();
        com.iemsoft.framework.cloud.core.thread.ThreadData.clear();
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // 清空线程参数
        TxCompensateLocal.clear();
        TxTransactionLocal.clear();
        ThreadData.clear();
        com.iemsoft.framework.cloud.core.thread.ThreadData.clear();
    }
 
}