diff --git a/unitool.js b/unitool.js index a09a8cc..4c38d0f 100644 --- a/unitool.js +++ b/unitool.js @@ -4,8 +4,8 @@ const BASE_TYPE_DEFAULT = 'SERVER' // one of { SERVER: 服务器, UNICLOUD_FUNC: 云函数, UNICLOUD_OBJECT: 云对象 } const my = { langNow () { - // getCurrentPages() 在 topWindow/App.vue 里有可能为空,所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里,getApp().$store.state 未定义,所以还是用 wo.ss - return wo?.ss?.i18n?.mylang || getApp()?.$store?.state?.i18n?.mylang + // getCurrentPages() 在 topWindow/App.vue 里有可能为空,所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里,getApp().$store.state 未定义,所以还是用 globalThis.wo?.ss + return globalThis.wo?.ss?.i18n?.mylang || getApp()?.$store?.state?.i18n?.mylang }, } @@ -43,17 +43,17 @@ export default { localizeText (i18nText) { i18nText = i18nText || // 如果传入i18n参数 ({zhCN:'...', enUS:'...'}) - this.i18nText || // 1) 如果挂载到具体页面的 computed { lote: wo.localizeText } 那么 this 就是当前页面,直接取用 this.i18nText 即可。2) 对于组件内定义的 i18nText,要使用 this 来获得组件内的 i18nText + this.i18nText || // 1) 如果挂载到具体页面的 computed { lote: wo?.localizeText } 那么 this 就是当前页面,直接取用 this.i18nText 即可。2) 对于组件内定义的 i18nText,要使用 this 来获得组件内的 i18nText getCurrentPages()?.pop()?.i18nText // 如果不是挂载到 Vue.prototype 而是 挂载到 wo 下调用,那么 this.i18nText 就不存在了。因此通过 pageNow.i18nText 访问。 return i18nText?.[my.langNow()] || i18nText?.earth || i18nText?.default || (typeof i18nText === 'string' ? i18nText : '') // 必须检测是否string,如果直接返回 i18nText 可能返回{}等,导致依赖于返回空值的前端出错 }, localeText () { - // 专供绑定到 computed { lote: wo.localeText } 使用,这时 this 就是当前页面。 + // 专供绑定到 computed { lote: wo?.localeText } 使用,这时 this 就是当前页面。 return this.i18nText?.[my.langNow()] || {} }, - setBarTitles ({ windowTitle, pageTitle, pagesJson = this.pagesJson || wo?.pagesJson, envar = this.envar || wo?.envar } = {}) { + setBarTitles ({ windowTitle, pageTitle, wo = globalThis.wo } = {}) { const langNow = my.langNow() const pageNow = getCurrentPages()?.pop?.() @@ -62,7 +62,7 @@ export default { pageNow?.$vm?.$data?.i18nPageTitle?.[langNow] || // 页面.vue 的 i18nPageTitle 变量 pageNow?.$vm?.$data?.i18nText?.[langNow]?.tPageTitle || // 页面.vue 的 i18nText 对象 pageNow?.$vm?.$data?.pageTitle || - pagesJson?.pages?.find((page) => page.path === pageNow?.route)?.i18nPageTitle?.[langNow] || // pages.json 的页面配置里 + wo?.pagesJson?.pages?.find((page) => page.path === pageNow?.route)?.i18nPageTitle?.[langNow] || // pages.json 的页面配置里 '' // 在app里,pageNow.route 是正确的,但是 pageNow.xxx 等自定义属性 都 undefined,必须 pageNow.$vm.$data.xxx 才可以。注意,$vm.$data 不包括 computed 属性,而 pageNow 里包括。 @@ -70,9 +70,8 @@ export default { windowTitle || wo?.metakey?.callnames?.[langNow] || wo?.envar?.callnames?.[langNow] || - wo?.envarRemote?.callnames?.[langNow] || - pagesJson?.appInfo?.i18nText?.[langNow] || - pagesJson?.globalStyle?.navigationBarTitleText || + wo?.pagesJson?.appInfo?.i18nText?.[langNow] || + wo?.pagesJson?.globalStyle?.navigationBarTitleText || '' if (uni.getSystemInfoSync().deviceType === 'pc') { @@ -98,12 +97,12 @@ export default { uni.hideTabBar() } else { // 必须要在有 tab 的页面里 setTabBarItem 才有效果 - //const midIndex = parseInt(pagesJson?.tabBar?.list?.length/2) // 如果存在midButton,实际上tabBar.list.length必须为偶数。不过为了心安,再parseInt一下。 - pagesJson?.tabBar?.list?.forEach((tab, tabIndex) => { + //const midIndex = parseInt(wo?.pagesJson?.tabBar?.list?.length/2) // 如果存在midButton,实际上tabBar.list.length必须为偶数。不过为了心安,再parseInt一下。 + wo?.pagesJson?.tabBar?.list?.forEach((tab, tabIndex) => { if (tab.i18nText && tab.i18nText[langNow]) { uni.setTabBarItem({ // #ifdef H5 - index: tabIndex, // + ((pagesJson?.tabBar?.midButton?.iconPath && tabIndex >= midIndex)?1:0), // H5 里,如果使用了 midButton,tabBarItem的index出现错位,需hack调整。推测,在H5里 midButton 作为一个普通tab被插入到 tabBar 里,导致 tabBar 的 index 和 pagesJson.tabBar.list 的 index 错位了。[20211031] 注意到,从 HBuilderX 3.2.12.20211029 起,在 H5 里也没有错位了。 + index: tabIndex, // + ((wo?.pagesJson?.tabBar?.midButton?.iconPath && tabIndex >= midIndex)?1:0), // H5 里,如果使用了 midButton,tabBarItem的index出现错位,需hack调整。推测,在H5里 midButton 作为一个普通tab被插入到 tabBar 里,导致 tabBar 的 index 和 wo?.pagesJson.tabBar.list 的 index 错位了。[20211031] 注意到,从 HBuilderX 3.2.12.20211029 起,在 H5 里也没有错位了。 // #endif // #ifndef H5 index: tabIndex, @@ -115,7 +114,8 @@ export default { } }, - make_server_url (route) { + make_server_url (route, wo = globalThis.wo) { + const envar = wo?.envar || {} if (typeof route === 'string') route = route.replace('\\', '/') else if (route?.apiWho && route?.apiTodo) { const { apiVersion = 'api', apiWho, apiTodo } = route @@ -134,10 +134,9 @@ export default { } // cid if (/^[\da-zA-Z]+$/.test(route)) { - return (wo?.ss?.envarRemote?.ipfsGateway || '') + route + return (envar.ipfsGateway || '') + route } // 需要组装url - const envar = this.envar || wo?.envar || {} route = route.replace(/^\//, '') // 已有现成后端服务域名 if (envar.servUrl) { @@ -157,7 +156,7 @@ export default { * - CLINET_WOBASE_EXCEPTION: 前端发现后台异常 **/ async callBase ({ - baseType = this.envar?.baseTypeDefault || wo?.envar?.baseTypeDefault || BASE_TYPE_DEFAULT, + baseType = globalThis.wo?.envar?.baseTypeDefault || BASE_TYPE_DEFAULT, httpMethod = 'POST', apiVersion = 'api', apiWho, @@ -169,7 +168,7 @@ export default { const startTime = new Date().toJSON() let url = undefined let { uniPlatform, deviceType, osName } = uni.getSystemInfoSync() - apiWhat._clientInfo = { uniPlatform, deviceType, osName } + apiWhat._clientInfo = { uniPlatform, deviceType, osName, url: globalThis.location?.href } apiWhat._passtoken = uni.getStorageSync('_passtoken') let result = {} if (baseType === 'UNICLOUD_OBJECT') { @@ -388,7 +387,7 @@ export default { }, async pickupFile ({ - baseType = this.envar?.baseTypeDefault || wo?.envar?.baseTypeDefault || BASE_TYPE_DEFAULT, + baseType = globalThis.wo?.envar?.baseTypeDefault || BASE_TYPE_DEFAULT, mediaType = 'image', count = 1, sizeType = ['original', 'compressed'], @@ -419,7 +418,6 @@ export default { }, parse_ua_to_browser () { - let userAgent = '' // #ifdef H5 userAgent = window.navigator.userAgent.toLowerCase() return /msie/.test(userAgent) && !/opera/.test(userAgent) @@ -455,16 +453,16 @@ export default { callback // 发生在 toast 之后 }) */ - showToast ({ tool, type = 'success', image, title, duration = 2000, ...rest }) { + showToast ({ tool, type = 'success', image, title, duration = 2000, wo = globalThis.wo, ...rest } = {}) { // rename to popToast? if (tool !== 'uni') { // 来自 - const toast = getCurrentPages()?.pop()?.$refs?.toast || getApp().globalData?.toast || wo.toast - const popup = getCurrentPages()?.pop()?.$refs?.popup || getApp().globalData?.popup || wo.popup + const toast = getCurrentPages()?.pop()?.$refs?.toast || getApp().globalData?.toast || wo?.toast + const popup = getCurrentPages()?.pop()?.$refs?.popup || getApp().globalData?.popup || wo?.popup if (toast) { toast.show({ type, title, duration, ...rest }) return - } else if (popup) { + } else if (popup && wo?.ss) { wo.ss.popMessage = title wo.ss.popType = type // success/error/warning/info wo.ss.popDuration = duration @@ -512,16 +510,6 @@ export default { return `${parseInt(Number(amount) * Math.pow(10, precision)) / Math.pow(10, precision)}` }, - format_coin (amount, { coin = wo.envar.KEYNAME, precision = 8 } = {}) { - if (Number.isNaN(parseInt(amount))) { - return '' - } - if (coin === wo.envar.KEYNAME && wo.ss.envarRemote?.pexPrecision) { - precision = wo.ss.envarRemote?.pexPrecision - } - return `${parseInt(Number(amount) * Math.pow(10, precision)) / Math.pow(10, precision)}` - }, - formatPercent (value, precision = 2) { return Number(value * 100 || 0).toFixed(precision) }, @@ -601,12 +589,12 @@ export default { }, 1000) }, - goto_page (pageName, { forget = false, pagesJson = this.pagesJson || wo?.pagesJson } = {}) { + goto_page (pageName, { type = 'navigateTo', wo = globalThis.wo } = {}) { if (pageName) { - if (pagesJson?.tabBar?.list?.find((item) => item?.pagePath?.substr(6) === pageName)) { + if (wo?.pagesJson?.tabBar?.list?.find((item) => item?.pagePath?.substr(6) === pageName)) { // 注意,即使在 PC 上 topWindow 代替了 tabBar 时,从标签页转化而来的菜单页,也是用 switchTab 跳转。 uni.switchTab({ url: pageName }) - } else if (forget) { + } else if (type === 'navigateTo') { uni.navigateTo({ url: pageName }) } else { uni.redirectTo({ url: pageName }) @@ -628,15 +616,15 @@ export default { } }, - copy_to_clipboard (text, { promptLength = 50, hidePrompt = false } = {}) { + copy_to_clipboard (text, { promptLength = 50, hidePrompt = false, wo = globalThis.wo } = {}) { uni.setClipboardData({ data: text, success: () => { uni.hideToast() if (!hidePrompt) - wo.tt.showToast({ - type: wo.c2t.GREEN, - title: `${wo.ll({ zhCN: '已成功拷贝', enUS: 'Successfully copied' })}\n${String(text).substring(0, promptLength)}`, + wo?.tt?.showToast?.({ + type: 'success', + title: `${wo?.ll?.({ zhCN: '已成功拷贝', enUS: 'Successfully copied' })}\n${String(text).substring(0, promptLength)}`, }) }, })