H5 和 微信小程序 之前的跳轉歸納下來主要有以下三大場景。
場景一:URL Scheme 打開小程序 (適用於從短信、郵件、微信外網頁等場景打開小程序)
TICKET 需要到微信小程序後臺工具頁面生成 -> 生成 URL Scheme 工具地址
weixin://dl/business/?t= *TICKET*
注意:Android系統不支持直接識別 URL Scheme ,需要通過 H5 頁面中轉打開
location.href = 'weixin://dl/business/?t= *TICKET*'
完整 H5 直接打開小程序代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta content="email=no" name="format-detection" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<!-- uc強制豎屏 -->
<meta name="screen-orientation" content="portrait">
<!-- QQ強制豎屏 -->
<meta name="x5-orientation" content="portrait">
<!-- UC強制全屏 -->
<meta name="full-screen" content="yes">
<!-- QQ強制全屏 -->
<meta name="x5-fullscreen" content="true">
<!-- UC應用模式 -->
<meta name="browsermode" content="application">
<!-- QQ應用模式 -->
<meta name="x5-page-mode" content="app">
<!-- windows phone 點擊無高光 -->
<meta name="msapplication-tap-highlight" content="no">
<title>H5 喚起微信小程序</title>
</head>
<body>
<script type="text/javascript">
window.location.href = 'weixin://dl/business/?t=wfq66CCAmKt'
</script>
</body>
</html>
場景二:微信小程序打開 H5 頁面,然後 H5 頁面跳回微信小程序
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>H5 跳轉微信小程序</title>
</head>
<body>
<h3 align="center">正在跳轉到微信小程序...</h3>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script>
wx.miniProgram.navigateTo({url: 'project/home/views/home/index?spm=a.b.c.d.e'})
</script>
</body>
</html>
注意:這種方式只能回到拉起 H5 對應的微信小程序,不能打開一個新的小程序,如需要打開其他小程序可以在小程序內做一箇中轉頁輔助跳轉到其他小程序
下面是 小程序訪問 H5 頁面的代碼
<view class="page-body">
<web-view src="https://os1.cc/"></web-view>
</view>
場景三:微信環境內,首先打開 H5 頁面,然後 H5 頁面打開任意指定的微信小程序
<html>
<head>
<title>打開小程序</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
<script>
window.onerror = e => {
console.error(e)
alert('發生錯誤' + e)
}
</script>
<!-- weui 樣式 -->
<link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.4.1/weui.min.css"></link>
<!-- 調試用的移動端 console -->
<!-- <script src="https://cdn.jsdelivr.net/npm/eruda"></script> -->
<!-- <script>eruda.init();</script> -->
<!-- 公衆號 JSSDK -->
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<!-- 雲開發 Web SDK -->
<script src="https://res.wx.qq.com/open/js/cloudbase/1.1.0/cloud.js"></script>
<script>
function docReady(fn) {
if (document.readyState === 'complete' || document.readyState === 'interactive') {
fn()
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
docReady(async function() {
var ua = navigator.userAgent.toLowerCase()
var isWXWork = ua.match(/wxwork/i) == 'wxwork'
var isWeixin = !isWXWork && ua.match(/micromessenger/i) == 'micromessenger'
var isMobile = false
var isDesktop = false
if (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|IEMobile)/i)) {
isMobile = true
} else {
isDesktop = true
}
if (isWeixin) {
var containerEl = document.getElementById('wechat-web-container')
containerEl.classList.remove('hidden')
containerEl.classList.add('full', 'wechat-web-container')
var launchBtn = document.getElementById('launch-btn')
launchBtn.addEventListener('ready', function (e) {
console.log('開放標籤 ready')
})
launchBtn.addEventListener('launch', function (e) {
console.log('開放標籤 success')
})
launchBtn.addEventListener('error', function (e) {
console.log('開放標籤 fail', e.detail)
})
wx.config({
// debug: true, // 調試時可開啓
appId: '小程序 AppID', // <!-- replace -->
timestamp: 0, // 必填,填任意數字即可
nonceStr: 'nonceStr', // 必填,填任意非空字符串即可
signature: 'signature', // 必填,填任意非空字符串即可
jsApiList: ['chooseImage'], // 必填,隨意一個接口即可
openTagList:['wx-open-launch-weapp'], // 填入打開小程序的開放標籤名
})
} else if (isDesktop) {
// 在 pc 上則給提示引導到手機端打開
var containerEl = document.getElementById('desktop-web-container')
containerEl.classList.remove('hidden')
containerEl.classList.add('full', 'desktop-web-container')
} else {
var containerEl = document.getElementById('public-web-container')
containerEl.classList.remove('hidden')
containerEl.classList.add('full', 'public-web-container')
var c = new cloud.Cloud({
// 必填,表示是未登錄模式
identityless: true,
// 資源方 AppID
resourceAppid: '小程序 AppID', // <!-- replace -->
// 資源方環境 ID
resourceEnv: '雲開發環境 ID', // <!-- replace -->
})
await c.init()
window.c = c
var buttonEl = document.getElementById('public-web-jump-button')
var buttonLoadingEl = document.getElementById('public-web-jump-button-loading')
try {
await openWeapp(() => {
buttonEl.classList.remove('weui-btn_loading')
buttonLoadingEl.classList.add('hidden')
})
} catch (e) {
buttonEl.classList.remove('weui-btn_loading')
buttonLoadingEl.classList.add('hidden')
throw e
}
}
})
async function openWeapp(onBeforeJump) {
var c = window.c
const res = await c.callFunction({
name: 'public',
data: {
action: 'getUrlScheme',
},
})
console.warn(res)
if (onBeforeJump) {
onBeforeJump()
}
location.href = res.result.openlink
}
</script>
<style>
.hidden {
display: none;
}
.full {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.public-web-container {
display: flex;
flex-direction: column;
align-items: center;
}
.public-web-container p {
position: absolute;
top: 40%;
}
.public-web-container a {
position: absolute;
bottom: 40%;
}
.wechat-web-container {
display: flex;
flex-direction: column;
align-items: center;
}
.wechat-web-container p {
position: absolute;
top: 40%;
}
.wechat-web-container wx-open-launch-weapp {
position: absolute;
bottom: 40%;
left: 0;
right: 0;
display: flex;
flex-direction: column;
align-items: center;
}
.desktop-web-container {
display: flex;
flex-direction: column;
align-items: center;
}
.desktop-web-container p {
position: absolute;
top: 40%;
}
</style>
</head>
<body>
<div class="page full">
<div id="public-web-container" class="hidden">
<p class="">正在打開 “填入你的小程序名稱”...</p> <!-- replace -->
<a id="public-web-jump-button" href="javascript:" class="weui-btn weui-btn_primary weui-btn_loading" onclick="openWeapp()">
<span id="public-web-jump-button-loading" class="weui-primary-loading weui-primary-loading_transparent"><i class="weui-primary-loading__dot"></i></span>
打開小程序
</a>
</div>
<div id="wechat-web-container" class="hidden">
<p class="">點擊以下按鈕打開 “填入你的小程序名稱”</p> <!-- replace -->
<!-- 跳轉小程序的開放標籤。文檔 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html -->
<wx-open-launch-weapp id="launch-btn" username="小程序原始賬號 ID(gh_ 開頭的)" path="要跳轉到的頁面路徑"> <!-- replace -->
<template>
<button style="width: 200px; height: 45px; text-align: center; font-size: 17px; display: block; margin: 0 auto; padding: 8px 24px; border: none; border-radius: 4px; background-color: #07c160; color:#fff;">打開小程序</button>
</template>
</wx-open-launch-weapp>
</div>
<div id="desktop-web-container" class="hidden">
<p class="">請在手機打開網頁鏈接</p>
</div>
</div>
</body>
</html>
注意:這個場景限制很大,目前不能實現自動跳轉,需要用戶點擊標籤按鈕才能拉起小程序
場景三參考文檔:
https://blog.csdn.net/weixin_40762926/article/details/110474945
https://www.cnblogs.com/haqiao/p/13434771.html