2024-05-29 01:29:31 +08:00
|
|
|
|
import $store from "@/peach/store";
|
|
|
|
|
import { showShareModal } from "@/peach/hooks/useModal";
|
|
|
|
|
import {
|
|
|
|
|
isNumber,
|
|
|
|
|
isString,
|
|
|
|
|
isEmpty,
|
|
|
|
|
startsWith,
|
|
|
|
|
isObject,
|
|
|
|
|
isNil,
|
|
|
|
|
clone,
|
|
|
|
|
} from "lodash";
|
|
|
|
|
import throttle from "@/peach/helper/throttle";
|
2024-05-22 15:42:13 +08:00
|
|
|
|
|
|
|
|
|
const _go = (
|
2024-05-29 01:29:31 +08:00
|
|
|
|
path,
|
|
|
|
|
params = {},
|
|
|
|
|
options = {
|
|
|
|
|
redirect: false,
|
|
|
|
|
}
|
2024-05-22 15:42:13 +08:00
|
|
|
|
) => {
|
2024-05-29 01:29:31 +08:00
|
|
|
|
let page = ""; // 跳转页面
|
|
|
|
|
let query = ""; // 页面参数
|
|
|
|
|
let url = ""; // 跳转页面完整路径
|
|
|
|
|
|
|
|
|
|
if (isString(path)) {
|
|
|
|
|
// 判断跳转类型是 path | 还是http
|
|
|
|
|
if (startsWith(path, "http")) {
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
window.location = path;
|
|
|
|
|
return;
|
|
|
|
|
// #endif
|
|
|
|
|
// #ifndef H5
|
|
|
|
|
page = `/pages/public/webview`;
|
|
|
|
|
query = `url=${encodeURIComponent(path)}`;
|
|
|
|
|
// #endif
|
|
|
|
|
} else if (startsWith(path, "action:")) {
|
|
|
|
|
handleAction(path);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
[page, query] = path.split("?");
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
2024-05-29 01:29:31 +08:00
|
|
|
|
if (!isEmpty(params)) {
|
|
|
|
|
let query2 = paramsToQuery(params);
|
|
|
|
|
if (isEmpty(query)) {
|
|
|
|
|
query = query2;
|
|
|
|
|
} else {
|
|
|
|
|
query += "&" + query2;
|
|
|
|
|
}
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
2024-05-29 01:29:31 +08:00
|
|
|
|
}
|
2024-05-22 15:42:13 +08:00
|
|
|
|
|
2024-05-29 01:29:31 +08:00
|
|
|
|
if (isObject(path)) {
|
|
|
|
|
page = path.url;
|
|
|
|
|
if (!isNil(path.params)) {
|
|
|
|
|
query = paramsToQuery(path.params);
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
2024-05-29 01:29:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextRoute = ROUTES_MAP[page];
|
|
|
|
|
|
|
|
|
|
// 未找到指定跳转页面
|
|
|
|
|
// mark: 跳转404页
|
|
|
|
|
if (!nextRoute) {
|
|
|
|
|
console.log(
|
|
|
|
|
`%c跳转路径参数错误<${page || "EMPTY"}>`,
|
|
|
|
|
"color:red;background:yellow"
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 页面登录拦截
|
|
|
|
|
if (nextRoute.meta?.auth && !$store("user").isLogin) {
|
|
|
|
|
uni.redirectTo({
|
|
|
|
|
url: "/pages/index/login",
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url = page;
|
|
|
|
|
if (!isEmpty(query)) {
|
|
|
|
|
url += `?${query}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 跳转底部导航
|
|
|
|
|
if (TABBAR.includes(page)) {
|
|
|
|
|
uni.switchTab({
|
|
|
|
|
url,
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用redirect跳转
|
|
|
|
|
if (options.redirect) {
|
|
|
|
|
uni.redirectTo({
|
|
|
|
|
url,
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url,
|
|
|
|
|
});
|
|
|
|
|
};
|
2024-05-22 15:42:13 +08:00
|
|
|
|
|
|
|
|
|
// 限流 防止重复点击跳转
|
|
|
|
|
function go(...args) {
|
2024-05-29 01:29:31 +08:00
|
|
|
|
throttle(() => {
|
|
|
|
|
_go(...args);
|
|
|
|
|
});
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function paramsToQuery(params) {
|
2024-05-29 01:29:31 +08:00
|
|
|
|
if (isEmpty(params)) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
// return new URLSearchParams(Object.entries(params)).toString();
|
|
|
|
|
let query = [];
|
|
|
|
|
for (let key in params) {
|
|
|
|
|
query.push(key + "=" + params[key]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return query.join("&");
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function back() {
|
2024-05-29 01:29:31 +08:00
|
|
|
|
// #ifdef H5
|
|
|
|
|
history.back();
|
|
|
|
|
// #endif
|
2024-05-22 15:42:13 +08:00
|
|
|
|
|
2024-05-29 01:29:31 +08:00
|
|
|
|
// #ifndef H5
|
|
|
|
|
uni.navigateBack();
|
|
|
|
|
// #endif
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function redirect(path, params = {}) {
|
2024-05-29 01:29:31 +08:00
|
|
|
|
go(path, params, {
|
|
|
|
|
redirect: true,
|
|
|
|
|
});
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检测是否有浏览器历史
|
|
|
|
|
function hasHistory() {
|
2024-05-29 01:29:31 +08:00
|
|
|
|
// #ifndef H5
|
|
|
|
|
const pages = getCurrentPages();
|
|
|
|
|
if (pages.length > 1) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
return !!history.back;
|
|
|
|
|
// #endif
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 01:29:31 +08:00
|
|
|
|
function getCurrentRoute(field = "") {
|
|
|
|
|
let currentPage = getCurrentPage();
|
|
|
|
|
// #ifdef MP
|
|
|
|
|
currentPage.$page["route"] = currentPage.route;
|
|
|
|
|
currentPage.$page["options"] = currentPage.options;
|
|
|
|
|
// #endif
|
|
|
|
|
if (field !== "") {
|
|
|
|
|
return currentPage.$page[field];
|
|
|
|
|
} else {
|
|
|
|
|
return currentPage.$page;
|
|
|
|
|
}
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getCurrentPage() {
|
2024-05-29 01:29:31 +08:00
|
|
|
|
let pages = getCurrentPages();
|
|
|
|
|
return pages[pages.length - 1];
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleAction(path) {
|
2024-05-29 01:29:31 +08:00
|
|
|
|
const action = path.split(":");
|
|
|
|
|
switch (action[1]) {
|
|
|
|
|
case "showShareModal":
|
|
|
|
|
showShareModal();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 01:29:31 +08:00
|
|
|
|
function error(errCode, errMsg = "") {
|
|
|
|
|
redirect("/pages/public/error", {
|
|
|
|
|
errCode,
|
|
|
|
|
errMsg,
|
|
|
|
|
});
|
2024-05-22 15:42:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
2024-05-29 01:29:31 +08:00
|
|
|
|
go,
|
|
|
|
|
back,
|
|
|
|
|
hasHistory,
|
|
|
|
|
redirect,
|
|
|
|
|
getCurrentPage,
|
|
|
|
|
getCurrentRoute,
|
|
|
|
|
error,
|
|
|
|
|
};
|