wuxw
2024-09-12 7935724516cb699835da888d59cd5b7bec5c6049
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.java110.job.mall;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.core.client.RestTemplate;
import com.java110.core.factory.AuthenticationFactory;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.db.dao.impl.QueryServiceDAOImpl;
import com.java110.dto.machine.MachineTranslateErrorDto;
import com.java110.intf.common.IMachineTranslateErrorInnerServiceSMO;
import com.java110.po.machine.MachineTranslateErrorPo;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.CommonConstant;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
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.HttpStatusCodeException;
 
import java.util.Date;
import java.util.UUID;
 
@Service
public class SendMallImpl implements ISendMall {
    private final static Logger logger = LoggerFactory.getLogger(SendMallImpl.class);
 
    public static final String GET_TOKEN_URL = "/mall/api/login.pcUserLogin";
    private static final String DEFAULT_MALL_URL = "https://mall.homecommunity.cn";
    public static final String MALL_DOMAIN = "MALL"; // 物联网域
 
    public static final String MALL_TOKEN = "MALL_ACCESS_TOKEN";
    private static final String MALL_URL = "MALL_URL";
 
 
    @Autowired
    private RestTemplate outRestTemplate;
 
    @Autowired
    private IMachineTranslateErrorInnerServiceSMO machineTranslateErrorInnerServiceSMOImpl;
 
 
    @Override
    public ResultVo get(String url) {
        url = getUrl(url);
        HttpHeaders header = getHeaders(url, "", HttpMethod.GET);
        HttpEntity<String> httpEntity = new HttpEntity<String>("", header);
        ResponseEntity<String> tokenRes = null;
        try {
            tokenRes = outRestTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
        } catch (HttpStatusCodeException e) {
            logger.error("调用异常", e);
            return new ResultVo(ResultVo.CODE_ERROR, e.getResponseBodyAsString());
        } catch (Exception e) {
            logger.error("调用异常", e);
            return new ResultVo(ResultVo.CODE_ERROR, e.getMessage());
        }
 
        String body = tokenRes.getBody();
        JSONObject paramOut = JSONObject.parseObject(body);
 
        return new ResultVo(paramOut.getIntValue("code"), paramOut.getString("msg"), paramOut.get("data"));
    }
 
