Compare commits

...

2 Commits

View File

@ -7,8 +7,8 @@ const path = require('path')
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 未定义,所以还是用 globalThis.wo?.ss
return globalThis.wo?.ss?.i18n?.mylang || getApp()?.$store?.state?.i18n?.mylang
// globalThis.getCurrentPages?.() 在 topWindow/App.vue 里有可能为空,所以用 getApp().$store 更安全. 20230513: 发现在微信小程序模拟器里getApp().$store.state 未定义,所以还是用 globalThis.wo?.ss
return globalThis.wo?.ss?.i18n?.mylang || globalThis.getApp?.()?.$store?.state?.i18n?.mylang
},
}
@ -53,21 +53,31 @@ module.exports = {
thisPage () {
return this.__page__
? this // constructor.name==='VueComponent' 只在 development 环境有用,在 production 环境会被简化成 'o'。
: getCurrentPages()?.pop?.() || {} // [20220401] 发现在 topWindow 里,或者在 App.vue 里, getCurrentPages() 是 undefined 和 空数组 [],因此在这里默认 {} 做保护。
: globalThis.getCurrentPages?.()?.pop?.() || {} // [20220401] 发现在 topWindow 里,或者在 App.vue 里, getCurrentPages() 是 undefined 和 空数组 [],因此在这里默认 {} 做保护。
},
localizeText (i18nText) {
localizeText (i18nText, langCode) {
i18nText =
i18nText || // 如果传入i18n参数 ({zhCN:'...', enUS:'...'})
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 ||
i18nText?.enUS ||
(['string', 'number', 'boolean'].includes(typeof i18nText) ? i18nText : '')
) // 必须检测是否标量值,如果直接返回 i18nText 可能返回{}等,导致依赖于返回空值的前端出错
if (['string', 'number', 'boolean'].includes(typeof i18nText)) {
// 必须先检测是否标量值,如果直接返回 i18nText 可能返回{}等,导致依赖于返回空值的前端出错
return i18nText
} else if (typeof i18nText === 'object' && i18nText) {
return (
i18nText?.[langCode] ||
i18nText?.[my.langNow()] ||
i18nText?.earTH ||
i18nText?.defLAN ||
i18nText?.gloBAL ||
i18nText?.enUS ||
Object.values(i18nText)[0] ||
''
)
} else {
return ''
}
},
localeText () {
@ -77,7 +87,7 @@ module.exports = {
setBarTitles ({ windowTitle, pageTitle, wo = globalThis.wo } = {}) {
const langNow = my.langNow()
const pageNow = getCurrentPages()?.pop?.()
const pageNow = globalThis.getCurrentPages?.()?.pop?.()
// 在ios/android app里pageNow.route 是正确的,但是 pageNow.xxx 等自定义属性 都 undefined必须 pageNow.$vm.$data.xxx 才可以。
// 注意,$vm.$data 不包括 computed 属性,而 pageNow 里包括。因此不能把 i18nPageTitle 放在 computed 里。
@ -188,7 +198,7 @@ module.exports = {
apiWhat = {},
timeout,
}) {
const thisRoute = getCurrentPages()?.pop?.()?.route || 'VueApp' // 立刻保存 route因为在调用后台后可能已切换到了其他页面。
const thisRoute = globalThis.getCurrentPages?.()?.pop?.()?.route || 'VueApp' // 立刻保存 route因为在调用后台后可能已切换到了其他页面。
const startTime = new Date().toJSON()
let apiurl = undefined
apiWhat._clientInfo = {
@ -314,7 +324,7 @@ module.exports = {
},
async pickupFile2Server ({
fileDragged,
fileDragged, // 形如 { filePath: "blob:http://localhost:8080/218e3007-f74b-440d-a2db-8f1d9aed746f", name: 'abc.txt', size: 2375, type: "text/plain", lastModified: 1732291306772 }
mediaType = 'image', // could be: image, video, array of supported extensions, anything else
count = 1,
sizeType = ['original', 'compressed'],
@ -328,6 +338,7 @@ module.exports = {
let filePath, fileSize, filePicked
if ('undefined' !== typeof fileDragged?.size) {
// size 为 0 的时候也该要做这些处理
fileSize = fileDragged.size
filePath = fileDragged.filePath
filePicked = fileDragged
@ -416,7 +427,8 @@ module.exports = {
formData[key] = JSON.stringify(formData[key])
}
// 在 Fileloader/fileloader.js 里,已经不再依赖 _passtoken而且 header 在被 nginx 或 cloudflare (没搞清楚是谁干的)代理之后也被过滤掉了,因此不再使用这一句: header._passtoken = uni.getStorageSync('_passtoken')
formData['_passtoken'] = uni.getStorageSync('_passtoken') // 20230527 加回这一句,让后台可以根据验证用户来决定怎样处理文件。
formData._passtoken = uni.getStorageSync('_passtoken') // 20230527 加回这一句,让后台可以根据验证用户来决定怎样处理文件。
formData.filenameOriginal = fileName // 20241020 不知为何,通过拖拽的文件上传后,后台得到的 file.originalname 形如 "file-1729354713176" 而不是真正的原始文件名,也不包含后缀。为了保持一致,这里主动把真实的文件名(不管是拖拽还是点击的)发给后台来处理。
this.showLoading({ title: { zhCN: '上传中...', enUS: 'Uploading...' } })
let [errorUpload, { data, statusCode } = {}] = await uni.uploadFile({ url: this.make_server_url(url), filePath, name, header, formData })
@ -428,11 +440,12 @@ module.exports = {
// 不知为何uni.uploadFile返回的 data 是字符串而不是对象
try {
data = JSON.parse(data)
if (fileDragged?.name) {
// 20241020 不知为何,通过拖拽的文件上传后,得到的结果里 originalname 形如 "file-1729354713176" 而不是真正的原始文件名,也不包含后缀。为了保持一致,这里强行重新设置
data.originalnameSystem = data.originalname
data.originalname = fileDragged.name
}
// 注释下面几句,改由后台来处理 filenameOriginal 以把正确的 data.originalname 返回给前端
// if (fileDragged?.name) {
// // 20241020 不知为何,通过拖拽的文件上传后,后台得到的 以及返回的 data.originalname 形如 "file-1729354713176" 而不是真正的原始文件名,也不包含后缀。为了保持一致,这里强行重新设置
// data.originalnameSystem = data.originalname
// data.originalname = fileDragged.name
// }
} catch (exp) {
return {
_state: 'BER_FAIL_RESPONSE_JSON_MALFORMED',
@ -626,7 +639,7 @@ module.exports = {
} else if (inWebview) {
wo.ss.webviewUrl = url
wo.ss.webviewTitle = title
uni.navigateTo({ url: 'tool-webview' })
uni.navigateTo({ url: 'show-webview' })
} else {
// #ifdef APP
plus.runtime.openURL(url)
@ -652,7 +665,7 @@ module.exports = {
} else {
wo.ss.webviewUrl = url
wo.ss.webviewTitle = title
uni.navigateTo({ url: 'tool-webview' })
uni.navigateTo({ url: 'show-webview' })
}
},
@ -697,8 +710,16 @@ module.exports = {
if (!title) {
return
}
const mypopup = getCurrentPages()?.pop()?.mypopup || getCurrentPages()?.pop()?.$refs?.mypopup || getApp().globalData?.mypopup || wo?.mypopup
const mytoast = getCurrentPages()?.pop()?.mytoast || getCurrentPages()?.pop()?.$refs?.mytoast || getApp().globalData?.mytoast || wo?.mytoast
const mypopup =
globalThis.getCurrentPages?.()?.pop()?.mypopup ||
globalThis.getCurrentPages?.()?.pop()?.$refs?.mypopup ||
globalThis.getApp().globalData?.mypopup ||
wo?.mypopup
const mytoast =
globalThis.getCurrentPages?.()?.pop()?.mytoast ||
globalThis.getCurrentPages?.()?.pop()?.$refs?.mytoast ||
globalThis.getApp().globalData?.mytoast ||
wo?.mytoast
if (mypopup) {
wo.ss.popMessage = title
wo.ss.popType = type // success/error/warning/info
@ -873,7 +894,7 @@ module.exports = {
},
next_focus (currentFocus, focusList) {
focusList = focusList || getCurrentPages()?.pop()?.focusList
focusList = focusList || globalThis.getCurrentPages?.()?.pop()?.focusList
if (focusList) {
for (let n in focusList) {
// 不论对数组或对象都有效n 是数组的index 或对象的key