OpenApiServiceImpl.java
4.37 KB
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
package com.glsx.service.impl;
import cn.com.glsx.openplatform.service.OpenApiService;
import cn.hutool.db.Page;
import com.glsx.base.aop.SysLog;
import com.glsx.base.controller.JwtService;
import com.glsx.constant.OpenAPIConstant;
import com.glsx.exception.ApiErrorMessage;
import com.glsx.exception.ApiException;
import com.glsx.model.BO.DataViewBo;
import com.glsx.model.BO.DeviceListBo;
import com.glsx.model.BO.TokenBo;
import com.glsx.model.BO.subscribeMessageBo;
import com.glsx.service.IOpenApiService;
import com.glsx.utils.MapUtils;
import com.glsx.utils.RSAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
/**
* @author fengzhi
* @version V1.0
* @Description: 开放平台业务层实现类
*/
@Service
public class OpenApiServiceImpl extends JwtService implements IOpenApiService {
/*
* 数据层服务
*/
@Autowired
OpenApiService openApiService;
/*
* 鉴权
*/
@SysLog
@Override
public String getToken(TokenBo tokenBo) {
//校验必传参数
TreeMap<String, String> params = MapUtils.transBean2Map(tokenBo);
logi("参数对象:" + params);
//获取私钥
Integer appId = tokenBo.getAppId();
//todo 测试参数,如果需要测试可以修改
String privateKey = openApiService.getPrivateSecret(appId);
//String privateKey = OpenAPIConstant.TEST_PRIVATE_KEY;
logi("getToken私钥======"+privateKey);
//获取公钥加密的密文
String sign = tokenBo.getSign();
String originalStr = "";
try {
//私钥解密获取明文
originalStr = RSAUtils.decrypt(sign, privateKey);
} catch (Exception e) {
throw ApiErrorMessage.ARGS_ERROR.exception("RSA解密错误==签名或私钥错误" + sign);
}
logi("获取明文:" + originalStr);
//除了sign以外,按照参数字母升序(参数+参数值)拼接
StringBuffer paramsStrbuf = new StringBuffer();
params.remove(OpenAPIConstant.PARAM_SIGN);
params.entrySet().stream().forEach(kv -> {
paramsStrbuf.append(kv.getKey()).append(kv.getValue());
});
String paramsStr = paramsStrbuf.toString();
//校验参数,防止篡改
if (!paramsStr.equals(originalStr)) {
throw ApiErrorMessage.ARGS_ERROR.exception("请求参数被篡改");
}
//根据appid查询用户信息,将客户信息转化为jwt
Map<String, Object> infos = openApiService.getCustomerInfo(appId);
logi("OpenApiServiceImpl===Userinfos===" + infos);
String jwtToken = jwtUtil.generateToken(infos);
return jwtToken;
}
/*
* 参数用公钥加密后的密文
*/
@SysLog
@Override
public Map<String, Object> getDeviceList(DeviceListBo deviceListBo) {
Page page = new Page(deviceListBo.getPage_no(), deviceListBo.getPage_size());
Date start_time = deviceListBo.getStartTime();
Date end_time = deviceListBo.getEndTime();
Integer appid = getCustomerID(deviceListBo);
Map<String, Object> res = openApiService.getDeviceList(appid, page, start_time, end_time);
return res;
}
@SysLog
@Override
public Map<String, Integer> getDataView(DataViewBo deviceStatusBo) {
Integer appid = getCustomerID(deviceStatusBo);
logi("OpenApiServiceImpl===getDataView==customerId:" + appid);
Map<String, Integer> res = openApiService.getDataView(appid, deviceStatusBo.getDevice_type());
return res;
}
@SysLog
@Override
public void subscribeMessage(subscribeMessageBo subscribeMessageBo) {
Integer appid = getCustomerID(subscribeMessageBo);
int msgID = subscribeMessageBo.getMsgid();
int deviceType = subscribeMessageBo.getDevice_type();
int method = subscribeMessageBo.getMethod();
String pushUrl = subscribeMessageBo.getPushurl();
Map<String, Object> res = openApiService.subscribeMessage(appid,msgID,deviceType,method,pushUrl);
boolean isSuccess= (boolean) res.get("success");
if(!isSuccess){
String msg = (String) res.get("msg");
logi("subscribeMessage=====Failed====msg:"+msg);
throw new ApiException(msg);
}
}
}