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