微信小程序h5頁(yè)面跳轉(zhuǎn)小程序的超詳細(xì)講解
h5跳轉(zhuǎn)小程序
實(shí)戰(zhàn)項(xiàng)目從無(wú)到完整的h5跳轉(zhuǎn)小程序經(jīng)驗(yàn),跳轉(zhuǎn)方式分為云函數(shù)跳轉(zhuǎn)和開(kāi)放性標(biāo)簽跳轉(zhuǎn)。
開(kāi)放性標(biāo)簽是針對(duì)微信里面瀏覽器使用的,而云函數(shù)跳轉(zhuǎn)在外部以及微信里面都可以使用。
建議需要的同學(xué)先全部瀏覽一遍本文。內(nèi)容較長(zhǎng)較為啰嗦。
一、不用開(kāi)放性標(biāo)簽(wx-open-launch-weapp)
官網(wǎng)鏈接:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/staticstorage/jump-miniprogram.html
官方網(wǎng)站寫(xiě)的是比較好的,但是還不夠詳細(xì)和許多坑點(diǎn),下面是實(shí)例代碼:
實(shí)例代碼鏈接:https://postpay-2g5hm2oxbbb721a4-1258211818.tcloudbaseapp.com/jump-mp.html
<html>
<head>
<title>打開(kāi)小程序</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ā)生錯(cuò)誤' + e)
}
</script>
<!-- weui 樣式 -->
<link rel="stylesheet" rel="external nofollow" ></link>
<!-- 調(diào)試用的移動(dòng)端 console -->
<script src="https://cdn.jsdelivr.net/npm/eruda"></script>
<script>eruda.init();</script>
<!-- 公眾號(hào) JSSDK -->
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<!-- 云開(kāi)發(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('開(kāi)放標(biāo)簽 ready')
})
launchBtn.addEventListener('launch', function (e) {
console.log('開(kāi)放標(biāo)簽 success')
})
launchBtn.addEventListener('error', function (e) {
console.log('開(kāi)放標(biāo)簽 fail', e.detail)
})
wx.config({
// debug: true, // 調(diào)試時(shí)可開(kāi)啟
appId: 'wxe5f52902cf4de896',
timestamp: 0, // 必填,填任意數(shù)字即可
nonceStr: 'nonceStr', // 必填,填任意非空字符串即可
signature: 'signature', // 必填,填任意非空字符串即可
jsApiList: ['chooseImage'], // 安卓上必填一個(gè),隨機(jī)即可
openTagList:['wx-open-launch-weapp'], // 填入打開(kāi)小程序的開(kāi)放標(biāo)簽名
})
} else if (isDesktop) {
// 在 pc 上則給提示引導(dǎo)到手機(jī)端打開(kāi)
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="">正在打開(kāi) “小程序示例”...</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>
打開(kāi)小程序
</a>
</div>
<div id="wechat-web-container" class="hidden">
<p class="">點(diǎn)擊以下按鈕打開(kāi) “小程序示例”</p>
<!-- 跳轉(zhuǎn)小程序的開(kāi)放標(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;">打開(kāi)小程序</button>
</template>
</wx-open-launch-weapp>
</div>
<div id="desktop-web-container" class="hidden">
<p class="">請(qǐng)?jiān)谑謾C(jī)打開(kāi)網(wǎng)頁(yè)鏈接</p>
</div>
</div>
</body>
</html>
純es5寫(xiě)的代碼模塊,可以改造成任意架構(gòu)代碼。下面講一下怎么改成自己的項(xiàng)目里面以及坑點(diǎn):
1、調(diào)試工具可以去掉:
<!-- 調(diào)試用的移動(dòng)端 console -->
<script src="https://cdn.jsdelivr.net/npm/eruda"></script>
<script>eruda.init();</script>
2、Appid替換:怎么找到我們需要的id呢?替換位置都在哪呢?
1)登錄微信公共平臺(tái)

2)登錄之后點(diǎn)擊右上角頭像的名字,基本設(shè)置里最下面,appid和原始id都在這里。


