鞍钢鲅鱼圈能源管控系统后端代码
houzhongjian
2024-12-26 07073fa5e1e14b1f9d5d4f3253d9403ab311ae3c
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
package com.iailab.module.ansteel.client;
 
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.security.core.LoginUser;
import com.iailab.module.ansteel.client.dto.user.UserInfoRespDTO;
import com.iailab.module.ansteel.client.dto.user.UserUpdateReqDTO;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
 
/**
 * 用户 User 信息的客户端
 *
 * 对应调用 OAuth2UserController 接口
 */
@Component
public class UserClient {
 
    private static final String BASE_URL = "http://127.0.0.1:48080/admin-api/system/oauth2/user";
 
    //    @Resource // 可优化,注册一个 RestTemplate Bean,然后注入
    private final RestTemplate restTemplate = new RestTemplate();
 
    public CommonResult<UserInfoRespDTO> getUser() {
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.set("tenant-id", OAuth2Client.TENANT_ID.toString());
        addTokenHeader(headers);
        // 1.2 构建请求参数
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
 
        // 2. 执行请求
        ResponseEntity<CommonResult<UserInfoRespDTO>> exchange = restTemplate.exchange(
                BASE_URL + "/get",
                HttpMethod.GET,
                new HttpEntity<>(body, headers),
                new ParameterizedTypeReference<CommonResult<UserInfoRespDTO>>() {}); // 解决 CommonResult 的泛型丢失
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        return exchange.getBody();
    }
 
    public CommonResult<Boolean> updateUser(UserUpdateReqDTO updateReqDTO) {
        // 1.1 构建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("tenant-id", OAuth2Client.TENANT_ID.toString());
        addTokenHeader(headers);
        // 1.2 构建请求参数
        // 使用 updateReqDTO 即可
 
        // 2. 执行请求
        ResponseEntity<CommonResult<Boolean>> exchange = restTemplate.exchange(
                BASE_URL + "/update",
                HttpMethod.PUT,
                new HttpEntity<>(updateReqDTO, headers),
                new ParameterizedTypeReference<CommonResult<Boolean>>() {}); // 解决 CommonResult 的泛型丢失
        Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
        return exchange.getBody();
    }
 
 
    private static void addTokenHeader(HttpHeaders headers) {
        LoginUser loginUser = getLoginUser();
        Assert.notNull(loginUser, "登录用户不能为空");
        headers.add("Authorization", "Bearer " + loginUser.getAccessToken());
    }
}