自动化立体仓库 - WMS系统
#
lty
4 天以前 026c1a5f0318a61e126c7062074eb4e7f9a712d5
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
package com.zy.asrs.task;
 
import com.alibaba.fastjson.JSONObject;
import com.core.common.Cools;
import com.zy.asrs.mapper.ErpTokenMapper;
import com.zy.common.utils.HttpHandler;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
@Slf4j
@Component
public class ErpTokenScheduler {
 
    @Autowired
    private ErpTokenMapper erpTokenMapper;
    @Autowired
    private ConfigService configService;
 
//    @Scheduled(fixedDelay = 7200 * 1000)
    public void refreshToken() {
        try {
            String appId = getConfigValue("ErpAppId", "jxlk");
            String appSecrect = getConfigValue("ErpAppSecrect", "jxlk123@jxlk");
            String url = getConfigValue("ErpTokenUrl", "http://127.0.0.1:8080/thb-cloud/rest");
 
            URI uri = URI.create(url);
            String host = uri.getHost();
            int port = uri.getPort();
            String scheme = uri.getScheme();
            String path = uri.getRawPath();
            if (Cools.isEmpty(host) || Cools.isEmpty(scheme) || Cools.isEmpty(path)) {
                log.error("ERP token接口地址配置错误:{}", url);
                return;
            }
            String base = host + (port > 0 ? ":" + port : "");
 
            Map<String, Object> params = new HashMap<>();
            params.put("funid", "rest_app");
            params.put("eventcode", "get_token");
            params.put("user_code", appId);
            params.put("appid", appId);
            params.put("appsecrect", appSecrect);
 
            String resp = new HttpHandler.Builder()
                    .setUri(base)
                    .setHttps("https".equalsIgnoreCase(scheme))
                    .setPath(path)
                    .setParams(params)
                    .build()
                    .doPost();
 
            if (Cools.isEmpty(resp)) {
                log.error("ERP token获取失败:空响应");
                return;
            }
            JSONObject result = JSONObject.parseObject(resp);
            Boolean success = result.getBoolean("success");
            if (!Boolean.TRUE.equals(success)) {
                log.error("ERP token获取失败:{}", result.getString("message"));
                return;
            }
            JSONObject data = result.getJSONObject("data");
            String token = data == null ? null : data.getString("access_token");
            if (Cools.isEmpty(token)) {
                log.error("ERP token获取失败:响应未包含access_token");
                return;
            }
            Date now = new Date();
            erpTokenMapper.upsertToken(appId, appSecrect, token, now);
            log.info("ERP token刷新成功,appId={}, modiTime={}", appId, now);
        } catch (Exception e) {
            log.error("ERP token刷新异常", e);
        }
    }
 
    private String getConfigValue(String code, String defaultValue) {
        try {
            Config config = configService.selectConfigByCode(code);
            if (config != null && !Cools.isEmpty(config.getValue())) {
                return String.valueOf(config.getValue()).trim();
            }
        } catch (Exception ignored) {
        }
        return defaultValue;
    }
}