    @Override
    public ResultVo post(String url, JSONObject paramIn) {
        url = getUrl(url);
        HttpHeaders header = getHeaders(url, paramIn.toJSONString(), HttpMethod.POST);
        HttpEntity<String> httpEntity = new HttpEntity<String>(paramIn.toJSONString(), header);
        ResponseEntity<String> tokenRes = null;
        try {
            tokenRes = outRestTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        } catch (HttpStatusCodeException e) {
            logger.error("调用异常", e);
            return new ResultVo(ResultVo.CODE_ERROR, e.getResponseBodyAsString());
        } catch (Exception e) {
            logger.error("调用异常", e);
            return new ResultVo(ResultVo.CODE_ERROR, e.getMessage());
        }
        String body = tokenRes.getBody();
        JSONObject paramOut = JSONObject.parseObject(body);
 
        if (paramOut.getIntValue("code") != ResultVo.CODE_OK) {
            saveTranslateError(paramIn.getString("communityId"), paramIn.toJSONString(), body, url);
        }
 
        int total = 1;
        int records = 1;
        if (paramOut.containsKey("total")) {
            total = paramOut.getIntValue("total");
        }
 
        if (paramOut.containsKey("records")) {
            records = paramOut.getIntValue("records");
        }
 
        return new ResultVo(records, total, paramOut.getIntValue("code"), paramOut.getString("msg"), paramOut.get("data"));
    }
 
 
    public void saveTranslateError(String communityId, String reqJson, String resJson, String url) {
        MachineTranslateErrorPo machineTranslateErrorPo = new MachineTranslateErrorPo();
        machineTranslateErrorPo.setLogId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_logId));
        machineTranslateErrorPo.setCommunityId(communityId);
        machineTranslateErrorPo.setMachineTranslateId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_machineTranslateId));
        machineTranslateErrorPo.setReqBody(reqJson);
        machineTranslateErrorPo.setReqHeader("");
        machineTranslateErrorPo.setResBody(resJson);
        machineTranslateErrorPo.setReqPath(url);
        machineTranslateErrorPo.setCommunityId("-1");
        machineTranslateErrorPo.setReqType(MachineTranslateErrorDto.REQ_TYPE_URL);
        machineTranslateErrorInnerServiceSMOImpl.saveMachineTranslateError(machineTranslateErrorPo);
    }
 
 
    /**
     * 封装头信息
     *
     * @return
     */
    private HttpHeaders getHeaders(String url, String param, HttpMethod method) {
        HttpHeaders header = new HttpHeaders();
        header.add(CommonConstant.APP_ID.toLowerCase(), MappingCache.getValue(MALL_DOMAIN, "APP_ID"));
        header.add(CommonConstant.USER_ID.toLowerCase(), CommonConstant.ORDER_DEFAULT_USER_ID);
        header.add(CommonConstant.TRANSACTION_ID.toLowerCase(), UUID.randomUUID().toString());
        header.add(CommonConstant.REQUEST_TIME.toLowerCase(), DateUtil.getDefaultFormateTimeString(new Date()));
        createSign(header, method, url, param);
        header.add("Authorization", "Bearer " + getToken(false));
        return header;
    }
 
    public String getToken(boolean refreshAccessToken) {
        String token = CommonCache.getValue(MALL_TOKEN);
        if (!StringUtil.isEmpty(token) && !refreshAccessToken) {
            return token;
        }
 
        String url = getUrl(GET_TOKEN_URL);
 
        String userName = MappingCache.getValue(MALL_DOMAIN, "MALL_USERNAME");
        String password = MappingCache.getValue(MALL_DOMAIN, "MALL_PASSWORD");
        JSONObject param = new JSONObject();
        param.put("username", userName);
        param.put("passwd", password);
 
        HttpHeaders header = new HttpHeaders();
        header.add(CommonConstant.APP_ID.toLowerCase(), MappingCache.getValue(MALL_DOMAIN, "APP_ID"));
        header.add(CommonConstant.USER_ID.toLowerCase(), CommonConstant.ORDER_DEFAULT_USER_ID);
        header.add(CommonConstant.TRANSACTION_ID.toLowerCase(), UUID.randomUUID().toString());
        header.add(CommonConstant.REQUEST_TIME.toLowerCase(), DateUtil.getDefaultFormateTimeString(new Date()));
        createSign(header, HttpMethod.POST, url, param.toJSONString());
        HttpEntity<String> httpEntity = new HttpEntity<String>(param.toJSONString(), header);
 
        ResponseEntity<String> tokenRes = outRestTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
 
 
        JSONObject tokenObj = JSONObject.parseObject(tokenRes.getBody());
        if (tokenObj.getIntValue("code") != 0) {
            throw new IllegalArgumentException("获取token失败" + tokenRes.getBody());
        }
        if (!tokenObj.containsKey("token")) {
            throw new IllegalArgumentException("获取token失败" + tokenRes.getBody());
        }
 
        token = tokenObj.getString("token");
        int expiresIn = 30 * 60; //todo 30分钟
 
        CommonCache.setValue(MALL_TOKEN, token, expiresIn - 200);
 
        return token;
    }
 
    private static String getUrl(String param) {
        String url = MappingCache.getValue(MALL_DOMAIN, MALL_URL);
 
        if (StringUtil.isEmpty(url)) {
            return DEFAULT_MALL_URL + param;
        }
 
        return url + param;
    }
 
    /**
     * 创建鉴权
     *
     * @param headers
     * @param httpMethod
     * @param url
     * @param param
     */
    public static void createSign(HttpHeaders headers, HttpMethod httpMethod, String url, String param) {
        String appId = headers.getFirst(CommonConstant.APP_ID);
        String transactionId = headers.getFirst(CommonConstant.TRANSACTION_ID);
        String requestTime = headers.getFirst(CommonConstant.REQUEST_TIME);
        String securityCode = MappingCache.getValue(MALL_DOMAIN, "APP_SECRET");
 
        if (StringUtil.isEmpty(securityCode)) {
            return;
        }
        String paramStr = "";
        if (HttpMethod.GET == httpMethod) {
            paramStr = url.substring(url.indexOf("?"));
        } else {
            paramStr = param;
        }
        String sign = transactionId + requestTime + appId + paramStr + securityCode;
        headers.remove("sign");
        headers.add("sign", AuthenticationFactory.md5(sign));
    }
 
}