my-uniapp-tools
Version:
一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能
2 lines (1 loc) • 4.26 kB
JavaScript
import{safeSync as e,ErrorHandler as n,UniAppToolsError as t}from"./core.js";const r=n.getInstance();let o=null,i=null,s=null;const a=()=>{s=null},c=e=>"weixin"===e||"alipay"===e;let u=!1;const l=()=>{if(o)return o;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(n){e="weixin",e="web",e="app",e="alipay",e="h5"}return o=e,e},f=(n=!0)=>n&&s?s:e(()=>{if("undefined"==typeof uni||"function"!=typeof uni.getWindowInfo)return null;const e=uni.getWindowInfo();return n&&(s=e),e},"system","GET_WINDOW_INFO_ERROR",null),y=()=>{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(e){return r.handleError(new t("GET_ACCOUNT_INFO_ERROR",`获取小程序账户信息失败: ${e instanceof Error?e.message:String(e)}`,"system"),"system"),{appId:"",version:"",envVersion:"",accountInfo:{}}}},d=()=>{if(!u)try{if("undefined"==typeof uni||"function"!=typeof uni.getUpdateManager)return;const e=uni.getUpdateManager();u=!0,e.onCheckForUpdate(function(e){e.hasUpdate}),e.onUpdateReady(function(n){uni.showModal({title:"更新提示",content:"新版本已经准备好,是否重启应用?",showCancel:!0,cancelText:"稍后",confirmText:"立即重启",success(n){n.confirm&&e.applyUpdate()}})}),e.onUpdateFailed(function(e){r.handleError(new t("UPDATE_DOWNLOAD_FAILED","新版本下载失败","system"),"system"),uni.showModal({title:"更新失败",content:"新版本下载失败,请检查网络连接或稍后重试",showCancel:!1,confirmText:"知道了"})})}catch(e){r.handleError(new t("CHECK_UPDATE_ERROR",`版本更新检查失败: ${e instanceof Error?e.message:String(e)}`,"system"),"system")}},m=()=>{if(null!==i)return i;try{const e=f();return i=e?.statusBarHeight||0,i}catch(e){return r.handleError(new t("GET_STATUS_BAR_HEIGHT_ERROR",`获取状态栏高度失败: ${e instanceof Error?e.message:String(e)}`,"system"),"system"),0}},g=()=>{try{const e=l();return c(e)?uni.getMenuButtonBoundingClientRect():null}catch(e){return r.handleError(new t("GET_MENU_BUTTON_ERROR",`获取菜单按钮边界信息失败: ${e instanceof Error?e.message:String(e)}`,"system"),"system"),null}},p=()=>{try{const e=l();if(c(e)){const e=g();return e?.height||44}return 44}catch(e){return r.handleError(new t("GET_NAVIGATION_BAR_HEIGHT_ERROR",`获取导航栏高度失败: ${e instanceof Error?e.message:String(e)}`,"system"),"system"),44}},E=()=>{const e=l(),n=m(),t=p();return{platform:e,statusBarHeight:n,navigationBarHeight:t,totalTopHeight:n+t}},h=()=>{const e=E();return{statusBarHeight:e.statusBarHeight,navHeight:e.totalTopHeight}},w=()=>E().totalTopHeight,T=e=>"string"!=typeof e?(console.warn("setPageTitle: title必须是字符串"),Promise.resolve(!1)):""===e.trim()?(console.warn("setPageTitle: title不能为空"),Promise.resolve(!1)):new Promise(n=>{try{if("undefined"==typeof uni||"function"!=typeof uni.setNavigationBarTitle)return void n(!1);uni.setNavigationBarTitle({title:e,success:()=>{n(!0)},fail:e=>{console.warn("设置页面标题失败:",e),n(!1)}})}catch(e){r.handleError(new t("SET_PAGE_TITLE_ERROR",`setPageTitle执行异常: ${e instanceof Error?e.message:String(e)}`,"system"),"system"),n(!1)}}),R=(n,t="image/x-icon")=>e(()=>{if("string"!=typeof n||""===n.trim())return!1;const e=l();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=t,e.href=n,document.head.appendChild(e),!0}}else console.warn("setPageIcon 仅在H5/Web平台有效");return!1},"system","SET_PAGE_ICON_ERROR",!1);export{a as clearSystemCache,y as getCurrentEnv,g as getMenuButtonBoundingClientRect,w as getNavHeight,p as getNavigationBarHeight,l as getPlatform,m as getStatusBarHeight,E as getTopBarMetrics,h as getTopNavBarHeight,d as onCheckForUpdate,R as setPageIcon,T as setPageTitle,f as useWindowInfo};