UNPKG

my-uniapp-tools

Version:

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

2 lines (1 loc) 4.4 kB
import{ErrorHandler as e,UniAppToolsError as t}from"./core.js";const n=e.getInstance(),i=()=>!("undefined"==typeof navigator||!navigator.userAgent)&&navigator.userAgent.toLowerCase().includes("micromessenger"),r=()=>"undefined"!=typeof window&&"undefined"!=typeof document,o=()=>r()&&i()&&window.wx||null,s=(e,t=!1)=>{const n={};return e.forEach(e=>{"string"==typeof e&&""!==e.trim()&&(n[e]=t)}),n};let u=!1,a=null,c=!1,l=null;const m=["https://res.wx.qq.com/open/js/jweixin-1.6.0.js","https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"],p=()=>{u=!1,c=!1,a=null,l=null},f=(e={})=>{const t=e&&"object"==typeof e?e:{},n="number"==typeof t.timeoutMs&&Number.isFinite(t.timeoutMs)&&t.timeoutMs>0?t.timeoutMs:1e4;if(u||r()&&window.wx)return u=!0,Promise.resolve(!0);if(!r())return Promise.resolve(!1);if(a)return a;const i=Array.isArray(t.cdnUrls)&&t.cdnUrls.length>0?t.cdnUrls.filter(e=>"string"==typeof e&&""!==e.trim()):[],o=i.length>0?i:m;return a=new Promise(e=>{let t=!1,i=null,r=null,s=0;const c=n=>{t||(t=!0,i&&(clearTimeout(i),i=null),r&&r.parentNode&&r.parentNode.removeChild(r),n&&(u=!0),a=null,e(n))},l=()=>{if(s>=o.length)return void c(!1);if(!document.head)return void c(!1);const e=o[s];s+=1;const t=document.createElement("script");r=t,t.src=e,t.async=!0,t.onload=()=>c(!0),t.onerror=()=>{t.parentNode&&t.parentNode.removeChild(t),r=null,l()},document.head.appendChild(t)};i=setTimeout(()=>{c(!1)},n),l()}),a},d=async(e,r={})=>{const s=r&&"object"==typeof r?r:{},u="number"==typeof s.timeoutMs&&Number.isFinite(s.timeoutMs)&&s.timeoutMs>0?s.timeoutMs:1e4;return!(!e||"object"!=typeof e||"string"!=typeof e.appId||"string"!=typeof e.nonceStr||"string"!=typeof e.signature||"number"!=typeof e.timestamp||!Array.isArray(e.jsApiList))&&(i()?l||(l=(async()=>{if(!await f({timeoutMs:u}))return n.handleError(new t("WECHAT_JSSDK_LOAD_FAILED","微信 JS-SDK 加载失败","weixin"),"weixin"),!1;const i=o();if(!i||"function"!=typeof i.config||"function"!=typeof i.ready||"function"!=typeof i.error)return!1;const r=i.config,s=i.ready,a=i.error,l=await new Promise(i=>{let o=!1,c=null;const l=e=>{o||(o=!0,c&&(clearTimeout(c),c=null),i(e))};c=setTimeout(()=>l(!1),u);try{r({debug:e.debug||!1,appId:e.appId,timestamp:e.timestamp,nonceStr:e.nonceStr,signature:e.signature,jsApiList:e.jsApiList}),s(()=>l(!0)),a(e=>{n.handleError(new t("WECHAT_JSSDK_CONFIG_ERROR",`微信 JS-SDK 配置错误: ${(e=>{try{return JSON.stringify(e)}catch{return String(e)}})(e)}`,"weixin"),"weixin"),l(!1)})}catch(e){n.handleError(new t("WECHAT_JSSDK_CONFIG_EXCEPTION",`微信 JS-SDK 配置异常: ${e instanceof Error?e.message:String(e)}`,"weixin"),"weixin"),l(!1)}});return c=l,l})().finally(()=>{l=null}),l):(console.warn("当前不在微信环境中"),!1))},g=(e,i={})=>{const r=i&&"object"==typeof i?i:{},u="number"==typeof r.timeoutMs&&Number.isFinite(r.timeoutMs)&&r.timeoutMs>0?r.timeoutMs:8e3,a=Array.isArray(e)?e:[];return new Promise(e=>{const i=o();if(!i||"function"!=typeof i.checkJsApi)return void e(s(a,!1));let r=!1;const c=t=>{r||(r=!0,e(t))},l=setTimeout(()=>c(s(a,!1)),u);try{i.checkJsApi({jsApiList:a,success:e=>{clearTimeout(l),c({...s(a,!1),...e?.checkResult||{}})}})}catch(e){clearTimeout(l),n.handleError(new t("CHECK_WECHAT_JSAPI_ERROR",`检查微信 JS-SDK API 异常: ${e instanceof Error?e.message:String(e)}`,"weixin"),"weixin"),c(s(a,!1))}})},h=e=>{if(!e||"object"!=typeof e)return;const t=o();t&&(t.updateTimelineShareData?t.updateTimelineShareData(e):t.onMenuShareTimeline&&t.onMenuShareTimeline(e))},y=e=>{if(!e||"object"!=typeof e)return;const t=o();t&&(t.updateAppMessageShareData?t.updateAppMessageShareData(e):t.onMenuShareAppMessage&&t.onMenuShareAppMessage(e))};class w{static instance;isConfigured=!1;config=null;initPromise=null;constructor(){}static getInstance(){return w.instance||(w.instance=new w),w.instance}async init(e){return this.initPromise||(this.initPromise=d(e).then(t=>(this.isConfigured=t,this.config=t?e:null,t)).finally(()=>{this.initPromise=null})),this.initPromise}isReady(){return(this.isConfigured||c)&&i()}getConfig(){return this.config}reset(){this.isConfigured=!1,this.config=null,this.initPromise=null,p()}setShareData(e){this.isReady()?(h(e),y(e)):console.warn("微信 SDK 未就绪")}}export{w as WechatSDK,g as checkWechatJSAPI,d as configWechatJSSDK,i as isWechat,f as loadWechatJSSDK,p as resetWechatJSSDK,y as shareToFriend,h as shareToTimeline};