3)替換位置
// 這里是給開(kāi)放性標(biāo)簽準(zhǔn)備的,第一種方式暫時(shí)用不到
wx.config({
debug: true, // 調(diào)試時(shí)可開(kāi)啟
appId: '替換的位置',
timestamp: 0, // 必填,填任意數(shù)字即可
nonceStr: 'nonceStr', // 必填,填任意非空字符串即可
signature: 'signature', // 必填,填任意非空字符串即可
jsApiList: ['chooseImage'], // 安卓上必填一個(gè),隨機(jī)即可
openTagList:['wx-open-launch-weapp'], // 填入打開(kāi)小程序的開(kāi)放標(biāo)簽名
})
<wx-open-launch-weapp id="launch-btn" username="原始id" path="跳轉(zhuǎn)頁(yè)面的路徑">
<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;">打開(kāi)小程序</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', // 這里下面會(huì)講
})
await c.init()
4)修改邏輯

這里把判斷固定寫(xiě)死false,就是想讓網(wǎng)頁(yè)一直走最后else的邏輯,以后想用開(kāi)放性標(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ù)名稱,下面文章有說(shuō)道
data: {
action: 'getUrlScheme', // 這是云函數(shù)中函數(shù)名字
},
})
console.warn(res)
if (onBeforeJump) {
onBeforeJump()
}
location.href = res.result.openlink
}
3、云函數(shù)的構(gòu)建:
1)我這是構(gòu)建好的:上文替換欄里面最后一張圖說(shuō)的云函數(shù)ID就是圖中紅色勾選框

沒(méi)構(gòu)建好的可以去:https://jingyan.baidu.com/article/a3aad71ac25f86f0fb0096ee.html
按照?qǐng)D文一步一步走就行了,我這里就不寫(xiě)那么多了。
點(diǎn)擊云函數(shù),然后點(diǎn)擊新構(gòu)建函數(shù):


云函數(shù)名稱填寫(xiě)的時(shí)候要注意點(diǎn),這個(gè)函數(shù)名test111就是我們要用到的云函數(shù)名稱!
2)調(diào)用的時(shí)候這里日志會(huì)顯示你在云函數(shù)里面寫(xiě)的打印console內(nèi)容。

3)云函數(shù)權(quán)限,測(cè)試的時(shí)候建議修改為所有用戶都可以訪問(wèn)!


4)創(chuàng)建的時(shí)候,并不是一定會(huì)創(chuàng)建云函數(shù)目錄,這時(shí)候需要手動(dòng)創(chuàng)建,首先在根目錄下面創(chuàng)建一個(gè)你喜歡的文件名字,然后在project.config.json中添上你所創(chuàng)建的目錄名字


右擊這個(gè)目錄的時(shí)候出現(xiàn)彈框這些選項(xiàng),開(kāi)發(fā)完畢后選擇上傳不上傳node_modules選項(xiàng)就行了。(這是直接發(fā)到線上,只要線上代碼不調(diào)用沒(méi)啥影響,放心用)

