如何修改 微信分享页链接中的标题

259 阅读1分钟

middle_img_v2_59bf3bb1-6851-446c-8457-f03053b4246g.png

实现代码

document.getElementsByTagName('title')[0].innerText =
  response.data.data.content || '指定标题' 
  //如果是在iframe中的话则
  window.parent.document.getElementsByTagName('title')[0].innerText =
  response.data.data.content || '指定标题' 
  

关于整体修改

// 这个是调用的官方微信的链接,前缀设计'//'是因为可以根据http和http不同自动的去切换
<script src="//res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>

//根据后端对接好,需要后端返回一段数据,根据数据去做处理配置即可

//举例数据
var wechat = {"appId":"wx753f43fe054313b7","nonceStr":"vzsOfE8DJ0Ct9pjf","timestamp":1645168835,"signature":"b2592c343c1bca779fdbe889eb8210919b3deb61","title":"\u6587\u7ae0\u70b9\u8d5e\u6d4b\u8bd5","link":"http:\/\/m.cmstop.baotounews.102.autops.xyz\/p\/4.html","desc":"test","imgUrl":"http:\/\/img.baotounews.102.autops.xyz\/a\/thumb\/10001\/202112\/c302f1c45b13346a07576a6049d46c8b.png!w300_h300.png","debug":""}; console.log(wechat);






        var wechat = {json_encode($wechat)};
        wx.config({
            debug: wechat.debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
            appId: wechat.appId, // 必填,公众号的唯一标识
            timestamp: wechat.timestamp, // 必填,生成签名的时间戳
            nonceStr: wechat.nonceStr, // 必填,生成签名的随机串
            signature: wechat.signature,// 必填,签名,见附录1
            jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
        });
        wx.ready(function(){
            //微信环境下重新加载音频
            if(typeof loadAudio == "function"){
                loadAudio();
            }
            //分享到朋友圈
            wx.onMenuShareTimeline({
                title: wechat.title, // 分享标题
                link: wechat.link, // 分享链接
                imgUrl: wechat.imgUrl, // 分享图标
                success: function () {
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
            //获取“分享给朋友”按钮点击状态及自定义分享内容接口
            wx.onMenuShareAppMessage({
                title: wechat.title, // 分享标题
                desc: wechat.desc, // 分享描述
                link: wechat.link, // 分享链接
                imgUrl: wechat.imgUrl, // 分享图标,注意图标宽高要大于300px
                type: '', // 分享类型,music、video或link,不填默认为link
                dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
                success: function () {
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                },
                fail:function (res) {
//                    alert("分享失败:" +JSON.stringify(res));
                    console.log("分享失败:" +JSON.stringify(res));
                }
            });
            //获取“分享到QQ”按钮点击状态及自定义分享内容接口
            wx.onMenuShareQQ({
                title: wechat.title, // 分享标题
                desc: wechat.desc, // 分享描述
                link: wechat.link, // 分享链接
                imgUrl: wechat.imgUrl, // 分享图标
                success: function () {
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
            //获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口
            wx.onMenuShareWeibo({
                title: wechat.title, // 分享标题
                desc: wechat.desc, // 分享描述
                link: wechat.link, // 分享链接
                imgUrl: wechat.imgUrl, // 分享图标
                success: function () {
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
            //获取“分享到QQ空间”按钮点击状态及自定义分享内容接口
            wx.onMenuShareQZone({
                title: wechat.title, // 分享标题
                desc: wechat.desc, // 分享描述
                link: wechat.link, // 分享链接
                imgUrl: wechat.imgUrl, // 分享图标
                success: function () {
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
        });
        wx.error(function(res){
            // config信息验证失败会执行error函数
            //alert(JSON.stringify(res));
            console.log(JSON.stringify(res));
        });
</script>