萬盛學電腦網

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

網頁整合微信公眾號關注鏈接的跨平台解決方案

核心限制

微信內建瀏覽器(WebView)對 WeixinJSBridge.invoke('profile', ...) 有嚴格限制:

  • iOS: 僅在 mp.weixin.qq.com 域名下生效,自建頁面直接調用會返回 not_allow。
  • Android / Windows Phone: 支援通用鏈接 https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=原始ID 直接跳轉關注頁。

方案一:iOS 繞過限制 —— 文章頁跳轉法

  1. 在微信公眾平台後台發佈一篇圖文消息(內容可極簡)。
  2. 發佈後獲得該圖文的**永久連結**(如 https://mp.weixin.qq.com/s/xxx)。
  3. 在自建頁面放置「關注公眾號」按鈕,連結指向該圖文頁。
  4. 用戶點進圖文頁 → 點擊標題下的公眾號名片 → 進入資料頁 → 關注。

此為官方唯一認可的 iOS 通路,利用公眾平台域名的特權。

方案二:Android / WP 直連法

<a href="https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzxxxxxxxxxxxxx#wechat_redirect">
    點擊關注公眾號
</a>

__biz 參數為公眾號原始 ID(以 Mz 或 gh_ 開頭),可在公眾平台設定頁獲取。

方案三:統一入口頁(推薦)

建立一個中介頁面 /follow-wechat.html,根據 UA 自動分流:

<script>
(function() {
    const ua = navigator.userAgent.toLowerCase();
    const isIOS = /iphone|ipad|ipod/.test(ua);
    const isWeixin = /micromessenger/.test(ua);
    const biz = "Mzxxxxxxxxxxxxx"; // 替換為你的原始 ID

    let targetUrl;
    if (isWeixin) {
        if (isIOS) {
            // iOS 跳轉圖文頁(需預先發文)
            targetUrl = "https://mp.weixin.qq.com/s/你的圖文文章ID";
        } else {
            // Android/WP 直連
            targetUrl = `https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=${biz}#wechat_redirect`;
        }
        location.href = targetUrl;
    } else {
        // 非微信環境:顯示二維碼或引導複製微信號
        document.getElementById("qrcodeContainer").style.display = "block";
    }
})();
</script>

微信開放標籤(新版 SDK)

若已接入 weixin-js-sdk 並完成域名驗證,可使用 wx.openBusinessView(需認證服務號):

wx.openBusinessView({
    businessType: "friendProfile", // 或其他類型
    queryString: "username=gh_xxxxxxxx", // 原始 ID
    success: () => {},
    fail: err => { /* 降級到方案一/二 */ }
});

總結建議

  • **最穩妥**:統一導流至圖文消息頁(iOS/Android 通用)。
  • **體驗最好**:Android 直連 + iOS 圖文頁,配合 UA 判斷自動分流。
  • **非微信環境**:顯示公眾號二維碼圖片,引導長按識別。
copyright © 萬盛學電腦網 all rights reserved