微信內建瀏覽器(WebView)對 WeixinJSBridge.invoke('profile', ...) 有嚴格限制:
mp.weixin.qq.com 域名下生效,自建頁面直接調用會返回 not_allow。https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=原始ID 直接跳轉關注頁。https://mp.weixin.qq.com/s/xxx)。此為官方唯一認可的 iOS 通路,利用公眾平台域名的特權。
<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>
若已接入 weixin-js-sdk 並完成域名驗證,可使用 wx.openBusinessView(需認證服務號):
wx.openBusinessView({
businessType: "friendProfile", // 或其他類型
queryString: "username=gh_xxxxxxxx", // 原始 ID
success: () => {},
fail: err => { /* 降級到方案一/二 */ }
});