提交 50dc7df0 编写于 作者: M mehaotian

feat(app): add saveFile getSavedFileList getSavedFileInfo removeSavedFile getFileInfo

上级 50291234
......@@ -36,7 +36,11 @@ export * from './protocols/device/soterAuthentication'
export * from './protocols/storage/storage'
export * from './protocols/file/saveFile'
export * from './protocols/file/getSavedFileList'
export * from './protocols/file/removeSavedFile'
export * from './protocols/file/getFileInfo'
export * from './protocols/file/getSavedFileInfo'
export * from './protocols/file/openDocument'
export * from './protocols/keyboard/keyboard'
......
import { getRealPath } from '@dcloudio/uni-platform'
export const API_GET_SAVED_FILE_INFO = 'getSavedFileInfo'
export type API_TYPE_GET_SAVED_FILE_INFO = typeof uni.getSavedFileInfo
export const GetSavedFileInfoOptions: ApiOptions<API_TYPE_GET_SAVED_FILE_INFO> =
{
formatArgs: {
filePath(filePath, params) {
params.filePath = getRealPath(filePath)
},
},
}
export const GetSavedFileInfoProtocol: ApiProtocol<API_TYPE_GET_SAVED_FILE_INFO> =
{
filePath: {
type: String,
required: true,
},
}
export const API_GET_SAVED_LIST = 'getSavedFileList'
export type API_TYPE_GET_SAVED_LIST = typeof uni.getSavedFileList
import { getRealPath } from '@dcloudio/uni-platform'
export const API_REMOVE_SAVED_FILE = 'removeSavedFile'
export type API_TYPE_REMOVE_SAVED_FILE = typeof uni.removeSavedFile
export const RemoveSavedFileOptions: ApiOptions<API_TYPE_REMOVE_SAVED_FILE> = {
formatArgs: {
filePath(filePath, params) {
params.filePath = getRealPath(filePath)
},
},
}
export const RemoveSavedFileProtocol: ApiProtocol<API_TYPE_REMOVE_SAVED_FILE> =
{
filePath: {
type: String,
required: true,
},
}
import { getRealPath } from '@dcloudio/uni-platform'
export const API_SAVE_FILE = 'saveFile'
export type API_TYPE_SAVE_FILE = typeof uni.saveFile
export const SaveFileOptions: ApiOptions<API_TYPE_SAVE_FILE> = {
formatArgs: {
tempFilePath(savedFilePath, params) {
params.tempFilePath = getRealPath(savedFilePath)
},
},
}
export const SaveFileProtocol: ApiProtocol<API_TYPE_SAVE_FILE> = {
tempFilePath: {
type: String,
required: true,
},
}
import {
defineAsyncApi,
API_GET_SAVED_FILE_INFO,
API_TYPE_GET_SAVED_FILE_INFO,
GetSavedFileInfoProtocol,
GetSavedFileInfoOptions,
} from '@dcloudio/uni-api'
import { warpPlusErrorCallback } from '../../../helpers/plus'
export const getSavedFileInfo = <API_TYPE_GET_SAVED_FILE_INFO>defineAsyncApi(
API_GET_SAVED_FILE_INFO,
({ filePath }, { resolve, reject }) => {
const errorCallback = warpPlusErrorCallback(reject)
plus.io.resolveLocalFileSystemURL(
filePath,
(entry) => {
entry.getMetadata(
(meta) => {
resolve({
createTime: meta.modificationTime!.getTime(),
size: meta.size!,
})
},
errorCallback,
false
)
},
errorCallback
)
},
GetSavedFileInfoProtocol,
GetSavedFileInfoOptions
)
import {
defineAsyncApi,
API_GET_SAVED_LIST,
API_TYPE_GET_SAVED_LIST,
} from '@dcloudio/uni-api'
import { warpPlusErrorCallback } from '../../../helpers/plus'
const SAVED_DIR = 'uniapp_save'
function getSavedFileDir(
success: (res: any) => void,
fail: (err: any) => void
) {
fail = fail || function () {}
plus.io.requestFileSystem(
plus.io.PRIVATE_DOC,
(fs) => {
// 请求_doc fs
fs.root!.getDirectory(
SAVED_DIR,
{
// 获取文件保存目录对象
create: true,
},
success,
fail
)
},
fail
)
}
export const getSavedFileList = <API_TYPE_GET_SAVED_LIST>(
defineAsyncApi(API_GET_SAVED_LIST, (_, { resolve, reject }) => {
const errorCallback = warpPlusErrorCallback(reject)
getSavedFileDir((entry) => {
var reader = entry.createReader()
var fileList: object[] = []
reader.readEntries((entries: any[]) => {
if (entries && entries.length) {
entries.forEach((entry) => {
entry.getMetadata(
(meta: {
modificationTime: { getTime: () => void }
size: any
}) => {
fileList.push({
filePath: plus.io.convertAbsoluteFileSystem(entry.fullPath),
createTime: meta.modificationTime.getTime(),
size: meta.size,
})
if (fileList.length === entries.length) {
resolve({
fileList,
})
}
},
errorCallback,
false
)
})
} else {
resolve({
fileList,
})
}
}, errorCallback)
}, errorCallback)
})
)
import {
defineAsyncApi,
API_REMOVE_SAVED_FILE,
API_TYPE_REMOVE_SAVED_FILE,
RemoveSavedFileProtocol,
RemoveSavedFileOptions,
} from '@dcloudio/uni-api'
import { warpPlusErrorCallback } from '../../../helpers/plus'
export const removeSavedFile = <API_TYPE_REMOVE_SAVED_FILE>defineAsyncApi(
API_REMOVE_SAVED_FILE,
({ filePath }, { resolve, reject }) => {
const errorCallback = warpPlusErrorCallback(reject)
plus.io.resolveLocalFileSystemURL(
filePath,
(entry) => {
entry.remove(() => {
resolve()
}, errorCallback)
},
errorCallback
)
},
RemoveSavedFileProtocol,
RemoveSavedFileOptions
)
import {
defineAsyncApi,
API_SAVE_FILE,
API_TYPE_SAVE_FILE,
SaveFileProtocol,
SaveFileOptions,
} from '@dcloudio/uni-api'
import { warpPlusErrorCallback } from '../../../helpers/plus'
import { getExtName } from '../../../helpers/file'
let index = 0
const SAVED_DIR = 'uniapp_save'
const SAVE_PATH = `_doc/${SAVED_DIR}`
function getSavedFileDir(
success: (res: any) => void,
fail: (err: any) => void
) {
fail = fail || function () {}
plus.io.requestFileSystem(
plus.io.PRIVATE_DOC,
(fs) => {
// 请求_doc fs
fs.root!.getDirectory(
SAVED_DIR,
{
// 获取文件保存目录对象
create: true,
},
success,
fail
)
},
fail
)
}
export const saveFile = <API_TYPE_SAVE_FILE>defineAsyncApi(
API_SAVE_FILE,
({ tempFilePath }, { resolve, reject }) => {
const errorCallback = warpPlusErrorCallback(reject)
const fileName = `${Date.now()}${index++}${getExtName(tempFilePath)}`
plus.io.resolveLocalFileSystemURL(
tempFilePath,
(entry) => {
// 读取临时文件 FileEntry
getSavedFileDir((dir) => {
entry.copyTo(
dir,
fileName,
() => {
// 复制临时文件 FileEntry,为了避免把相册里的文件删除,使用 copy,微信中是要删除临时文件的
const savedFilePath = SAVE_PATH + '/' + fileName
resolve({
savedFilePath,
})
},
errorCallback
)
}, errorCallback)
},
errorCallback
)
},
SaveFileProtocol,
SaveFileOptions
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册