UNPKG

my-uniapp-tools

Version:

一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能

2 lines (1 loc) 4.4 kB
"use strict";var e=require("./core.cjs");const t=e.ErrorHandler.getInstance();let n=null,r=null,o=null;const i=e=>"weixin"===e||"alipay"===e;let s=!1;const a=()=>{if(n)return n;let e="unknown";try{"undefined"!=typeof wx&&wx.getSystemInfoSync?e="weixin":"undefined"!=typeof my&&my.getSystemInfoSync?e="alipay":"undefined"!=typeof window&&window.document?e="h5":"undefined"!=typeof plus&&(e="app")}catch(t){e="weixin",e="web",e="app",e="alipay",e="h5"}return n=e,e},c=(t=!0)=>t&&o?o:e.safeSync(()=>{if("undefined"==typeof uni||"function"!=typeof uni.getWindowInfo)return null;const e=uni.getWindowInfo();return t&&(o=e),e},"system","GET_WINDOW_INFO_ERROR",null),u=()=>{if(null!==r)return r;try{const e=c();return r=e?.statusBarHeight||0,r}catch(n){return t.handleError(new e.UniAppToolsError("GET_STATUS_BAR_HEIGHT_ERROR",`获取状态栏高度失败: ${n instanceof Error?n.message:String(n)}`,"system"),"system"),0}},l=()=>{try{const e=a();return i(e)?uni.getMenuButtonBoundingClientRect():null}catch(n){return t.handleError(new e.UniAppToolsError("GET_MENU_BUTTON_ERROR",`获取菜单按钮边界信息失败: ${n instanceof Error?n.message:String(n)}`,"system"),"system"),null}},p=()=>{try{const e=a();if(i(e)){const e=l();return e?.height||44}return 44}catch(n){return t.handleError(new e.UniAppToolsError("GET_NAVIGATION_BAR_HEIGHT_ERROR",`获取导航栏高度失败: ${n instanceof Error?n.message:String(n)}`,"system"),"system"),44}},f=()=>{const e=a(),t=u(),n=p();return{platform:e,statusBarHeight:t,navigationBarHeight:n,totalTopHeight:t+n}};exports.clearSystemCache=()=>{o=null},exports.getCurrentEnv=()=>{try{if("undefined"==typeof uni||"function"!=typeof uni.getAccountInfoSync)throw new Error("当前环境不支持 getAccountInfoSync");const e=uni.getAccountInfoSync();return{appId:e.miniProgram?.appId||"",version:e.miniProgram?.version||"",envVersion:e.miniProgram?.envVersion||"",accountInfo:e}}catch(n){return t.handleError(new e.UniAppToolsError("GET_ACCOUNT_INFO_ERROR",`获取小程序账户信息失败: ${n instanceof Error?n.message:String(n)}`,"system"),"system"),{appId:"",version:"",envVersion:"",accountInfo:{}}}},exports.getMenuButtonBoundingClientRect=l,exports.getNavHeight=()=>f().totalTopHeight,exports.getNavigationBarHeight=p,exports.getPlatform=a,exports.getStatusBarHeight=u,exports.getTopBarMetrics=f,exports.getTopNavBarHeight=()=>{const e=f();return{statusBarHeight:e.statusBarHeight,navHeight:e.totalTopHeight}},exports.onCheckForUpdate=()=>{if(!s)try{if("undefined"==typeof uni||"function"!=typeof uni.getUpdateManager)return;const n=uni.getUpdateManager();s=!0,n.onCheckForUpdate(function(e){e.hasUpdate}),n.onUpdateReady(function(e){uni.showModal({title:"更新提示",content:"新版本已经准备好,是否重启应用?",showCancel:!0,cancelText:"稍后",confirmText:"立即重启",success(e){e.confirm&&n.applyUpdate()}})}),n.onUpdateFailed(function(n){t.handleError(new e.UniAppToolsError("UPDATE_DOWNLOAD_FAILED","新版本下载失败","system"),"system"),uni.showModal({title:"更新失败",content:"新版本下载失败,请检查网络连接或稍后重试",showCancel:!1,confirmText:"知道了"})})}catch(n){t.handleError(new e.UniAppToolsError("CHECK_UPDATE_ERROR",`版本更新检查失败: ${n instanceof Error?n.message:String(n)}`,"system"),"system")}},exports.setPageIcon=(t,n="image/x-icon")=>e.safeSync(()=>{if("string"!=typeof t||""===t.trim())return!1;const e=a();if("h5"===e||"web"===e){if("undefined"!=typeof document){document.querySelectorAll('link[rel*="icon"]').forEach(e=>e.remove());const e=document.createElement("link");return e.rel="icon",e.type=n,e.href=t,document.head.appendChild(e),!0}}else console.warn("setPageIcon 仅在H5/Web平台有效");return!1},"system","SET_PAGE_ICON_ERROR",!1),exports.setPageTitle=n=>"string"!=typeof n?(console.warn("setPageTitle: title必须是字符串"),Promise.resolve(!1)):""===n.trim()?(console.warn("setPageTitle: title不能为空"),Promise.resolve(!1)):new Promise(r=>{try{if("undefined"==typeof uni||"function"!=typeof uni.setNavigationBarTitle)return void r(!1);uni.setNavigationBarTitle({title:n,success:()=>{r(!0)},fail:e=>{console.warn("设置页面标题失败:",e),r(!1)}})}catch(n){t.handleError(new e.UniAppToolsError("SET_PAGE_TITLE_ERROR",`setPageTitle执行异常: ${n instanceof Error?n.message:String(n)}`,"system"),"system"),r(!1)}}),exports.useWindowInfo=c;