package com.izouma.awesomeadmin.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.Formatter; import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.UUID; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import org.json.JSONObject; import org.springframework.stereotype.Service; import com.izouma.awesomeadmin.dto.wx.TemplateMessage; import com.google.gson.Gson; /** * 微信工具类 * @author * */ @Service public class WeixinUtil { private final static String SEND_TEMPLAYE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN"; private static Logger logger = Logger.getLogger(WeixinUtil.class); public static Map sign(String jsapi_ticket, String url) { Map ret = new HashMap (); String nonce_str = create_nonce_str(); String timestamp = create_timestamp(); String string1; String signature = ""; //注意这里参数名必须全部小写,且必须有序 string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "×tamp=" + timestamp + "&url=" + url; System.out.println(string1); try { MessageDigest crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(string1.getBytes("UTF-8")); signature = byteToHex(crypt.digest()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } ret.put("url", url); ret.put("jsapi_ticket", jsapi_ticket); ret.put("nonceStr", nonce_str); ret.put("timestamp", timestamp); ret.put("signature", signature); return ret; } public static JSONObject loadJSON(String url) { StringBuilder json = new StringBuilder(); try { URL oracle = new URL(url); URLConnection yc = oracle.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8")); String inputLine = null; while ((inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } return new JSONObject(json.toString()); } /** * 用SHA1算法生成安全签名 * @param token 票据 * @param timestamp 时间戳 * @param nonce 随机字符串 * @param encrypt 密文 * @return 安全签名 * @throws NoSuchAlgorithmException * @throws AesException */ public String getSHA1(String token, String timestamp, String nonce) throws NoSuchAlgorithmException { String[] array = new String[] {token, timestamp, nonce}; StringBuffer sb = new StringBuffer(); // 字符串排序 Arrays.sort(array); for (int i = 0; i < 3; i++) { sb.append(array[i]); } String str = sb.toString(); // SHA1签名生成 MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(str.getBytes()); byte[] digest = md.digest(); StringBuffer hexstr = new StringBuffer(); String shaHex = ""; for (int i = 0; i < digest.length; i++) { shaHex = Integer.toHexString(digest[i] & 0xFF); if (shaHex.length() < 2) { hexstr.append(0); } hexstr.append(shaHex); } return hexstr.toString(); } public static String byteToHex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } String result = formatter.toString(); formatter.close(); return result; } public static String create_nonce_str() { return UUID.randomUUID().toString().replace("-", ""); } public static String create_timestamp() { return Long.toString(System.currentTimeMillis() / 1000); } public static String create_out_trade_no() { return Long.toString(System.currentTimeMillis() / 1000) + getRandomNum(10); } private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.append(byteToHexString(b[i])); return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n += 256; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } public static String MD5Encode(String origin, String charsetname) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); if (charsetname == null || "".equals(charsetname)) resultString = byteArrayToHexString(md.digest(resultString.getBytes())); else resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname))); } catch (Exception exception) { } return resultString; } private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; /** * *

获取ip地址。

* * @return */ @SuppressWarnings("static-access") public static String getHostAdderss() { InetAddress ia = null; try { ia = ia.getLocalHost(); String localname = ia.getHostName(); String localip = ia.getHostAddress(); System.out.println("本机名称是:" + localname); System.out.println("本机的ip是 :" + localip); return localip; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 生成特定位数的随机数字 * @param length * @return */ public static String getRandomNum(int length) { String val = ""; Random random = new Random(); for (int i = 0; i < length; i++) { val += String.valueOf(random.nextInt(10)); } return val; } /** * POST请求 * * @param url * @param outStr * @return * @throws ParseException * @throws IOException */ public static JSONObject doPostStr(String url, String outStr) throws ParseException, IOException { DefaultHttpClient client = new DefaultHttpClient(); HttpPost httpost = new HttpPost(url); JSONObject jsonObject = null; httpost.setEntity(new StringEntity(outStr, "UTF-8")); HttpResponse response = client.execute(httpost); String result = EntityUtils.toString(response.getEntity(), "UTF-8"); jsonObject = new JSONObject(result); return jsonObject; } public static void sendTemplateMessage(String accessToken, TemplateMessage templateMessage) throws ParseException, IOException { String jsonString = new Gson().toJson(templateMessage).toString(); String requestUrl = SEND_TEMPLAYE_MESSAGE_URL.replace("ACCESS_TOKEN", accessToken); JSONObject result = WeixinUtil.doPostStr(requestUrl, jsonString); logger.info("jsonObject=" + result); if (null != result) { int errorCode = result.getInt("errcode"); if (0 == errorCode) { logger.info("模板消息发送成功"); } else { String errorMsg = result.getString("errmsg"); logger.info("模板消息发送失败,错误是 " + errorCode + ",错误信息是" + errorMsg); } } } }