From 5fc9b3ef1cce55dc68ac48d54efb12b4b8c9169b Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Thu, 9 Nov 2023 15:15:12 +0800 Subject: [PATCH] use wo.envar.clientInfo.* instead of uni.getSystemInfoSync() --- unitool.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/unitool.js b/unitool.js index 16d8e34..56d0b54 100644 --- a/unitool.js +++ b/unitool.js @@ -74,7 +74,7 @@ export default { wo?.pagesJson?.globalStyle?.navigationBarTitleText || '' - if (uni.getSystemInfoSync().deviceType === 'pc') { + if (wo.envar.clientInfo.deviceType === 'pc') { uni.setNavigationBarTitle({ title: windowTitle + (navibarTitle ? ` - ${navibarTitle}` : '') }) } else { uni.setNavigationBarTitle({ title: navibarTitle }) @@ -84,7 +84,7 @@ export default { //// 设置窗口标题栏 document.title //// navibarTitle 也会被用于浏览器的标签标题,可用 document.title 去覆盖。必须放在 setNavigationBarTitle 之后。 //// 但这个方案,在电脑上,还是会显示 navibarTitle 在浏览器窗口顶栏,不知为何。 - if (uni.getSystemInfoSync().deviceType === 'phone' && /MicroMessenger/i.test(globalThis.window?.navigator?.userAgent)) { + if (wo.envar.clientInfo.deviceType === 'phone' && /MicroMessenger/i.test(globalThis.window?.navigator?.userAgent)) { //// 微信浏览器里,本身就显示了标题栏,和自有的导航栏形成功能重叠和混淆。 //// 设置标题栏为空或覆盖 document.title = windowTitle @@ -93,7 +93,7 @@ export default { } //#endif - if (uni.getSystemInfoSync().deviceType === 'pc') { + if (wo.envar.clientInfo.deviceType === 'pc') { uni.hideTabBar() } else { // 必须要在有 tab 的页面里 setTabBarItem 才有效果 @@ -167,8 +167,7 @@ export default { const thisRoute = getCurrentPages()?.pop?.()?.route || 'VueApp' // 立刻保存 route,因为在调用后台后,可能已切换到了其他页面。 const startTime = new Date().toJSON() let url = undefined - let { uniPlatform, deviceType, osName } = uni.getSystemInfoSync() - apiWhat._clientInfo = { uniPlatform, deviceType, osName, url: globalThis.location?.href } + apiWhat._clientInfo = globalThis.wo?.envar?.clientInfo apiWhat._passtoken = uni.getStorageSync('_passtoken') let result = {} if (baseType === 'UNICLOUD_OBJECT') { @@ -269,14 +268,14 @@ export default { let filePath if (mediaType === 'image') { let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType }) - filePath = tempFilePaths[0] + filePath = tempFilePaths?.[0] } else if (mediaType === 'video') { let [errorChoose, { tempFilePath }] = await uni.chooseVideo({ sourceType }) filePath = tempFilePath } else { // #ifdef H5 let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseFile({ count, type: 'all' }) - filePath = tempFilePaths[0] + filePath = tempFilePaths?.[0] // #endif // #ifndef H5 return { _state: 'UNKNOWN_MEDIATYPE' } @@ -291,11 +290,11 @@ export default { // 在 Fileloader/fileloader.js 里,已经不再依赖 _passtoken,而且 header 在被 nginx 或 cloudflare (没搞清楚是谁干的)代理之后也被过滤掉了,因此不再使用这一句: header._passtoken = uni.getStorageSync('_passtoken') formData['_passtoken'] = uni.getStorageSync('_passtoken') // 20230527 加回这一句,让后台可以根据验证用户来决定怎样处理文件。 - uni.showLoading() + this.showLoading() let [errorUpload, { data, statusCode } = {}] = await uni.uploadFile({ url: this.make_server_url(url), filePath, name, header, formData }) // 后台 Multer 处理 req.file = { destination, filename, originalname, path, mimetype, size }, 其中 path 包括了 destination 和 filename 的文件相对路径。 // url 指向的后台方法进一步处理后,通过 uni.uploadFile 存在 data 里返回结果: { ...file, cid?, ipfsUrl?, baseUrl? } - uni.hideLoading() + this.hideLoading() if (typeof data === 'string') { // 不知为何,uni.uploadFile返回的 data 是字符串而不是对象 @@ -323,9 +322,7 @@ export default { // return { _state: 'USER_OFFLINE', errMsg: 'offline user cannot upload files' } // } - let filePath, - cloudPath, - osName = uni.getSystemInfoSync().osName + let filePath, cloudPath if (mediaType === 'image') { let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType }) // uni.showModal({ title: 'tempFilePaths[0]=' + tempFilePaths[0] }) @@ -333,20 +330,20 @@ export default { // #ifndef H5 // let [errorGetImageInfo, { path, width, height, orientation, type }] = await uni.getImageInfo({ src: filePath }) // cloudPath = path // 完整路径,包含后缀名。形如 file:///var/mobile/Containers/Data/Application/55A76332-44F5-4D5F-A9F6-3F857D584883/Documents/Pandora/apps/D064A425A8BEC13F9D8F741B98B37BC5/doc/uniapp_temp_1598593902955/compressed/1598593925815.png - cloudPath = `APP_${osName}__${filePath}` // 在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名 + cloudPath = `APP__${filePath}` // 在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名 // #endif // #ifdef H5 - cloudPath = `H5_${osName}_${this.parse_ua_to_browser()}__${tempFiles[0].name}` // name is available in H5 only. 只包含文件名和后缀名,不包含路径。 + cloudPath = `H5__${tempFiles[0].name}` // name is available in H5 only. 只包含文件名和后缀名,不包含路径。 // #endif } else if (mediaType === 'video') { let [errorChoose, { tempFilePath, tempFile, duration, size, width, height, name }] = await uni.chooseVideo({ sourceType, maxDuration }) // uni.showModal({ title: 'tempFilePath=' + tempFilePath }) filePath = tempFilePath // 在 iOS 上形如 "file:///var/mobile/Containers/Data/Application/55A76332-44F5-4D5F-A9F6-3F857D584883/Documents/Pandora/apps/26B43CD2F587D37FC6799108434A6F84/doc/uniapp_temp_1598596171580/gallery/IMG_3082.MOV" // #ifndef H5 - cloudPath = `APP_${osName}_dur${duration}__${filePath}` + cloudPath = `APP__dur${duration}__${filePath}` // #endif // #ifdef H5 - cloudPath = `H5_${osName}_${this.parse_ua_to_browser()}_dur${duration}__${name}` // tempFile and name are H5 only + cloudPath = `H5__dur${duration}__${name}` // tempFile and name are H5 only // #endif // iOS 上测试,filePath 为 *.MOV,而阿里云只允许 *.mp4, 所以添加 .mp4 后缀。参见 https://uniapp.dcloud.net.cn/uniCloud/storage?id=clouduploadfile // 20200915测试,阿里云支持上传 *.mov 了。 @@ -367,7 +364,7 @@ export default { } if (filePath) { - uni.showLoading() + this.showLoading() const { fileID, requestId } = await uniCloud.uploadFile({ filePath: filePath, cloudPath: process.env.NODE_ENV !== 'production' ? 'dev_' + cloudPath : cloudPath, // 关键是要具有文件格式后缀名,这样可以保持阿里云下载链接也用这个后缀名。 @@ -376,7 +373,7 @@ export default { var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total) }, }) - uni.hideLoading() + this.hideLoading() if (fileID) { return { _state: 'SUCCESS', fileUrl: fileID, requestId } @@ -419,7 +416,7 @@ export default { parse_ua_to_browser () { // #ifdef H5 - userAgent = window.navigator.userAgent.toLowerCase() + let userAgent = globalThis.window?.navigator?.userAgent?.toLowerCase?.() return /msie/.test(userAgent) && !/opera/.test(userAgent) ? 'msie' : /opera/.test(userAgent) @@ -479,7 +476,8 @@ export default { // #endif }, - showLoading ({ title, mask }) { + showLoading ({ title = { zhCN: '加载中...', enUS: 'Loading...' }, mask } = {}) { + title = this.localizeText(title) // #ifndef APP-PLUS uni.showLoading({ title, mask }) // #endif