package com.sys.service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Random; import com.sys.model.WechatToken; import net.sf.json.JSONObject; public class WechatService { private WechatToken wechatToken = null; //chongaibao wechat public static String APPID = "wxb07c51f90953cf69"; public static String APPSECRET = "bf6ee3892eca9108653fb894f5ee0ea0 "; // public static String APPID = "wx5f927be0a661d932"; // public static String APPSECRET = "9e27cf634578670aae56aa0962f1622b"; private WechatService(){} private static WechatService wechatService = null; public static WechatService getInstance(){ if(wechatService==null){ wechatService = new WechatService(); } return wechatService; } public WechatToken getWechatToken() { return wechatToken; } public void setWechatToken(WechatToken wechatToken) { this.wechatToken = wechatToken; } //随机串 public String getRandomString(int length) { //length表示生成字符串的长度 String base = "abcdefghijklmnopqrstuvwxyzABCKEFGHIJKLMNUVWXYZ0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } //access_token public String getAccessToken(){ String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ APPID+ "&secret=" + APPSECRET; String accessToken = null; try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("GET"); // 必须是get方式请求 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒 http.connect(); InputStream is = http.getInputStream(); int size = is.available(); byte[] jsonBytes = new byte[size]; is.read(jsonBytes); String message = new String(jsonBytes, "UTF-8"); JSONObject demoJson = JSONObject.fromObject(message); accessToken = demoJson.getString("access_token"); System.out.println(message); System.out.println(accessToken); is.close(); } catch (Exception e) { e.printStackTrace(); } return accessToken; } //getticket public String getJSToken(String accessToken){ String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi"; String jsapi_ticket = null; try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("GET"); // 必须是get方式请求 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒 http.connect(); InputStream is = http.getInputStream(); int size = is.available(); byte[] jsonBytes = new byte[size]; is.read(jsonBytes); String message = new String(jsonBytes, "UTF-8"); JSONObject demoJson = JSONObject.fromObject(message); jsapi_ticket = demoJson.getString("ticket"); System.out.println(message); System.out.println(jsapi_ticket); is.close(); } catch (Exception e) { e.printStackTrace(); } return jsapi_ticket; } }