123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- import { warn, err } from '../helpers/warn';
- import { isObject, resolveRule, copyObject } from '../helpers/util';
- import { proxyBeforeEnter } from './proxy/proxy';
- import { Global } from '../helpers/config';
- const pagesConfigReg = /props:\s*\(.*\)\s*(\([\s\S]*\))\s*},/;
- const pagesConfigRegCli = /props:\s*Object\.assign\s*(\([\s\S]*\))\s*},/;
- const defRoutersReg = /props:\s*{([\s\S]+)}\s*},/;
- export const resolveRender = function ({
- component,
- components,
- }, item, useUniConfig) {
- if (components != null) {
- warn(`vueRouterDev:false时 路由表配置中 ‘components’ 无效,\r\n\r\n ${JSON.stringify(item)}`);
- }
- if (useUniConfig == true) {
- return false;
- }
- if (item.path == '*') {
- return true;
- }
- if (component == null) {
- return err(`vueRouterDev:false时 路由表中 ‘component’ 选项不能为空:\r\n\r\n ${JSON.stringify(item)}`);
- }
- if (component.constructor === Function) {
- item.component = {
- render: component,
- };
- } else if (component.constructor === Object) {
- if (component.render == null || component.render.constructor !== Function) {
- err(`vueRouterDev:false时 路由表配置中 ‘render’ 函数缺失或类型不正确:\r\n\r\n ${JSON.stringify(item)}`);
- }
- } else {
- err(
- `vueRouterDev:false时 路由表配置中 ‘component’ 选项仅支持 Function、Object 类型。并确保 Object 类型时传递了 ‘render’ 函数 :\r\n\r\n ${JSON.stringify(item)}`,
- );
- }
- };
- export const resloveChildrenPath = function (objRoutes, children, useUniConfig) {
- for (let i = 0; i < children.length; i += 1) {
- const item = children[i];
- resolveRender(item, item, useUniConfig);
- if (item.path != null) {
- objRoutes[item.path] = {
- ...item,
- ...{
- _ROUTERPATH: true,
- },
- };
- } else {
- objRoutes[item.aliasPath] = {
- ...item,
- ...{
- _ROUTERPATH: false,
- },
- };
- }
- if (item.children && item.children.constructor === Array) {
- resloveChildrenPath(objRoutes, item.children, useUniConfig);
- }
- }
- };
- export const fromatRoutes = function (routes, userRoute, {
- vueRouterDev,
- useUniConfig,
- }) {
- if (userRoute && vueRouterDev) {
- return routes;
- }
- const objRoutes = {};
- for (let i = 0; i < routes.length; i += 1) {
- const item = routes[i];
- const path = item.path === '/' ? item.alias : item.path;
- if (userRoute) {
- if (item.children && item.children.constructor === Array) {
- resloveChildrenPath(objRoutes, item.children, useUniConfig);
- }
- resolveRender(item, item, useUniConfig);
- }
- objRoutes[path] = {
- ...item,
- ...{
- _PAGEPATH: path.substring(1),
- },
- };
- }
- return objRoutes;
- };
- export const getFuntionConfig = function (FunStr) {
- let matchText = FunStr.match(pagesConfigReg);
- let prefix = '';
- if (matchText == null) {
- matchText = FunStr.match(pagesConfigRegCli);
- if (matchText == null) {
- try {
-
- matchText = FunStr.match(defRoutersReg)[1];
-
- matchText = eval(`Object.assign({${matchText}})`);
- prefix = 'system-';
- } catch (error) {
- err(`读取uni-app页面构建方法配置错误 \r\n\r\n ${error}`);
- }
- } else {
-
- matchText = eval(`Object.assign${matchText[1]}`);
- }
- } else {
-
- matchText = eval(`Object.assign${matchText[1]}`);
- }
- return {
- config: matchText,
- prefix,
- FunStr,
- };
- };
- export const pathToRute = function (path, routes) {
- let PATHKEY = '';
- let rute = {};
- const routeKeys = Object.keys(routes);
- for (let i = 0; i < routeKeys.length; i += 1) {
- const key = routeKeys[i];
- const item = routes[key];
- rute = item;
- if (item.aliasPath == path) {
- PATHKEY = 'aliasPath';
- break;
- }
-
- if (`/${item._PAGEPATH}` == path) {
- PATHKEY = 'path';
- break;
- }
- }
- return {
- PATHKEY: {
- [PATHKEY]: path,
- },
- rute,
- };
- };
- export const nameToRute = function (name, routes) {
- const routesKeys = Object.keys(routes);
- for (let i = 0; i < routesKeys.length; i += 1) {
- const key = routesKeys[i];
- const item = routes[key];
- if (item.name == name) {
- return item;
- }
- }
- err(`路由表中没有找到 name为:'${name}' 的路由`);
- };
- export const formatUserRule = function (rule, selfRoutes, CONFIG) {
- let type = '';
- const ruleQuery = (type = 'query', rule.query || (type = 'params', rule.params)) || (type = '', {});
- let rute = {};
- if (type == '' && rule.name != null) {
- type = 'params';
- }
- if (type != 'params') {
- const route = pathToRute(rule.path || rule, selfRoutes);
- if (Object.keys(route.PATHKEY)[0] == '') {
- err(`'${route.PATHKEY['']}' 路径在路由表中未找到`);
- return null;
- }
- rute = route.rute;
- if (rule.path) {
- rule.path = rute.path;
- }
- }
- if (type != '') {
- if (type == 'params' && CONFIG.h5.paramsToQuery) {
- const {
- aliasPath,
- path,
- } = nameToRute(rule.name, selfRoutes);
- delete rule.name;
- delete rule.params;
- rule.path = aliasPath || path;
- type = 'query';
- }
- const query = Global.$parseQuery.transfer(ruleQuery);
- if (CONFIG.encodeURI) {
- if (query != '') {
- rule[type] = {
- query: query.replace(/^query=/, ''),
- };
- }
- } else {
- rule[type] = ruleQuery;
- }
- } else {
- rule = rute.path;
- }
- return rule;
- };
- export const getRouterNextInfo = function (to, from, Router) {
- let [toRoute, fromRoute] = [to, from];
- const H5 = Router.CONFIG.h5;
- if (H5.vueNext === false && H5.vueRouterDev === false) {
- let [toPath, fromPath] = [{}, {}];
- toPath[to.meta.PATHKEY] = to.meta.PATHKEY === 'path' ? `/${to.meta.pagePath}` : `${to.path}`;
- fromPath[from.meta.PATHKEY] = from.meta.PATHKEY === 'path' ? `/${from.meta.pagePath}` : `${from.path}`;
- if (to.meta.PATHKEY == null) {
- toPath = pathToRute(to.path, Router.selfRoutes).PATHKEY;
- }
- if (from.meta.PATHKEY == null) {
- fromPath = pathToRute(from.path, Router.selfRoutes).PATHKEY;
- }
- const isEmptyTo = Object.keys(to.query).length != 0 ? copyObject(to.query) : copyObject(to.params);
- const isEmptyFrom = Object.keys(from.query).length != 0 ? copyObject(from.query) : copyObject(from.params);
-
- delete isEmptyTo.__id__;
- delete isEmptyFrom.__id__;
-
- const toQuery = Global.$parseQuery.queryGet(isEmptyTo).decode;
- const fromQuery = Global.$parseQuery.queryGet(isEmptyFrom).decode;
- toRoute = resolveRule(Router, toPath, toQuery, Object.keys(toPath)[0]);
- fromRoute = resolveRule(Router, fromPath, fromQuery, Object.keys(fromPath)[0]);
- } else {
- if (fromRoute.name == null && toRoute.name != null) {
- fromRoute = {
- ...fromRoute,
- ...{
- name: toRoute.name,
- },
- };
- }
- }
- return {
- toRoute,
- fromRoute,
- };
- };
- export const vueDevRouteProxy = function (routes, Router) {
- const proxyRoutes = [];
- for (let i = 0; i < routes.length; i += 1) {
- const item = routes[i];
- const childrenRoutes = Reflect.get(item, 'children');
- if (childrenRoutes != null) {
- const childrenProxy = vueDevRouteProxy(childrenRoutes, Router);
- item.children = childrenProxy;
- }
- const ProxyRoute = proxyBeforeEnter(Router, item);
- proxyRoutes.push(ProxyRoute);
- }
- return proxyRoutes;
- };
- export const encodeURLQuery = function (CONFIG, query, mode) {
- if (Object.keys(query).length == 0) {
- return '';
- }
- if (CONFIG.h5.vueRouterDev === false) {
- const { strQuery, historyObj } = Global.$parseQuery.queryGet(query);
- if (mode === 'history') {
- return historyObj;
- }
- return strQuery;
- }
- if (mode === 'history') {
- return query;
- }
- return Global.$parseQuery.stringify(query);
- };
- export const strPathToObjPath = function (path) {
- if (path == null) {
- return path;
- }
- if (isObject(path)) {
- return path;
- }
- return {
- path,
- };
- };
- export const getPages = function (index = 0, all) {
- const pages = getCurrentPages(all);
- return pages.reverse()[index];
- };
- export const H5GetPageRoute = function (pages, Vim) {
- if (pages.length > 0) {
- const currentRoute = pages[pages.length - 1].$route;
- return getRouterNextInfo(currentRoute, currentRoute, this).toRoute;
- } if (Vim && Vim.$route) {
- return getRouterNextInfo(Vim.$route, Vim.$route, this).toRoute;
- }
- return {};
- };
- export const diffRouter = function (Router, vueRouter, useUniConfig, routes) {
- const newRouterMap = [];
- if (useUniConfig) {
- const Routes = routes || vueRouter.options.routes;
- const cloneSelfRoutes = copyObject(Router.selfRoutes);
- Routes.forEach(((item) => {
- const path = item.path === '/' ? item.alias : item.path;
- const vueRoute = (Router.vueRoutes[path] || Router.vueRoutes[item.path]) || Router.selfRoutes[path];
- const CselfRoute = Router.selfRoutes[path];
- delete cloneSelfRoutes[path];
- if (CselfRoute == null) {
- return err(
- `读取 ‘pages.json’ 中页面配置错误。实例化时传递的路由表中未找到路径为:${path} \r\n\r\n 可以尝试把 ‘useUniConfig’ 设置为 ‘false’。或者配置正确的路径。如果你是动态添加的则不用理会`,
- );
- }
- let pageConfigJson = {
- config: {},
- };
- if (vueRoute.component) {
- pageConfigJson = getFuntionConfig(vueRoute.component.render.toString());
- CselfRoute.component = {
- render: (h) => vueRoute.component.render(h),
- };
- }
- delete CselfRoute.components;
- delete CselfRoute.children;
- CselfRoute.meta = {
- ...pageConfigJson.config,
- ...item.meta || {},
- PATHKEY: CselfRoute.aliasPath ? 'aliasPath' : 'path',
- pagePath: CselfRoute.path.substring(1),
- };
- CselfRoute.path = CselfRoute.aliasPath || (item.path === '/' ? item.path : CselfRoute.path);
- item.alias = item.path === '/' ? item.alias : CselfRoute.path;
- const ProxyRoute = proxyBeforeEnter(Router, CselfRoute);
- newRouterMap.push(ProxyRoute);
- }));
- if (Object.keys(cloneSelfRoutes).length > 0) {
- const testG = cloneSelfRoutes['*'];
- if (testG && routes == null) {
- const ProxyRoute = proxyBeforeEnter(Router, Router.selfRoutes['*']);
- newRouterMap.push(ProxyRoute);
- }
- if (routes == null) {
- const cloneSelfRoutesKeys = Object.keys(cloneSelfRoutes);
- for (let i = 0; i < cloneSelfRoutesKeys.length; i += 1) {
- const key = cloneSelfRoutesKeys[i];
- if (key !== '*') {
- warn(`实例化时传递的routes参数:\r\n\r\n ${JSON.stringify(cloneSelfRoutes[key])} \r\n\r\n 在pages.json中未找到。自定排除掉,不会添加到路由中`);
- }
- }
- }
- }
- } else {
- const Routes = routes || Router.selfRoutes;
- const RoutesKeys = Object.keys(Routes);
- for (let i = 0; i < RoutesKeys.length; i += 1) {
- const key = RoutesKeys[i];
- const item = Routes[key];
-
- if (item._ROUTERPATH != null) {
- continue;
- }
- delete item.components;
- delete item.children;
- item.path = item.aliasPath || item.path;
- if (item.path !== '*') {
- item.component = item.component.render || item.component;
- }
- item.meta = {
- ...item.meta || {},
- PATHKEY: item.aliasPath ? 'aliasPath' : 'path',
- pagePath: item.path.substring(1),
- };
- const ProxyRoute = proxyBeforeEnter(Router, item);
- newRouterMap.push(ProxyRoute);
- }
- }
- return newRouterMap;
- };
|