微信分享和微信支付

1.检测微信浏览器

let ua = navigator.userAgent.toLowerCase();

if(ua.match(/MicroMessenger/i) == 'micromessager'){

// 微信浏览器

}

2.微信分享

let shareInfo={
title: shareInfo.title,
brief: desc || '点击查看详情',
imageUrl: shareInfo.imgUrl ? vm.$store.state.picHead + shareInfo.imgUrl : 'http://crm.shequnyi.com/src/assets/img/sqtlogo.png',
url: url,
}
//分享到朋友圈
wx.onMenuShareTimeline({
title: shareInfo.title, // 分享标题
link: shareInfo.url, // 分享链接
imgUrl: shareInfo.imageUrl, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
},
trigger: function () {
console.log(shareInfo)
// 用户取消分享后执行的回调函数
}
});
//分享给好友
wx.onMenuShareAppMessage({
title: shareInfo.title, // 分享标题
desc: shareInfo.brief, // 分享描述
// desc: '认识更多社群会员,享受会员专属福利', // 分享描述
link: shareInfo.url, // 分享链接
imgUrl: shareInfo.imageUrl, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
// alert(shareInfo.title,'分享成功的标题')
},
cancel: function () {
// 用户取消分享后执行的回调函数
},
trigger: function () {
console.log(shareInfo)
// 用户取消分享后执行的回调函数
}
});
//分享到QQ
wx.onMenuShareQQ({
title: shareInfo.title, // 分享标题
desc: shareInfo.brief, // 分享描述
link: shareInfo.url, // 分享链接
imgUrl: shareInfo.imageUrl, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
//分享到微博
wx.onMenuShareWeibo({
title: shareInfo.title, // 分享标题
desc: shareInfo.brief, // 分享描述
link: shareInfo.url, // 分享链接
imgUrl: shareInfo.imageUrl, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
//分享到QQ空间
wx.onMenuShareQZone({
title: shareInfo.title, // 分享标题
desc: shareInfo.brief, // 分享描述
link: shareInfo.url, // 分享链接
imgUrl: shareInfo.imageUrl, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
3.微信支付
let temp=res.data.datas.payParam;
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: temp.appId, // 必填,公众号的唯一标识
timestamp: temp.timeStamp+'', // 必填,生成签名的时间戳
nonceStr: temp.nonceStr, // 必填,生成签名的随机串
signature: temp.paySign, // 必填,签名,见附录1
jsApiList: [
'chooseWXPay'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function(){
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
wx.chooseWXPay({
timestamp: temp.timeStamp+'', // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: temp.nonceStr, // 支付签名随机串,不长于 32 位
package: temp.wxPackage, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
signType:"MD5", // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign:temp.paySign, // 支付签名
success: function (res) {
    // 支付成功后的回调函数
},
cancel:function(){
  // 拒绝支付后的回调函数
}
});
});
wx.error(function(res){
// 弹出错误信息,而不是跳转到订单详情页
 
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});
posted @ 2026-07-14 10:12  海底流  阅读(9)  评论(0)    收藏  举报