add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.dobbinsoft.fw.support.component.open;
 
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import com.alibaba.fastjson.JSONObject;
import com.dobbinsoft.fw.support.component.open.exception.OpenPlatformException;
import com.dobbinsoft.fw.support.component.open.model.*;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
 
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * ClassName: AbstractOpenPlatform
 * Description: 一个抽象的开放平台,定义了抽象的功能,持久层方案,由业务层决定
 *
 * @author: e-weichaozheng
 * @date: 2021-04-23
 */
@Slf4j
public class OpenPlatform {
 
    @Autowired
    protected OpenPlatformStorageStrategy openPlatformStorageStrategy;
 
    private OkHttpClient okHttpClient = new OkHttpClient();
 
    private ThreadLocal<String> clientCodeThreadLocal = new ThreadLocal<>();
 
 
    public void setClientCode(String clientCode) {
        clientCodeThreadLocal.set(clientCode);
    }
 
    public String getClientCode() {
        return clientCodeThreadLocal.get();
    }
 
    public void removeClientCode() {
        clientCodeThreadLocal.remove();
    }
 
    /**
     * 初始化客户端
     *
     * @param client
     * @return
     */
    public OPClient init(OPClient client) throws OpenPlatformException {
        // 1. 校验Code是否重复
        if (StrUtil.isEmpty(client.getCode())) {
            throw new OpenPlatformException("客户代号不能为空");
        }
        if (openPlatformStorageStrategy.checkClientExists(client.getCode())) {
            throw new OpenPlatformException("客户代号重复,请换一个");
        }
        // 2. 持久化
        OPClient opClient = openPlatformStorageStrategy.initClient(client.getCode(), client.getNotifyUrl(), client.getPublicKey2());
        opClient.setPrivateKey1(null);
        return opClient;
    }
 
    public Boolean setPublicKey(String clientCode, String publicKey) {
        if (StrUtil.isEmpty(clientCode)) {
            throw new OpenPlatformException("客户代号不能为空");
        }
        if (StrUtil.isEmpty(publicKey)) {
            throw new OpenPlatformException("公钥不能为空");
        }
        return openPlatformStorageStrategy.setClientPublicKey(clientCode, publicKey);
    }
 
    public Boolean setNotifyUrl(String clientCode, String notifyUrl) {
        if (StrUtil.isEmpty(clientCode)) {
            throw new OpenPlatformException("客户代号不能为空");
        }
        if (StrUtil.isEmpty(notifyUrl)) {
            throw new OpenPlatformException("回调地址不能为空");
        }
        return openPlatformStorageStrategy.setClientNotifyUrl(clientCode, notifyUrl);
    }
 
    public OPClient getClient(String clientCode) {
        OPClient client = openPlatformStorageStrategy.getClient(clientCode);
        if (client == null) {
            throw new OpenPlatformException("客户代号并不存在");
        }
        return client;
    }
 
    /**
     * 获取客户权限列表
     *
     * @param clientCode
     * @return
     */
    public Boolean setPermissionList(String clientCode, List<OPClientPermission> permissions) {
        if (StrUtil.isEmpty(clientCode)) {
            throw new OpenPlatformException("客户代号不能为空");
        }
        return openPlatformStorageStrategy.setClientPermissionList(clientCode, permissions);
    }
 
    public Boolean checkApiPermission(String clientCode, String group, String method) {
        if (StrUtil.isEmpty(clientCode)) {
            throw new OpenPlatformException("客户代号不能为空");
        }
        return openPlatformStorageStrategy.checkApiPermission(clientCode, group, method);
    }
 
    public Map<String, String[]> decrypt(String clientCode, String ciphertext) {
        String publicKey2 = openPlatformStorageStrategy.getClient(clientCode).getPublicKey2();
        String parameterMapStr = new String(SecureUtil.rsa(null, publicKey2).decrypt(ciphertext, KeyType.PublicKey), StandardCharsets.UTF_8);
        String[] pairs = parameterMapStr.split("&");
        Map<String, String[]> map = new HashMap<>();
        for (String v : pairs) {
            if (!StringUtils.isEmpty(v)) {
                String[] pair = v.split("=");
                if (pair.length > 1 && !StringUtils.isEmpty(pair[0])) {
                    map.put(pair[0], new String[] {pair[1]});
                }
            }
        }
        return map;
    }
 
 
    /**
     * 向客户回调地址发送请求
     *
     * @param clientCode
     * @param params     请求参数
     */
    public void sendNotify(String clientCode, Map<String, String> params) {
        params.put("opversion", UUID.randomUUID().toString());
        List<String> paramsRawList = params.keySet().stream().map(item -> (item + "=" + params.get(item))).collect(Collectors.toList());
        OPNotify opNotify = new OPNotify();
        opNotify.setParams(paramsRawList);
        opNotify.setClientCode(clientCode);
        opNotify.setTimes(1);
        int status = this.sendNotify(opNotify);
        opNotify.setStatus(status);
        if (status == 0) {
            opNotify.setNextNotify(new Date(System.currentTimeMillis() + 3000l));
        } else {
            opNotify.setNextNotify(new Date());
        }
        openPlatformStorageStrategy.storeNotify(opNotify);
 
    }
 
    public int sendNotify(OPNotify opNotify) {
        try {
            opNotify.getParams().add("timestamp=" + System.currentTimeMillis());
            opNotify.getParams().sort(String::compareTo);
            // 发送一次Http请求
            String rawBody = opNotify.getParams().stream().collect(Collectors.joining("&"));
            OPData opData = new OPData();
            opData.setClientCode(opNotify.getClientCode());
            OPClient clientWithPK = openPlatformStorageStrategy.getClientWithPK(opNotify.getClientCode());
            opData.setCiphertext(SecureUtil.rsa(clientWithPK.getPrivateKey1(),
                    clientWithPK.getPublicKey1()).encryptBase64(rawBody, KeyType.PrivateKey));
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, JSONObject.toJSONString(opData));
            Response responseBody = okHttpClient.newCall(new Request
                    .Builder()
                    .url(clientWithPK.getNotifyUrl())
                    .post(body).build())
                    .execute();
            if (responseBody.code() == 200) {
                return OPNotifyStatusType.OK.getCode();
            } else {
                return OPNotifyStatusType.FAIL.getCode();
            }
        } catch (IOException e) {
            return OPNotifyStatusType.FAIL.getCode();
        } catch (Exception e) {
            log.error("[发送通知] 异常", e);
            return OPNotifyStatusType.FAIL.getCode();
        }
    }
 
}