JS 微信判断及分享参数修改

时间:2022-09-12 19:45:48
var iswechat = (window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger');
var cloc = location.href.replace(/^#/, "").replace(/[^\/]*\.[^\/*]+$/, "");
var mainTitle = "",
    mainDesc = "",
    mainURL = cloc,
    mainImgUrl = cloc + "images/wechat_icon.jpg";

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
    iswechat = true;
    WeixinJSBridge.call('hideToolbar');
    onBridgeReady1();
});

function onBridgeReady1() {
    //转发朋友圈
    WeixinJSBridge.on("menu:share:timeline", function (e) {
        var data = {
            img_url: mainImgUrl,
            img_width: "120",
            img_height: "120",
            link: mainURL,
            //desc这个属性要加上,虽然不会显示,但是不加暂时会导致无法转发至朋友圈,
            desc: mainDesc,
            title: mainTitle
        };
        WeixinJSBridge.invoke("shareTimeline", data, function (res) {
            WeixinJSBridge.log(res.err_msg)
        });
    });
    //同步到微博
    WeixinJSBridge.on("menu:share:weibo", function () {
        WeixinJSBridge.invoke("shareWeibo", {
            "content": mainDesc,
            "url": mainURL
        }, function (res) {
            WeixinJSBridge.log(res.err_msg);
        });
    });
    //分享给朋友
    WeixinJSBridge.on('menu:share:appmessage', function (argv) {
        WeixinJSBridge.invoke("sendAppMessage", {
            img_url: mainImgUrl,
            img_width: "120",
            img_height: "120",
            link: mainURL,
            desc: mainDesc,
            title: mainTitle
        }, function (res) {
            WeixinJSBridge.log(res.err_msg)
        });
    });
};