define(function (require, exports, module) { var session = null; var ev = require("../platform/EventObject.js"); var ajax = require("../platform/webRequest.js"); var storage = require("../platform/storage.js"); var keepAlive = require("../account/keepalive.js"); //var LoginUser = require("./LoginUser.js"); var SessionMgr = function () { var that = this; ev.apply(this, arguments); 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>/i.exec(this.text) && RegExp.$1; var status = /核验状态:[\w\W]+?>([^<>]+?)<\/div>/.exec(this.text) && RegExp.$1; var un = /userDTO\.loginUserDTO\.user_name.*?value=['"]([^'"]+)['"]/.exec(this.text) && RegExp.$1; if (!m || !status) callback({ logined: false }); else { callback({ logined: true, realName: m, status: status, isChecked: status === '已通过', username: un }); } } }, function () { }); }; 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); }; Object.defineProperty(this, "current", { get: function () { return session; }, set: function (v) { if (session === v) return; if (v) session = v; else { that.resetProfile(); } that.fireEvent("sessionChanged"); (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"); } that.resetProfile(); }); return this; }; SessionMgr.prototype = Object.create(ev); SessionMgr.constructor = SessionMgr; module.exports = new SessionMgr(); });