From b2dc5fa1b7e710a4515c90c65b4303fa71f79b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=B1=B3?= <470431449@qq.com> Date: Fri, 8 Dec 2023 10:51:42 +0800 Subject: [PATCH] =?UTF-8?q?1,=E7=94=B3=E8=AF=B7=E7=89=A9=E6=96=99=E7=BC=96?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bomnew/api/user/OriginalBomApi.java | 14 + .../product/bomnew/config/NacosConfig.java | 3 + .../bomnew/pojo/dto/MaterialMainDTO.java | 4 - .../bomnew/service/MaterialService.java | 20 +- .../nflg/product/bomnew/util/HttpUtils.java | 393 ++++++++++++++++++ .../nflg/product/base/core/api/BaseApi.java | 1 + .../base/core/conmon/util/SessionUtil.java | 4 + .../product/common/dto/LoginUserInfoDTO.java | 2 + 8 files changed, 429 insertions(+), 12 deletions(-) create mode 100644 nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/util/HttpUtils.java diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/api/user/OriginalBomApi.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/api/user/OriginalBomApi.java index 4eb91c57..e115d600 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/api/user/OriginalBomApi.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/api/user/OriginalBomApi.java @@ -11,6 +11,7 @@ import com.nflg.product.bomnew.pojo.dto.OriginalSaveBomDTO; import com.nflg.product.bomnew.pojo.query.OriginalBomQuery; import com.nflg.product.bomnew.pojo.vo.BomOriginalListVO; import com.nflg.product.bomnew.service.BomNewOriginalParentService; +import com.nflg.product.bomnew.service.MaterialService; import com.nflg.product.bomnew.util.EecExcelUtil; import com.nflg.product.bomnew.util.TreeNode; import com.nflg.product.bomnew.util.VUtils; @@ -147,5 +148,18 @@ public class OriginalBomApi extends BaseApi { return ResultVO.success(true); } + @Resource + MaterialService materialService; + @ApiOperation("testaddMaterial") + @GetMapping("新增物料") + @Transactional(rollbackFor = Exception.class) + public ResultVO importBom(@RequestParam("drawingNo")String drawingNo, + @RequestParam("materialName")String materialName, + @RequestParam("materialCategoryCode")String materialCategoryCode) throws IOException { + + materialService.addMaterial(drawingNo, materialName,materialCategoryCode); + return ResultVO.success("true"); + } + } diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/config/NacosConfig.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/config/NacosConfig.java index d250b4e5..40d0a68d 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/config/NacosConfig.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/config/NacosConfig.java @@ -81,6 +81,9 @@ public class NacosConfig { @Value("${ini.share.path}") private String sharePath; + @Value("${material.add.url}") + private String addMaterialUrl; + diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/pojo/dto/MaterialMainDTO.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/pojo/dto/MaterialMainDTO.java index df2714f8..223b89be 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/pojo/dto/MaterialMainDTO.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/pojo/dto/MaterialMainDTO.java @@ -94,11 +94,7 @@ public class MaterialMainDTO implements Serializable { private List materialTypeThreeFiles=new ArrayList<>(); - private Object materialTypeZeroFile=new Object(); - private Object materialTypeOneFile=new Object(); - - private Object materialTypeTwoFile=new Object(); private List attrs=new ArrayList<>(); diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/MaterialService.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/MaterialService.java index 6fdcb436..2896034f 100644 --- a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/MaterialService.java +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/service/MaterialService.java @@ -2,15 +2,21 @@ package com.nflg.product.bomnew.service; import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson.JSON; import com.nflg.product.base.core.conmon.util.SessionUtil; import com.nflg.product.base.core.exception.NflgBusinessException; import com.nflg.product.bomnew.client.MaterialMainClient; +import com.nflg.product.bomnew.config.NacosConfig; import com.nflg.product.bomnew.pojo.dto.MaterialMainDTO; +import com.nflg.product.bomnew.util.HttpUtils; import nflg.product.common.constant.STATE; import nflg.product.common.vo.ResultVO; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; @Service public class MaterialService { @@ -26,7 +32,7 @@ public class MaterialService { * @param materialCategoryCode 物料类别编码 * @return 物料编码 */ - public String addMaterial(String drawingNo, String materialName, String materialCategoryCode){ + public String addMaterial(String drawingNo, String materialName, String materialCategoryCode) throws IOException { MaterialMainDTO materialMainDTO=new MaterialMainDTO(); materialMainDTO.setMaterialName(materialName); materialMainDTO.setDrawingNo(drawingNo); @@ -34,15 +40,13 @@ public class MaterialService { materialMainDTO.setMaterialCategoryCode(materialCategoryCode); materialMainDTO.setOpEnum(1); materialMainDTO.setApplyDeptName(SessionUtil.getDepartName()); + String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySW5mbyI6IntcImRlcGFydE5hbWVcIjpcIuS_oeaBr-a1geeoi-S4reW_gy9JVOaUr-aMgVwiLFwiZGVwYXJ0Um93SWRcIjoxNTg1MTk2OTc4NDEzNjk0OTc4LFwiZnVsbERlcHROYW1lXCI6XCJPVT1JVOaUr-aMgSxPVT3kv6Hmga_mtYHnqIvkuK3lv4MsT1U956aP5bu65Y2X5pa56Lev6Z2i5py65qKw5pyJ6ZmQ5YWs5Y-4LERDPW5mbGdcIixcInF1ZXJ5RGF0YVwiOjMsXCJyZWFsTmFtZVwiOlwi6YOR5Yab5qaVXCIsXCJyb3dJZFwiOjE1ODUxNjQ2NjgzMzU0Mzk4ODEsXCJ1c2VyQ29kZVwiOlwiUUhJMTcwNjIxMDBcIixcInVzZXJOYW1lXCI6XCJRSEkxNzA2MjEwMFwifSIsImV4cCI6MTcwMjAzMTg2MiwianRpIjoiMjdmMjg2MDUtNzgxZi00MDQwLWJkZDMtMzcwNGI2MWU1YzJiIn0.hpzLa9-ZYEqclA5iT-Jv62Ow3PduuP7Gqyvb7uYJcRE"; +// String token=SessionUtil.getsessionKey(); + HttpUtils httpUtils=new HttpUtils(); + String s = httpUtils.doPost(NacosConfig.getNacosConfig().getAddMaterialUrl(),JSON.toJSONString(materialMainDTO),token); + return s; - ResultVO stringResultVO = materialMainClient.addMaterialMainEntity(materialMainDTO); - if(stringResultVO.getState().equals(STATE.Success)){ - return stringResultVO.getData(); - } - else { - throw new NflgBusinessException(STATE.Error, "申请编码失败"+stringResultVO.getMsg()); - } } } diff --git a/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/util/HttpUtils.java b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/util/HttpUtils.java new file mode 100644 index 00000000..d352e6f3 --- /dev/null +++ b/nflg_project_dev/nflg-bom-new/src/main/java/com/nflg/product/bomnew/util/HttpUtils.java @@ -0,0 +1,393 @@ +package com.nflg.product.bomnew.util; + + +import com.nflg.product.base.core.exception.NflgBusinessException; +import lombok.extern.slf4j.Slf4j; +import nflg.product.common.constant.STATE; +import okhttp3.*; +import org.springframework.http.HttpStatus; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * http请求工具类,封装于okHttp3 + */ +@Slf4j +public class HttpUtils { + + //请求类 + private OkHttpClient okHttpClient; + + //请求头类型 + private final MediaType jsonMediaType = MediaType.parse("application/json;charset=utf-8"); + + //请求时间 + private int timeout = 80; + + public HttpUtils() { + + } + + public HttpUtils(int timeout) { + this.timeout = timeout; + } + + /*请求返回响应头*/ + + /** + * get请求返回响应头 + * + * @param url 请求地址 + * @return Response + * @throws IOException e + */ + public Response doGetRtnRsp(String url) throws IOException { + Request request = new Request.Builder() + .url(url) + .get() + .build(); + Response response = getOkHttpClient().newCall(request).execute(); + + if (response.code() != HttpStatus.OK.value()) { + log.error("请求失败" + (response.body() == null ? "" : ":" + response.body().string())); + throw new NflgBusinessException(STATE.ParamErr, "请求失败" + (response.body() == null ? "" : ":" + response.body().string())); + } + + return response; + } + + /** + * get请求,自动构造参数,传空则不构造 + * + * @param url URL地址 + * @param map 参数 + * @return Response + * @throws IOException e + */ + public Response doGetRtnRsp(String url, Map map) throws IOException { + Request request = new Request.Builder() + .url(url + buildGetParams(map)) + .get() + .build(); + return getOkHttpClient().newCall(request).execute(); + } + + /** + * post请求(表单) + * + * @param url URL地址 + * @param map 参数 + * @return Response + * @throws IOException e + */ + public Response doPostRtnRsp(String url, Map map) throws IOException { + RequestBody requestBody = buildRequestBody(map); + Request request = new Request.Builder() + .url(url) + .post(requestBody) + .build(); + return getOkHttpClient().newCall(request).execute(); + } + + /** + * post请求(JSON) + * + * @param url URL地址 + * @param json json字符串 + * @return Response + * @throws IOException e + */ + public Response doPostRtnRsp(String url, String json,String token) throws IOException { + RequestBody requestBody = RequestBody.create(jsonMediaType, json); + Request request = new Request.Builder() + .url(url) + .post(requestBody) + .addHeader("authorization",token) + .build(); + return getOkHttpClient().newCall(request).execute(); + } + + /** + * 表单请求 + * + * @param url URL地址 + * @param map 参数 + * @return Response + * @throws IOException e + */ + public Response doFormUrlEncodedPostRtnRsp(String url, Map map) throws IOException { + RequestBody requestBody = buildRequestBody(map); + Request request = new Request.Builder() + .url(url) + .header("Content-Type", "application/x-www-form-urlencoded") + .post(requestBody) + .build(); + + return getOkHttpClient().newCall(request).execute(); + } + + + /** + * form-data表单请求(兼容图片png\jpg\jpeg上传) + * + * @param url URL地址 + * @param map 参数 + * @return Response + * @throws IOException e + */ + public Response doFileRequestBodyRtnRsp(String url, Map map, File file) throws Exception { + RequestBody requestBody = buildFileRequestBody(map,file); + Request request = new Request.Builder() + .url(url) + .header("Content-Type", "multipart/form-data") + .post(requestBody) + .build(); + + return getOkHttpClient().newCall(request).execute(); + } + + + /*请求返回响应内容*/ + + /** + * get请求(表单) + * + * @param url 请求地址 + * @return String + * @throws IOException e + */ + public String doGet(String url) throws IOException { + Response response = doGetRtnRsp(url); + if (response == null || response.body() == null) { + return null; + } + return response.body().string(); + } + + /** + * get请求(表单),自动构造参数 + * + * @param url 请求地址 + * @param map 参数 + * @return String + * @throws IOException e + */ + public String doGet(String url, Map map) throws IOException { + Response response = doGetRtnRsp(url, map); + if (response == null || response.body() == null) { + return null; + } + return response.body().string(); + } + + /** + * post请求(表单) + * + * @param url 请求地址 + * @param map 参数 + * @return String + * @throws IOException e + */ + public String doPost(String url, Map map) throws IOException { + Response response = doPostRtnRsp(url, map); + if (response == null || response.body() == null) { + return null; + } + return response.body().string(); + } + + /** + * post请求(JSON) + * + * @param url 请求地址 + * @param json json字符串 + * @return String + * @throws IOException e + */ + public String doPost(String url, String json,String token) throws IOException { + System.out.println(url); + System.out.println(json); + Response response = doPostRtnRsp(url, json,token); + if (response == null || response.body() == null) { + return null; + } + return response.body().string(); + } + + /** + * 表单请求 + * + * @param url URL地址 + * @param map 参数 + * @return String + * @throws IOException e + */ + public String doformPost(String url, Map map) throws IOException { + Response response = doFormUrlEncodedPostRtnRsp(url, map); + if (response == null || response.body() == null) { + return null; + } + return response.body().string(); + } + + /** + * multipart文件类型表单请求(兼容图片png\jpg\jpeg上传) + * + * @param url URL地址 + * @param map 参数 + * @return String + * @throws IOException e + */ + public String doMultipartFormPost(String url, Map map, File file) throws Exception { + Response response = doFileRequestBodyRtnRsp(url, map,file); + if (response == null || response.body() == null) { + return null; + } + return response.body().string(); + } + + /** + * 获取网络图片流 + * + * @param url url地址 + * @return InputStream + * @throws IOException e + */ + public InputStream getImageStream(String url) throws IOException { + OkHttpClient client = getOkHttpClient(); + Request request = new Request.Builder() + .url(url) + .get() + .build(); + ResponseBody responseBody = client.newCall(request).execute().body(); + if (responseBody == null) { + return null; + } + return responseBody.byteStream(); + } + + /*公共方法*/ + + /** + * 构建键值对表单 + * + * @param map 参数 + * @return RequestBody + */ + private RequestBody buildRequestBody(Map map) { + FormBody.Builder builder = new FormBody.Builder(StandardCharsets.UTF_8); + if (map != null && map.size() > 0) { + for (String string : map.keySet()) { + builder.add(string, map.get(string)); + } + } + return builder.build(); + } + + /** + * 获取对象 + * + * @return OkHttpClient + */ + private OkHttpClient getOkHttpClient() { + if (okHttpClient != null) { + return okHttpClient; + } + + okHttpClient = new OkHttpClient.Builder() + .connectTimeout(timeout, TimeUnit.SECONDS) + .writeTimeout(timeout, TimeUnit.SECONDS) + .readTimeout(timeout, TimeUnit.SECONDS) + .build(); + return okHttpClient; + } + + /** + * 构建url请求字符串 + * + * @param map 参数 + * @return String + */ + private String buildGetParams(Map map) { + if (map == null || map.size() == 0) { + return ""; + } + + StringBuilder params = new StringBuilder(); + for (String string : map.keySet()) { + params.append(string).append("=").append(map.get(string)).append("&"); + } + + return "?" + params.toString(); + } + + /** + * 构建multipart类型键值对表单 + * + * @param map 参数 + * @return RequestBody + */ + private RequestBody buildFileRequestBody(Map map, File file)throws Exception { + + MultipartBody.Builder multiBuilder=new MultipartBody.Builder(); + // 设置请求体 + multiBuilder.setType(MultipartBody.FORM); + String name = file.getName(); + RequestBody filebody=null; + if(name.endsWith(".png")||name.endsWith(".PNG")) + { + filebody = MultipartBody.create(MediaType.parse("image/png"), file); + }else if(name.endsWith(".jpeg")||name.endsWith(".jpg")||name.endsWith(".JPEG")||name.endsWith(".JPG")) + { + filebody = MultipartBody.create(MediaType.parse("image/jpeg"), file); + } + if(filebody==null) + { + throw new Exception("图片文件上传类型"+name+"与要求不符"); + } + + multiBuilder.addFormDataPart("file", name, filebody); + if (map != null && map.size() > 0) { + for (String string : map.keySet()) { + multiBuilder.addFormDataPart(string, map.get(string)); + } + } + + return multiBuilder.build(); + } + + /** + * @author liusongsheng + * @date 2021/12/22 9:48 + */ + + public String doGet(String url,String token) throws IOException { + Response response = doGetByToken(url,token); + if (response == null || response.body() == null) { + return null; + } + return response.body().string(); + } + /** + * get请求返回响应头 + * tokenType 为 Bearer + * @param url 请求地址 + * @return Response + * @throws IOException e + */ + public Response doGetByToken(String url,String token) throws IOException { + Request request = new Request.Builder() + .url(url) + .get() + .addHeader("Authorization", "Bearer " + token) + .build(); + return getOkHttpClient().newCall(request).execute(); + } + + +} diff --git a/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/api/BaseApi.java b/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/api/BaseApi.java index b47d34ff..1e796c51 100644 --- a/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/api/BaseApi.java +++ b/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/api/BaseApi.java @@ -30,6 +30,7 @@ public class BaseApi { ResultVO result = JwtUtil.parse(token); if (result.getState().equals(STATE.Success.getState())) { LoginUserInfoDTO userInfo = (LoginUserInfoDTO) result.getData(); + userInfo.setSessionKey(token); userInfo.setLanguage(language); if (!Objects.isNull(userInfo)) { loginUser.set(userInfo); diff --git a/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/conmon/util/SessionUtil.java b/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/conmon/util/SessionUtil.java index 33334395..8664b62c 100644 --- a/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/conmon/util/SessionUtil.java +++ b/nflg_project_dev/nflg-boot-base/nflg-boot-core/src/main/java/com/nflg/product/base/core/conmon/util/SessionUtil.java @@ -87,4 +87,8 @@ public class SessionUtil { public static String getLanguage() { return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getLanguage).orElse(null); } + + public static String getsessionKey() { + return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getSessionKey).orElse(null); + } } diff --git a/nflg_project_dev/nflg-boot-base/nflg-common/src/main/java/nflg/product/common/dto/LoginUserInfoDTO.java b/nflg_project_dev/nflg-boot-base/nflg-common/src/main/java/nflg/product/common/dto/LoginUserInfoDTO.java index a1ddb7f6..b192ca3b 100644 --- a/nflg_project_dev/nflg-boot-base/nflg-common/src/main/java/nflg/product/common/dto/LoginUserInfoDTO.java +++ b/nflg_project_dev/nflg-boot-base/nflg-common/src/main/java/nflg/product/common/dto/LoginUserInfoDTO.java @@ -31,4 +31,6 @@ public class LoginUserInfoDTO { private String fullDeptName; private String language; + + private String sessionKey; }