|
@@ -3,6 +3,7 @@ package com.diagbot.facade;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.diagbot.dto.RespDTO;
|
|
import com.diagbot.dto.RespDTO;
|
|
import com.diagbot.vo.WeixinVO;
|
|
import com.diagbot.vo.WeixinVO;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
@@ -10,6 +11,7 @@ import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.security.MessageDigest;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
+import java.util.HashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.UUID;
|
|
@@ -22,18 +24,44 @@ import java.util.UUID;
|
|
@Component
|
|
@Component
|
|
public class WeixinFacade {
|
|
public class WeixinFacade {
|
|
|
|
|
|
|
|
+ @Value("${weixin.appid}")
|
|
|
|
+ private String appid;
|
|
|
|
+
|
|
|
|
+ @Value("${weixin.secret}")
|
|
|
|
+ private String secret;
|
|
|
|
+
|
|
|
|
+ // 存储accessToken 和 ticket
|
|
|
|
+ public static Map<String, String> cacheMap = new HashMap<>();
|
|
|
|
+
|
|
public Map getConfig(WeixinVO weixinVO) {
|
|
public Map getConfig(WeixinVO weixinVO) {
|
|
Map<String, String> map = new LinkedHashMap<>();
|
|
Map<String, String> map = new LinkedHashMap<>();
|
|
- String appid = "wxedd53be102996426";
|
|
|
|
- String secret = "c164e280950342f166a933dd7aa6daf7";
|
|
|
|
- String url=weixinVO.getUrl();
|
|
|
|
- try {
|
|
|
|
- /* Map<String, Object> map2 = WxUtil.oppen_id(request, request.getSession());
|
|
|
|
- String oppen_id = (String) map2.get("oppen_id");
|
|
|
|
- String accessToken = (String) map2.get("access_token");*/
|
|
|
|
- String accessToken = getAccessToken(appid,secret);
|
|
|
|
|
|
+ String url= weixinVO.getUrl();
|
|
|
|
+ try {
|
|
|
|
+ /* Map<String, Object> map2 = WxUtil.oppen_id(request, request.getSession());
|
|
|
|
+ String oppen_id = (String) map2.get("oppen_id");
|
|
|
|
+ String accessToken = (String) map2.get("access_token");*/
|
|
|
|
+
|
|
|
|
+ // 1.获取accessToken
|
|
|
|
+ String accessToken = "";
|
|
|
|
+ if (cacheMap.get("accessTokenExpire") == null
|
|
|
|
+ || Long.parseLong(cacheMap.get("accessTokenExpire")) < System.currentTimeMillis()) {
|
|
|
|
+ accessToken = getAccessToken(appid, secret);
|
|
|
|
+ cacheMap.put("accessTokenExpire", String.valueOf(System.currentTimeMillis() + 7200 * 1000)); // 过期时间
|
|
|
|
+ cacheMap.put("accessToken", accessToken);
|
|
|
|
+ } else {
|
|
|
|
+ accessToken = cacheMap.get("accessToken");
|
|
|
|
+ }
|
|
|
|
+
|
|
//2、获取Ticket
|
|
//2、获取Ticket
|
|
- String jsapi_ticket = getTicket(accessToken);
|
|
|
|
|
|
+ String jsapi_ticket = "";
|
|
|
|
+ if (cacheMap.get("ticketExpire") == null
|
|
|
|
+ || Long.parseLong(cacheMap.get("ticketExpire")) < System.currentTimeMillis()) {
|
|
|
|
+ jsapi_ticket = getTicket(accessToken);
|
|
|
|
+ cacheMap.put("ticketExpire", String.valueOf(System.currentTimeMillis() + 7200 * 1000)); // 过期时间
|
|
|
|
+ cacheMap.put("ticket", jsapi_ticket);
|
|
|
|
+ } else {
|
|
|
|
+ jsapi_ticket = cacheMap.get("ticket");
|
|
|
|
+ }
|
|
|
|
|
|
//3、时间戳和随机字符串
|
|
//3、时间戳和随机字符串
|
|
String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串
|
|
String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串
|
|
@@ -99,9 +127,9 @@ public class WeixinFacade {
|
|
is.read(jsonBytes);
|
|
is.read(jsonBytes);
|
|
String message = new String(jsonBytes, "UTF-8");
|
|
String message = new String(jsonBytes, "UTF-8");
|
|
Map<String,String> map = JSON.parseObject(message, Map.class);
|
|
Map<String,String> map = JSON.parseObject(message, Map.class);
|
|
- System.out.println(map);
|
|
|
|
|
|
+ // System.out.println(map);
|
|
access_token = map.get("access_token");
|
|
access_token = map.get("access_token");
|
|
- System.out.println(access_token);
|
|
|
|
|
|
+ // System.out.println(access_token);
|
|
// is.close();
|
|
// is.close();
|
|
//
|
|
//
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -131,7 +159,7 @@ public class WeixinFacade {
|
|
String message = new String(jsonBytes, "UTF-8");
|
|
String message = new String(jsonBytes, "UTF-8");
|
|
|
|
|
|
Map<String,String> map = JSON.parseObject(message, Map.class);
|
|
Map<String,String> map = JSON.parseObject(message, Map.class);
|
|
- System.out.println(map);
|
|
|
|
|
|
+ //System.out.println(map);
|
|
ticket = map.get("ticket");
|
|
ticket = map.get("ticket");
|
|
// JSONObject demoJson = JSONObject.fromObject(message);
|
|
// JSONObject demoJson = JSONObject.fromObject(message);
|
|
// System.out.println("JSON字符串:"+demoJson);
|
|
// System.out.println("JSON字符串:"+demoJson);
|