自动化立体仓库 - WMS系统
zwl
昨天 780ff45fdc167cadf4724c6c94530929b7445aab
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
package com.zy.system.timer;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.Cools;
import com.zy.common.utils.HttpHandler;
import com.zy.system.entity.LicenseInfos;
import com.zy.system.entity.license.LicenseUtils;
import com.zy.system.entity.license.LicenseVerify;
import com.zy.system.entity.license.LicenseVerifyParam;
import com.zy.system.service.LicenseInfosService;
import de.schlichtherle.license.LicenseContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.Date;
import java.util.HashMap;
 
@Component
public class LicenseTimer {
 
    private static boolean SYSTEM_SUPPORT = false;//系统激活状态,默认关闭
 
    private static int LICENSE_DAYS = 0;//许可证天数
 
    @Value("${license.subject}")
    private String subject;
 
    @Value("${license.publicAlias}")
    private String publicAlias;
 
    @Value("${license.storePass}")
    private String storePass;
 
    @Value("${license.publicKeysStorePath}")
    private String publicKeysStorePath;
 
    @Value("${license.remoteServerUrl:http://net.zoneyung.net:9999/license}")
    private String remoteServerUrl;
 
    @Autowired
    private LicenseInfosService licenseInfosService;
 
    //每天晚上11点更新系统激活状态
    @Scheduled(cron = "0 0 23 * * ? ")
    public void timer() {
        try {
            getRemoteLicense();
        } catch (Exception ignored) {
        }
 
        try {
            verify();
        } catch (Exception ignored) {
        }
    }
 
    public void getRemoteLicense() {
        try {
            String requestCode = LicenseUtils.buildRequestCode(subject);
            JSONObject response = requestRemoteLicense(buildRequestCodePayload(requestCode));
            if (isSuccess(response)) {
                String license = response.getString("data");
                if (Cools.isEmpty(license)) {
                    return;
                }
                LicenseInfos latestLicense = licenseInfosService.getLatestLicenseByRequestCode(requestCode);
                if (latestLicense != null && Cools.eq(latestLicense.getLicense(), license)) {
                    return;
                }
                LicenseInfos licenseInfos = new LicenseInfos();
                licenseInfos.setLicense(license);
                licenseInfos.setCreateTime(new Date());
                licenseInfos.setLicenseTime(response.getString("licenseTime"));
                licenseInfos.setRequestCode(requestCode);
                licenseInfosService.insert(licenseInfos);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private JSONObject requestRemoteLicense(String json) {
        try {
            String response = new HttpHandler.Builder()
                    .setUri(remoteServerUrl)
                    .setPath("/remoteQueryLicense")
                    .setJson(json)
                    .build()
                    .doPost();
            if (response == null || response.trim().isEmpty()) {
                return null;
            }
            return JSON.parseObject(response);
        } catch (Exception e) {
            return null;
        }
    }
 
    private String buildRequestCodePayload(String requestCode) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("subject", subject);
        map.put("requestCode", requestCode);
        return JSON.toJSONString(map);
    }
 
    private boolean isSuccess(JSONObject jsonObject) {
        return jsonObject != null && "ok".equalsIgnoreCase(jsonObject.getString("result"));
    }
 
    public void verify() {
        LicenseVerifyParam param = new LicenseVerifyParam();
        param.setSubject(subject);
        param.setPublicAlias(publicAlias);
        param.setStorePass(storePass);
        param.setPublicKeysStorePath(publicKeysStorePath);
 
        String requestCode = LicenseUtils.buildRequestCode(subject);
        LicenseInfos latestLicense = licenseInfosService.getLatestLicenseByRequestCode(requestCode);
 
        LicenseVerify licenseVerify = new LicenseVerify();
        LicenseContent install = null;
        if (latestLicense != null && !Cools.isEmpty(latestLicense.getLicense())) {
            install = licenseVerify.install(param, latestLicense.getLicense());
        }
 
        if (install != null) {
            Date start = new Date();
            Date end = install.getNotAfter();
            long num = end.getTime() - start.getTime();
            int day = (int) (num / 24 / 60 / 60 / 1000);
            setLicenseDays(day);
            setSystemSupport(true);
        } else {
            setLicenseDays(0);
            setSystemSupport(false);
        }
    }
 
    public boolean getSystemSupport() {
        return SYSTEM_SUPPORT;
    }
 
    public void setSystemSupport(boolean systemSupport) {
        SYSTEM_SUPPORT = systemSupport;
    }
 
    public int getLicenseDays() {
        return LICENSE_DAYS;
    }
 
    public void setLicenseDays(int licenseDays) {
        LICENSE_DAYS = licenseDays;
    }
 
}