通过转发形式分享图片/图片内容链接到朋友
1.判断超过2个小时重新加载
/** * 初始化获取有效的jsapi_ticket,方便后续生成签名 * 1.定义全局jsapi_ticket用于保存临时票据 * 2.定义全局lastCallTime用于存放最后一次调用的时间 * 3.首先判断jsapi_ticket为空或者当前时间 - 全局lastCallTime >= 2个小时则重新获取临时票据 */ if ("".equals(weixinTicket) || ((new Date().getTime() - lastCallTime) >= 7200 * 1000)) { try { weixinTicket=weiXinRedPackLinkService.callWeixinAccessToken(); lastCallTime=new Date().getTime(); } catch (BusinessException e) { logger.error("RomotionRuleController.enterRedPack", e); } catch (Exception e) { logger.error(e.getMessage(), e); } }
2.调用微信JS接口的临时票据jsapi_ticket的有效期为7200秒(2小时)
// 定义微信wx.config函数用到时间戳 String timestamp = new Date().getTime() + ""; // 定义微信生成签名的随机串 String noncestr = UUID.randomUUID().toString(); String signature = ""; try { /** * 通过临时票据获取签名 * 这里的newUrl指的路径为需要转发的链接请求地址 */ signature = weiXinRedPackLinkService.signAppend(noncestr, newUrl + "&from=singlemessage&isappinstalled=0", timestamp, weixinTicket); } catch (BusinessException e) { logger.error("RomotionRuleController.enterRedPack", e); }
3.以下为获取微信签名提供方法
/** * 获取微信临时签证 * @return * @throws BusinessException */ public String callWeixinAccessToken() throws BusinessException { // 获取微信返回的token String accessToken = callWeixinAccessTokencallWeixinAccessToken(); String ticket = ""; if (StringUtils.isNotEmpty(accessToken)) { // 获取微信返回的临时票据 ticket = callWeixinTicket(accessToken); } return ticket; } /** * 将获取到的微信返回的签名进行有顺序的拼接 * @param noncestr * @param signUrl * @param timestamp * @param ticket * @throws BusinessException */ public String signAppend(String noncestr, String signUrl, String timestamp, String ticket) throws BusinessException { String signature = ""; //注意这里参数名必须全部小写,且必须有序 String string1 = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + signUrl; try { logger.debug("WeiXinRedPackLinkService.signAppend =: " + string1); MessageDigest crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(string1.getBytes("UTF-8")); signature = byteToHex(crypt.digest()); } catch (NoSuchAlgorithmException e) { throw new BusinessException("WeiXinRedPackLinkService.signAppend he encryption algorithm is not available field", e); } catch (UnsupportedEncodingException e) { throw new BusinessException("WeiXinRedPackLinkService.signAppend Does not support the character encoding field", e); } return signature; } /** * 根据微信平台公众号appId和appSecret获取签名所需的token * @return */ private String callWeixinAccessTokencallWeixinAccessToken() throws BusinessException { String weixinURl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx7b0b95f2acea61e2&secret=30514dd42fea986e6a647cf9ccf96da8"; try { String temp = getHttpContent(weixinURl); if (checkaAccessTocket(temp)) { String[] resultArray = StringUtils.split(temp, ","); temp = resultArray[0]; temp = StringUtils.replace(temp, "{\"access_token\":\"", ""); temp = StringUtils.replace(temp, "\"", ""); return temp; } } catch (Exception e) { throw new BusinessException("WeiXinRedPackLinkService.callWeixinAccessTokencallWeixinAccessToken field", e); } return null; } /** * 根据token获取微信返回的临时签证 * @param accessToken * @return */ private String callWeixinTicket(String accessToken) throws BusinessException { String tokenUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi"; try { String temp = getHttpContent(tokenUrl); WinxinTokenDto winxinTokenDto = new Gson().fromJson(temp, WinxinTokenDto.class); if (winxinTokenDto != null) { return winxinTokenDto.getTicket(); } } catch (Exception e) { throw new BusinessException("WeiXinRedPackLinkService.callWeixinTicket field", e); } return null; } /** * 根据url通过HttpClient请求链接地址,返回数据 * @param weixinURl * @returnd * @throws Exception */ private String getHttpContent(String weixinURl) throws Exception { HttpClient client = new DefaultHttpClient(); // 实例化HTTP方法 HttpGet request = new HttpGet(); request.setURI(new URI(weixinURl)); HttpResponse response = client.execute(request); String result = EntityUtils.toString(response.getEntity()); return result; } /** * 校验返回的tocket是否符合条件 * @param result * @return */ private boolean checkaAccessTocket(String result) { boolean flg = true; if (result.indexOf("{\"access_token\":\"") == -1) { flg = false; } if (flg) { if (result.indexOf(",") == -1) { flg = false; } } return flg; } public 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; }
==================================================================================
二。微信app分享
1.打开网页https://open.weixin.qq.com/ 注册微信开发平台账号
2.参照提示填写相关信息,其中应用包名为项目包的名字,等待客户审核
3.解压微信官方下载的appDemo文件wechat_sdk_sample_android
4.找到该目录下的debug.keystore
5.将debug.keystore复制到自己项目的根目录下,运行该app
6.解压文件Gen_Signature_Android221cbf.jar,并安装到手机,运行该签名工具,写上项目包名获取签名的MD5加密串.
6.将MD5加密串填写到微信开放平台,开发者信息中的应用签名
7.参照官方appDemo进行开发。
说明:debug.keystore添加的2中方式:
1.eclipse:Eclipse--->Windows--->Preferences--->Android--->Build 手动变更路径(Custom debug keystore)
2.在gradle中配置:
signingConfigs {
debug {
storeFile file('../debug.keystore') //如果只有一层目录则不需要../
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}