微信h5实现分享给朋友url改变_H5微信分享(好友、朋友圈)默认缩略图、标题和描述设置...

本文记录了H5页面在微信中分享给好友和朋友圈时,如何自定义分享的标题、描述和缩略图。通过调用服务端接口获取微信配置,并在微信JS-SDK中设置分享参数来实现个性化分享。
摘要由CSDN通过智能技术生成

最近一个项目用到H5微信分享(好友、朋友圈),中间碰到好多难点,搜了好多才解决,现记录如下

先直接上源码

//分享朋友圈、好友

$.ajax({

url:weburl+'/p/jsapi?url='+encodeURIComponent(location.href),

type:"get",

datatype:"json",

async:true,

timeout : 30000,

error: function(XMLHttpRequest, textStatus, errorThrown){

console.log('进入到error')

console.log('status:'+XMLHttpRequest.status);

console.log('readyState:'+XMLHttpRequest.readyState);

console.log('textStatus:'+textStatus);

alert(textStatus);

weixinSign()

},

success: function(data){

wx.config({

debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来

appId:data.appId,

nonceStr:data.nonceStr,

timestamp:data.timestamp,

//url:"http://assistant.flow.wostore.cn/WoShieldServer/video/index.html?channelId=123",

signature:data.signature,

jsApiList: [

"onMenuShareTimeline",//分享朋友圈接口

"onMenuShareAppMessage",//分享给朋友接口

"checkJsApi"

] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2

});

wx.ready(function () {

console.log('分享前')

wx.onMenuShareTimeline({

title: $('.videoitem .title').text(),

desc: '最强王者视频悬赏令',

link: location.href,

imgUrl: $('.videoitem .video').prop('poster'),//'http://assistant.flow.wostore.cn/WoShieldServer/video/img/prize.jpg'

trigger: function (res) {

// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回

// alert('用户点击发送给朋友');

},

success: function (res) {

console.log('已分享');

},

cancel: function (res) {

console.log('已取消');

},

fail: function (res) {

console.log(JSON.stringify(res));

}

});

wx.onMenuShareAppMessage({

title: $('.videoitem .title').text(),

desc: '最强王者视频悬赏令',

link: location.href,

imgUrl: $('.videoitem .video').prop('poster'),//'http://assistant.flow.wostore.cn/WoShieldServer/video/img/prize.jpg'

trigger: function (res) {

// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回

// alert('用户点击发送给朋友');

},

success: function (res) {

console.log('已分享');

},

cancel: function (res) {

console.log('已取消');

},

fail: function (res) {

console.log(JSON.stringify(res));

}

});

wx.error(function (res) {

alert(res.errMsg);

});

});

},

complete : function(XMLHttpRequest,status){

if(status=='timeout'){

showtip("网络通信错误,请稍后再试!");

}

}

});

解析:需要服务端配合,这个接口weburl+'/p/jsapi?url='+encodeURIComponent(location.href),用来获取wx.config里面的几项配置信息,其他的配置参照微信官网文档陪直就行了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值