我的cordova应用程序有一个登录名(用户名/密码),经过身份验证后,会在应用程序文件系统的根目录中创建一个名为stk.db的文件。
当我卸载应用程序&重新安装它,我自动使用我登录的以前帐户登录(由于正在读取stk.db文件并提取与用户帐户相关的“令牌”)。
显然,我想让用户在重新安装应用时被迫输入新的用户帐户,根据以下代码处理此问题的最佳方式是什么?
define([
"jquery",
"backbone",
"config",
"stk/controllers/errorController",
"stk/controllers/accountController",
"stk/controllers/onboardController",
"stk/controllers/stkController"
],
function(
$,
Backbone,
Config,
ErrorController,
AccountController,
OnboardController,
stkController)
{
"use strict";
// --------------------------------------------------------------------------------------------
// Controller
// --------------------------------------------------------------------------------------------
var app = function(router) {
var self = this;
self.router = router;
self.views = {};
self.ctors = {};
self.stack = [
{ id: 0, name : "Account", selected: true, direction: null },
{ id: 1, name : "Achievements", selected: false, direction: null },
{ id: 2, name : "Prizes", selected: false, direction: null },
{ id: 3, name : "Shop", selected: false, direction: null },
{ id: 4, name : "Leaderboard", selected: false, direction: null },
{ id: 5, name : "Stats", selected: false, direction: null },
{ id: 6, name : "Contactus", selected: false, direction: null }
];
// --------------------------------------------------------------------------------------------
// register events
// --------------------------------------------------------------------------------------------
_.extend(this, Backbone.Events);
this.on("error", function (args) { this.error(args); }, this);
this.on("start", function (args) { this.db(args); }, this);
this.on("login:complete", function (args) { this.loginComplete(args); }, this);
this.on("onboard:complete", function (args) { this.onboardComplete(args); }, this);
// --------------------------------------------------------------------------------------------
// event handlers
// --------------------------------------------------------------------------------------------
this.db = function() {
var self = this;
var loadDB = function(callback) {
self.storage(callback);
}
loadDB(function(db) {
var check = {};
$.each(db.split("|"), function() {
var entry = this.split(":");
check[entry[0]] = entry[1];
})
self.start(check);
})
};
this.start = function(db) {
if (db.loggedin != "true") {
this.login();
} else {
this.token = db.token;
if (db.boarded === "true") {
this.launch();
} else {
this.onboard();
}
}
};
this.isCarUser = function (usercb) {
var self = this;
var check = {};
self.storage(function (db) {
$.each(db.split("|"), function () {
var entry = this.split(":");
check[entry[0]] = entry[1];
})
usercb(check.Car_user);
});
};
this.error = function(message) {
var handle = "errorCtor";
(this.ctors[handle] === undefined)
? new ErrorController().setup(handle).events().init(message)
: this.ctors[handle].init(message);
};
this.login = function(args) {
var handle = "accountCtor";
(this.ctors[handle] === undefined)
? new AccountController().setup(handle).events().init(args)
: this.ctors[handle].init(args);
};
this.loginComplete = function(args) {
var self = this;
self.token = args;
var store = function(callback) {
self.setLoggedIn(callback, self.token);
}
store(function(db) {
self.onboard();
});
};
this.onboard = function(args) {
var handle = "onboardCtor";
(this.ctors[handle] === undefined)
? new OnboardController().setup(handle).events().init(args)
: this.ctors[handle].init(args);
};
this.onboardComplete = function(args) {
var self = this;
var store = function(callback) {
self.setBoarded(callback, self.token);
}
store(function() {
self.launch();
});
};
this.launch = function(args) {
var handle = "stkCtor";
(this.ctors[handle] === undefined)
? new stkController().setup(handle).events().init(args)
: this.ctors[handle].init(args);
};
// --------------------------------------------------------------------------------------------
// FS
// --------------------------------------------------------------------------------------------
this.setBoarded = function(callback, token) {
var self = this;
var fc = 'opened:true|loggedin:true|boarded:true||Car_user:false|token:' + token;
var errorHandler = function(e) {
self.error(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var boarded = new Blob([fc], {type: 'text/plain'});
fileWriter.write(boarded);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
};
this.setLoggedIn = function(callback, token) {
var self = this;
var fc = 'opened:true|loggedin:true|boarded:false|Car_user:false|token:' + token;
var errorHandler = function(e) {
self.error(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var loggedIn = new Blob([fc], {type: 'text/plain'});
fileWriter.write(loggedIn);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
};
this.setCarUser = function(callback, CarUser){
var self = this;
var fc = 'opened:true|loggedin:true|boarded:true|token:' + this.token + "|Car_user:"+ CarUser;
var errorHandler = function(e) {
self.error(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var loggedIn = new Blob([fc], {type: 'text/plain'});
fileWriter.write(loggedIn);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
}
this.storage = function(callback) {
var self = this;
var fc = 'opened:true|loggedin:false|boarded:false|token:null|Car_user:false';
var errorHandler = function(e) {
console.log(e);
};
var onInitFs = function(fs) {
fs.root.getFile('stk.db', {}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
callback(this.result);
};
reader.readAsText(file);
}, errorHandler);
}, function() {
fs.root.getFile('stk.db', {create: true, exclusive: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
callback(fc);
};
fileWriter.onerror = function(e) {
self.error(e.toString());
};
var opened = new Blob([fc], {type: 'text/plain'});
fileWriter.write(opened);
}, errorHandler);
}, errorHandler);
});
};
if (window.isDevice) {
window.requestFileSystem(window.PERSISTENT, 512, onInitFs, errorHandler);
} else {
callback(fc);
}
};
};
return app;
}
);
答案 0 :(得分:2)
如果由于某些原因确实需要使用本地文件,可以在本地文件中存储版本号,并将其与index.html中存储的版本号进行比较,如果这两个版本号不同,则擦除登录信息在本地文件中并更新版本号。在本地文件中。
顺便说一下,想告诉你本地文件Api在所有手机中都没有按预期工作。例如有时它挂在三星Note 2中,即既不成功也不失败,但相同的代码在其他手机中运行良好。