2014-05-16 20:10:45 +08:00
|
|
|
|
define(function (require, exports, module) {
|
|
|
|
|
var session = null;
|
|
|
|
|
var ev = require("../platform/EventObject.js");
|
2014-05-29 19:41:38 +08:00
|
|
|
|
var ajax = require("../platform/webRequest.js");
|
2014-06-12 21:36:05 +08:00
|
|
|
|
var storage = require("../platform/storage.js");
|
|
|
|
|
var keepAlive = require("../account/keepalive.js");
|
2014-05-29 19:41:38 +08:00
|
|
|
|
|
2014-05-16 20:10:45 +08:00
|
|
|
|
//var LoginUser = require("./LoginUser.js");
|
|
|
|
|
|
|
|
|
|
var SessionMgr = function () {
|
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
|
|
ev.apply(this, arguments);
|
2014-05-29 19:41:38 +08:00
|
|
|
|
|
|
|
|
|
that.checkLoginState = function (callback) {
|
|
|
|
|
ajax.sendGet("modifyUser/initQueryUserInfo", "", null, "text", function () {
|
|
|
|
|
if (this.text.indexOf("登录名:") !== -1) {
|
|
|
|
|
callback({ logined: false });
|
|
|
|
|
} else {
|
|
|
|
|
var m = /姓名:.*[\r\n]+<div[^>]+>([^<]+)<\/div>/i.exec(this.text) && RegExp.$1;
|
|
|
|
|
var status = /核验状态:[\w\W]+?>([^<>]+?)<\/div>/.exec(this.text) && RegExp.$1;
|
2014-06-12 21:36:05 +08:00
|
|
|
|
var un = /userDTO\.loginUserDTO\.user_name.*?value=['"]([^'"]+)['"]/.exec(this.text) && RegExp.$1;
|
2014-05-29 19:41:38 +08:00
|
|
|
|
|
|
|
|
|
if (!m || !status)
|
|
|
|
|
callback({ logined: false });
|
|
|
|
|
else {
|
2014-06-12 21:36:05 +08:00
|
|
|
|
callback({ logined: true, realName: m, status: status, isChecked: status === '已通过', username: un });
|
2014-05-29 19:41:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}, function () {
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
};
|
2014-06-12 21:36:05 +08:00
|
|
|
|
that.loadProfile = function (username, checkData) {
|
|
|
|
|
var profile = storage.obj("12306_user_" + username) || { username: username, passengers: [], savedProfile: {}, currentProfile: {}, rawPassenger: [], options: {} };
|
|
|
|
|
that.current = profile;
|
|
|
|
|
|
|
|
|
|
if (checkData) {
|
|
|
|
|
that.current.dispname = checkData.realName;
|
|
|
|
|
that.fireEvent("userInfoUpdated");
|
|
|
|
|
|
|
|
|
|
if (!checkData.isChecked)
|
|
|
|
|
that.fireEvent("userNotChecked");
|
|
|
|
|
} else {
|
|
|
|
|
that.checkLoginState(function (data) {
|
|
|
|
|
that.current.dispname = data.realName;
|
|
|
|
|
that.fireEvent("userInfoUpdated");
|
|
|
|
|
|
|
|
|
|
if (!data.isChecked)
|
|
|
|
|
that.fireEvent("userNotChecked");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
that.resetProfile = function () {
|
|
|
|
|
session = null;
|
|
|
|
|
};
|
|
|
|
|
that.loadPassengers = function(force) {
|
|
|
|
|
if (!that.isLogined)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
that.save = function () {
|
|
|
|
|
if (!that.current)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var key = "12306_user_" + that.current.username;
|
|
|
|
|
storage.put(key, that.current);
|
|
|
|
|
};
|
2014-05-16 20:10:45 +08:00
|
|
|
|
|
|
|
|
|
Object.defineProperty(this, "current", {
|
|
|
|
|
get: function () {
|
|
|
|
|
return session;
|
|
|
|
|
},
|
2014-05-29 19:41:38 +08:00
|
|
|
|
set: function (v) {
|
2014-05-16 20:10:45 +08:00
|
|
|
|
if (session === v) return;
|
|
|
|
|
|
2014-06-12 21:36:05 +08:00
|
|
|
|
if (v) session = v;
|
|
|
|
|
else {
|
|
|
|
|
that.resetProfile();
|
|
|
|
|
}
|
2014-05-16 20:10:45 +08:00
|
|
|
|
that.fireEvent("sessionChanged");
|
2014-06-12 21:36:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(that.isLogined && keepAlive.start()) || keepAlive.stop();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Object.defineProperty(this, "isLogined", {
|
|
|
|
|
get: function () {
|
|
|
|
|
return session && session.username || false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//主动检测
|
|
|
|
|
that.checkLoginState(function (data) {
|
|
|
|
|
if (data.logined) {
|
|
|
|
|
that.loadProfile(data.username, data);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
document.addEventListener("loginInvalid", function () {
|
|
|
|
|
if (that.isLogined) {
|
|
|
|
|
document.dispatchEvent(new CustomEvent("userForcedOut"));
|
|
|
|
|
that.fireEvent("userForcedOut");
|
2014-05-16 20:10:45 +08:00
|
|
|
|
}
|
2014-06-12 21:36:05 +08:00
|
|
|
|
that.resetProfile();
|
2014-05-16 20:10:45 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
SessionMgr.prototype = Object.create(ev);
|
|
|
|
|
SessionMgr.constructor = SessionMgr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = new SessionMgr();
|
|
|
|
|
});
|