diff --git a/tool_core.js b/tool_core.js index 96ff142..f8061de 100644 --- a/tool_core.js +++ b/tool_core.js @@ -332,7 +332,11 @@ module.exports = { // 返回新的数组 filter_story (story) { if (Array.isArray(story) && story.length) { - return story.filter((section) => Object.values(section || {}).some((val) => !this.is_empty(val))) // (section.text || section.image || section.video)?.trim?.() + // section can contain property '_xxx' which shall not change filter result. + return story.filter((section) => { + if (!section) return false + return Object.entries(section).some(([key, val]) => !key.startsWith('_') && !this.is_empty(val)) + }) } else { return [] } @@ -354,7 +358,7 @@ module.exports = { summarize_story (story = []) { // story is an array of objects, each object could either be {text:'some string'}, {image: url} or {video:url}. Please construct a summary object as result: { textLength, imageCount, VideoCount } return story.reduce( - (summary, { text, image, video, file, linkTarget } = {}) => { + (summary, { text, image, video, audio, file, linkTarget } = {}) => { if (text) { summary.textLength += text.length summary.wordCount += text.split(/\s+/).length @@ -362,6 +366,8 @@ module.exports = { summary.imageCount++ } else if (video) { summary.videoCount++ + } else if (audio) { + summary.audioCount++ } else if (file) { summary.fileCount++ } else if (linkTarget) { @@ -371,7 +377,7 @@ module.exports = { } return summary }, - { textLength: 0, wordCount: 0, imageCount: 0, videoCount: 0, fileCount: 0, linkCount: 0, untypeCount: 0 } + { textLength: 0, wordCount: 0, imageCount: 0, videoCount: 0, audioCount: 0, fileCount: 0, linkCount: 0, untypeCount: 0 } ) }, diff --git a/tool_uniapp.js b/tool_uniapp.js index 8a02484..c23f47b 100644 --- a/tool_uniapp.js +++ b/tool_uniapp.js @@ -39,15 +39,19 @@ module.exports = { is_text_file (fileName = '') { const ext = /\./.test(fileName) ? fileName.split('.').pop().toLowerCase() : '' - return (wo.envar.textExtensionList || ['txt', 'text']).includes(ext) + return wo.envar.textExtensionList?.includes?.(ext) }, is_image_file (fileName = '') { const ext = /\./.test(fileName) ? fileName.split('.').pop().toLowerCase() : '' - return (wo.envar.imageExtensionList || ['jpg', 'jpeg', 'png', 'gif', 'webp', 'image']).includes(ext) + return wo.envar.imageExtensionList?.includes?.(ext) }, is_video_file (fileName = '') { const ext = /\./.test(fileName) ? fileName.split('.').pop().toLowerCase() : '' - return (wo.envar.videoExtensionList || ['avi', 'mp4', 'mov', 'wmv', 'video']).includes(ext) + return wo.envar.videoExtensionList?.includes?.(ext) + }, + is_audio_file (fileName = '') { + const ext = /\./.test(fileName) ? fileName.split('.').pop().toLowerCase() : '' + return wo.envar.audioExtensionList?.includes?.(ext) }, thisPage () { @@ -343,7 +347,13 @@ module.exports = { filePath = fileDragged.filePath filePicked = fileDragged if (!mediaType) { - mediaType = this.is_image_file(fileDragged.name) ? 'image' : this.is_video_file(fileDragged.name) ? 'video' : 'file' + mediaType = this.is_image_file(fileDragged.name) + ? 'image' + : this.is_video_file(fileDragged.name) + ? 'video' + : this.is_audio_file(fileDragged.name) + ? 'audio' + : 'file' } } else if (mediaType === 'image') { let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType }) @@ -367,6 +377,27 @@ module.exports = { fileSize = size filePicked = tempFile filePath = tempFilePath + } else if (mediaType === 'audio') { + // #ifdef WEB + // https://uniapp.dcloud.net.cn/api/media/file.html + let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseFile({ + count, + extension: wo.envar.audioExtensionList, + type: undefined, + }) // 20240429 但是测试下来 extension 参数无效 + if (errorChoose) { + return { + _state: 'CER_FAIL_CHOOSE', + _msg: '', // { zhCN: '文件选择失败。请稍后再试,或向客服投诉。', enUS: 'File choose failed. Please try again later, or report to customer service.' }, + } + } + fileSize = tempFiles?.[0]?.size + filePicked = tempFiles?.[0] + filePath = tempFilePaths?.[0] + // #endif + // #ifndef WEB + return { _state: 'UNSUPPORTED_FILETYPE', _msg: { zhCN: '请切换到网页端上传文件!', enUS: 'Please switch to WebApp to upload files.' } } + // #endif } else { // #ifdef WEB // https://uniapp.dcloud.net.cn/api/media/file.html