在此目錄下會(huì)出現(xiàn)test111文件夾,然后打開(kāi)此文件夾,執(zhí)行npm init,在package.json中寫(xiě)上:
{
"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文件,寫(xiě)入:
// 云函數(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 '沒(méi)有找到對(duì)應(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)路徑,一定是線上有的頁(yè)面路徑,否則會(huì)報(bào)錯(cuò)
query: `id=${param.id}`, // 注意傳參格式,可以不傳參
},
// 如果想不過(guò)期則置為 false,并可以存到數(shù)據(jù)庫(kù)
isExpire: false,
// 一分鐘有效期
expireTime: parseInt(Date.now() / 1000 + 60),
})
}到此,云函數(shù)跳轉(zhuǎn),就結(jié)束了,有什么問(wèn)題可以評(píng)論告訴我。
二、使用開(kāi)放性標(biāo)簽
使用開(kāi)性標(biāo)簽,把上面修改邏輯的代碼還原。
然后就是幾個(gè)問(wèn)題:
一、域名的坑:
1、開(kāi)發(fā)使用者必須是已認(rèn)證的服-務(wù)-號(hào)(服務(wù)號(hào)可以置頂,訂閱號(hào)不能),服務(wù)號(hào)綁定“JS接口安全域名”下的網(wǎng)頁(yè)可使用此標(biāo)簽跳轉(zhuǎn)任意合法合規(guī)的小程序。
2、開(kāi)發(fā)使用者必須是已認(rèn)證的非個(gè)人主體的小程序,使用小程序云開(kāi)發(fā)的靜態(tài)網(wǎng)站托管綁定的域名下的網(wǎng)頁(yè),可以使用此標(biāo)簽跳轉(zhuǎn)任意合法合規(guī)的小程序。
3、必須部署到正式服務(wù)器,測(cè)試公眾號(hào)不顯示圖標(biāo)。
4、必須是在已認(rèn)證的服務(wù)號(hào)中做JS接口安全域名驗(yàn)證,“JS接口安全域名”和“IP白名單”都要,IP是指獲取微信簽名的服務(wù)器的IP。
注意:1、2兩個(gè)條件必須滿足其中一個(gè);3、4兩個(gè)條件必須滿足,否則標(biāo)簽中的按鈕不會(huì)顯示(微信文檔不講武德,寫(xiě)的不清楚,稍微不注意就會(huì)掉坑)。
官方文檔入口:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html#21
還有幾點(diǎn):
1、微信開(kāi)發(fā)者工具,“verifyOpenTagList”:[],是顯示不了圖標(biāo)的。微信開(kāi)發(fā)工具和真機(jī)測(cè)試結(jié)果可能不一樣,一定要用真機(jī)測(cè)試。安卓和ios測(cè)試結(jié)果也可能不同,都要測(cè)試。
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ù)端自己看。
// 這里是給開(kāi)放性標(biāo)簽準(zhǔn)備的,第一種方式暫時(shí)用不到
wx.config({
debug: true, // 調(diào)試時(shí)可開(kāi)啟
appId: '替換的位置',
timestamp: 0, // 必填,填任意數(shù)字即可
nonceStr: 'nonceStr', // 必填,填任意非空字符串即可
signature: 'signature', // 必填,服務(wù)端生成的,不能任意
jsApiList: ['chooseImage'], // 安卓上必填一個(gè),
openTagList:['wx-open-launch-weapp'], // 填入打開(kāi)小程序的開(kāi)放標(biāo)簽名
})
ps:小程序測(cè)試的時(shí)候使用線上首頁(yè)測(cè)試最佳~
總結(jié)
到此這篇關(guān)于微信小程序h5頁(yè)面跳轉(zhuǎn)小程序的文章就介紹到這了,更多相關(guān)微信小程序h5頁(yè)面跳轉(zhuǎn)小程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
前端如何實(shí)現(xiàn)動(dòng)畫(huà)過(guò)渡效果
這篇文章主要介紹了前端如何實(shí)現(xiàn)動(dòng)畫(huà)過(guò)渡效果,幫助大家更好的理解和學(xué)習(xí)前端開(kāi)發(fā)網(wǎng)頁(yè),感興趣的朋友可以了解下2021-02-02
js 右側(cè)浮動(dòng)層效果實(shí)現(xiàn)代碼(跟隨滾動(dòng))
因?yàn)轫?xiàng)目上有這樣的需求,在網(wǎng)上也查了些東西,之前是想找個(gè)差不多類似的套用一下。后來(lái)發(fā)覺(jué)沒(méi)有合適的,因時(shí)間緊迫就自己動(dòng)手寫(xiě)了一個(gè)簡(jiǎn)單的 ,示例代碼如下 兼容火狐和IE7+2015-11-11
JS中的算法與數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(shù)(Binary Sort Tree)實(shí)例詳解
這篇文章主要介紹了JS中的算法與數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(shù)(Binary Sort Tree),結(jié)合實(shí)例形式詳細(xì)分析了二叉查找樹(shù)(Binary Sort Tree)的原理、定義、遍歷、查找、插入、刪除等常見(jiàn)操作技巧,需要的朋友可以參考下2019-08-08
JS中使用new Date(str)創(chuàng)建時(shí)間對(duì)象不兼容firefox和ie的解決方法(兩種)
這篇文章主要介紹了JS中使用new Date(str)創(chuàng)建時(shí)間對(duì)象不兼容firefox和ie的解決方法的相關(guān)資料,需要的朋友可以參考下2016-12-12
微信小程序基于slider組件動(dòng)態(tài)修改標(biāo)簽透明度的方法示例
這篇文章主要介紹了微信小程序基于slider組件動(dòng)態(tài)修改標(biāo)簽透明度的方法,可通過(guò)slider組件拖動(dòng)實(shí)現(xiàn)圖片透明度的改變功能,涉及微信小程序事件綁定、base64格式圖片載入及slider組件使用技巧,需要的朋友可以參考下2017-12-12
JavaScript實(shí)現(xiàn)帶音效的煙花特效
這篇文章主要為大家介紹了通過(guò)JavaScript實(shí)現(xiàn)的帶音效的煙花特效,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)JavaScript有一定的幫助,感興趣的可以了解一下2021-12-12

