type FileEncodeing =
| "utf8"
| "ascii"
| "base64"
| "binary"
| "hex"
| "ucs2"
| "ucs-2"
| "utf16le"
| "utf-16le"
| "utf-8"
| "latin1"
| undefined;
type OpenFileFlag =
| "a+"
| "a"
| "ax"
| "ax+"
| "as"
| "as+"
| "r"
| "r+"
| "w"
| "wx"
| "w+"
| "wx+"
| undefined;
type FileType =
| "doc"
| "docx"
| "xls"
| "xlsx"
| "ppt"
| "pptx"
| "pdf"
| undefined;
const fileSystemManager = wx.getFileSystemManager();
export default {
saveTempFile(tempFilePath: string) {
return new Promise((resolve, reject) => {
fileSystemManager.saveFile({
tempFilePath: tempFilePath,
success(res) {
resolve(res);
},
fail(err) {
reject(err);
},
});
});
},
removeSavedFile(filePath: string) {
return new Promise((resolve, reject) => {
fileSystemManager.removeSavedFile({
filePath: filePath,
success(res) {
resolve(res);
},
fail(err) {
reject(err);
},
});
});
},
openDocumentInNewPage(filePath: string, fileType: FileType) {
return new Promise((resolve, reject) => {
wx.openDocument({
filePath: filePath,
fileType: fileType,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
openFile(filePath: string, flag: OpenFileFlag = "a+") {
return new Promise((resolve, reject) => {
fileSystemManager.open({
filePath: `${wx.env.USER_DATA_PATH}` + filePath,
flag: flag,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
closeFile(fd: any) {
return new Promise((resolve, reject) => {
fileSystemManager.close({
fd: fd,
success(res) {
resolve(res);
},
fail(err) {
reject(err);
},
});
});
},
getSavedFileList() {
return new Promise((resolve, reject) => {
fileSystemManager.getSavedFileList({
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
getFileInfo(filePath: string) {
return new Promise((resolve, reject) => {
fileSystemManager.getFileInfo({
filePath: filePath,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
hasFileOrPath(path: string) {
console.log("查看路径:" + `${wx.env.USER_DATA_PATH}` + path);
return new Promise((resolve, reject) => {
fileSystemManager.access({
path: `${wx.env.USER_DATA_PATH}` + path,
success(res) {
resolve(res);
},
fail(err) {
reject(err);
},
});
});
},
fstatFile(fd: any) {
return new Promise((resolve, reject) => {
fileSystemManager.fstat({
fd: fd,
success(res) {
console.log(res);
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
readDir(path: string) {
return new Promise((resolve, reject) => {
fileSystemManager.readdir({
dirPath: `${wx.env.USER_DATA_PATH}/` + path,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
readFile(path: string, encoding: FileEncodeing = "utf8") {
const fs = wx.getFileSystemManager();
return new Promise((resolve, reject) => {
fs.readFile({
filePath: `${wx.env.USER_DATA_PATH}` + path,
encoding: encoding,
position: 0,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
saveImageToPhotosAlbum(filePath: string) {
return new Promise((resolve, reject) => {
wx.saveImageToPhotosAlbum({
filePath: filePath,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
saveVideoToPhotosAlbum(filePath: string) {
return new Promise((resolve, reject) => {
wx.saveVideoToPhotosAlbum({
filePath: filePath,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
makeDir(path: string) {
return new Promise((resolve, reject) => {
fileSystemManager.mkdir({
dirPath: `${wx.env.USER_DATA_PATH}` + path,
recursive: false,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
});
},
appendFile(path: string, data: any) {
return new Promise((resolve, reject) => {
fileSystemManager.open({
filePath: `${wx.env.USER_DATA_PATH}` + path,
flag: "a+",
success(res) {
fileSystemManager.write({
fd: res.fd,
data: data,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
},
});
});
},
writeFile(path: string, data: any) {
return new Promise((resolve, reject) => {
fileSystemManager.open({
filePath: `${wx.env.USER_DATA_PATH}` + path,
flag: "w+",
success(res) {
fileSystemManager.write({
fd: res.fd,
data: data,
success(res) {
resolve(res);
},
fail(res) {
reject(res);
},
});
},
});
});
},
};