一,你需要知道的
1.在開發(fā)小程序時,有時會有掃描二維碼進入小程序的指定頁面的業(yè)務需求。
2.微信小程序提供了這種功能,具體實現(xiàn)可以分三步
(1)在微信小程序后臺配置好
(2)如何跳轉
(3)代碼中
二,小程序配置
1.首先需要在小程序后臺-開發(fā)管理-開發(fā)設置下的掃描二維碼進小程序模塊下配置二維碼規(guī)則
獲取參數(shù)信息并跳轉
2.配置二維碼規(guī)則(很重要?。。。?/strong>
耐心看完
2.小程序中跳轉處理
你可以將測試鏈接復制下來,隨便找到一個二維碼生成工具網站,生成二維碼,以便下面的測試
在首頁中onLoad中接受參數(shù)
sharedParameterAnalysis是自己定義的函數(shù),統(tǒng)一處理參數(shù)
// 小程序分享進入或二維碼進入封裝方法
sharedParameterAnalysis(options) {
// 小程序分享跳轉處理
if (options?.share) {
console.log("小程序分享,路徑為" + options.path + "參數(shù)為" + options.query);
// 小程序分享跳轉處理
uni.navigateTo({
url: "/" + options.path + "?" + options.query,
});
} else if (options?.q) {//直接看這個?。。。。。。。。。。。。。。。。。。。。?!
// 小程序自動加密,需要解碼
const url = decodeURIComponent(options.q);
const regex = /[?&]([^=#]+)=([^&#]*)/g;
const params = {};
let match;
while ((match = regex.exec(url))) {
params[match[1]] = match[2];
}
console.log("二維碼分享,路徑為" + params.path + "參數(shù)為" + params.query);
// 小程序分享跳轉處理
uni.navigateTo({
url: "/" + params.path + "?" + params.query,
});
} else {
// 未知參數(shù)
console.log("未知參數(shù)");
uni.showToast({
title: "未知參數(shù)",
icon: "none",
});
}
},
如果是二維碼跳轉options中會有q這個參數(shù),內容為完整的測試鏈接,小程序會自動加密其中的路徑地址
const url = decodeURIComponent(options.q);
const regex = /[?&]([^=#]+)=([^&#]*)/g;
const params = {};
let match;
while ((match = regex.exec(url))) {
params[match[1]] = match[2];
}
以上方法可以將options中q的路徑解密出來,然后將其中的參數(shù)一一取出
eg:xxx.cn/qwe/index?share=1&path=pages_branch/details/index&query=id=36
此時params中會提取出
const params={
share:"1",
path:"pages_branch/details/index",
query:"id=36"
}
這個時候路徑有了參數(shù)也有了,不就可以了嗎!!文章來源:http://www.zghlxwxcb.cn/news/detail-768342.html
用戶微信掃一掃即可打開指定頁面了,多試幾遍,一定可以文章來源地址http://www.zghlxwxcb.cn/news/detail-768342.html
到了這里,關于掃描二維碼進小程序指定頁面(包解決?。。┑奈恼戮徒榻B完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!