详细配置可以参考[扫普通链接二维码打开小程序详解_二维码微信小程序开发_普通二维码跳转小程序注意事项](https://www.lihuanting.com/blog/a/126)
例如,我们要生成一个跳转到小程序首页并携带参数id=123的短连接,可以在页面路径中填写/pages/index/index?id=123。
Page({
onLoad: function(options) {
// 小程序分享跳转处理
if (options?.share) {
console.log("小程序分享,路径为" + options.path + "参数为" + options.query);
// 小程序分享跳转处理
// uni.navigateTo({
// url: "/" + options.path + "?" + options.query,
// });
} else if (options?.q) {
// 小程序自动加密,需要解码
const url = decodeURIComponent(options.q);
const params = {};
// 自定义参数拼接
if (url.includes('?') && !url.includes('&')) {
const queryStr = url.split('?')[1]
const paramList = queryStr.split('=')
if (paramList.length) {
params.query = {
cn: paramList[1],//会将第一参数转为cn
}
}
} else {
const regex = /[?&]([^=#]+)=([^&#]*)/g;
let match;
while ((match = regex.exec(url))) {
params[match[1]] = match[2];
}
}
console.log("二维码分享,路径为" + params.path + "参数为" + JSON.stringify(params.query), url);
// 小程序分享跳转处理
// uni.navigateTo({
// url: "/" + params.path + "?" + params.query,
// });
return params.query
} else {
// 未知参数
console.log("未知参数");
uni.showToast({
title: "未知参数",
icon: "none",
});
}
}
});
通过以上步骤,我们可以在微信小程序中方便地接收短连接传递的参数,实现更加灵活和个性化的功能。在实际开发中,可以根据具体需求进行更多的扩展和优化。