This commit is contained in:
luk 2024-11-28 10:18:16 +08:00
parent e11583b284
commit 948e16cc91

View File

@ -327,7 +327,7 @@ module.exports = {
// 有的管理后台不需要登录就允许上传,例如 cmctoy。因此不要在这里依赖登录状态。
let filePath, fileSize, filePicked
if (fileDragged?.size) {
if ('undefined' !== typeof fileDragged?.size) {
fileSize = fileDragged.size
filePath = fileDragged.filePath
filePicked = fileDragged
@ -339,7 +339,7 @@ module.exports = {
if (errorChoose) {
return {
_state: 'CER_FAIL_CHOOSE',
_msg: { zhCN: '图像选择失败。请稍后再试,或向客服投诉。', enUS: 'Image choose failed. Please try again later, or report to customer service.' },
_msg: '', // { zhCN: '图像选择失败。请稍后再试,或向客服投诉。', enUS: 'Image choose failed. Please try again later, or report to customer service.' },
}
}
fileSize = tempFiles?.[0]?.size
@ -350,7 +350,7 @@ module.exports = {
if (errorChoose) {
return {
_state: 'CER_FAIL_CHOOSE',
_msg: { zhCN: '视频选择失败。请稍后再试,或向客服投诉。', enUS: 'Video choose failed. Please try again later, or report to customer service.' },
_msg: '', // { zhCN: '视频选择失败。请稍后再试,或向客服投诉。', enUS: 'Video choose failed. Please try again later, or report to customer service.' },
}
}
fileSize = size
@ -367,7 +367,7 @@ module.exports = {
if (errorChoose) {
return {
_state: 'CER_FAIL_CHOOSE',
_msg: { zhCN: '文件选择失败。请稍后再试,或向客服投诉。', enUS: 'File choose failed. Please try again later, or report to customer service.' },
_msg: '', // { zhCN: '文件选择失败。请稍后再试,或向客服投诉。', enUS: 'File choose failed. Please try again later, or report to customer service.' },
}
}
fileSize = tempFiles?.[0]?.size
@ -379,18 +379,20 @@ module.exports = {
// #endif
}
const fileName = filePicked?.name || filePath?.split?.('/')?.pop?.() // filePicked.name is available in WEB only. on the other hand, filePath 在 WEB 上并不是文件路径名,而是类似 "blob:http://localhost:8080/f0d3e54d-0694-4803-8097-641d76a10b0d“。在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名。
if (!fileSize) {
return { _state: 'CER_EMPTY_FILE', _msg: { zhCN: '文件为空,无法上传。', enUS: 'Empty files cannot be uploaded.' } }
return {
_state: 'CER_EMPTY_FILE',
_msg: { zhCN: '文件为空,无法上传:\n' + fileName, enUS: 'Empty files cannot be uploaded:\n' + fileName },
}
} else if (fileSize > (globalThis.wo?.envar?.fileSizeLimit || 10485760)) {
let sizeLimitMB = parseInt((globalThis.wo?.envar?.fileSizeLimit || 10485760) / 1048576) + 'MB'
return {
_state: 'CER_FILE_TOO_LARGE',
_msg: { zhCN: `文件大于 ${sizeLimitMB} MB,无法上传`, enUS: `The file exceeds ${sizeLimitMB} MB and cannot be uploaded` },
_msg: { zhCN: `文件大于 ${sizeLimitMB},无法上传`, enUS: `The file exceeds ${sizeLimitMB} and cannot be uploaded` },
}
}
const fileName = filePicked?.name || filePath?.split?.('/')?.pop?.() // filePicked.name is available in WEB only. on the other hand, filePath 在 WEB 上并不是文件路径名,而是类似 "blob:http://localhost:8080/f0d3e54d-0694-4803-8097-641d76a10b0d“。在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名。
if (
// #ifndef APP
// 20240830 luk: 在 App 上,就相信 iOS/Android不检查文件后缀名。
@ -404,8 +406,8 @@ module.exports = {
if (!filePath) {
return {
_state: 'BER_FAIL_CHOOSE_FILE',
_msg: { zhCN: '文件选择失败。请稍后再试,或向客服投诉。', enUS: 'File choose failed. Please try again later, or report to customer service.' },
_state: 'CER_FAIL_CHOOSE',
_msg: '', // { zhCN: '文件选择失败。请稍后再试,或向客服投诉。', enUS: 'File choose failed. Please try again later, or report to customer service.' },
}
}
@ -416,7 +418,7 @@ module.exports = {
// 在 Fileloader/fileloader.js 里,已经不再依赖 _passtoken而且 header 在被 nginx 或 cloudflare (没搞清楚是谁干的)代理之后也被过滤掉了,因此不再使用这一句: header._passtoken = uni.getStorageSync('_passtoken')
formData['_passtoken'] = uni.getStorageSync('_passtoken') // 20230527 加回这一句,让后台可以根据验证用户来决定怎样处理文件。
this.showLoading()
this.showLoading({ title: { zhCN: '上传中...', enUS: 'Uploading...' } })
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? }
@ -434,7 +436,7 @@ module.exports = {
} catch (exp) {
return {
_state: 'BER_FAIL_RESPONSE_JSON_MALFORMED',
_msg: { zhCN: '文件上传失败。请稍后再试,或向客服投诉。', enUS: 'File upload failed. Please try again later, or report to customer service.' },
_msg: { zhCN: '文件上传失败。请稍后再试,或向客服报告。', enUS: 'File upload failed. Please try again, or report to customer service.' },
}
}
}
@ -445,7 +447,7 @@ module.exports = {
} else {
return {
_state: 'BER_FAIL_UPLOAD_FILE',
_msg: { zhCN: '文件上传失败。请稍后再试,或向客服投诉。', enUS: 'File upload failed. Please try again later, or report to customer service.' },
_msg: { zhCN: '文件上传失败。请稍后再试,或向客服报告。', enUS: 'File upload failed. Please try again, or report to customer service.' },
error: errorUpload,
}
}
@ -466,7 +468,7 @@ module.exports = {
let filePath, cloudPath, fileSize, filePicked
let random = crypto.randomBytes(16).toString('hex')
if (fileDragged?.size) {
if ('undefined' !== typeof fileDragged?.size) {
fileSize = fileDragged.size
filePath = fileDragged.filePath
filePicked = fileDragged
@ -476,7 +478,7 @@ module.exports = {
if (errorChoose) {
return {
_state: 'CER_FAIL_CHOOSE',
_msg: { zhCN: '图像选择失败。请稍后再试,或向客服投诉。', enUS: 'Image choose failed. Please try again later, or report to customer service.' },
_msg: '', // { zhCN: '图像选择失败。请稍后再试,或向客服投诉。', enUS: 'Image choose failed. Please try again later, or report to customer service.' },
}
}
fileSize = tempFiles?.[0]?.size
@ -495,7 +497,7 @@ module.exports = {
if (errorChoose) {
return {
_state: 'CER_FAIL_CHOOSE',
_msg: { zhCN: '视频选择失败。请稍后再试,或向客服投诉。', enUS: 'Video choose failed. Please try again later, or report to customer service.' },
_msg: '', // { zhCN: '视频选择失败。请稍后再试,或向客服投诉。', enUS: 'Video choose failed. Please try again later, or report to customer service.' },
}
}
fileSize = size
@ -523,7 +525,7 @@ module.exports = {
if (errMsg !== 'chooseAndUploadFile:ok') {
return {
_state: 'CER_FAIL_CHOOSE',
_msg: { zhCN: '文件选择失败。请稍后再试,或向客服投诉。', enUS: 'File choose failed. Please try again later, or report to customer service.' },
_msg: '', // { zhCN: '文件选择失败。请稍后再试,或向客服投诉。', enUS: 'File choose failed. Please try again later, or report to customer service.' },
}
}
fileSize = tempFiles?.[0]?.size
@ -536,18 +538,18 @@ module.exports = {
// #endif
}
const fileName = filePicked?.name || filePath?.split?.('/')?.pop?.() // filePicked.name is available in WEB only. on the other hand, filePath 在 WEB 上并不是文件路径名,而是类似 "blob:http://localhost:8080/f0d3e54d-0694-4803-8097-641d76a10b0d“。在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名。
if (!fileSize) {
return { _state: 'CER_EMPTY_FILE', _msg: { zhCN: '文件为空,无法上传。', enUS: 'Empty files cannot be uploaded.' } }
return { _state: 'CER_EMPTY_FILE', _msg: { zhCN: '文件为空,无法上传\n' + fileName, enUS: 'Empty files cannot be uploaded:\n' + fileName } }
} else if (fileSize > (globalThis.wo?.envar?.fileSizeLimit || 10485760)) {
let sizeLimitMB = parseInt((globalThis.wo?.envar?.fileSizeLimit || 10485760) / 1048576) + 'MB'
return {
_state: 'CER_FILE_TOO_LARGE',
_msg: { zhCN: `文件大于 ${sizeLimitMB} MB,无法上传`, enUS: `The file exceeds ${sizeLimitMB} MB and cannot be uploaded` },
_msg: { zhCN: `文件大于 ${sizeLimitMB},无法上传`, enUS: `The file exceeds ${sizeLimitMB} and cannot be uploaded` },
}
}
const fileName = filePicked?.name || filePath?.split?.('/')?.pop?.() // filePicked.name is available in WEB only. on the other hand, filePath 在 WEB 上并不是文件路径名,而是类似 "blob:http://localhost:8080/f0d3e54d-0694-4803-8097-641d76a10b0d“。在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名。
if (
// #ifndef APP
// 20240830 luk: 在 App 上,就相信 iOS/Android不检查文件后缀名。
@ -567,11 +569,11 @@ module.exports = {
if (!filePath) {
return {
_state: 'BER_FAIL_CHOOSE_FILE',
_msg: { zhCN: '文件上传失败。请稍后再试,或向客服投诉。', enUS: 'File upload failed. Please try again later, or report to customer service.' },
_msg: { zhCN: '文件上传失败。请稍后再试,或向客服报告。', enUS: 'File upload failed. Please try again, or report to customer service.' },
}
}
this.showLoading()
this.showLoading({ title: { zhCN: '上传中...', enUS: 'Uploading...' } })
const { fileID, requestId } = await uniCloud.uploadFile({
filePath: filePath,
cloudPath: (process.env.NODE_ENV !== 'production' ? 'dev_' : '') + cloudPath, // 关键是要具有文件格式后缀名,这样可以保持阿里云下载链接也用这个后缀名。
@ -587,7 +589,7 @@ module.exports = {
} else {
return {
_state: 'BER_FAIL_UPLOAD_FILE',
_msg: { zhCN: '文件上传失败。请稍后再试,或向客服投诉。', enUS: 'File upload failed. Please try again later, or report to customer service.' },
_msg: { zhCN: '文件上传失败。请稍后再试,或向客服报告。', enUS: 'File upload failed. Please try again, or report to customer service.' },
}
}
},
@ -615,7 +617,7 @@ module.exports = {
},
open_url ({ url, title, inWebview } = {}) {
url = this.localizeText?.(url) || url
url = this.localizeText?.(url)
if (!url) {
return
}
@ -690,9 +692,22 @@ module.exports = {
callback // 发生在 toast 之后
})
*/
showToast ({ tool, type = 'success', image, title, duration = 2000, wo = globalThis.wo, ...rest } = {}) {
showToast ({ type = 'success', image, title, duration = 2000, wo = globalThis.wo, ...rest } = {}) {
title = this.localizeText(title)
if (tool === 'uni') {
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
if (mypopup) {
wo.ss.popMessage = title
wo.ss.popType = type // success/error/warning/info
wo.ss.popDuration = duration
mypopup.open()
} else if (mytoast) {
// 来自 <ucToast> 或 <u-toast> 或 <u-top-tips> // rename to popToast?
mytoast?.show?.({ type, title, duration, ...rest })
} else {
// #ifdef APP
uni.showToast({ icon: 'none', title, duration, ...rest })
// plus.nativeUI.toast( title, { align: center/left/right, verticalAlign: bottom/center/top, duration:long/short, icon, iconWidth, iconHeight, style: block/inline, type:text/richtext })
@ -700,18 +715,6 @@ module.exports = {
// #ifndef APP
uni.showToast({ icon: 'none', image, title, duration, ...rest })
// #endif
return
}
// 来自 <ucToast> 或 <u-toast> 或 <u-top-tips> // rename to popToast?
const mypopup = getCurrentPages()?.pop()?.mypopup || getCurrentPages()?.pop()?.$refs?.mypopup || getApp().globalData?.mypopup || wo?.mypopup
if (mypopup) {
wo.ss.popMessage = title
wo.ss.popType = type // success/error/warning/info
wo.ss.popDuration = duration
mypopup.open()
} else {
const mytoast = getCurrentPages()?.pop()?.mytoast || getCurrentPages()?.pop()?.$refs?.mytoast || getApp().globalData?.mytoast || wo?.mytoast
mytoast?.show?.({ type, title, duration, ...rest })
}
},