uni中H5转发

和扫码类似,注意下获取url是

url = window.location.href.split('#')[0]

调取的API,新旧接口简单使用上没什么区别

                jsApiList: ['updateAppMessageShareData',//新接口'updateTimelineShareData',//新接口
                    'onMenuShareAppMessage',//即将废弃的接口
                    'onMenuShareTimeline'//即将废弃的接口
                ]

比较坑的是微信规则调整,直接点开发送的转发只能转发文字,要从收藏或者公众号的链接转发才可以,完整代码

const jweixin = require('./jweixin/index.js')


function share(data) {
    const url = window.location.href.split('#')[0]
    uni.request({
        url: '服务器链接',
        method: 'GET',
        data: {
            url: url
        },
        success: function(result) {
            jweixin.config({
                debug: false,
                appId: result.data.data.appId,
                timestamp: result.data.data.timestamp,
                nonceStr: result.data.data.nonceStr,
                signature: result.data.data.signature,
                jsApiList: [
                    'checkJsApi',
                    'updateAppMessageShareData',
                    'updateTimelineShareData',
                    'onMenuShareAppMessage',
                    'onMenuShareTimeline'
                ]
            });
            shareReady(data)
        }
    })
}

function shareReady(data) {
    jweixin.ready(function() {
        var shareData = {
            title: data.title || '',
            desc: data.desc || '',
            imgUrl:  data.imgUrl  || '',
            link: data.link || '',
            success: function(res) {
                // 分享后的一些操作                  
            },
            cancel: function(res) {}
        };
        //分享给朋友接口                  
        jweixin.updateAppMessageShareData(shareData);
        //分享到朋友圈接口                  
        jweixin.updateTimelineShareData(shareData);
    });
}
export {
    share
}

 

posted @ 2022-01-19 20:02  朱依漾  阅读(129)  评论(0编辑  收藏  举报