1.H5页面配置
(1)引入微信JS SDK
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
(2)在js里配置接口参数
wx.config({ debug : false,//这里是开启测试,如果设置为true,则打开每个步骤,都会有提示,是否成功或者失败 appId : '${appid}', timestamp : '${timestamp}', nonceStr : '${nonceStr}', signature : '${signature}', jsApiList : [ // 所有要调用的 API 都要加到这个列表中 'onMenuShareTimeline',"onMenuShareAppMessage","onMenuShareQQ","hideMenuItems"] }); var strTiele=""; var strDesc=""; var strLink="url"; var strImgUrl="图片路径"; wx.ready(function() { // 1 判断当前版本是否支持指定 JS 接口,支持批量判断 wx.checkJsApi({ jsApiList : ['"hideMenuItems"','onMenuShareTimeline','onMenuShareAppMessage',"onMenuShareQQ"], }); //隐藏QQ空间分享,QQ浏览器,safari wx.hideMenuItems({ menuList: ["menuItem:share:QZone","menuItem:openWithSafari","menuItem:openWithQQBrowser"] }); //分享朋友圈 wx.onMenuShareTimeline({ title : strTiele, // 分享标题 desc : strDesc,//分享描述 link : strLink, // 分享链接 imgUrl: strImgUrl,// 分享图标 }); //分享给微信好友 wx.onMenuShareAppMessage({ title : strTiele, // 分享标题 desc : strDesc,//分享描述 link : strLink, // 分享链接 imgUrl: strImgUrl,// 分享图标 }); //分享到QQ wx.onMenuShareQQ({ title : strTiele, // 分享标题 desc : strDesc,//分享描述 link : strLink, // 分享链接 imgUrl: strImgUrl,// 分享图标 });
2.ACTION 类设置
String params = PastUtil.getParam("wx05a97b02437b983d","2a5ea68d9679b0de1ad09de72e4159fe"); JSONObject jsonObject = new JSONObject(params); this.setResult("appid", jsonObject.get("appid")); this.setResult("url", jsonObject.get("url")); this.setResult("jsapi_ticket", jsonObject.get("jsapi_ticket")); this.setResult("timestamp", jsonObject.get("timestamp")); this.setResult("nonceStr", jsonObject.get("nonceStr")); this.setResult("signature", jsonObject.get("signature"));
Pastutil 类
public class PastUtil { public static String token = null; public static String time = null; public static String jsapi_ticket = null; public static String url = null ; /** * * @param appId 公账号appId * @param appSecret * @param url 当前网页的URL,不包含#及其后面部分 * @return */ public static String getParam(String appId,String appSecret){ if(token == null){ token = CommonUtil.getToken(appId, appSecret); jsapi_ticket = CommonUtil.getJsApiTicket(token); time = getTime(); }else{ if(!time.substring(0, 13).equals(getTime().substring(0, 13))){ //每小时刷新一次 token = null; token = CommonUtil.getToken(appId, appSecret); jsapi_ticket = CommonUtil.getJsApiTicket(token); time = getTime(); } } String url = getUrl(); Map<String, String> params = sign(jsapi_ticket, url); params.put("appid", appId); JSONObject jsonObject = JSONObject.fromObject(params); String jsonStr = jsonObject.toString(); System.out.println(jsonStr); return jsonStr; } private static String getUrl(){ HttpServletRequest request = ActionContext.getActionContext().getRequest(); //StringBuffer requestUrl = request.getRequestURL(); System.out.println(request.getServerName()); //如果你的微信后台端口是80的话,一定不能把HTTP_HOST也拼接到url上面去,否则签名失败 String share2FirendUrl = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ request.getRequestURI(); System.out.println(share2FirendUrl); //带参数的url拼接 if(StringUtils.isNotBlank(request.getQueryString())){ share2FirendUrl += "?"+request.getQueryString(); } System.out.println("share2FirendUrl "+share2FirendUrl+" "); return share2FirendUrl; } public static Map<String, String> sign(String jsapi_ticket, String url) { Map<String, String> ret = new HashMap<String, String>(); String nonce_str = create_nonce_str(); String timestamp = create_timestamp(); String str; String signature = ""; //注意这里参数名必须全部小写,且必须有序 str = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "×tamp=" + timestamp + "&url=" + url; try { MessageDigest crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(str.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; } private 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; } private static String create_nonce_str() { return UUID.randomUUID().toString(); } private static String create_timestamp() { return Long.toString(System.currentTimeMillis() / 1000); } //获取当前系统时间 用来判断access_token是否过期 public static String getTime(){ Date dt=new Date(); SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(dt); } public static void main(String[] args){ PastUtil.url = "http://m.e7560.com/" ; System.out.println(getParam("wx973e01320ce8ebb6","1f7f1ac16dfaf7bd6e087b3831e1bbe2")); } }