zouyu
2025-09-24 2bb12b1ca40b29b7edcf06ef3f3d6de24dde1c4c
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
package com.ruoyi.common.utils.api;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
 
@Slf4j
@Component
public class MesApiUtils {
 
    @Value("${mes.ztzb.ip}")
    String ip;
 
    @Value("${mes.ztzb.user}")
    String user;
 
    @Value("${mes.ztzb.password}")
    String password;
 
    /**
     * 获取token请求链接
     * @return
     */
    private String getAuthTokenUrl() {
        return ip + "/auth/oauth/token?randomStr=blockPuzzle&grant_type=password";
    }
 
    /**
     * 新增实时库存请求链接
     * @return
     */
    private String getBatchAddStockUrl(){
        return ip + "/mes/stock/batchAddStock";
    }
 
    /**
     * 获取mes系统token
     * @return 接口响应结果
     */
    public String getToken(){
        try{
            Map<String,Object> bodyMap = new HashMap<>();
            bodyMap.put("username",user);
            String bodyStr = "username="+user+"&password="+password;
            String response = HttpRequest.post(getAuthTokenUrl())
                    .header("Authorization", "Basic cGlnOnBpZw==")
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .body(bodyStr,"application/x-www-form-urlencoded").execute().body();
            JSONObject responseObj = JSONUtil.parseObj(response);
            if(Objects.nonNull(responseObj.getStr("access_token"))){
                return "Bearer " + responseObj.getStr("access_token");
            }else{
                throw new RuntimeException(responseObj.getStr("msg"));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("获取MES系统token接口异常:"+e.getMessage());
        }
    }
 
    /**
     * mes新增实时库存接口
     * @param requestJsonStr 请求参数json字符串
     * @return 接口响应结果
     */
    public boolean batchAddStock(String requestJsonStr){
        try{
            String response = HttpRequest.post(getBatchAddStockUrl())
                    .header("Authorization", getToken())
                    .header("Content-Type", "application/json")
                    .body(requestJsonStr)
                    .execute()
                    .body();
            JSONObject entries = JSONUtil.parseObj(response);
            if(entries.getInt("code")==0){
                return true;
            }else{
                throw new RuntimeException("同步到MES失败:"+entries.getStr("msg"));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("同步MES实时库存接口异常:"+e.getMessage());
        }
    }
 
}