UNPKG

@morjs/runtime-web

Version:
191 lines 6.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAbsolutePath = exports.getRelaunchOptions = exports.getOriginUrl = exports.getPageId = exports.getRoute = exports.getPathNameAndSearch = exports.getQueryParams = exports.getOptions = exports.getPathAndOptions = exports.removeLeadingSlash = exports.addLeadingSlash = exports.getCustomUrl = exports.setPages = exports.setCustomRoutes = exports.setBaseName = void 0; const tslib_1 = require("tslib"); const lodash_get_1 = tslib_1.__importDefault(require("lodash.get")); const history_1 = require("./history"); const KEY_REFERRER = '__referrerInfo'; let pages; let referrerInfo = {}; const customRoutes = []; let baseName; function setBaseName(path) { baseName = path || ''; } exports.setBaseName = setBaseName; function setCustomRoutes(routes) { for (const key in routes) { // eslint-disable-next-line no-prototype-builtins if (routes.hasOwnProperty(key)) { customRoutes.push([(0, exports.addLeadingSlash)(key), routes[key]]); } } window.$customRoutes = customRoutes; } exports.setCustomRoutes = setCustomRoutes; function setPages(pageList) { pages = pageList; } exports.setPages = setPages; const getCustomUrl = (url) => { var _a; const urlArr = url.split('?', 2); url = urlArr[0]; if (customRoutes) { const dest = (_a = customRoutes.find((route) => route[0] === url)) === null || _a === void 0 ? void 0 : _a[1]; // tslint:disable-next-line:no-unused-expression dest ? (url = dest) : null; } if (urlArr[1]) { return `${(0, exports.addLeadingSlash)(url)}?${urlArr[1]}`; } else { return (0, exports.addLeadingSlash)(url); } }; exports.getCustomUrl = getCustomUrl; const addLeadingSlash = (str) => str && str[0] === '/' ? str : `/${str}`; exports.addLeadingSlash = addLeadingSlash; const removeLeadingSlash = (str) => str && str[0] === '/' ? str.substring(1) : str; exports.removeLeadingSlash = removeLeadingSlash; const getPathAndOptions = (location) => { const route = (0, exports.getRoute)(location); return { route, path: route, options: (0, exports.getOptions)(location) }; }; exports.getPathAndOptions = getPathAndOptions; /** * 小程序页面的options */ const getOptions = (location = window.location) => { const options = getQueryParams(location.search); return history_1.mode === 'hash' ? Object.assign(options, getQueryParams(location.hash)) : options; }; exports.getOptions = getOptions; /** * 获取当前url的参数 */ function getQueryParams(url) { const params = {}; const searchArr = url.split('?'); if (searchArr[1]) { searchArr[1].split('&').forEach((pair) => { const [key, value] = pair.split('='); params[key] = value; }); } return params; } exports.getQueryParams = getQueryParams; /** * h5 url的 pathname 和 search */ const getPathNameAndSearch = (location) => { let pathName; let search; if (history_1.mode === 'hash') { const hashUrl = (location.hash || '#/').substr(1); const pathArr = hashUrl.split('?'); pathName = pathArr[0]; search = pathArr[1]; } else { pathName = location.pathname; search = location.search.split('?')[1]; // 往外暴露修改 search 的方法,有些业务中 url 上存在动态变化的参数,导致 url => page component 的映射关系混乱 const formatSearch = (0, lodash_get_1.default)(window.$MOR_APP_CONFIG, 'router.formatSearch'); if (typeof formatSearch === 'function') search = formatSearch(search); } return { pathName: stripBasename(pathName), search }; }; exports.getPathNameAndSearch = getPathNameAndSearch; function stripBasename(path) { return hasBasename(path) ? path.substr(baseName.length) : path; } function hasBasename(path) { if (!baseName) return false; return (path.toLowerCase().indexOf(baseName.toLowerCase()) === 0 && '/?#'.indexOf(path.charAt(baseName.length)) !== -1); } /** * route,小程序route,例如 pages/index/index */ const getRoute = (location = window.location) => { var _a; const { pathName } = (0, exports.getPathNameAndSearch)(location); const customRoute = (customRoutes || window.$customRoutes).filter(([url, customUrl]) => pathName === url || pathName === customUrl); const route = customRoute.length ? (_a = customRoute === null || customRoute === void 0 ? void 0 : customRoute[0]) === null || _a === void 0 ? void 0 : _a[0] : pathName === '/' || pathName === '' ? pages[0] : pathName; return (0, exports.removeLeadingSlash)(route); }; exports.getRoute = getRoute; /** * pageId,标识唯一page */ const getPageId = (location = window.location) => { return (0, exports.getOriginUrl)(location); }; exports.getPageId = getPageId; /** * 小程序完整地址, 例如 pages/index/index?test=1 */ const getOriginUrl = (location) => { const { search } = (0, exports.getPathNameAndSearch)(location); const route = (0, exports.getRoute)(location); return search ? `${route}?${search}` : route; }; exports.getOriginUrl = getOriginUrl; /** * 小程序启动参数 */ const getRelaunchOptions = () => { const lsReferrerInfo = localStorage.getItem(KEY_REFERRER); referrerInfo = (lsReferrerInfo && JSON.parse(lsReferrerInfo)) || referrerInfo; localStorage.removeItem(KEY_REFERRER); return { query: (0, exports.getOptions)(), referrerInfo }; }; exports.getRelaunchOptions = getRelaunchOptions; /** * 根据页面相对路径,计算出绝对路径 '../swiper/index' => 'pages/swiper/index' * @param {String} relativePath 相对路径 * @returns 绝对路径 */ const getAbsolutePath = (relativePath) => { var _a; const rootPath = (_a = getCurrentPages().pop()) === null || _a === void 0 ? void 0 : _a.route; if (rootPath && relativePath[0] === '.') { const rootParts = rootPath.split('/'); const relativeParts = relativePath.split('/'); const absoluteParts = [...relativeParts]; relativeParts.forEach((part, index) => { if (part === '.') { rootParts.pop(); absoluteParts.shift(); } if (part === '..') { index === 0 ? rootParts.splice(-2, 2) : rootParts.pop(); absoluteParts.shift(); } }); return rootParts.concat(absoluteParts).join('/'); } return relativePath; }; exports.getAbsolutePath = getAbsolutePath; //# sourceMappingURL=url.js.map