// 进入小程序参数集中处理
function switchOptionsType(options) {
let obj = {}
// 小程序二维码进入
if (options.scene) {
obj = parseMinicode(options)
}
// 普通二维码
else if (options.q) {
obj = parseQRcode(options)
}
// 跳转进入
else if (!options.scene && !options.q) {
obj = parseNormal(options)
}
navigateToPage(obj)
}
function parseMinicode(options) {
// decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。
let scene = decodeURIComponent(options.scene);
// 下面根据逻辑自行处理
let dataarr = scene.split("_");
switch (dataarr[0]) {
case 'G':
break;
case 'S':
break;
}
}
function parseQRcode(options) {
let qrUrl = decodeURIComponent(options.q)
// 不包含type字段不做处理
if (qrUrl.indexOf('type=') == -1) {
// API.showToast("未获取信息!")
return
} else {
let obj = parseUrlToObj(qrUrl)
switch (obj.type) {
case 'user':
return {
path: '/pages/page/home/home',
data: options
}
break;
}
}
}
// 小程序跳转小程序
function parseNormal(options){
switch (options.type){
case 'user':
return {
path: '/pages/page/user/user',
data: options
}
break;
}
}
function navigateToPage(obj){
if(obj && obj.path){
wx.reLaunch({
url: obj.path + '?' + handleObject(obj.data)
})
}else{
API.showToast('数据出错拉~~')
}
}
// 处理url传参
function parseUrlToObj(qrUrl){
let data = qrUrl.split("?")[1],
arr = data.split("&"),
obj = {};
arr.forEach(el => {
obj[el.split("=")[0]] = el.split("=")[1]
})
return obj
}
// obj转url参数
function handleObject(obj) {
try {
let arr = []
for (let key in obj) {
if (key == 'q') {
arr.push(key + "=" + encodeURIComponent(obj[key]))
continue
}
arr.push(key + "=" + obj[key])
}
return arr.join("&")
} catch (e) {
return ""
}
}
module.exports = {
switchOptionsType
}
扫码跳转小程序讲解(公用js入口)
最新推荐文章于 2024-04-25 16:49:14 发布