From 8151d2f6cac7d3273223541e5e4ee4cb19b184a1 Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 17 Sep 2020 21:04:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=20App=20=E7=AB=AF?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3=E9=94=99=E8=AF=AF=E5=9B=9E?= =?UTF-8?q?=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/helpers/protocol/file/file.js | 50 ++++++ .../app-plus/service/api/file/file.js | 146 +++++------------- .../service/api/media/choose-video.js | 11 +- src/platforms/app-plus/service/api/util.js | 17 +- 4 files changed, 104 insertions(+), 120 deletions(-) create mode 100644 src/core/helpers/protocol/file/file.js diff --git a/src/core/helpers/protocol/file/file.js b/src/core/helpers/protocol/file/file.js new file mode 100644 index 000000000..f198b763d --- /dev/null +++ b/src/core/helpers/protocol/file/file.js @@ -0,0 +1,50 @@ +import getRealPath from 'uni-platform/helpers/get-real-path' + +export const saveFile = { + tempFilePath: { + type: String, + required: true, + validator (value, params) { + params.tempFilePath = getRealPath(value) + } + } +} + +const TYPES = ['md5', 'sha1'] + +export const getFileInfo = { + filePath: { + type: String, + required: true, + validator (value, params) { + params.filePath = getRealPath(value) + } + }, + digestAlgorithm: { + type: String, + validator (value, params) { + params.digestAlgorithm = TYPES.includes(value) ? value : TYPES[0] + }, + default: TYPES[0] + } +} + +export const getSavedFileInfo = { + filePath: { + type: String, + required: true, + validator (value, params) { + params.filePath = getRealPath(value) + } + } +} + +export const removeSavedFile = { + filePath: { + type: String, + required: true, + validator (value, params) { + params.filePath = getRealPath(value) + } + } +} diff --git a/src/platforms/app-plus/service/api/file/file.js b/src/platforms/app-plus/service/api/file/file.js index dd7dbb57d..5f8456049 100644 --- a/src/platforms/app-plus/service/api/file/file.js +++ b/src/platforms/app-plus/service/api/file/file.js @@ -1,5 +1,6 @@ import { - getRealPath + warpPlusMethod, + warpPlusErrorCallback } from '../util' import { @@ -8,67 +9,44 @@ import { const SAVED_DIR = 'uniapp_save' const SAVE_PATH = `_doc/${SAVED_DIR}` -const REGEX_FILENAME = /^.*[/]/ function getSavedFileDir (success, fail) { - fail = fail || function () {} + fail = fail || function () { } plus.io.requestFileSystem(plus.io.PRIVATE_DOC, fs => { // 请求_doc fs fs.root.getDirectory(SAVED_DIR, { // 获取文件保存目录对象 create: true - }, dir => { - success(dir) - }, err => { - fail('目录[' + SAVED_DIR + ']创建失败' + err.message) - }) - }, err => { - fail('目录[_doc]读取失败' + err.message) - }) + }, success, fail) + }, fail) +} + +function getFileName (path) { + const array = path.split('/') + return array[array.length - 1] } export function saveFile ({ tempFilePath } = {}, callbackId) { - let fileName = tempFilePath.replace(REGEX_FILENAME, '') - if (fileName) { - let extName = '' - if (~fileName.indexOf('.')) { - extName = '.' + fileName.split('.').pop() - } + const errorCallback = warpPlusErrorCallback(callbackId, 'saveFile') + let fileName = getFileName(tempFilePath) + fileName = `${Date.now()}_${fileName}` - fileName = (+new Date()) + '' + extName - - plus.io.resolveLocalFileSystemURL(getRealPath(tempFilePath), entry => { // 读取临时文件 FileEntry - getSavedFileDir(dir => { - entry.copyTo(dir, fileName, () => { // 复制临时文件 FileEntry,为了避免把相册里的文件删除,使用 copy,微信中是要删除临时文件的 - const savedFilePath = SAVE_PATH + '/' + fileName - invoke(callbackId, { - errMsg: 'saveFile:ok', - savedFilePath - }) - }, err => { - invoke(callbackId, { - errMsg: 'saveFile:fail 保存文件[' + tempFilePath + - '] copyTo 失败:' + err.message - }) - }) - }, message => { + plus.io.resolveLocalFileSystemURL(tempFilePath, entry => { // 读取临时文件 FileEntry + getSavedFileDir(dir => { + entry.copyTo(dir, fileName, () => { // 复制临时文件 FileEntry,为了避免把相册里的文件删除,使用 copy,微信中是要删除临时文件的 + const savedFilePath = SAVE_PATH + '/' + fileName invoke(callbackId, { - errMsg: 'saveFile:fail ' + message + errMsg: 'saveFile:ok', + savedFilePath }) - }) - }, err => { - invoke(callbackId, { - errMsg: 'saveFile:fail 文件[' + tempFilePath + ']读取失败' + err.message - }) - }) - } else { - return { - errMsg: 'saveFile:fail 文件名[' + tempFilePath + ']不存在' - } - } + }, errorCallback) + }, errorCallback) + }, errorCallback) } export function getSavedFileList (options, callbackId) { + const errorCallback = warpPlusErrorCallback(callbackId, 'getSavedFileList') + getSavedFileDir(entry => { var reader = entry.createReader() @@ -88,11 +66,7 @@ export function getSavedFileList (options, callbackId) { fileList }) } - }, error => { - invoke(callbackId, { - errMsg: 'getSavedFileList:fail ' + error.message - }) - }, false) + }, errorCallback, false) }) } else { invoke(callbackId, { @@ -100,82 +74,38 @@ export function getSavedFileList (options, callbackId) { fileList }) } - }, error => { - invoke(callbackId, { - errMsg: 'getSavedFileList:fail ' + error.message - }) - }) - }, message => { - invoke(callbackId, { - errMsg: 'getSavedFileList:fail ' + message - }) - }) + }, errorCallback) + }, errorCallback) } -export function getFileInfo ({ - filePath, - digestAlgorithm = 'md5' -} = {}, callbackId) { - // TODO 计算文件摘要 - plus.io.resolveLocalFileSystemURL(getRealPath(filePath), entry => { - entry.getMetadata(meta => { - invoke(callbackId, { - errMsg: 'getFileInfo:ok', - size: meta.size, - digestAlgorithm: '' - }) - }, err => { - invoke(callbackId, { - errMsg: 'getFileInfo:fail 文件[' + - filePath + - '] getMetadata 失败:' + err.message - }) - }) - }, err => { - invoke(callbackId, { - errMsg: 'getFileInfo:fail 文件[' + filePath + ']读取失败:' + err.message - }) - }) -} +export const getFileInfo = warpPlusMethod(plus.io, 'getFileInfo') export function getSavedFileInfo ({ filePath } = {}, callbackId) { - plus.io.resolveLocalFileSystemURL(getRealPath(filePath), entry => { + const errorCallback = warpPlusErrorCallback(callbackId, 'getSavedFileInfo') + + plus.io.resolveLocalFileSystemURL(filePath, entry => { entry.getMetadata(meta => { invoke(callbackId, { createTime: meta.modificationTime.getTime(), size: meta.size, errMsg: 'getSavedFileInfo:ok' }) - }, error => { - invoke(callbackId, { - errMsg: 'getSavedFileInfo:fail ' + error.message - }) - }, false) - }, () => { - invoke(callbackId, { - errMsg: 'getSavedFileInfo:fail file not find' - }) - }) + }, errorCallback, false) + }, errorCallback) } export function removeSavedFile ({ filePath } = {}, callbackId) { - plus.io.resolveLocalFileSystemURL(getRealPath(filePath), entry => { + const errorCallback = warpPlusErrorCallback(callbackId, 'removeSavedFile') + + plus.io.resolveLocalFileSystemURL(filePath, entry => { entry.remove(() => { invoke(callbackId, { errMsg: 'removeSavedFile:ok' }) - }, err => { - invoke(callbackId, { - errMsg: 'removeSavedFile:fail 文件[' + filePath + ']删除失败:' + err.message - }) - }) - }, () => { - invoke(callbackId, { - errMsg: 'removeSavedFile:fail file not find' - }) - }) + }, errorCallback) + }, errorCallback) } diff --git a/src/platforms/app-plus/service/api/media/choose-video.js b/src/platforms/app-plus/service/api/media/choose-video.js index 8e3f0f766..b001ba62f 100644 --- a/src/platforms/app-plus/service/api/media/choose-video.js +++ b/src/platforms/app-plus/service/api/media/choose-video.js @@ -6,17 +6,16 @@ import { invoke } from '../../bridge' +import { + warpPlusErrorCallback +} from '../util' + export function chooseVideo ({ sourceType = ['album', 'camera'], maxDuration = 60, camera = 'back' } = {}, callbackId) { - function errorCallback (error) { - error = error || {} - invoke(callbackId, { - errMsg: `chooseVideo:fail ${error.message || 'cancel'}` - }) - } + const errorCallback = warpPlusErrorCallback(callbackId, 'chooseVideo', 'cancel') function successCallback (tempFilePath = '') { plus.io.getVideoInfo({ diff --git a/src/platforms/app-plus/service/api/util.js b/src/platforms/app-plus/service/api/util.js index 4b3f0561b..94591ab39 100644 --- a/src/platforms/app-plus/service/api/util.js +++ b/src/platforms/app-plus/service/api/util.js @@ -179,6 +179,16 @@ export function warpPlusEvent (origin, name) { } } +export function warpPlusErrorCallback (callbackId, neme, errMsg) { + return function errorCallback (error) { + error = error || {} + invoke(callbackId, { + errMsg: `${neme}:fail ${error.message || errMsg || ''}`, + errCode: error.code || 0 + }) + } +} + export function warpPlusMethod (origin, name, before) { return function (options, callbackId) { if (typeof before === 'function') { @@ -192,12 +202,7 @@ export function warpPlusMethod (origin, name, before) { errMsg: `${name}:ok` })) }, - fail (error = {}) { - invoke(callbackId, { - errMsg: `${name}:fail ${error.message || ''}`, - errCode: error.code || 0 - }) - } + fail: warpPlusErrorCallback(callbackId, name) })) } } -- GitLab