wuxw7
2019-03-20 81b89aecf17ec0bd5cac9caeb1963489e505b53b
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.java110.web.smo.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.common.cache.MappingCache;
import com.java110.common.constant.CommonConstant;
import com.java110.common.constant.MappingConstant;
import com.java110.common.constant.ResponseConstant;
import com.java110.common.constant.ServiceCodeConstant;
import com.java110.common.exception.SMOException;
import com.java110.common.util.DateUtil;
import com.java110.core.context.IPageData;
import com.java110.core.factory.AuthenticationFactory;
import com.java110.core.factory.DataTransactionFactory;
import com.java110.common.log.LoggerEngine;
import com.java110.common.util.Assert;
import com.java110.common.util.StringUtil;
import com.java110.web.smo.IConsoleServiceSMO;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.context.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 业务服务类
 * Created by wuxw on 2018/4/28.
 */
@Service("consoleServiceSMOImpl")
public class ConsoleServiceSMOImpl  implements IConsoleServiceSMO {
 
    private final static Logger logger = LoggerFactory.getLogger(ConsoleServiceSMOImpl.class);
 
    @Autowired
    private RestTemplate restTemplate;
 
 
 
    /**
     *  colModel 字段处理
     * @param jsonArray
     * @throws SMOException
     */
    private void paramColModelToJson(JSONArray jsonArray) throws SMOException{
        if(jsonArray == null || jsonArray.size() == 0){
            return ;
        }
        try {
            for(int arrIndex = 0; arrIndex < jsonArray.size();arrIndex ++){
                JSONObject tObj = jsonArray.getJSONObject(arrIndex);
                tObj.put("colModel",JSONObject.parse(tObj.getString("colModel")));
            }
        }catch (Exception e){
            logger.error("c_template_col 表 colModel 配置错误",e);
            throw new SMOException(ResponseConstant.RESULT_CODE_CONFIG_ERROR,"colModel 配置错误");
        }
    }
 
    /**
     * 删除BUTTON 字段 值
     * @param jsonObject
     */
    private void removeButtonName(JSONObject jsonObject){
        JSONArray template = jsonObject.getJSONArray("template");
        for(int colIndex = 0 ; colIndex < template.size() ; colIndex ++){
            if(CommonConstant.TEMPLATE_COLUMN_NAME_BUTTON.equals(template.getJSONObject(colIndex).getString("colName"))){
                template.getJSONObject(colIndex).put("colName","");
            }
        }
    }
 
    private JSONObject doExecute(Map<String,Object> paramIn) {
        //获取组件
        String appId = MappingCache.getValue(MappingConstant.KEY_CONSOLE_SERVICE_APP_ID);
 
        Assert.hasLength(appId, "组件不能为空");
 
        String apiServiceUrl = MappingCache.getValue(MappingConstant.KEY_API_SERVICE_URL);
 
        Assert.hasLength(apiServiceUrl, "api服务器地址没有配置");
 
        String securityCode = MappingCache.getValue(MappingConstant.KEY_CONSOLE_SECURITY_CODE);
        Assert.hasLength(securityCode, "签名秘钥没有配置");
 
        String serviceCode = paramIn.get(ServiceCodeConstant.SERVICE_CODE).toString();
        if(paramIn.containsKey(ServiceCodeConstant.SERVICE_CODE)){
            paramIn.remove(ServiceCodeConstant.SERVICE_CODE);
        }
        if(paramIn.containsKey(ServiceCodeConstant.SERVICE_CODE_NAME)){
            paramIn.remove(ServiceCodeConstant.SERVICE_CODE_NAME);
        }
 
        if(paramIn.containsKey(CommonConstant.ORDER_USER_ID)){
            paramIn.remove(CommonConstant.ORDER_USER_ID);
        }
        String params = "?";
        for(String key : paramIn.keySet()){
            params += (key +"=" + paramIn.get(key)+"&");
        }
        params = params.lastIndexOf("&") >-1 ? params.substring(0,params.length()-1):params;
        apiServiceUrl = apiServiceUrl.replace("#serviceCode#",serviceCode) + params;
        ResponseEntity<String> responseEntity = null;
        if (MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_CONSOLE_SERVICE_SECURITY_ON_OFF))) {
            try {
//                requestBody = DataTransactionFactory.encrypt(requestBody, 2048);
//                //调用查询菜单信息
//                HttpHeaders header = new HttpHeaders();
//                header.add(CommonConstant.ENCRYPT, MappingConstant.VALUE_ON);
//                header.add(CommonConstant.ENCRYPT_KEY_SIZE, "2048");
//                HttpEntity<String> httpEntity = new HttpEntity<String>(requestBody, header);
//                responseMsg = restTemplate.postForObject(centerServiceUrl, httpEntity, String.class);
//                responseMsg = DataTransactionFactory.decrypt(responseMsg, 2048);
            }catch (Exception e){
                logger.error("调用接口失败",e);
                throw new SMOException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"调用接口失败"+e);
            }
        } else {
            HttpHeaders header = new HttpHeaders();
            header.add(CommonConstant.HTTP_APP_ID, appId);
            header.add(CommonConstant.HTTP_TRANSACTION_ID, GenerateCodeFactory.getTransactionId());
            header.add(CommonConstant.HTTP_REQ_TIME, DateUtil.getNowDefault());
            header.add(CommonConstant.HTTP_SIGN, "");
            HttpEntity<String> httpEntity = new HttpEntity<String>(header);
            try {
                responseEntity = restTemplate.exchange(apiServiceUrl, HttpMethod.GET, httpEntity, String.class);
            }catch (HttpClientErrorException e){
                //responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(),e.getStatusCode());
 
                return null;
            }
        }
 
        Assert.isJsonObject(responseEntity.getBody(),"下游系统返回报文不是有效的json格式");
 
        return JSONObject.parseObject(responseEntity.getBody());
    }
 
 
 
    public RestTemplate getRestTemplate() {
        return restTemplate;
    }
 
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
 
    @Override
    public void login(IPageData pd) throws SMOException {
 
    }
}