From e7f03acfa5ee4ad4fd6d1ee9e9ae9a5655488f6d Mon Sep 17 00:00:00 2001
From: 101captain <237651143@qq.com>
Date: 星期二, 09 十一月 2021 17:04:46 +0800
Subject: [PATCH] 1109修改

---
 springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/WxXCXTempSend.java |  206 ++++++++++++++++++++++++++-------------------------
 1 files changed, 105 insertions(+), 101 deletions(-)

diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/WxXCXTempSend.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/WxXCXTempSend.java
index 85cac1b..78caa69 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/WxXCXTempSend.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/WxXCXTempSend.java
@@ -1,9 +1,11 @@
 package com.panzhihua.common.utlis;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import com.panzhihua.common.constants.SecurityConstants;
-import lombok.extern.slf4j.Slf4j;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+
 import org.apache.commons.fileupload.FileItem;
 import org.apache.commons.fileupload.FileItemFactory;
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
@@ -15,120 +17,39 @@
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
-import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.panzhihua.common.constants.SecurityConstants;
+
+import lombok.extern.slf4j.Slf4j;
 
 @Slf4j
 @Component
 public class WxXCXTempSend {
 
+    public static final String APP_ID = "wx0cef797390444b75";
+    private static final String APP_SECRET = "c7ea9aaa7e391a487e8a5b9ba61045d1";
     private static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
     private static String APP_IMAGE_URL = "https://www.psciio.com//idcard/8fa82cfba258498eab2fa818220fb592.jpg";
     private static String APP_IMAGE_NAME = "5.jpg";
-
-    public static final String APP_ID = "wx0cef797390444b75";
-    private static final String APP_SECRET = "c7ea9aaa7e391a487e8a5b9ba61045d1";
-
     @Autowired
     private StringRedisTemplate redisTemplate;
 
     /**
-     * 获取小程序token,(ps:0=token获取失败)
-     *
-     * @return
-     */
-    public String getAccessToken() throws Exception {
-//        String accessToken = ;
-//        Boolean aBoolean = redisTemplate.hasKey(SecurityConstants.APPLETS_ACCESS_TOKEN);
-//        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
-//        if(aBoolean){
-//            Long expireTime = Long.parseLong(valueOperations.get(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME));
-//            if(expireTime <= System.currentTimeMillis()){
-//                redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_TOKEN);//如果过期则删除
-//                redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME);//如果过期则删除
-//
-//                valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN, accessToken);
-//                valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME, System.currentTimeMillis() + 1100000 + "");
-//            }else{
-//                accessToken = valueOperations.get(SecurityConstants.APPLETS_ACCESS_TOKEN);
-//            }
-//        }else{
-//            accessToken = getAppAccessToken();
-//            valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN, accessToken);
-//            valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME, System.currentTimeMillis() + 1100000 + "");
-//        }
-        return getAppAccessToken();
-    }
-
-    private String getAppAccessToken() throws Exception{
-        String accessToken = "0";
-        try {
-            //此处APP_ID APP_SECRET  在微信小程序后端可见
-//            String accessTokenUrl = String.format(TEMP_URL, APP_ID, APP_SECRET);
-            String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + APP_ID + "&secret=" + APP_SECRET;
-            String result = HttpClientUtil.httpGet(accessTokenUrl, null, null);
-            Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
-            if(resultMap.containsKey("access_token")) {
-                accessToken = resultMap.get("access_token").toString();
-            }
-        } catch (IOException ioe) {
-            log.error("小程序http请求异常");
-            ioe.printStackTrace();
-        }
-        return accessToken;
-    }
-
-    public String getMediaId(String accessToken){
-        String mediaId = "";
-        Boolean aBoolean = redisTemplate.hasKey(SecurityConstants.APPLETS_ACCESS_MEDIA_ID);
-        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
-        if(aBoolean){
-            Long expireTime = Long.parseLong(valueOperations.get(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME));
-            if(expireTime <= System.currentTimeMillis()){
-                redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_MEDIA_ID);//如果过期则删除
-                redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME);//如果过期则删除
-                mediaId = getAppMediaId(accessToken);
-                valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID, mediaId);
-                valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME, System.currentTimeMillis() + 259000000 + "");
-            }else{
-                mediaId = valueOperations.get(SecurityConstants.APPLETS_ACCESS_MEDIA_ID);
-            }
-        }else{
-            mediaId = getAppMediaId(accessToken);
-            valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID, mediaId);
-            valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME, System.currentTimeMillis() + 259000000 + "");
-        }
-        return mediaId;
-    }
-
-    private String getAppMediaId(String accessToken){
-        String appMediaId = "";
-        try {
-            MultipartFile file = createFileItem(APP_IMAGE_URL,APP_IMAGE_NAME);
-            appMediaId = uploadFile(file,accessToken);
-        }catch (Exception e){
-            log.error("上传临时图片素材失败,错误原因:" + e.getMessage());
-        }
-        return appMediaId;
-    }
-
-    /**
      * url转变为 MultipartFile对象
+     *
      * @param url
      * @param fileName
      * @return
      * @throws Exception
      */
