function returnWxApp() {
// 判断当前环境是否为小程序
const ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
wx.miniProgram.getEnv((res) => {
if (res.miniprogram) {
console.log('在小程序内');
let url = getCallbackPagePath()
// 此处为tabbar页面跳转判断,若需要跳转至其他tabbar页面
//请在下方if判断中添加
if (url === '/pages/index/index') {
// 此处以/pages/index/index为例
wx.miniProgram.switchTab({
url,
success: (res) => {
console.log(res); // 页面跳转成功的回调函数
},
fail: (err) => {
console.log(err); // 页面跳转失败的回调函数
}
});
} else {
// 非tabbar页面跳转
wx.miniProgram.redirectTo({
url,
success: (res) => {
console.log(res); // 页面跳转成功的回调函数
},
fail: (err) => {
console.log(err); // 页面跳转失败的回调函数
}
});
}
} else {
console.log('不在小程序内');
}
});
} else {
console.log('不在微信浏览器内');
}
console.log('callbackPagePath' + getCallbackPagePath());
}
function getCallbackPagePath() {
// 获取完整的URL
var url = window.location.href;
// 解析URL中的参数
var urlParams = new URLSearchParams(url.substr(url.indexOf('?')));
console.log('urlParams=' + urlParams);
// 获取回调页面路径参数
var pageUrl = urlParams.get('callbackPagePath');
console.log('pageUrl=' + pageUrl);
return pageUrl != null ? pageUrl : '/pages/index/index';
}