press-next
Version:
Vue3 组件库,支持 Composition API
84 lines (67 loc) • 1.77 kB
text/typescript
import pathToRegexp from 't-comm/lib/router/path-to-regexp';
import { LINK_PREFIX_MAP } from '../config/config';
function getLocationHash(link = '') {
let curLink = link;
if (link.includes('?')) {
curLink = curLink.split('?')[0];
}
if (curLink.includes('#')) {
return curLink.split('#')[1];
}
}
function parseCurrentLink(link = '', context: any, useUni = false) {
if (!link.startsWith(LINK_PREFIX_MAP.NEW)) {
return false;
}
const query = link.split('?')[1] || '';
const queryObj: {
siteId?: string
} = query.split('&')
.reduce((acc, item) => {
const list = item.split('=');
return {
...acc,
[list[0]]: list[1],
};
}, {});
if (queryObj.siteId) {
routePush(queryObj.siteId, context, useUni);
return true;
}
return false;
}
export function parseLink(link = '', context: any, useUni = false) {
if (!context) {
return false;
}
return parseOldLink(link, context, useUni) || parseCurrentLink(link, context, useUni) || false;
}
function parseOldLink(link = '', context: any, useUni = false) {
if (!link.startsWith(LINK_PREFIX_MAP.OLD)) {
return false;
}
const route = getLocationHash(link);
const path = '/match-list/:siteId(\\d+)';
const regexp = pathToRegexp(path);
const match = regexp.exec(route);
const siteId = match?.[1];
if (!siteId) {
return false;
}
routePush(siteId, context, useUni);
return true;
}
function routePush(siteId: string, context: any, useUni = false) {
if (useUni) {
// @ts-ignore
uni.navigateTo({ url: `/views/match-list/match-list?siteId=${siteId}` });
return;
}
const newRoute = {
path: '/match-list',
query: {
siteId,
},
};
context.$router.push(newRoute);
}