萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

微信內置 WeixinJSBridge 使用實例:分享朋友圈、發送好友、分享微博、關注公眾號

前言

微信內置瀏覽器暴露私有 JavaScript 物件 WeixinJSBridge,透過 invoke 呼叫原生功能:分享到朋友圈、發送給好友、分享到騰訊微博、關注指定公眾號等。本文整理實用封裝函式,附帶參數說明與調用時機。

核心 API 封裝

1. 分享到朋友圈

function weixinShareTimeline(title, desc, link, imgUrl) {
    WeixinJSBridge.invoke('shareTimeline', {
        "img_url": imgUrl,
        // "img_width": "640",
        // "img_height": "640",
        "link": link,
        "desc": desc,
        "title": title
    });
}

2. 發送給好友

function weixinSendAppMessage(title, desc, link, imgUrl) {
    WeixinJSBridge.invoke('sendAppMessage', {
        // "appid": appId,
        "img_url": imgUrl,
        // "img_width": "640",
        // "img_height": "640",
        "link": link,
        "desc": desc,
        "title": title
    });
}

3. 分享到騰訊微博

function weixinShareWeibo(title, link) {
    WeixinJSBridge.invoke('shareWeibo', {
        "content": title + ' ' + link,
        "url": link
    });
}

4. 關注指定公眾號

function weixinAddContact(username) {
    WeixinJSBridge.invoke('addContact', { webtype: '1', username: username }, function (res) {
        // res.err_msg 可能值:
        // 'add_contact:added'   已關注
        // 'add_contact:cancel'  取消關注
        // 'add_contact:ok'      關注成功
        WeixinJSBridge.log(res.err_msg);
        if (res.err_msg === 'add_contact:added' || res.err_msg === 'add_contact:ok') {
            // 關注成功或已關注,執行後續邏輯
        }
    });
}

調用時機:WeixinJSBridgeReady 事件

WeixinJSBridge 在微信瀏覽器內部初始化完成後會觸發 WeixinJSBridgeReady 事件,**必須在該事件後**才能安全調用 invoke,否則物件可能尚未定義。

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
    // 綁定分享按鈕點擊事件
    document.getElementById('btnShareTimeline').onclick = function () {
        weixinShareTimeline('標題', '描述', 'https://example.com', 'https://example.com/img.jpg');
    };
    document.getElementById('btnSendFriend').onclick = function () {
        weixinSendAppMessage('標題', '描述', 'https://example.com', 'https://example.com/img.jpg');
    };
    document.getElementById('btnFollow').onclick = function () {
        weixinAddContact('gh_xxxxxxxx'); // 公眾號原始ID
    };
}, false);

進階:自定義分享內容 (菜單按鈕觸發)

若需在用戶點擊右上角選單「分享給朋友」/「分享到朋友圈」時自定義內容,需監聽 menu:share:appmessage / menu:share:timeline 事件並在回調中 invoke:

document.addEventListener('WeixinJSBridgeReady', function () {
    // 分享給好友
    WeixinJSBridge.on('menu:share:appmessage', function (argv) {
        WeixinJSBridge.invoke('sendAppMessage', {
            "img_url": 'https://example.com/share.jpg',
            "link": 'https://example.com/page',
            "desc": '自定義描述',
            "title": '自定義標題'
        });
    });

    // 分享到朋友圈
    WeixinJSBridge.on('menu:share:timeline', function (argv) {
        WeixinJSBridge.invoke('shareTimeline', {
            "img_url": 'https://example.com/share.jpg',
            "link": 'https://example.com/page',
            "desc": '自定義描述',
            "title": '自定義標題'
        });
    });
});

注意事項

  • 僅在微信內置瀏覽器生效,外部瀏覽器 WeixinJSBridge 為 undefined。
  • 圖片建議使用 HTTPS、尺寸 300x300 以上、大小 < 2MB。
  • 分享鏈接域名需在微信公眾平台「JS 接口安全域名」白名單中。
  • 新版微信 (6.0.2+) 推薦使用 微信 JS-SDK (wx.ready/wx.updateAppMessageShareData),WeixinJSBridge 屬舊版內部接口,隨時可能變更或移除。

總結

本文提供 WeixinJSBridge 核心分享與關注功能的封裝與調用範例。生產環境強烈建議遷移至官方 微信 JS-SDK,以獲得穩定、文檔完善、持續維護的介面。

copyright © 萬盛學電腦網 all rights reserved