From f9544703867cff5286177008b99ee42ee43cb6c7 Mon Sep 17 00:00:00 2001 From: zhaoqifeng Date: Tue, 28 Jul 2020 14:32:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- epmet-commons/epmet-commons-tools/pom.xml | 9 ++ .../epmet/commons/tools/utils/FileUtils.java | 80 +++++++++++------ .../tools/utils/HttpClientManager.java | 23 ++--- .../service/impl/CodeCustomerServiceImpl.java | 2 + .../epmet/service/impl/CodeServiceImpl.java | 85 ++++--------------- .../wxapi/constant/WxMaCodeConstant.java | 4 +- .../service/impl/WxMaCodeServiceImpl.java | 3 +- 7 files changed, 92 insertions(+), 114 deletions(-) diff --git a/epmet-commons/epmet-commons-tools/pom.xml b/epmet-commons/epmet-commons-tools/pom.xml index 01d92799d7..1a21a9a30e 100644 --- a/epmet-commons/epmet-commons-tools/pom.xml +++ b/epmet-commons/epmet-commons-tools/pom.xml @@ -133,6 +133,15 @@ org.apache.httpcomponents httpmime + + org.apache.httpcomponents + httpmime + + + commons-httpclient + commons-httpclient + 3.1 + diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/FileUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/FileUtils.java index a9623b7cde..f309630863 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/FileUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/FileUtils.java @@ -1,37 +1,65 @@ package com.epmet.commons.tools.utils; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +/** + * @author kamui + */ public class FileUtils { - /** - * 创建临时文件. - * - * @param inputStream 输入文件流 - * @param name 文件名 - * @param ext 扩展名 - * @param tmpDirFile 临时文件夹目录 - */ - public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException { - File resultFile = File.createTempFile(name, '.' + ext, tmpDirFile); - - resultFile.deleteOnExit(); - org.apache.commons.io.FileUtils.copyToFile(inputStream, resultFile); - return resultFile; - } - - /** - * 创建临时文件. - * - * @param inputStream 输入文件流 - * @param name 文件名 - * @param ext 扩展名 - */ - public static File createTmpFile(InputStream inputStream, String name, String ext) throws IOException { - return createTmpFile(inputStream, name, ext, Files.createTempDirectory("weixin-java-tools-temp").toFile()); - } + /** + * 创建临时文件 + * + * @param inputStream + * @param name 文件名 + * @param ext 扩展名 + * @param tmpDirFile 临时文件夹目录 + */ + public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException { + File tmpFile; + if (tmpDirFile == null) { + tmpFile = File.createTempFile(name, '.' + ext); + } else { + tmpFile = File.createTempFile(name, '.' + ext, tmpDirFile); + } + + tmpFile.deleteOnExit(); + FileOutputStream fos = new FileOutputStream(tmpFile); + try { + int read = 0; + byte[] bytes = new byte[1024 * 100]; + while ((read = inputStream.read(bytes)) != -1) { + fos.write(bytes, 0, read); + } + + fos.flush(); + } catch (Exception e) { + + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + } + } + + } + return tmpFile; + } + + /** + * 创建临时文件 + * + * @param inputStream + * @param name 文件名 + * @param ext 扩展名 + */ + public static File createTmpFile(InputStream inputStream, String name, String ext) throws IOException { + return createTmpFile(inputStream, name, ext, null); + } } diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java index 151ad8e10d..7d456a6707 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java @@ -22,7 +22,6 @@ import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; @@ -171,20 +170,14 @@ public class HttpClientManager { try { HttpPost httppost = new HttpPost(url); httppost.setConfig(requestConfig); - String boundaryStr = "------------" + System.currentTimeMillis(); - httppost.addHeader("Connection", "keep-alive"); - httppost.addHeader("Accept", "*/*"); - httppost.addHeader("Content-Type", "multipart/form-data;boundary=" + boundaryStr); - httppost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) "); - MultipartEntityBuilder meb = MultipartEntityBuilder.create(); - meb.setBoundary(boundaryStr).setCharset(StandardCharsets.UTF_8).setMode(HttpMultipartMode.BROWSER_COMPATIBLE); - meb.addBinaryBody("media", file, ContentType.APPLICATION_OCTET_STREAM, file.getName()); - HttpEntity entity = meb.build(); - httppost.setEntity(entity); -// FileBody fileBody = new FileBody(file); -// HttpEntity reqEntity = MultipartEntityBuilder.create() -// .addPart("media", fileBody).build(); -// httppost.setEntity(reqEntity); + if (file != null) { + HttpEntity entity = MultipartEntityBuilder + .create() + .addBinaryBody("media", file) + .setMode(HttpMultipartMode.RFC6532) + .build(); + httppost.setEntity(entity); + } return execute(httppost,false); } catch (Exception e) { log.error("send exception", e); diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/CodeCustomerServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/CodeCustomerServiceImpl.java index 1962b917a9..c250644044 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/CodeCustomerServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/CodeCustomerServiceImpl.java @@ -89,6 +89,7 @@ public class CodeCustomerServiceImpl extends BaseServiceImpl { + List sortList = + wxResult.getData().stream().sorted(Comparator.comparing(WxMaTemplateResult::getCreateTime).reversed()).collect(Collectors.toList()); + sortList.forEach(temp -> { TemplateListResultDTO dto = new TemplateListResultDTO(); dto.setId(temp.getTemplateId()); dto.setUserVersion(temp.getUserVersion()); dto.setUserDesc(temp.getUserDesc()); - dto.setCreateTime(DateUtils.formatTimestamp(temp.getCreateTime(), DateUtils.DATE_PATTERN)); + dto.setCreateTime(DateUtils.formatTimestamp(temp.getCreateTime(), DateUtils.DATE_TIME_PATTERN)); resultList.add(dto); }); return resultList; @@ -101,14 +103,6 @@ public class CodeServiceImpl implements CodeService { return codeExtDTO.getExtJson(); } - public static void main(String[] args) { - String json = "{\"extEnable\":true,\"extAppid\":\"wx2679392c4cc2af22\",\"directCommit\":false,\"ext\":{\"extAppid\":\"wx2679392c4cc2af22\",\"footbar\":{\"work\":{\"name\":\"工作\",\"pageTile\":\"工作\"},\"org\":{\"name\":\"组织\",\"pageTile\":\"组织\"},\"data\":{\"name\":\"数据\",\"pageTile\":\"数据\"},\"find\":{\"name\":\"更多\",\"pageTile\":\"更多\"}}}}"; - WxExtJson wxExtJson = JSONObject.parseObject(json, WxExtJson.class); - wxExtJson.setExtAppid("123456"); - wxExtJson.getExt().setExtAppid("123456"); - String extJson = JSON.toJSONString(wxExtJson); - System.out.println(extJson); - } @Override @Transactional(rollbackFor = Exception.class) @@ -122,14 +116,11 @@ public class CodeServiceImpl implements CodeService { if (null == authInfo) { throw new RenException("未授权"); } - if (!isJson(formDTO.getExtJson())) { - throw new RenException("第三方配置不是有效的Json"); - } - - WxExtJson wxExtJson = JSONObject.parseObject(formDTO.getExtJson(), WxExtJson.class); + String extJson = getExtJson(formDTO); + WxExtJson wxExtJson = JSONObject.parseObject(extJson, WxExtJson.class); wxExtJson.setExtAppid(authInfo.getAuthorizerAppid()); wxExtJson.getExt().setExtAppid(authInfo.getAuthorizerAppid()); - String extJson = JSON.toJSONString(wxExtJson); + extJson = JSON.toJSONString(wxExtJson); CodeExtDTO codeExtDTO = codeExtService.getExtByCustomer(formDTO.getCustomerId(), formDTO.getClientType()); if (null == codeExtDTO) { codeExtDTO = new CodeExtDTO(); @@ -373,7 +364,7 @@ public class CodeServiceImpl implements CodeService { //获取上传代码信息 CodeCustomerDTO codeCustomerDTO = codeCustomerService.get(formDTO.getCodeId()); if (null != codeCustomerDTO.getQrCode()) { - result.setQrcode(codeCustomerDTO.getQrCode()); + result.setQrcode(codeCustomerDTO.getQrCode()); return result; } //是否授权 @@ -409,8 +400,8 @@ public class CodeServiceImpl implements CodeService { @Override public String mediaUpload(MediaUploadFormDTO formDTO) { try { - Result uploadWxImg = ossFeignClient.uploadWxImg(formDTO.getMedia()); - File file = new File(uploadWxImg.getData().getUrl()); + File file = new File(formDTO.getMedia().getOriginalFilename()); + FileUtils.copyInputStreamToFile(formDTO.getMedia().getInputStream(), file); //获取上传代码信息 CodeCustomerDTO codeCustomerDTO = codeCustomerService.get(formDTO.getCodeId()); //获取小程序调用令牌 @@ -443,50 +434,4 @@ public class CodeServiceImpl implements CodeService { codeOperationHistoryService.save(operationDTO); } - /** - * 校验是否是Json - * - * @param content - * @return boolean - * @author zhaoqifeng - * @date 2020/7/17 15:43 - */ - private boolean isJson(String content) { - try { - JSONObject jsonStr = JSONObject.parseObject(content); - return true; - } catch (Exception e) { - return false; - } - } - - private byte[] toPrimitives(Byte[] oBytes) { - byte[] bytes = new byte[oBytes.length]; - - for (int i = 0; i < oBytes.length; i++) { - bytes[i] = oBytes[i]; - } - return bytes; - } - - private void byteToFile(byte[] bytes) - { - try - { - // 根据绝对路径初始化文件 - File localFile = new File("."); - if (!localFile.exists()) - { - localFile.createNewFile(); - } - // 输出流 - OutputStream os = new FileOutputStream(localFile); - os.write(bytes); - os.close(); - } - catch (Exception e) - { - e.printStackTrace(); - } - } } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java index 9302f876ff..6d82fae8da 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java @@ -159,10 +159,10 @@ public interface WxMaCodeConstant { /** * 新增临时素材 */ - String MEDIA_UPLOAD_URL = "https://api.weixin.qq.com/wxa/setwebviewdomain"; + String MEDIA_UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/upload"; /** - * 新增临时素材 + * 获取模板列表 */ String GET_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/gettemplatelist"; diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java index 97f7cac543..e6fcafb53f 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java @@ -156,7 +156,7 @@ public class WxMaCodeServiceImpl implements WxMaCodeService { return result; } WxMaAuditStatusResult auditStatusResult = JSONObject.parseObject(statusResult.getData(), WxMaAuditStatusResult.class); - if (!auditStatusResult.success()){ + if (!auditStatusResult.success()) { result.setErrorCode(auditStatusResult.getErrcode()); result.setErrorMsg(WxMaErrorMsgEnum.findMsgByCode(auditStatusResult.getErrcode())); return result; @@ -269,6 +269,7 @@ public class WxMaCodeServiceImpl implements WxMaCodeService { WxMaUploadMediaResult mediaInfo = gson.fromJson(mediaResult.getData(), WxMaUploadMediaResult.class); result.setErrorCode(mediaInfo.getErrCode()); result.setErrorMsg(WxMaErrorMsgEnum.findMsgByCode(mediaInfo.getErrCode())); + result.ok(mediaInfo); return result; }