package com.ruoyi.common.utils.api;
|
|
import cn.hutool.http.HttpRequest;
|
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONUtil;
|
import com.ruoyi.common.config.mes.MesConfig;
|
import com.ruoyi.common.config.mes.MesProperties;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
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 {
|
|
@Autowired
|
private MesConfig mesConfig;
|
|
/**
|
* 根据工厂域回去mes配置
|
* @param contract
|
* @return
|
*/
|
public MesProperties getMesPropertiesByContract(String contract){
|
return mesConfig.getProps().stream().filter(f-> org.apache.commons.lang3.StringUtils.equals(f.getContract(),contract)).findFirst().orElse(new MesProperties());
|
}
|
|
/**
|
* 获取token请求链接
|
* @return
|
*/
|
private String getAuthTokenUrl(String ip) {
|
return ip + "/auth/oauth/token?randomStr=blockPuzzle&grant_type=password";
|
}
|
|
/**
|
* 新增实时库存请求链接
|
* @return
|
*/
|
private String getBatchAddStockUrl(String ip){
|
return ip + "/mes/stock/batchAddStock";
|
}
|
|
/**
|
* 获取mes系统token
|
* @return 接口响应结果
|
*/
|
public String getToken(MesProperties mesProperties){
|
try{
|
Map<String,Object> bodyMap = new HashMap<>();
|
bodyMap.put("username",mesProperties.getUser());
|
String bodyStr = "username="+mesProperties.getUser()+"&password="+mesProperties.getPassword();
|
String response = HttpRequest.post(getAuthTokenUrl(mesProperties.getIp()))
|
.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 contract 工厂域
|
* @param requestJsonStr 请求参数json字符串
|
* @return 接口响应结果
|
*/
|
public boolean batchAddStock(String contract,String requestJsonStr){
|
try{
|
MesProperties mesProperties = getMesPropertiesByContract(contract);
|
String response = HttpRequest.post(getBatchAddStockUrl(mesProperties.getIp()))
|
.header("Authorization", getToken(mesProperties))
|
.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("【"+contract+"】同步到MES失败:"+entries.getStr("msg"));
|
}
|
}catch (Exception e){
|
e.printStackTrace();
|
throw new RuntimeException("【"+contract+"】同步MES实时库存接口异常:"+e.getMessage());
|
}
|
}
|
|
}
|