This commit is contained in:
陆柯 2023-12-30 13:21:50 +08:00
parent 2cc8f66651
commit c71e514af2

View File

@ -9,6 +9,9 @@ const my = {
}, },
} }
const crypto = require('crypto')
const path = require('path')
export default { export default {
// 用直观的色彩,代替语义化的类型: // 用直观的色彩,代替语义化的类型:
// uView 中大量组件都使用 type 来设置颜色,而 uni-ui 中只有少量组件用到。 // uView 中大量组件都使用 type 来设置颜色,而 uni-ui 中只有少量组件用到。
@ -56,19 +59,21 @@ export default {
setBarTitles ({ windowTitle, pageTitle, wo = globalThis.wo } = {}) { setBarTitles ({ windowTitle, pageTitle, wo = globalThis.wo } = {}) {
const langNow = my.langNow() const langNow = my.langNow()
const pageNow = getCurrentPages()?.pop?.() const pageNow = getCurrentPages()?.pop?.()
// 在ios/android app里pageNow.route 是正确的,但是 pageNow.xxx 等自定义属性 都 undefined必须 pageNow.$vm.$data.xxx 才可以。
// 注意,$vm.$data 不包括 computed 属性,而 pageNow 里包括。因此不能把 i18nPageTitle 放在 computed 里。
const navibarTitle = const navibarTitle = this.localizeText(
pageTitle || pageTitle ||
pageNow?.$vm?.$data?.i18nPageTitle?.[langNow] || // 页面.vue 的 i18nPageTitle 变量 pageNow?.$vm?.$data?.i18nPageTitle || // 页面.vue 的 i18nPageTitle 变量
pageNow?.$vm?.$data?.i18nText?.[langNow]?.tPageTitle || // 页面.vue 的 i18nText 对象 pageNow?.$vm?.$data?.i18nText?.[langNow]?.tPageTitle || // 页面.vue 的 i18nText 对象
pageNow?.$vm?.$data?.pageTitle || pageNow?.$vm?.$data?.pageTitle ||
wo?.pagesJson?.pages?.find((page) => page.path === pageNow?.route)?.i18nPageTitle?.[langNow] || // pages.json 的页面配置里 wo?.pageSet?.[pageNow?.route?.substring?.(6)]?.i18nPageTitle ||
wo?.pagesJson?.pages?.find((page) => page.path === pageNow?.route)?.i18nPageTitle || // pages.json 的页面配置里
'' ''
// 在app里pageNow.route 是正确的,但是 pageNow.xxx 等自定义属性 都 undefined必须 pageNow.$vm.$data.xxx 才可以。注意,$vm.$data 不包括 computed 属性,而 pageNow 里包括。 )
windowTitle = windowTitle =
windowTitle || windowTitle ||
wo?.metakey?.callnames?.[langNow] ||
wo?.envar?.callnames?.[langNow] || wo?.envar?.callnames?.[langNow] ||
wo?.pagesJson?.appInfo?.i18nText?.[langNow] || wo?.pagesJson?.appInfo?.i18nText?.[langNow] ||
wo?.pagesJson?.globalStyle?.navigationBarTitleText || wo?.pagesJson?.globalStyle?.navigationBarTitleText ||
@ -167,8 +172,9 @@ export default {
const thisRoute = getCurrentPages()?.pop?.()?.route || 'VueApp' // 立刻保存 route因为在调用后台后可能已切换到了其他页面。 const thisRoute = getCurrentPages()?.pop?.()?.route || 'VueApp' // 立刻保存 route因为在调用后台后可能已切换到了其他页面。
const startTime = new Date().toJSON() const startTime = new Date().toJSON()
let url = undefined let url = undefined
// #ifdef H5 apiWhat._clientInfo = globalThis.wo?.envar?.clientInfo
apiWhat._clientInfo = Object.assign(globalThis.wo?.envar?.clientInfo, { url: globalThis.location?.href, lang: globalThis.wo?.ss?.i18n?.mylang }) // #ifdef WEB
Object.assign(apiWhat._clientInfo, { url: globalThis.location?.href, lang: globalThis.wo?.ss?.i18n?.mylang })
// #endif // #endif
apiWhat._passtoken = uni.getStorageSync('_passtoken') apiWhat._passtoken = uni.getStorageSync('_passtoken')
let result = {} let result = {}
@ -235,7 +241,7 @@ export default {
// 注意2虽然预设了 resultServer 和 resultCloud = {},但如果后台返回了 null那么 resultServer/resultCloud 也是 null。 // 注意2虽然预设了 resultServer 和 resultCloud = {},但如果后台返回了 null那么 resultServer/resultCloud 也是 null。
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
console.log( console.log(
`%c(IN) ${startTime} ${thisRoute} :: ${apiTodo}`, `%c ${startTime} (IN) ${thisRoute} :: ${apiTodo}`,
'background: #87cefa; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;', 'background: #87cefa; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;',
{ {
baseType, baseType,
@ -247,7 +253,7 @@ export default {
} }
) )
console.log( console.log(
`%c(OUT) ${new Date().toJSON()} ${thisRoute} :: ${apiTodo}`, `%c ${new Date().toJSON()} (OUT) ${thisRoute} :: ${apiTodo}`,
'background: #4169e1; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;', 'background: #4169e1; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;',
result result
) )
@ -327,29 +333,36 @@ export default {
let filePath, cloudPath let filePath, cloudPath
if (mediaType === 'image') { if (mediaType === 'image') {
let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType }) let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
if (!errorChoose) {
// uni.showModal({ title: 'tempFilePaths[0]=' + tempFilePaths[0] }) // uni.showModal({ title: 'tempFilePaths[0]=' + tempFilePaths[0] })
filePath = tempFilePaths[0] // 在 H5 上并不是文件路径名,而是类似 "blob:http://localhost:8080/f0d3e54d-0694-4803-8097-641d76a10b0d“。 filePath = tempFilePaths?.[0] // 在 H5 上并不是文件路径名,而是类似 "blob:http://localhost:8080/f0d3e54d-0694-4803-8097-641d76a10b0d“。// 在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名
let random = crypto.randomBytes(16).toString('hex')
// #ifndef H5 // #ifndef H5
// let [errorGetImageInfo, { path, width, height, orientation, type }] = await uni.getImageInfo({ src: filePath }) // 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 = 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__${filePath}` // 在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名 cloudPath = `APP_${wo.envar.clientInfo.osName}_${random}${path.extname(filePath || '')}`
// #endif // #endif
// #ifdef H5 // #ifdef H5
cloudPath = `H5__${tempFiles[0].name}` // name is available in H5 only. 只包含文件名和后缀名,不包含路径。 cloudPath = `WEB_${wo.envar.clientInfo.osName}_${random}${path.extname(tempFiles?.[0]?.name || '')}` // name is available in H5 only. 只包含文件名和后缀名,不包含路径。
// #endif // #endif
if (!path.extname(cloudPath)) cloudPath += '.jpg'
}
} else if (mediaType === 'video') { } else if (mediaType === 'video') {
let [errorChoose, { tempFilePath, tempFile, duration, size, width, height, name }] = await uni.chooseVideo({ sourceType, maxDuration }) let [errorChoose, { tempFilePath, tempFile, duration, size, width, height, name }] = await uni.chooseVideo({ sourceType, maxDuration })
if (!errorChoose) {
// uni.showModal({ title: 'tempFilePath=' + tempFilePath }) // 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" 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"
let random = crypto.randomBytes(16).toString('hex')
// #ifndef H5 // #ifndef H5
cloudPath = `APP__dur${duration}__${filePath}` cloudPath = `APP_${wo.envar.clientInfo.osName}_${random}_dur${duration}${path.extname(filePath || '')}`
// #endif // #endif
// #ifdef H5 // #ifdef H5
cloudPath = `H5__dur${duration}__${name}` // tempFile and name are H5 only cloudPath = `WEB_${wo.envar.clientInfo.osName}_${random}_dur${duration}${path.extname(name || '')}` // tempFile and name are H5 only
// #endif // #endif
// iOS 上测试filePath 为 *.MOV而阿里云只允许 *.mp4, 所以添加 .mp4 后缀。参见 https://uniapp.dcloud.net.cn/uniCloud/storage?id=clouduploadfile // iOS 上测试filePath 为 *.MOV而阿里云只允许 *.mp4, 所以默认添加 .mp4 后缀。参见 https://uniapp.dcloud.net.cn/uniCloud/storage?id=clouduploadfile
// 20200915测试阿里云支持上传 *.mov 了。 // 20200915测试阿里云支持上传 *.mov 了。
if (!/\.(mp4|mov)$/i.test(cloudPath)) cloudPath = cloudPath + '.mp4' if (!path.extname(cloudPath)) cloudPath += '.mp4'
}
} else { } else {
let { errMsg, tempFilePaths, tempFiles } = await uniCloud.chooseAndUploadFile({ let { errMsg, tempFilePaths, tempFiles } = await uniCloud.chooseAndUploadFile({
type: 'all', // valid for H5 only: https://uniapp.dcloud.net.cn/uniCloud/storage.html#uploadfile type: 'all', // valid for H5 only: https://uniapp.dcloud.net.cn/uniCloud/storage.html#uploadfile
@ -369,7 +382,7 @@ export default {
this.showLoading() this.showLoading()
const { fileID, requestId } = await uniCloud.uploadFile({ const { fileID, requestId } = await uniCloud.uploadFile({
filePath: filePath, filePath: filePath,
cloudPath: process.env.NODE_ENV !== 'production' ? 'dev_' + cloudPath : cloudPath, // 关键是要具有文件格式后缀名,这样可以保持阿里云下载链接也用这个后缀名。 cloudPath: (process.env.NODE_ENV !== 'production' ? 'dev_' : '') + cloudPath, // 关键是要具有文件格式后缀名,这样可以保持阿里云下载链接也用这个后缀名。
//fileType: mediaType, // = image, video, audio. Looks like only necessary for for 支付宝小程序: https://uniapp.dcloud.net.cn/uniCloud/storage.html#uploadfile //fileType: mediaType, // = image, video, audio. Looks like only necessary for for 支付宝小程序: https://uniapp.dcloud.net.cn/uniCloud/storage.html#uploadfile
onUploadProgress: function (progressEvent) { onUploadProgress: function (progressEvent) {
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total) var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
@ -514,15 +527,22 @@ export default {
}, },
formatDate (date, format) { formatDate (date, format) {
if (!(date instanceof Date)) { if (typeof date === 'string' && date) { // new Date('') ==> Invalid Date
if (typeof date === 'string' && /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d$/.test(date)) { if (/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d$/.test(date)) {
// 这是从 typeorm 数据库得到的Date类型的值 // 这是从 typeorm 数据库得到的Date类型的值
date = date.replace(/-/g, '/') // safari 不支持 yyyy-mm-dd必须改成 yyyy/mm/dd date = date.replace(/-/g, '/') // safari 不支持 yyyy-mm-dd必须改成 yyyy/mm/dd
} else if (/^\d{13}$/.test(date)) { // new Date('1702179364450') ==> Invalid Date所以要先转成纯数字
date = parseInt(date)
} else if (/^\d{10}$/.test(date)) {
date = parseInt(date) * 1000
} }
date = new Date(date) date = new Date(date)
} else if (typeof date === 'number') {
date = new Date(date)
} }
if (!date.toJSON()) {
date = new Date() if (!(date instanceof Date) || !date.toJSON()) {
return ''
} }
format = format && typeof format === 'string' ? format : 'yyyy-mm-dd HH:MM:SS' format = format && typeof format === 'string' ? format : 'yyyy-mm-dd HH:MM:SS'