您好,登錄后才能下訂單哦!
這篇文章主要介紹“H5和微信小程序之間的跳轉有哪些場景”,在日常操作中,相信很多人在H5和微信小程序之間的跳轉有哪些場景問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”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> <h4 align="center">正在跳轉到微信小程序...</h4> <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 >打開小程序</button> </template> </wx-open-launch-weapp> </div> <div id="desktop-web-container" class="hidden"> <p class="">請在手機打開網頁鏈接</p> </div> </div> </body> </html>
注意:這個場景限制很大,目前不能實現自動跳轉,需要用戶點擊標簽按鈕才能拉起小程序
到此,關于“H5和微信小程序之間的跳轉有哪些場景”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。