s-uni-router
Version:
一个uni-app 路由管理解决方案
292 lines (291 loc) • 8.12 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
import { stringify } from "qs";
import { shallowRef, ref, toRaw, onMounted as onMounted$1, onBeforeMount as onBeforeMount$1 } from "vue";
import { onBeforeUnmount, onUpdated } from "vue";
import { onLoad as onLoad$1, onShow as onShow$1, onReady as onReady$1, onLaunch } from "@dcloudio/uni-app";
var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
var freeSelf = typeof self == "object" && self && self.Object === Object && self;
var root = freeGlobal || freeSelf || Function("return this")();
var Symbol$1 = root.Symbol;
var objectProto$1 = Object.prototype;
var hasOwnProperty = objectProto$1.hasOwnProperty;
var nativeObjectToString$1 = objectProto$1.toString;
var symToStringTag$1 = Symbol$1 ? Symbol$1.toStringTag : void 0;
function getRawTag(value) {
var isOwn = hasOwnProperty.call(value, symToStringTag$1), tag = value[symToStringTag$1];
try {
value[symToStringTag$1] = void 0;
var unmasked = true;
} catch (e) {
}
var result = nativeObjectToString$1.call(value);
if (unmasked) {
if (isOwn) {
value[symToStringTag$1] = tag;
} else {
delete value[symToStringTag$1];
}
}
return result;
}
var objectProto = Object.prototype;
var nativeObjectToString = objectProto.toString;
function objectToString(value) {
return nativeObjectToString.call(value);
}
var nullTag = "[object Null]", undefinedTag = "[object Undefined]";
var symToStringTag = Symbol$1 ? Symbol$1.toStringTag : void 0;
function baseGetTag(value) {
if (value == null) {
return value === void 0 ? undefinedTag : nullTag;
}
return symToStringTag && symToStringTag in Object(value) ? getRawTag(value) : objectToString(value);
}
function isObjectLike(value) {
return value != null && typeof value == "object";
}
var isArray = Array.isArray;
function isObject(value) {
var type = typeof value;
return value != null && (type == "object" || type == "function");
}
var stringTag = "[object String]";
function isString(value) {
return typeof value == "string" || !isArray(value) && isObjectLike(value) && baseGetTag(value) == stringTag;
}
function normalizingPath(location) {
const { url, query } = location;
const slashPad = url.startsWith("/") ? "" : "/";
const slashPadUrl = `${slashPad}${url}`;
if (!query) {
return slashPadUrl;
}
const maybeQuery = stringify(query);
if (maybeQuery) {
return `${slashPadUrl}?${maybeQuery}`;
}
return slashPadUrl;
}
function hasLocationsRouter(v) {
return isObject(v) && isString(v.url);
}
const NavigateNames = {
navigateTo: "navigateTo",
redirectTo: "redirectTo",
switchTab: "switchTab",
reLaunch: "reLaunch"
};
class RouterGuardsEvent {
constructor() {
__publicField(this, "event", {
afterEach: [],
beforeEach: []
});
}
beforeEach(callback) {
const index = this.event.beforeEach.push(callback);
return () => {
this.event.beforeEach.splice(index, 1);
};
}
afterEach(callback) {
const index = this.event.afterEach.push(callback);
return () => {
this.event.afterEach.splice(index, 1);
};
}
}
class RouterGuards extends RouterGuardsEvent {
constructor() {
super();
__publicField(this, "currentRouterPath", shallowRef(null));
__publicField(this, "isReady", ref(false));
__publicField(this, "pendingLocation", null);
uni.$RouterGuards = this;
}
updateRouter(to) {
const url = to.url.startsWith("/") ? to.url : `/${to.url}`;
this.currentRouterPath.value = {
url,
query: to.query,
fullPath: normalizingPath({
url,
query: to.query
})
};
return this.currentRouterPath;
}
fixRouterPath() {
if (this.isReady.value) {
const [page] = getCurrentPages().slice(-1);
if (page) {
this.updateRouter({
url: page.route,
query: page.options
});
} else {
this.currentRouterPath.value = null;
}
return this.currentRouterPath;
}
}
async navigate(to, navigateName = NavigateNames.navigateTo) {
var _a;
const serializableFullPath = normalizingPath(to);
if (((_a = this.currentRouterPath.value) == null ? void 0 : _a.fullPath) === serializableFullPath) {
return;
}
if (this.pendingLocation && normalizingPath(to) === this.pendingLocation) {
return;
}
this.pendingLocation = serializableFullPath;
try {
await this.navigateTemp(to, () => {
return new Promise((r, s) => {
uni[navigateName]({
url: serializableFullPath,
success: r,
fail: s
});
});
});
} finally {
this.pendingLocation = null;
}
}
async navigateTemp(to, callback) {
for (const beforeGuards of this.event.beforeEach) {
const hasNewRoute = await beforeGuards(this.currentRouterPath.value, to);
if (hasNewRoute === false) {
return;
}
if (hasLocationsRouter(hasNewRoute)) {
await this.navigate(hasNewRoute, hasNewRoute.navigateName);
return;
}
}
await (callback == null ? void 0 : callback());
const afterRouter = toRaw(this.currentRouterPath.value);
this.updateRouter(to);
for (const afterGuards of this.event.afterEach) {
await afterGuards(afterRouter, to);
}
}
async ready(to) {
await this.navigateTemp(to);
this.isReady.value = true;
this.fixRouterPath();
}
}
function onLoad(callback) {
onLoad$1(async (v) => {
await uni.$RouterGuards.withLaunchPromise;
await callback(v);
});
}
function onShow(callback) {
onShow$1(async () => {
await uni.$RouterGuards.withLaunchPromise;
await callback();
});
}
function onReady(callback) {
onReady$1(async () => {
await uni.$RouterGuards.withLaunchPromise;
await callback();
});
}
function onMounted(callback) {
onMounted$1(async () => {
await uni.$RouterGuards.withLaunchPromise;
await callback();
});
}
function onBeforeMount(callback) {
onBeforeMount$1(async () => {
await uni.$RouterGuards.withLaunchPromise;
await callback();
});
}
function useGuardsImpl() {
return uni.$RouterGuards;
}
function useNavigateTo(to, navigateName = NavigateNames.navigateTo) {
const routerGuards = useGuardsImpl();
if (isString(to)) {
return routerGuards.navigate(
{
url: to
},
navigateName
);
}
return routerGuards.navigate(to, navigateName);
}
function useRouter() {
return {
route: useRoute(),
push: (to) => useNavigateTo(to),
pushTab: (to) => useNavigateTo(to, NavigateNames.switchTab),
replace: (to) => useNavigateTo(to, NavigateNames.redirectTo),
replaceAll: (to) => useNavigateTo(to, NavigateNames.reLaunch),
back: (data = { delta: 1 }) => {
return new Promise((resolve, reject) => {
uni.navigateBack({
success: resolve,
fail: reject,
...data
});
});
}
};
}
function useRoute() {
const route = ref(
{}
);
onLoad(() => {
const cuPage = useGuardsImpl().fixRouterPath();
route.value = cuPage.value || {};
});
return route;
}
function useGuardsReady() {
return useGuardsImpl().isReady;
}
function onRouterReady(to) {
let resolve;
uni.$RouterGuards.withLaunchPromise = new Promise((_resolve) => {
resolve = _resolve;
});
onLaunch((options) => {
useGuardsImpl().ready(
to ?? {
url: options == null ? void 0 : options.path,
query: options == null ? void 0 : options.query
}
).then(resolve);
});
}
export {
NavigateNames,
RouterGuards,
RouterGuardsEvent,
onBeforeMount,
onBeforeUnmount,
onLoad,
onMounted,
onReady,
onRouterReady,
onShow,
onUpdated,
useGuardsImpl,
useGuardsReady,
useRoute,
useRouter
};