您好,登錄后才能下訂單哦!
這篇文章主要介紹“H5和微信小程序之間的跳轉(zhuǎn)有哪些場景”,在日常操作中,相信很多人在H5和微信小程序之間的跳轉(zhuǎn)有哪些場景問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”H5和微信小程序之間的跳轉(zhuǎn)有哪些場景”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
場景一:URL Scheme 打開小程序 (適用于從短信、郵件、微信外網(wǎng)頁等場景打開小程序)
TICKET 需要到微信小程序后臺工具頁面生成 -> 生成 URL Scheme 工具地址
weixin://dl/business/?t= *TICKET*
注意:Android系統(tǒng)不支持直接識別 URL Scheme ,需要通過 H5 頁面中轉(zhuǎn)打開
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應(yīng)用模式 --> <meta name="browsermode" content="application"> <!-- QQ應(yīng)用模式 --> <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 跳轉(zhuǎn)微信小程序</title> </head> <body> <h4 align="center">正在跳轉(zhuǎn)到微信小程序...</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 對應(yīng)的微信小程序,不能打開一個新的小程序,如需要打開其他小程序可以在小程序內(nèi)做一個中轉(zhuǎn)頁輔助跳轉(zhuǎn)到其他小程序
下面是 小程序訪問 H5 頁面的代碼
<view class="page-body"> <web-view src="https://os1.cc/"></web-view> </view>
場景三:微信環(huán)境內(nèi),首先打開 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('發(fā)生錯誤' + e) } </script> <!-- weui 樣式 --> <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.4.1/weui.min.css"></link> <!-- 調(diào)試用的移動端 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> <!-- 云開發(fā) 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, // 調(diào)試時可開啟 appId: '小程序 AppID', // <!-- replace --> timestamp: 0, // 必填,填任意數(shù)字即可 nonceStr: 'nonceStr', // 必填,填任意非空字符串即可 signature: 'signature', // 必填,填任意非空字符串即可 jsApiList: ['chooseImage'], // 必填,隨意一個接口即可 openTagList:['wx-open-launch-weapp'], // 填入打開小程序的開放標簽名 }) } else if (isDesktop) { // 在 pc 上則給提示引導(dǎo)到手機端打開 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 --> // 資源方環(huán)境 ID resourceEnv: '云開發(fā)環(huán)境 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 --> <!-- 跳轉(zhuǎn)小程序的開放標簽。文檔 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="要跳轉(zhuǎn)到的頁面路徑"> <!-- replace --> <template> <button >打開小程序</button> </template> </wx-open-launch-weapp> </div> <div id="desktop-web-container" class="hidden"> <p class="">請在手機打開網(wǎng)頁鏈接</p> </div> </div> </body> </html>
注意:這個場景限制很大,目前不能實現(xiàn)自動跳轉(zhuǎn),需要用戶點擊標簽按鈕才能拉起小程序
到此,關(guān)于“H5和微信小程序之間的跳轉(zhuǎn)有哪些場景”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。