jiazx0107@163.com
2023-10-24 f2cacfb5c8760d2b9b539821cc9e13054dbc1320
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
package com.fzzy.gateway.sc2023.api;
 
import com.fzzy.api.data.AuthToken;
import com.fzzy.api.data.PushProtocol;
import com.fzzy.api.entity.ApiLog;
import com.fzzy.api.utils.ContextUtil;
import com.fzzy.api.utils.RedisConst;
import com.fzzy.api.utils.RedisUtil;
import com.fzzy.api.view.repository.ApiLogRep;
import com.fzzy.gateway.data.GatewayResponse;
import com.fzzy.gateway.entity.GatewayConf;
import com.fzzy.gateway.sc2023.ScConstant;
import com.fzzy.push.gb2022.HttpClientUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
 
 
@Slf4j
@Data
@Service
public class ScGatewayRemoteService implements GatewayRemoteService {
 
 
    @Resource
    private ApiLogRep apiLogRep;
    @Resource
    private RedisUtil redisUtil;
 
 
    @Override
    public String getProtocol() {
        return PushProtocol.GATEWAY_SC_2023.getCode();
    }
 
    @Override
    public GatewayResponse authorize(GatewayConf conf) {
 
        //添加LOG
        ApiLog apiLog = new ApiLog();
        apiLog.setData("鉴权接口");
        apiLog.setId(ContextUtil.getUUID());
        apiLog.setKqdm(conf.getKqdm());
 
        try {
 
            Map<String, Object> map = new HashMap<>();
            map.put("username", conf.getUserName());
            map.put("username", conf.getPassword());
 
            log.debug("-----------------数据报文----------------{}", map);
 
 
            String url = conf.getApiUrl() + ScConstant.API_URL_AUTH;
 
 
            GatewayResponse responseDto = HttpClientUtil.pushGateway(url, map);
            apiLog.setStatus(responseDto.getStatus());
            apiLog.setResult(responseDto.getMessage());
            apiLogRep.save(apiLog);
 
            updateAuthToken(responseDto, conf);
 
            return responseDto;
        } catch (Exception e) {
            apiLog.setStatus(99);
            apiLog.setResult("失败:" + e.getMessage());
            apiLogRep.save(apiLog);
            log.error(e.getMessage(), e);
            return new GatewayResponse(99, e.getMessage());
        }
    }
 
    /**
     * @param kqdm
     * @return
     */
    public AuthToken getAuthToken(String kqdm) {
        try {
            String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, kqdm);
 
            AuthToken token = (AuthToken) redisUtil.get(key);
 
            if (null == token) {
                log.error("------------------未获取到TOKEN---------------");
                return null;
            }
 
            return token;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
 
    private void updateAuthToken(GatewayResponse dto, GatewayConf conf) {
        String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, conf.getKqdm());
        AuthToken token = getAuthToken(conf.getKqdm());
 
        if (null == token) {
            token = new AuthToken();
            token.setKqdm(conf.getKqdm());
        }
 
        if (null != dto.getResult()) {
            token.setToken(dto.getResult().getToken());
        }
 
        redisUtil.set(key, token);
    }
 
 
}