微信小程序h5頁面跳轉(zhuǎn)小程序的超詳細講解
h5跳轉(zhuǎn)小程序
實戰(zhàn)項目從無到完整的h5跳轉(zhuǎn)小程序經(jīng)驗,跳轉(zhuǎn)方式分為云函數(shù)跳轉(zhuǎn)和開放性標簽跳轉(zhuǎn)。
開放性標簽是針對微信里面瀏覽器使用的,而云函數(shù)跳轉(zhuǎn)在外部以及微信里面都可以使用。
建議需要的同學先全部瀏覽一遍本文。內(nèi)容較長較為啰嗦。
一、不用開放性標簽(wx-open-launch-weapp)
官網(wǎng)鏈接:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/staticstorage/jump-miniprogram.html
官方網(wǎng)站寫的是比較好的,但是還不夠詳細和許多坑點,下面是實例代碼:
實例代碼鏈接: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('開放標簽 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: 'wxe5f52902cf4de896',
timestamp: 0, // 必填,填任意數(shù)字即可
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: '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)小程序的開放標簽。文檔 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="">請在手機打開網(wǎng)頁鏈接</p>
</div>
</div>
</body>
</html>
純es5寫的代碼模塊,可以改造成任意架構代碼。下面講一下怎么改成自己的項目里面以及坑點:
1、調(diào)試工具可以去掉:
<!-- 調(diào)試用的移動端 console -->
<script src="https://cdn.jsdelivr.net/npm/eruda"></script>
<script>eruda.init();</script>
2、Appid替換:怎么找到我們需要的id呢?替換位置都在哪呢?
1)登錄微信公共平臺

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


3)替換位置
// 這里是給開放性標簽準備的,第一種方式暫時用不到
wx.config({
debug: true, // 調(diào)試時可開啟
appId: '替換的位置',
timestamp: 0, // 必填,填任意數(shù)字即可
nonceStr: 'nonceStr', // 必填,填任意非空字符串即可
signature: 'signature', // 必填,填任意非空字符串即可
jsApiList: ['chooseImage'], // 安卓上必填一個,隨機即可
openTagList:['wx-open-launch-weapp'], // 填入打開小程序的開放標簽名
})
<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的邏輯,以后想用開放性標簽,就把這里還原即可。代碼塊放下面了。
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ù)的構建:
1)我這是構建好的:上文替換欄里面最后一張圖說的云函數(shù)ID就是圖中紅色勾選框

沒構建好的可以去:https://jingyan.baidu.com/article/a3aad71ac25f86f0fb0096ee.html
按照圖文一步一步走就行了,我這里就不寫那么多了。
點擊云函數(shù),然后點擊新構建函數(shù):


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

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


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 '沒有找到對應的函數(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),就結束了,有什么問題可以評論告訴我。
二、使用開放性標簽
使用開性標簽,把上面修改邏輯的代碼還原。
然后就是幾個問題:
一、域名的坑:
1、開發(fā)使用者必須是已認證的服-務-號(服務號可以置頂,訂閱號不能),服務號綁定“JS接口安全域名”下的網(wǎng)頁可使用此標簽跳轉(zhuǎn)任意合法合規(guī)的小程序。
2、開發(fā)使用者必須是已認證的非個人主體的小程序,使用小程序云開發(fā)的靜態(tài)網(wǎng)站托管綁定的域名下的網(wǎng)頁,可以使用此標簽跳轉(zhuǎn)任意合法合規(guī)的小程序。
3、必須部署到正式服務器,測試公眾號不顯示圖標。
4、必須是在已認證的服務號中做JS接口安全域名驗證,“JS接口安全域名”和“IP白名單”都要,IP是指獲取微信簽名的服務器的IP。
注意:1、2兩個條件必須滿足其中一個;3、4兩個條件必須滿足,否則標簽中的按鈕不會顯示(微信文檔不講武德,寫的不清楚,稍微不注意就會掉坑)。
官方文檔入口:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html#21
還有幾點:
1、微信開發(fā)者工具,“verifyOpenTagList”:[],是顯示不了圖標的。微信開發(fā)工具和真機測試結果可能不一樣,一定要用真機測試。安卓和ios測試結果也可能不同,都要測試。
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.dhdzp.com/article/273996.htm
二:config配置坑:簽名一定要找服務端要,生成規(guī)則讓服務端自己看。
// 這里是給開放性標簽準備的,第一種方式暫時用不到
wx.config({
debug: true, // 調(diào)試時可開啟
appId: '替換的位置',
timestamp: 0, // 必填,填任意數(shù)字即可
nonceStr: 'nonceStr', // 必填,填任意非空字符串即可
signature: 'signature', // 必填,服務端生成的,不能任意
jsApiList: ['chooseImage'], // 安卓上必填一個,
openTagList:['wx-open-launch-weapp'], // 填入打開小程序的開放標簽名
})
ps:小程序測試的時候使用線上首頁測試最佳~
總結
到此這篇關于微信小程序h5頁面跳轉(zhuǎn)小程序的文章就介紹到這了,更多相關微信小程序h5頁面跳轉(zhuǎn)小程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
URLSearchParams快速解析URL查詢參數(shù)實現(xiàn)
這篇文章主要為大家介紹了URLSearchParams快速解析URL查詢參數(shù)實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
基于JS實現(xiàn)移動端向左滑動出現(xiàn)刪除按鈕功能
最近在做移動端項目時,需要實現(xiàn)一個列表頁面的每一項item向左滑動時出現(xiàn)相應的刪除按鈕,其實實現(xiàn)此功能很簡單的。這篇文章主要介紹了基于js實現(xiàn)移動端向左滑動出現(xiàn)刪除按鈕,需要的朋友可以參考下2017-02-02
使用php的mail()函數(shù)實現(xiàn)發(fā)送郵件功能
php中的mail()函數(shù)允許您從腳本中直接發(fā)送電子郵件,下面這篇文章主要給大家介紹了關于如何使用php的mail()函數(shù)實現(xiàn)發(fā)送郵件功能的相關資料,需要的朋友可以參考下2021-06-06
微信小程序scroll-view橫向滑動嵌套for循環(huán)的示例代碼
這篇文章主要介紹了微信小程序scroll-view橫向滑動嵌套for循環(huán)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
ES6使用新特性Proxy實現(xiàn)的數(shù)據(jù)綁定功能實例
這篇文章主要介紹了ES6使用新特性Proxy實現(xiàn)的數(shù)據(jù)綁定功能,結合具體實例形式分析了ES6使用Proxy實現(xiàn)數(shù)據(jù)綁定具體原理、操作步驟與相關注意事項,需要的朋友可以參考下2020-05-05

