微信内置浏览器(WebView)中特有的javascript API(javascript Interface),随着微信官方的调整,部分API已经不能直接使用,比如类似直接分享到朋友圈WeixinJSBridge.invoke(shareTimeline,data,callback) 这样的功能,直接调用,会得到一个访问拒绝的response
function WeiXinShareBtn() {
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
alert("请先通过微信搜索 wow36kr 添加36氪为好友,通过微信分享文章 ");
} else {
WeixinJSBridge.invoke('shareTimeline', {
"title": "36氪",
"link": "http://www.36kr.com",
"desc": "关注互联网创业",
"img_url": "http://www.36kr.com/assets/images/apple-touch-icon.png"
});
}
}
此方法已过时,被微信禁用了。只能用微信官方的接口
1、前台脚本
<script src="../Scripts/jquery-1.10.2.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript"></script>
<script>
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '<%=appid%>', // 必填,公众号的唯一标识
timestamp: '<%=timestamp%>', // 必填,生成签名的时间戳
nonceStr: '<%=nonce%>', // 必填,生成签名的随机串
signature: '<%=signature%>',// 必填,签名,见附录1
jsApiList: ["checkJsApi", "onMenuShareTimeline", "onMenuShareAppMessage"] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
//当前客户端版本是否支持指定JS接口,分享到朋友圈,分享给朋友
});
wx.ready(function () {
wx.checkJsApi({
jsApiList: ['chooseImage'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function (res) {
alert(JSON.stringify(res));
// 以键值对的形式返回,可用的api值true,不可用为false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
}
});
//朋友圈
wx.onMenuShareTimeline({
title: '春天里的健康', // 分享标题
desc: '春天里的健康春天里的健康春天里的健康', // 分享描述
link: '<%=thisUrl%>', // 分享链接
imgUrl: 'https://imgsa.baidu.com/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=c31e0863bf315c60579863bdecd8a076/4034970a304e251fb62a408ea486c9177e3e53e2.jpg',
success: function (res) {
alert('已分享');
},
cancel: function (res) {
alert('已取消');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
//朋友
wx.onMenuShareAppMessage({
title: '春天里的健康', // 分享标题
desc: '春天里的健康春天里的健康春天里的健康', // 分享描述
link: '<%=thisUrl%>', // 分享链接
imgUrl: 'https://imgsa.baidu.com/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=c31e0863bf315c60579863bdecd8a076/4034970a304e251fb62a408ea486c9177e3e53e2.jpg', // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户确认分享后执行的回调函数
alert("分享成功");
},
cancel: function () {
// 用户取消分享后执行的回调函数
alert("取消分享");
}
});
});
</script>
2、后台代码
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Helpers;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace jkmobile.share
{
public partial class ReportShare : System.Web.UI.Page
{
public string appid = "", timestamp="", nonce="",thisUrl="", signature="";
protected void Page_Load(object sender, EventArgs e)
{
WXShareModel wxmodel = new WXShareModel();
wxmodel = Ge_WXShareModel();
appid = wxmodel.appid;
timestamp = wxmodel.timestamp;
nonce = wxmodel.nonce;
thisUrl = wxmodel.thisUrl;
signature = wxmodel.signature;
}
#region 微信分享
public WXShareModel Ge_WXShareModel()
{
string APPID = ConfigurationManager.AppSettings["appid"];
string secret = ConfigurationManager.AppSettings["secret"];
WXShareModel fxModel = new WXShareModel();
fxModel.appid = APPID;
fxModel.timestamp = JSSDKHelper.GetTimestamp();
fxModel.nonce = JSSDKHelper.GetNoncestr();
fxModel.thisUrl = Request.Url.ToString().Split('#')[0];
fxModel.ticket = JsApiTicketContainer.TryGetJsApiTicket(APPID, secret);
//最后一个参数url,必须为当前的网址
var signature = JSSDKHelper.GetSignature(fxModel.ticket, fxModel.nonce, fxModel.timestamp, fxModel.thisUrl);
fxModel.signature = signature;
return fxModel;
}
#endregion
}
public class WXShareModel
{
/// <summary>
/// 公众号的唯一标识
/// </summary>
public string appid { get; set; }
/// <summary>
/// 生成签名的随机串
/// </summary>
public string nonce { get; set; }
/// <summary>
/// 生成签名的时间戳
/// </summary>
public string timestamp { get; set; }
/// <summary>
/// 签名
/// </summary>
public string signature { get; set; }
public string ticket { get; set; }
/// <summary>
/// 分享的地址
/// </summary>
public string thisUrl { get; set; }
}
}
请引用微信官方封装好的dll
Senparc.Weixin.dll
Senparc.Weixin.MP.dll
见下载资源 源码下载