项目背景,我们是用uniapp编写的收银程序,然后打包到android工程,再打包成apk包安装到用户的收银机上,收银机是android系统。由于项目需求,每次升级我们收银系统版本后,之前连接的打印机和LED显示屏的缓存数据都会被重新清除,因为升级相当于重新安装apk。所以把这几个配置写入到本地文件里面,登入时自动读取配置,再自动连接。下面直接贴代码
一、在common目录下新建convertFormat.js
// 读取json文件
function getJsonData(path) { //path:路径
return new Promise(resolve => {
plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, fs => { //请求文件系统
fs.root.getFile(
path, {
create: true //当文件不存在时创建
}, fileEntry => {
fileEntry.file(function(file) {
let fileReader = new plus.io
.FileReader(); //new一个可以用来读取文件的对象fileReader
fileReader.readAsText(file, "utf-8"); //读文件的格式
fileReader.onerror = e => { //读文件失败
// console.log("获取文件失败", fileReader.error);
// plus.nativeUI.toast("获取文件失败,请重启应用", {
// background: "#ffa38c",
// });
return;
};
fileReader.onload = e => { //读文件成功
// console.log("读取文件成功");
let txtData = e.target.result;
resolve(txtData);
// 回调函数内的值想返回到函数外部 就用promise+resolve来返回出去
};
});
}, error => {
// console.log("2新建获取文件失败", error);
// plus.nativeUI.toast("获取文件失败,请重启应用", {
// background: "#ffa38c",
// });
return;
});
},
e => {
// console.log("1请求文件系统失败", e.message);
// plus.nativeUI.toast("请求系统失败,请重启应用", {
// background: "#ffa38c",
// });
return;
}
);
});
};
// 写入josn文件
function changeData(path, seek, writeData) { //参数1:上传路径,参数2:seek方法可设置文件操作指定位置,参数3:写入的json数据
plus.nativeUI.showWaiting("正在保存信息");
return new Promise(resolve => {
plus.io.requestFileSystem(plus.io.PUBLIC_DOWNLOADS, fs => {
fs.root.getFile(path, {
create: true
}, fileEntry => {
fileEntry.file(file => {
fileEntry.createWriter(writer => {
//plus.nativeUI.showWaiting("正在保存信息");
writer.seek(seek); //覆盖文件
const writeDataTemp = JSON.stringify(writeData, null,
"\r").replace(/[\r]/g, "");
writer.write(writeDataTemp); // 整个文件重写
writer.onerror = function() {
//console.log("4写入文件失败", writer.error.message);
plus.nativeUI.closeWaiting();
plus.nativeUI.toast("修改信息失败,请重新操作", {
background: "#ffa38c",
});
return;
};
writer.onsuccess = function() { //填写文件成功
plus.nativeUI.closeWaiting();
// plus.nativeUI.toast("填写文件成功", {
// background: "rgba(255, 255, 255, 0.6)",
// });
resolve("1");
};
},
error => {
// console.log("3创建creactWriter失败", error);
// plus.nativeUI.toast("保存文件失败,请重新操作", {
// background: "#ffa38c",
// });
return;
});
});
},
error => {
// console.log("2获取文件失败", error);
// plus.nativeUI.toast("保存文件失败,请重新操作", {
// background: "#ffa38c",
// });
return;
}
);
}, e => {
//console.log("1请求文件系统失败", e.message);
plus.nativeUI.toast("请求系统失败,请重新操作", {
background: "#ffa38c",
});
return;
});
});
}
async function saveFile(url, file, newfilename) {
let c = await creatDirs(url)
let isokm = moveDirectyOrFile(file, url + "/", newfilename);
return isokm
}
//循环创建目录 url:"_doc/...." _doc开头
async function creatDirs(url) {
let urllist = url.split("/");
console.log(urllist)
//创建文件夹
let u = "";
for (let i = 0; i < urllist.length - 1; i++) {
let j = i;
if (i == 0) {
u = urllist[i];
} else {
u = u + "/" + urllist[i];
}
console.log(i + "-------------------")
console.log(u)
console.log(urllist[j + 1])
await CreateNewDir(u, urllist[j + 1]);
}
}
//重命名目录或文件名
function moveDirectyOrFile(srcUrl, dstUrl, newName) { //srcUrl需要移动的目录或文件,dstUrl要移动到的目标目录(父级)
plus.io.resolveLocalFileSystemURL(srcUrl, function(srcEntry) {
//console.log(111)
plus.io.resolveLocalFileSystemURL(dstUrl, function(dstEntry) {
//console.log(222)
if (srcEntry.isDirectory) {
//console.log(33)
srcEntry.moveTo(dstEntry, newName, function(entry) {
//console.log("New Path: " + entry.fullPath);
return true;
}, function(e) {
return e;
//console.log(e.message);
});
} else {
srcEntry.moveTo(dstEntry, newName, function(entry) {
//console.log("New Path: " + entry.fullPath);
return true;
}, function(e) {
return e;
//console.log(e.message);
});
}
}, function(e) {
uni.showToast({
title: '获取目标目录失败:' + e.message,
duration: 2000,
icon: 'none'
});
});
}, function(e) {
uni.showToast({
title: '获取目录失败:' + e.message,
duration: 2000,
icon: 'none'
});
});
}
//创建一个新目录
function CreateNewDir(url, dirName) {
//url值可支持相对路径URL、本地路径URL
return new Promise((resolver, reject) => {
plus.io.resolveLocalFileSystemURL(url, function(entry) {
entry.getDirectory(dirName, {
create: true,
exclusive: false
}, function(dir) {
resolver(true)
}, function(error) {
reject(error.message)
uni.showToast({
title: dirName + '目录创建失败:' + error.message,
duration: 2000,
icon: 'none'
});
});
}, function(e) {
reject(error.message)
uni.showToast({
title: '获取目录失败:' + e.message,
duration: 2000,
icon: 'none'
});
});
})
}
function copyFileTo(url, newUrl, dirName, newName) {
if (url.length >= 7 && "file://" == url.substring(0, 7)) {
url = url.substring(7)
}
let tempUrl = url.substring(0, url.lastIndexOf('/'));
let addUrl = newUrl + '/' + dirName;
console.log(addUrl, tempUrl)
if (addUrl == tempUrl) {
return url;
}
console.log(newUrl, dirName, newName)
return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(url, async (entry) => {
if (entry.isFile) {
let c = await CreateNewDir(newUrl, dirName)
let u = await getDirsys(addUrl)
entry.copyTo(u, newName, en => {
resolve(en.fullPath);
}, e => {
console.log(e);
reject('错误:复制时出现错误')
uni.showModal({
title: "错误",
content: "复制时出现错误"
})
})
} else {
reject('错误:路径必须是文件')
uni.showModal({
title: "错误",
content: "路径必须是文件"
})
}
}, (e) => {
console.log(e);
reject(e)
uni.showModal({
title: "错误",
content: "打开文件系统时出错"
})
});
})
}
//获取目录对象
function getDirsys(url) {
return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(url, (entry) => {
resolve(entry)
}, (e) => {
reject(e)
console.log(e);
});
})
}
//将这些方法暴露出去
export {
getJsonData,
changeData,
saveFile,
creatDirs,
moveDirectyOrFile,
copyFileTo,
getDirsys,
};
二、写入
import {changeData,getJsonData} from '@/common/convertFormat.js';
//写在对应的方法上
let ledData = {
serialPortIndex : this.serialPortIndex,
serialBaudrate: this.serialBaudrate
}
//绝对路径,以"file://"开头。加粗样式 // /sdcard–>为根目录
const pathUrl = 'file:///sdcard/records/led.txt';
changeData(pathUrl, 0,ledData);
三、读取
import {getJsonData} from '@/common/convertFormat.js';
async initFile (){
//获取配置文件中的打印机配置和led配置,赋值到缓存中
const pathUrl = 'file:///sdcard/records/led.txt';
let ledData = await getJsonData(pathUrl);
ledData = JSON.parse(ledData);
if (ledData){
uni.setStorageSync('serialPortIndex',ledData.serialPortIndex)
uni.setStorageSync('serialBaudrate',ledData.serialBaudrate)
}
const printUrl = 'file:///sdcard/records/print.txt';
let printData = await getJsonData(printUrl);
printData = JSON.parse(printData)
if (printData){
uni.setStorageSync('vendorId',printData.vendorId)
}
},
这样每次升级后,这个文件都会在,你也可以用android的文件管理工具,找到生成的这几个文件。路径 首页/ records/
如果有遇到权限问题则加 AndroidManifest.xml