You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.7 KiB
JavaScript

const ConfigManager = {
// 读取配置文件
readConfigFile(callback) {
if (typeof plus === 'undefined') {
callback(new Error('plus is not defined. Run on a real device or emulator.'));
return;
}
plus.io.resolveLocalFileSystemURL('_doc/config.json', function(entry) {
entry.file(function(file) {
const reader = new plus.io.FileReader();
reader.onloadend = function(e) {
try {
const config = JSON.parse(e.target.result);
callback(null, config);
} catch (error) {
callback(error);
}
};
reader.readAsText(file);
}, function(error) {
callback(error);
});
}, function(error) {
callback(error);
});
},
// 保存配置文件
saveConfigFile(newConfig, callback) {
if (typeof plus === 'undefined') {
callback(new Error('plus is not defined. Run on a real device or emulator.'));
return;
}
plus.io.resolveLocalFileSystemURL('_doc/config.json', function(entry) {
entry.createWriter(function(writer) {
writer.onwriteend = function() {
callback(null);
};
writer.onerror = function(error) {
callback(error);
};
writer.write(JSON.stringify(newConfig));
}, function(error) {
callback(error);
});
}, function(error) {
callback(error);
});
},
// 初始化配置文件
initializeConfigFile(defaultConfig, callback) {
if (typeof plus === 'undefined') {
callback(new Error('plus is not defined. Run on a real device or emulator.'));
return;
}
plus.io.resolveLocalFileSystemURL('_doc/', function(directoryEntry) {
directoryEntry.getFile('config.json', { create: true }, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.onwriteend = function() {
callback(null, defaultConfig);
};
writer.onerror = function(error) {
callback(error);
};
writer.write(JSON.stringify(defaultConfig));
});
});
});
},
// 重置配置文件
resetConfigFile(defaultConfig, callback) {
this.initializeConfigFile(defaultConfig, callback);
}
};
export default ConfigManager;