大家帮忙看看,微信分享功能,{“errMsg”:"config:fail"}我是根据官方文档存入的参数。
大家帮忙看看代码。。
下面是java中的代码
@RequestMapping("/jsp")
public ModelAndView jsp() throws JSONException{
String jsapi_ticket = WeixinUtil.getJSSDKAccessToken();
// 注意 URL 一定要动态获取,不能 hardcode
//这个url不是很清楚。
String url = "http://192.168.18.197:8080/ghpt/pages/ghInterface/twtpage/shareInterface.jsp";
Map<String, String> ret = sign(jsapi_ticket, url);
for (Map.Entry entry : ret.entrySet()) {
System.out.println("第一"+entry.getKey() + ", " + entry.getValue());
}
return new ModelAndView("ghInterface/twtpage/shareInterface",ret);
}
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 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;
}
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);
}
}
下面是jsp中的代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>分享</title>
<!-- 调用JS接口的页面引入如下JS文件 -->
<meta name="viewport"
content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=0;" />
<script type="text/javascript">
var timestamp = $("#timestamp").val();//时间戳
var nonceStr = $("#nonceStr").val();//随机串
var signature = $("#signature").val();//签名
</script>
</head>
<body onload="fun()">
<table cellpadding="0" cellspacing="0" align="center"
style="font-size: 15px; margin-top: 20%;">
<tr>
<td><input type="text" id="timestamp" value="${timestamp}" />
</td>
</tr>
<tr>
<td><input type="text" id="nonceStr" value="${nonceStr}" />
</td>
</tr>
<tr>
<td><input type="text" id="signature" value="${signature}" />
</td>
</tr>
</table>
<div style="margin-left: 26%;">
<span class="desc">判断当前客户端是否支持指定JS接口</span>
<button class="btn btn_primary" id="checkJsApi">checkJsApi</button>
<span class="desc">获取“分享到朋友圈”按钮点击状态及自定义分享内容接口</span>
<hr>
<button class="btn btn_primary" id="onMenuShareAppMessage">onMenuShareAppMessage</button>
</div>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
function fun() {
alert(location.href.split('#')[0]);
}
// 微信配置
wx.config({
debug : true,
appId : 'wxdd97b9378e910152',
timestamp : timestamp,
nonceStr : nonceStr,
signature : signature,
jsApiList : [ 'checkJsApi','onMenuShareAppMessage' ]
// 功能列表,我们要使用JS-SDK的什么功能
});
wx
.ready(function() {
// 1 判断当前版本是否支持指定 JS 接口,支持批量判断
document.querySelector('#checkJsApi').onclick = function() {
wx.checkJsApi({
jsApiList : [ 'getNetworkType', 'previewImage' ],
success : function(res) {
alert(JSON.stringify(res));
}
});
};
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在 页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready 函数中。
document.querySelector('#onMenuShareAppMessage').onclick = function() {
wx
.onMenuShareAppMessage({
title : '互联网之子',
desc : '在长大的过程中,我才慢慢发现,我身边的所有事,别人跟我说的所有事,那些所谓本来如此,注定如此的事,它们其实没有非得如此,事情是可以改变的。更重要的是,有些事既然错了,那就该做出改变。',
link : 'http://192.168.18.197:8080/ghpt/pages/ghInterface/twtpage/shareInterface.jsp',
imgUrl : 'http://img3.douban.com/view/movie_poster_cover/spst/public/p2166127561.jpg',
trigger : function(res) {
alert('用户点击发送给朋友');
},
success : function(res) {
alert('已分享');
},
cancel : function(res) {
alert('已取消');
},
fail : function(res) {
alert(JSON.stringify(res));
}
});
alert('已注册获取“发送给朋友”状态事件');
}
</script>
</html>
在线等啊