公众号 H5页面分享链接携带图片与标题(自定义卡片式分享链接)

第一步:引入微信sdk(npm install weixin-js-sdk --save)

第二步:创建wechat.js

import wx from "../node_modules/weixin-js-sdk/"
export default {
getConfig(infoTitle, infoDesc, infoLink, infoImg) { //自定义分享所需要的参数
uni.request({
url: '后台接口',
method: 'POST',
header: {
"content-Type": "application/x-www-form-urlencoded",
},
data:{
url:"线上链接"
},
success: (res) => {
if(res.data.code==1){
wx.config({
debug: false, //测试时候用true 能看见wx.config的状态是否是config:ok
appId: res.data.data.appid, // 必填,公众号的唯一标识(公众号的APPid)
timestamp: res.data.data.timestamp, // 必填,生成签名的时间戳
nonceStr: res.data.data.noncestr, // 必填,生成签名的随机串
signature: res.data.data.signature, // 必填,签名
jsApiList: [
'onMenuShareTimeline', // 分享给好友
'onMenuShareAppMessage', // 分享到朋友圈
'updateAppMessageShareData', // 分享给好友1.4
'updateTimelineShareData' // 分享到朋友圈1.4
], // 必填,需要使用的JS接口列表
openTagList: ['wx-open-launch-app'] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
});
wx.ready(function() {
var shareData = {
title: infoTitle, // 分享标题
desc: infoDesc, // 分享描述
link: infoLink, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: infoImg, // 分享图标
success: function(res) {}
};
//自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容(1.4.0)
wx.updateTimelineShareData(shareData);
//自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0)
wx.updateAppMessageShareData(shareData);
})
}else{
uni.showToast({
title:res.data.msg,
icon:"none",
duration:2000
})
}
}
});
}
}

第三步:main.js引入

import wechat from 'static/wechat.js'

Vue.prototype.$wx = wechat

第四步:页面调用

import wx from "../../static/jweixin-1.4.0.js"

let title = that.title
let infoDesc = that.contents
let infoLink = 'xx'
let infoImg= that.imgurl
that.$wx.getConfig(title,infoDesc,infoLink,infoImg);

..
原文地址:https://www.cnblogs.com/shoolnight/p/14805768.html