微信小程序h5頁面跳轉(zhuǎn)小程序的超詳細(xì)講解
h5跳轉(zhuǎn)小程序
實戰(zhàn)項目從無到完整的h5跳轉(zhuǎn)小程序經(jīng)驗,跳轉(zhuǎn)方式分為云函數(shù)跳轉(zhuǎn)和開放性標(biāo)簽跳轉(zhuǎn)。
開放性標(biāo)簽是針對微信里面瀏覽器使用的,而云函數(shù)跳轉(zhuǎn)在外部以及微信里面都可以使用。
建議需要的同學(xué)先全部瀏覽一遍本文。內(nèi)容較長較為啰嗦。
一、不用開放性標(biāo)簽(wx-open-launch-weapp)
官網(wǎng)鏈接:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/staticstorage/jump-miniprogram.html
官方網(wǎng)站寫的是比較好的,但是還不夠詳細(xì)和許多坑點,下面是實例代碼:
實例代碼鏈接:https://postpay-2g5hm2oxbbb721a4-1258211818.tcloudbaseapp.com/jump-mp.html
<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" rel="external nofollow" ></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 } console.warn('ua', ua) console.warn(ua.match(/MicroMessenger/i) == 'micromessenger') var m = ua.match(/MicroMessenger/i) console.warn(m && m[0] === 'micromessenger') 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('開放標(biāo)簽 ready') }) launchBtn.addEventListener('launch', function (e) { console.log('開放標(biāo)簽 success') }) launchBtn.addEventListener('error', function (e) { console.log('開放標(biāo)簽 fail', e.detail) }) wx.config({ // debug: true, // 調(diào)試時可開啟 appId: 'wxe5f52902cf4de896', timestamp: 0, // 必填,填任意數(shù)字即可 nonceStr: 'nonceStr', // 必填,填任意非空字符串即可 signature: 'signature', // 必填,填任意非空字符串即可 jsApiList: ['chooseImage'], // 安卓上必填一個,隨機(jī)即可 openTagList:['wx-open-launch-weapp'], // 填入打開小程序的開放標(biāo)簽名 }) } else if (isDesktop) { // 在 pc 上則給提示引導(dǎo)到手機(jī)端打開 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: 'wxe5f52902cf4de896', // 資源方環(huán)境 ID resourceEnv: 'postpay-2g5hm2oxbbb721a4', }) 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> <a id="public-web-jump-button" href="javascript:" rel="external nofollow" 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> <!-- 跳轉(zhuǎn)小程序的開放標(biāo)簽。文檔 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html --> <wx-open-launch-weapp id="launch-btn" username="gh_d43f693ca31f" path="/page/component/index"> <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="">請在手機(jī)打開網(wǎng)頁鏈接</p> </div> </div> </body> </html>
純es5寫的代碼模塊,可以改造成任意架構(gòu)代碼。下面講一下怎么改成自己的項目里面以及坑點:
1、調(diào)試工具可以去掉:
<!-- 調(diào)試用的移動端 console --> <script src="https://cdn.jsdelivr.net/npm/eruda"></script> <script>eruda.init();</script>
2、Appid替換:怎么找到我們需要的id呢?替換位置都在哪呢?
1)登錄微信公共平臺
2)登錄之后點擊右上角頭像的名字,基本設(shè)置里最下面,appid和原始id都在這里。
3)替換位置
// 這里是給開放性標(biāo)簽準(zhǔn)備的,第一種方式暫時用不到 wx.config({ debug: true, // 調(diào)試時可開啟 appId: '替換的位置', timestamp: 0, // 必填,填任意數(shù)字即可 nonceStr: 'nonceStr', // 必填,填任意非空字符串即可 signature: 'signature', // 必填,填任意非空字符串即可 jsApiList: ['chooseImage'], // 安卓上必填一個,隨機(jī)即可 openTagList:['wx-open-launch-weapp'], // 填入打開小程序的開放標(biāo)簽名 })
<wx-open-launch-weapp id="launch-btn" username="原始id" path="跳轉(zhuǎn)頁面的路徑"> <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>
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: '替換位置', // 資源方環(huán)境 ID resourceEnv: '云函數(shù)id', // 這里下面會講 }) await c.init()
4)修改邏輯
這里把判斷固定寫死false,就是想讓網(wǎng)頁一直走最后else的邏輯,以后想用開放性標(biāo)簽,就把這里還原即可。代碼塊放下面了。
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 } console.warn('ua', ua) console.warn(ua.match(/MicroMessenger/i) == 'micromessenger') var m = ua.match(/MicroMessenger/i) console.warn(m && m[0] === 'micromessenger') if (false) { var containerEl = document.getElementById('wechat-web-container') containerEl.classList.remove('hidden') containerEl.classList.add('full', 'wechat-web-container')
5)跳轉(zhuǎn)函數(shù)改造
async function openWeapp(onBeforeJump) { var c = window.c const res = await c.callFunction({ name: 'test111', // 這里是創(chuàng)建云函數(shù)的云函數(shù)名稱,下面文章有說道 data: { action: 'getUrlScheme', // 這是云函數(shù)中函數(shù)名字 }, }) console.warn(res) if (onBeforeJump) { onBeforeJump() } location.href = res.result.openlink }
3、云函數(shù)的構(gòu)建:
1)我這是構(gòu)建好的:上文替換欄里面最后一張圖說的云函數(shù)ID就是圖中紅色勾選框
沒構(gòu)建好的可以去:https://jingyan.baidu.com/article/a3aad71ac25f86f0fb0096ee.html
按照圖文一步一步走就行了,我這里就不寫那么多了。
點擊云函數(shù),然后點擊新構(gòu)建函數(shù):
云函數(shù)名稱填寫的時候要注意點,這個函數(shù)名test111就是我們要用到的云函數(shù)名稱!
2)調(diào)用的時候這里日志會顯示你在云函數(shù)里面寫的打印console內(nèi)容。
3)云函數(shù)權(quán)限,測試的時候建議修改為所有用戶都可以訪問!
4)創(chuàng)建的時候,并不是一定會創(chuàng)建云函數(shù)目錄,這時候需要手動創(chuàng)建,首先在根目錄下面創(chuàng)建一個你喜歡的文件名字,然后在project.config.json中添上你所創(chuàng)建的目錄名字
右擊這個目錄的時候出現(xiàn)彈框這些選項,開發(fā)完畢后選擇上傳不上傳node_modules選項就行了。(這是直接發(fā)到線上,只要線上代碼不調(diào)用沒啥影響,放心用)
在此目錄下會出現(xiàn)test111文件夾,然后打開此文件夾,執(zhí)行npm init,在package.json中寫上:
{ "name": "test111", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "wx-server-sdk": "~2.3.2" } }
然后創(chuàng)建index.js文件,寫入:
// 云函數(shù)入口文件 const cloud = require('wx-server-sdk') let page = false; cloud.init(); // 云函數(shù)入口函數(shù) exports.main = async (event, context) => { const wxContext = cloud.getWXContext(); console.log('---------------') console.log(event) var query = {} if (event.query && event.query.page && event.query.page == 'showImage'){ page = true; query.id = event.query.id; } else { page = false; } switch (event.action) { case 'getUrlScheme': { // 自定義函數(shù)名 return getUrlScheme(page,query) } } return '沒有找到對應(yīng)的函數(shù)' } async function getUrlScheme(flag,param) { // 自定義函數(shù) console.log('-------getUrlScheme-----') console.log(flag) console.log(param) param = param || {}; return cloud.openapi.urlscheme.generate({ jumpWxa: { path: '', // 跳轉(zhuǎn)路徑,一定是線上有的頁面路徑,否則會報錯 query: `id=${param.id}`, // 注意傳參格式,可以不傳參 }, // 如果想不過期則置為 false,并可以存到數(shù)據(jù)庫 isExpire: false, // 一分鐘有效期 expireTime: parseInt(Date.now() / 1000 + 60), }) }
到此,云函數(shù)跳轉(zhuǎn),就結(jié)束了,有什么問題可以評論告訴我。
二、使用開放性標(biāo)簽
使用開性標(biāo)簽,把上面修改邏輯的代碼還原。
然后就是幾個問題:
一、域名的坑:
1、開發(fā)使用者必須是已認(rèn)證的服-務(wù)-號(服務(wù)號可以置頂,訂閱號不能),服務(wù)號綁定“JS接口安全域名”下的網(wǎng)頁可使用此標(biāo)簽跳轉(zhuǎn)任意合法合規(guī)的小程序。
2、開發(fā)使用者必須是已認(rèn)證的非個人主體的小程序,使用小程序云開發(fā)的靜態(tài)網(wǎng)站托管綁定的域名下的網(wǎng)頁,可以使用此標(biāo)簽跳轉(zhuǎn)任意合法合規(guī)的小程序。
3、必須部署到正式服務(wù)器,測試公眾號不顯示圖標(biāo)。
4、必須是在已認(rèn)證的服務(wù)號中做JS接口安全域名驗證,“JS接口安全域名”和“IP白名單”都要,IP是指獲取微信簽名的服務(wù)器的IP。
注意:1、2兩個條件必須滿足其中一個;3、4兩個條件必須滿足,否則標(biāo)簽中的按鈕不會顯示(微信文檔不講武德,寫的不清楚,稍微不注意就會掉坑)。
官方文檔入口:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html#21
還有幾點:
1、微信開發(fā)者工具,“verifyOpenTagList”:[],是顯示不了圖標(biāo)的。微信開發(fā)工具和真機(jī)測試結(jié)果可能不一樣,一定要用真機(jī)測試。安卓和ios測試結(jié)果也可能不同,都要測試。
2、小程序的web-view不支持wx-open-launch-weapp。
3、jsApiList:[‘chooseImage’, ‘previewImage’](必須有,不然安卓不顯示)
4、微信版本要求為:7.0.12及以上。 系統(tǒng)版本要求為:iOS 10.3及以上、Android 5.0及以上
原文鏈接:http://www.dbjr.com.cn/article/273996.htm
二:config配置坑:簽名一定要找服務(wù)端要,生成規(guī)則讓服務(wù)端自己看。
// 這里是給開放性標(biāo)簽準(zhǔn)備的,第一種方式暫時用不到 wx.config({ debug: true, // 調(diào)試時可開啟 appId: '替換的位置', timestamp: 0, // 必填,填任意數(shù)字即可 nonceStr: 'nonceStr', // 必填,填任意非空字符串即可 signature: 'signature', // 必填,服務(wù)端生成的,不能任意 jsApiList: ['chooseImage'], // 安卓上必填一個, openTagList:['wx-open-launch-weapp'], // 填入打開小程序的開放標(biāo)簽名 })
ps:小程序測試的時候使用線上首頁測試最佳~
總結(jié)
到此這篇關(guān)于微信小程序h5頁面跳轉(zhuǎn)小程序的文章就介紹到這了,更多相關(guān)微信小程序h5頁面跳轉(zhuǎn)小程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js 右側(cè)浮動層效果實現(xiàn)代碼(跟隨滾動)
因為項目上有這樣的需求,在網(wǎng)上也查了些東西,之前是想找個差不多類似的套用一下。后來發(fā)覺沒有合適的,因時間緊迫就自己動手寫了一個簡單的 ,示例代碼如下 兼容火狐和IE7+2015-11-11JS中的算法與數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(Binary Sort Tree)實例詳解
這篇文章主要介紹了JS中的算法與數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(Binary Sort Tree),結(jié)合實例形式詳細(xì)分析了二叉查找樹(Binary Sort Tree)的原理、定義、遍歷、查找、插入、刪除等常見操作技巧,需要的朋友可以參考下2019-08-08JS中使用new Date(str)創(chuàng)建時間對象不兼容firefox和ie的解決方法(兩種)
這篇文章主要介紹了JS中使用new Date(str)創(chuàng)建時間對象不兼容firefox和ie的解決方法的相關(guān)資料,需要的朋友可以參考下2016-12-12微信小程序基于slider組件動態(tài)修改標(biāo)簽透明度的方法示例
這篇文章主要介紹了微信小程序基于slider組件動態(tài)修改標(biāo)簽透明度的方法,可通過slider組件拖動實現(xiàn)圖片透明度的改變功能,涉及微信小程序事件綁定、base64格式圖片載入及slider組件使用技巧,需要的朋友可以參考下2017-12-12