公众号 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);

posted @   Monroe_Yu  阅读(2403)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示