-    private static MultipartFile createFileItem(String url, String fileName) throws Exception{
+    private static MultipartFile createFileItem(String url, String fileName) throws Exception {
         FileItem item = null;
         try {
-            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
+            HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
             conn.setReadTimeout(30000);
             conn.setConnectTimeout(30000);
-            //设置应用程序要从网络连接读取数据
+            // 设置应用程序要从网络连接读取数据
             conn.setDoInput(true);
             conn.setRequestMethod("GET");
             if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
@@ -136,7 +57,8 @@
 
                 FileItemFactory factory = new DiskFileItemFactory(16, null);
                 String textFieldName = "uploadfile";
-                item = factory.createItem(textFieldName, ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName);
+                item =
+                    factory.createItem(textFieldName, ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName);
                 OutputStream os = item.getOutputStream();
 
                 int bytesRead = 0;
@@ -155,6 +77,88 @@
     }
 
     /**
+     * 获取小程序token,(ps:0=token获取失败)
+     *
+     * @return
+     */
+    public String getAccessToken() throws Exception {
+        // String accessToken = ;
+        // Boolean aBoolean = redisTemplate.hasKey(SecurityConstants.APPLETS_ACCESS_TOKEN);
+        // ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
+        // if(aBoolean){
+        // Long expireTime = Long.parseLong(valueOperations.get(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME));
+        // if(expireTime <= System.currentTimeMillis()){
+        // redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_TOKEN);//如果过期则删除
+        // redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME);//如果过期则删除
+        //
+        // valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN, accessToken);
+        // valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME, System.currentTimeMillis() + 1100000 + "");
+        // }else{
+        // accessToken = valueOperations.get(SecurityConstants.APPLETS_ACCESS_TOKEN);
+        // }
+        // }else{
+        // accessToken = getAppAccessToken();
+        // valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN, accessToken);
+        // valueOperations.set(SecurityConstants.APPLETS_ACCESS_TOKEN_TIME, System.currentTimeMillis() + 1100000 + "");
+        // }
+        return getAppAccessToken();
+    }
+
+    public String getAppAccessToken() throws Exception {
+        String accessToken = "0";
+        try {
+            // 此处APP_ID APP_SECRET 在微信小程序后端可见
+            // String accessTokenUrl = String.format(TEMP_URL, APP_ID, APP_SECRET);
+            String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + APP_ID + "&secret=" + APP_SECRET;
+            String result = HttpClientUtil.httpGet(accessTokenUrl, null, null);
+            Map<String, Object> resultMap = JSON.parseObject(result, Map.class);
+            if (resultMap.containsKey("access_token")) {
+                accessToken = resultMap.get("access_token").toString();
+            }
+        } catch (IOException ioe) {
+            log.error("小程序http请求异常");
+            ioe.printStackTrace();
+        }
+        return accessToken;
+    }
+
+    public String getMediaId(String accessToken) {
+        String mediaId = "";
+        Boolean aBoolean = redisTemplate.hasKey(SecurityConstants.APPLETS_ACCESS_MEDIA_ID);
+        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
+        if (aBoolean) {
+            Long expireTime = Long.parseLong(valueOperations.get(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME));
+            if (expireTime <= System.currentTimeMillis()) {
+                redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_MEDIA_ID);// 如果过期则删除
+                redisTemplate.delete(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME);// 如果过期则删除
+                mediaId = getAppMediaId(accessToken);
+                valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID, mediaId);
+                valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME,
+                    System.currentTimeMillis() + 259000000 + "");
+            } else {
+                mediaId = valueOperations.get(SecurityConstants.APPLETS_ACCESS_MEDIA_ID);
+            }
+        } else {
+            mediaId = getAppMediaId(accessToken);
+            valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID, mediaId);
+            valueOperations.set(SecurityConstants.APPLETS_ACCESS_MEDIA_ID_TIME,
+                System.currentTimeMillis() + 259000000 + "");
+        }
+        return mediaId;
+    }
+
+    private String getAppMediaId(String accessToken) {
+        String appMediaId = "";
+        try {
+            MultipartFile file = createFileItem(APP_IMAGE_URL, APP_IMAGE_NAME);
+            appMediaId = uploadFile(file, accessToken);
+        } catch (Exception e) {
+            log.error("上传临时图片素材失败,错误原因:" + e.getMessage());
+        }
+        return appMediaId;
+    }
+
+    /**
      * 微信小程序临时素材上传
      *
      * @param file
@@ -162,12 +166,12 @@
      * @author yixiu
      * @throws Exception
      */
-    public String uploadFile(MultipartFile file,String access_token) throws Exception {
+    public String uploadFile(MultipartFile file, String access_token) throws Exception {
         String url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" + access_token + "&type=image";
         String result = null;
         String fileName = file.getOriginalFilename();
         URL urlObj = new URL(url);
-        HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
+        HttpURLConnection con = (HttpURLConnection)urlObj.openConnection();
         con.setRequestMethod("POST");
         con.setDoInput(true);
         con.setDoOutput(true);
@@ -186,7 +190,7 @@
         sb.append("\r\n");
         sb.append("Content-Disposition: form-data;name=\"media\";filename=\"" + fileName + "\"\r\n");
         sb.append("Content-Type:application/octet-stream\r\n\r\n");
-        byte[] head = sb.toString().getBytes("utf-8");
+        byte[] head = sb.toString().getBytes(StandardCharsets.UTF_8);
         // 获得输出流
         OutputStream out = new DataOutputStream(con.getOutputStream());
         // 输出表头
@@ -201,7 +205,7 @@
         }
         in.close();
         // 结尾部分
-        byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
+        byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes(StandardCharsets.UTF_8);// 定义最后数据分隔线
         out.write(foot);
         out.flush();
         out.close();

--
Gitblit v1.7.1