my-uniapp-tools
Version:
一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能
2 lines (1 loc) • 3.11 kB
JavaScript
;var e=require("./utils.cjs"),t=require("./core.cjs");const n=t.ErrorHandler.getInstance();let o={defaultHomePage:"/pages/index/index"};function r(e,t){const n="string"==typeof e?e:String(e??"");if(!t||"object"!=typeof t)return n;const o=Object.entries(t).map(([e,t])=>`${encodeURIComponent(e)}=${encodeURIComponent(String(t))}`).join("&");return o?n+(n.includes("?")?"&":"?")+o:n}const i=r;function s(e,o,r){const i=r?.errMsg?`${o} - ${r.errMsg}`:o;n.handleError(new t.UniAppToolsError(e,i,"navigation"),"navigation")}function a(){return"function"==typeof getCurrentPages?getCurrentPages():[]}function u(){return"undefined"==typeof uni?null:uni}async function c(e="",t={}){return new Promise(n=>{const o=t&&"object"==typeof t?t:{},r="number"==typeof o.delta&&Number.isFinite(o.delta)&&o.delta>0?Math.floor(o.delta):1,i="number"==typeof o.timeout&&Number.isFinite(o.timeout)&&o.timeout>0?o.timeout:5e3,c=u();if(!c||"function"!=typeof c.navigateBack)return s("NAVIGATION_ENV_ERROR","当前环境不支持 navigateBack"),void n(!1);const f=a();if(f.length<=r)return console.warn(`[navigation] 无法返回${r}页,当前页面栈深度不足(当前: ${f.length})`),void n(!1);let l=!1;const g=setTimeout(()=>{l||(l=!0,console.warn("[navigation] 页面init回调执行超时"),n(!1))},i);c.navigateBack({delta:r,success:()=>{clearTimeout(g),setTimeout(()=>{if(!l)try{const t=a();if(t.length>0){const n=t[t.length-1];n.$vm&&"function"==typeof n.$vm.init&&n.$vm.init(e)}l=!0,n(!0)}catch(e){s("PAGE_CALLBACK_ERROR",`页面回调执行失败: ${e instanceof Error?e.message:String(e)}`),l=!0,n(!0)}},100)},fail:e=>{clearTimeout(g),s("NAVIGATE_BACK_FAILED","导航返回失败",e),l=!0,n(!1)}})})}const f=e.debounce(c,300);function l(){try{const e=a();if(0===e.length)return null;const t=e[e.length-1];return{route:t.route||"",options:t.options||{}}}catch(e){return s("GET_CURRENT_PAGE_ERROR",`获取当前页面信息失败: ${e instanceof Error?e.message:String(e)}`),null}}const g=l;function p(){try{return a().map(e=>({route:e.route||"",options:e.options||{}}))}catch(e){return s("GET_PAGE_STACK_ERROR",`获取页面栈信息失败: ${e instanceof Error?e.message:String(e)}`),[]}}const m=p;exports.buildUrl=i,exports.configureNavigation=function(e){e&&"object"==typeof e&&(o={...o,...e})},exports.getCurrentPageInfo=g,exports.getPageStack=m,exports.useBack=c,exports.useBackDebounced=f,exports.useBackOrHome=async function(e="",t={}){const n=t&&"object"==typeof t?t:{},i=a(),f="number"==typeof n.delta&&Number.isFinite(n.delta)&&n.delta>0?Math.floor(n.delta):1;if(i.length>f)return await c(e,t);const l=n.homePage||o.defaultHomePage,g=l.startsWith("/")?l:`/${l}`;return console.info(`[navigation] 页面栈深度不足,重定向到首页: ${g}`),new Promise(e=>{const t=u();if(!t||"function"!=typeof t.reLaunch)return s("NAVIGATION_ENV_ERROR","当前环境不支持 reLaunch"),void e(!1);const o=r(g,n.homeParams);t.reLaunch({url:o,success:()=>e(!0),fail:t=>{s("RELAUNCH_FAILED",`重新启动失败: ${o}`,t),e(!1)}})})},exports.useBuildUrl=r,exports.useCurrentPageInfo=l,exports.usePageStack=p;