我分享个微信分享带自定义图片的接口自己写。- var bttitle=$("#bookname").text();var btftitle=$("meta[name='description']").attr("content");var fximgUrl = pcurl + 'uploads/images/' + randomNum(0, 2510) + '.jpg';// 定义图片URLvar isWeixinhaha = navigator.userAgent.toLowerCase().indexOf('micromessenger') != -1; //检测用户是否正在使用微信内置浏览器//新增微信公众号分享(JS开发)if(isWeixinhaha) {//如果是微信里面document.write("<script src="https:\/\/res2.wx.qq.com\/open\/js\/jweixin-1.6.0.js"><\/script>"); $('#wechatFriend').removeAttr('onclick');$('#wechatTimeline').removeAttr('onclick');$('#qqFriend').removeAttr('onclick');$('#qZone').removeAttr('onclick');// 移除原有的 onclick 事件处理器$.ajax({type: "GET",url: pcurl + 'ecmsapi/index.php?mod=wechat&act=gzh&ticket=ticket',dataType: "json",data: {"url": location.href.split("#")[0]},success: function(json) {wx.config({debug: false, // 开启调试模式false与trueappId: json.data.appid, // 必填,公众号的唯一标识timestamp: json.data.timestamp, // 必填,生成签名的时间戳nonceStr: json.data.noncestr, // 必填,生成签名的随机串signature: json.data.signature, // 必填,签名jsApiList: ['updateAppMessageShareData','updateTimelineShareData','getNetworkType'] // 必填,需要使用的JS接口列表});$('#wechatFriend').on('click', function(e) { wx.updateAppMessageShareData({title: bttitle,desc: btftitle,link: location.href,imgUrl: fximgUrl,success: function (res) {layer.msg('分享给好友成功', { offset: '300px' });},fail: function () {layer.msg('无权限');},});});$('#wechatTimeline').on('click', function(e) { wx.updateTimelineShareData({title: bttitle,link: location.href,imgUrl: fximgUrl,success: function (res) {layer.msg('分享朋友圈成功', { offset: '300px' });},fail: function () {layer.msg('无权限');},});});$('#qqFriend').on('click', function(e) { wx.updateAppMessageShareData({title: bttitle,desc: btftitle,link: location.href,imgUrl: fximgUrl,success: function (res) {layer.msg('分享给好友成功', { offset: '300px' });},fail: function () {layer.msg('无权限');},});});$('#qZone').on('click', function(e) { wx.updateTimelineShareData({title: bttitle,desc: btftitle,link: location.href,imgUrl: fximgUrl,success: function (res) {layer.msg('分享QQ空间成功', { offset: '300px' });},fail: function () {layer.msg('无权限');},});});wx.error(function (res) {console.log("Error message:", res.errMsg);});}});//AJAX异步请求结束}
复制代码 |