+
+
diff --git a/Web12306/js/account/keepalive.js b/Web12306/js/account/keepalive.js
new file mode 100644
index 0000000..61e53cf
--- /dev/null
+++ b/Web12306/js/account/keepalive.js
@@ -0,0 +1,38 @@
+define(function (require, exports, module) {
+ var ajax = require("../platform/webRequest.js");
+ var interval = 1 * 60 * 1000;
+ var timer = null;
+
+ var checkUser = function() {
+ ajax.sendPost("login/checkUser", "login/init", null, "json", function () {
+ var m = this.model;
+
+ if (!m || !m.status) {
+ setTimeout(checkUser, interval);
+ }
+ else if (!m.data || !m.data.flag) {
+ //登录无效
+ document.dispatchEvent(new CustomEvent("loginInvalid"));
+ }
+ else {
+ ajax.userAtts = m.data.attributes;
+ setTimeout(checkUser, interval);
+ }
+ }, function () {
+ setTimeout(checkUser, interval);
+ });
+ };
+
+ //
+ exports.start = function() {
+ if (!timer)
+ checkUser();
+ return true;
+ };
+
+ exports.stop = function() {
+ if (timer)
+ clearTimeout(timer);
+ return true;
+ };
+});
diff --git a/Web12306/js/account/sessionMgr.js b/Web12306/js/account/sessionMgr.js
index ea76b63..59aa0fc 100644
--- a/Web12306/js/account/sessionMgr.js
+++ b/Web12306/js/account/sessionMgr.js
@@ -1,8 +1,9 @@
define(function (require, exports, module) {
var session = null;
var ev = require("../platform/EventObject.js");
- var widget = require("../ui/widget.js");
var ajax = require("../platform/webRequest.js");
+ var storage = require("../platform/storage.js");
+ var keepAlive = require("../account/keepalive.js");
//var LoginUser = require("./LoginUser.js");
@@ -10,17 +11,7 @@
var that = this;
ev.apply(this, arguments);
- that.showLogin = function () {
- if (that.current != null) return;
- widget.showFloatDialog($("#user-login-dialog"));
- };
- that.loadLoginVcImage = function (target) {
-
- };
- that.loginAsync = function (callback) {
-
- };
that.checkLoginState = function (callback) {
ajax.sendGet("modifyUser/initQueryUserInfo", "", null, "text", function () {
if (this.text.indexOf("登录名:") !== -1) {
@@ -28,11 +19,12 @@
} 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 === '已通过' });
+ callback({ logined: true, realName: m, status: status, isChecked: status === '已通过', username: un });
}
}
@@ -40,6 +32,41 @@
});
};
+ 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 () {
@@ -48,10 +75,35 @@
set: function (v) {
if (session === v) return;
- session = v;
+ 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;
};
diff --git a/Web12306/js/boot.js b/Web12306/js/boot.js
index 9f2a809..2e1004c 100644
--- a/Web12306/js/boot.js
+++ b/Web12306/js/boot.js
@@ -25,19 +25,39 @@
}
});
- loadScript("modules/seajs/sea.js", function () {
- seajs.config({
- base: basePath,
- alias: {
- "jquery": "modules/jquery/jquery.js",
- "underscore": "modules/underscore/underscore.js"
- },
- vars: {
- 'locale': 'zh-cn'
- },
- charset: "utf-8",
- debug: true
+ //确保内容脚本启动
+ var start = $.Deferred();
+ start.done(function() {
+ loadScript("modules/seajs/sea.js", function() {
+ seajs.config({
+ base: basePath,
+ alias: {
+ "jquery": "modules/jquery/jquery.js",
+ "underscore": "modules/underscore/underscore.js"
+ },
+ vars: {
+ 'locale': 'zh-cn'
+ },
+ charset: "utf-8",
+ debug: true
+ });
+ seajs.use("ui/" + pagename);
});
- seajs.use("ui/" + pagename);
});
+ start.fail(function() {
+ alert("没有安装扩展!");
+ });
+
+ if (document.body.dataset["mobileSupportInitialized"]) {
+ start.resolve();
+ } else {
+ var timer = setTimeout(function() {
+ start.reject();
+ }, 3000);
+ document.addEventListener("mobileSupportInitialized", function () {
+ window.clearTimeout(timer);
+ start.resolve();
+ });
+ }
+
})(window, document);
diff --git a/Web12306/js/passenger/Passenger.js b/Web12306/js/passenger/Passenger.js
deleted file mode 100644
index 9712170..0000000
--- a/Web12306/js/passenger/Passenger.js
+++ /dev/null
@@ -1,46 +0,0 @@
-define(function (require) {
- var eo = require("../platform/EventObject.js");
- var $ = require("jquery");
-
-
-
- function Passenger(name, type, typename, idtype, idtypename, id, firstLetter) {
- this.name = name;
- this.type = type;
- this.idtype = idtype;
- this.id = id;
- this.key = type + "$" + name + "$" + idtype + "$" + this.id;
- this.save = false;
- this.firstLetter = firstLetter;
- this.idtypeName = idtypename;
- this.typename = typename;
- var __ = this;
-
- this.toString = function () {
- return name;
- };
- this.toHtml = function (removeFlag) {
- return "";
- };
- //e.passengers.push(new Passenger(this.passenger_name, this.passenger_type, this.passenger_id_type_code, this.passenger_id_no, this.first_letter));
- this.toRawPassenger = function () {
- return {
- passenger_name: __.name,
- passenger_type: __.type,
- passenger_id_type_code: __.idtype,
- passenger_id_no: __.id,
- passenger_first_letter: __.firstLetter,
- mobile_no: "",
- passenger_id_type_name: __.idtypeName,
- passenger_type_name: __.typename
- };
- };
-
- return this;
- };
-
- Passenger.prototype = Object.create(eo);
- Passenger.constructor = Passenger;
-
- return Passenger;
-});
\ No newline at end of file
diff --git a/Web12306/js/passenger/PassengerList.js b/Web12306/js/passenger/PassengerList.js
deleted file mode 100644
index a202028..0000000
--- a/Web12306/js/passenger/PassengerList.js
+++ /dev/null
@@ -1,3 +0,0 @@
-define(function() {
-
-});
\ No newline at end of file
diff --git a/Web12306/js/platform/extensionConnect.js b/Web12306/js/platform/extensionConnect.js
deleted file mode 100644
index 14013af..0000000
--- a/Web12306/js/platform/extensionConnect.js
+++ /dev/null
@@ -1,43 +0,0 @@
-define(function (require, exports, module) {
- var EventObject = require("EventObject");
-
- var Connector = function () {
- var token = 0;
- var callback = {};
-
- this.send = function (type, data, cb, target) {
- /// 发送消息到后台
-
- var args = { type: type, data: data, token: token++ };
- if (cb)
- callback[args.token] = { func: cb, target: target, arg: args };
- window.postMessage({ command: "ccontentScriptConnection", data: args });
- };
-
-
- //回调
- window.addEventListener("message", function (event) {
- if (!event.data || !event.data.command || event.data.command !== "contentScriptConnection")
- return;
-
- var data = event.data;
- var result = data.result;
- var destToken = data.token;
-
- if (callback[destToken]) {
- var cb = callback[destToken];
- delete callback[destToken];
- if (cb.target) {
- cb.func.apply(cb.target, [result, cb.arg]);
- } else {
- cb.func(result, cb.arg);
- }
- }
- });
- };
-
- Connector.prototype = Object.create(EventObject);
- Connector.constructor = Connector;
-
- return Object.create(Connector);
-});
diff --git a/Web12306/js/platform/extensionPort.js b/Web12306/js/platform/extensionPort.js
new file mode 100644
index 0000000..af9f1e5
--- /dev/null
+++ b/Web12306/js/platform/extensionPort.js
@@ -0,0 +1,28 @@
+define(function (require, exports, module) {
+ var targetExtension = [
+ "bpbefagpafkfgoihbmcgeileodldkpnf",
+ "gkbheeokbgmmnbjhhlphckobccejghjn"
+ ];
+ exports.targetId = null;
+ for (var id in targetExtension) {
+ var port = chrome.runtime.connect(targetExtension[id]);
+ try {
+ port.postMessage("");
+ port.disconnect();
+
+ exports.targetId = targetExtension[id];
+ break;
+ } catch (e) {
+
+ }
+ }
+ if (!exports.targetId)
+ document.dispatchEvent(new CustomEvent("supportError"));
+
+ exports.sendMessage = function(m, response) {
+ if (!exports.targetId)
+ throw "extension not connected.";
+
+ chrome.runtime.sendMessage(exports.targetId, m, response || function () { });
+ };
+});
diff --git a/Web12306/js/platform/parser.js b/Web12306/js/platform/parser.js
new file mode 100644
index 0000000..2a847f9
--- /dev/null
+++ b/Web12306/js/platform/parser.js
@@ -0,0 +1,17 @@
+define(function (require, exports) {
+
+ exports.getError = function (data) {
+ /// 获得指定返回数据中的错误信息
+
+ if (data.messages && data.messages instanceof Array) {
+ return { message: data.messages.join(";") };
+ }
+ if (data.data && data.data.isRelogin) {
+ return { message: "登录状态异常,请重新登录。", relogin: true };
+ }
+
+ return { message: "未知错误信息" };
+ };
+
+
+});
\ No newline at end of file
diff --git a/Web12306/js/platform/paser.js b/Web12306/js/platform/paser.js
deleted file mode 100644
index 9202c6c..0000000
--- a/Web12306/js/platform/paser.js
+++ /dev/null
@@ -1,6 +0,0 @@
-define(function(require, exports) {
- var _ = require("../modules/underscore/underscore.js");
- var $ = require("jquery");
-
-
-});
\ No newline at end of file
diff --git a/Web12306/js/platform/storage.js b/Web12306/js/platform/storage.js
index cac7259..7e99c56 100644
--- a/Web12306/js/platform/storage.js
+++ b/Web12306/js/platform/storage.js
@@ -1,22 +1,40 @@
-define(function (require) {
- var EventObject = require("./EventObject.js");
-
- var Storage = function () {
- var __ = this;
-
- this.get = function(key) {
- return localStorage[key] || "";
- };
- this.put = function(key, value) {
- localStorage[key] = value + '';
- };
- this.remove = function(key) {
- localStorage.remove(key);
- };
- Object.defineProperty(this, "");
- };
- Storage.prototype = Object.create(EventObject);
- Storage.constructor = Storage;
-
- return Object.create(Storage);
-});
+define(function (require) {
+ var EventObject = require("./EventObject.js");
+ var extension = require("./extensionPort.js");
+
+ var Storage = function () {
+ var __ = this;
+ var st = {};
+
+ EventObject.apply(this, Array.prototype.slice.call(arguments));
+
+ this.get = function (key) {
+ return st[key] || "";
+ };
+ this.put = function (key, value) {
+ if (typeof (value) !== 'string') {
+ value = JSON.stringify(value);
+ }
+ st[key] = value + '';
+ extension.sendMessage({ action: "setStorage", detail: { key: value } });
+ };
+ this.remove = function (key) {
+ delete st[key];
+ };
+ this.obj=function(key) {
+ var value = __.get(key);
+ if (!value)
+ return null;
+
+ return JSON.parse(value);
+ }
+
+ extension.sendMessage({ action: "getStorage" }, function (m) {
+ st = m.detail;
+ });
+ };
+ Storage.prototype = Object.create(EventObject);
+ Storage.constructor = Storage;
+
+ return new Storage();
+});
diff --git a/Web12306/js/platform/webRequest.js b/Web12306/js/platform/webRequest.js
index 36318f4..a4495e8 100644
--- a/Web12306/js/platform/webRequest.js
+++ b/Web12306/js/platform/webRequest.js
@@ -2,20 +2,26 @@
define(function (require, exports, module) {
var EventObject = require("../platform/EventObject.js");
var config = require("../data.js");
-
- var WebRequest = function() {
+
+ var WebRequest = function () {
var that = this;
var requestMap = {};
var ajaxCount = 0;
+ var jsonAtt = null;
EventObject.apply(this, arguments);
- this.getFullUri= function (url) {
+ this.getFullUri = function (url) {
/// 获得完整地址
if (url[4] === ":" || url[5] === ":") return url;
return config.baseUri + url;
};
- this.send = function (method, url, refer, data, responseType, done, failed) {
+ this.send = function (method, url, refer, data, responseType, done, failed, ignoreGlobalAtt) {
+ if (!ignoreGlobalAtt && jsonAtt) {
+ data = $.extend({
+ _json_att: jsonAtt
+ }, data);
+ }
var xhrData = {
url: that.getFullUri(url),
data: data,
@@ -33,7 +39,19 @@ define(function (require, exports, module) {
};
return;
} else {
- that.fireEvent("requestSupportError");
+ document.dispatchEvent(new CustomEvent("requestSupportError"));
+ }
+ };
+ this.loadImage = function (url, refer, done, failed) {
+ var e = new CustomEvent("ajaxLoadVerifyCode", { detail: { url: that.getFullUri(url), refer: that.getFullUri(url), index: ajaxCount++ }, cancelable: true });
+ if (!document.dispatchEvent(e)) {
+ requestMap[e.detail.index] = {
+ done: done,
+ fail: failed
+ };
+ return;
+ } else {
+ document.dispatchEvent(new CustomEvent("requestSupportError"));
}
};
this.sendGet = function (url, refer, data, responseType, done, failed) {
@@ -47,7 +65,7 @@ define(function (require, exports, module) {
that.send("GET", url, refer, data, responseType, done, failed);
};
this.sendPost = function (url, refer, data, responseType, done, failed) {
- that.send("GET", url, refer, data, responseType, done, failed);
+ that.send("POST", url, refer, data, responseType, done, failed);
};
document.addEventListener("ajaxproxyfinished", function (e) {
@@ -56,8 +74,20 @@ define(function (require, exports, module) {
var param = requestMap[data.index];
delete requestMap[data.index];
+
data.success ? param.done.call(data) : param.fail.call(data);
});
+
+ Object.defineProperties(this, {
+ "userAtts": {
+ get: function () {
+ return jsonAtt;
+ },
+ set: function (value) {
+ jsonAtt = value;
+ }
+ }
+ });
};
WebRequest.prototype = Object.create(EventObject);
diff --git a/Web12306/js/ui/index.js b/Web12306/js/ui/index.js
index d4cc60e..61d2a76 100644
--- a/Web12306/js/ui/index.js
+++ b/Web12306/js/ui/index.js
@@ -4,6 +4,7 @@
var storage = require('../platform/storage.js');
var widget = require("./widget.js");
var sessMgr = require("../account/sessionMgr.js");
+ var mp = require("./widget_message_popup.js");
//初始化UI
(function() {
@@ -25,19 +26,28 @@
//初始化日期
require("./widget_datedropdown.js").init("input.ui-date");
require("./widget_cityselector.js").init("input.ui-cityselector");
+ require("./widget_verifycode.js").init();
//加载各模块
var ui_login = require("./ui-login.js");
+ //界面交互初始化
+ (function() {
+ $("#index-tip-body").click(function() {
+ $(this).next().fadeIn();
+ });
+ $("section.search-tips-container i.fa-times").click(function() {
+ $(this).closest("section.search-tips-container").fadeOut();
+ });
+ //登录失效
+ sessMgr.on("userForcedOut", function() {
+ mp.showMessagePopup("error", "您的登录出现了问题,请重新登录");
+ });
+ })();
+
//初始化
$("#J-search-form").submit(function() {
return false;
});
-
- setTimeout(function () {
- sessMgr.checkLoginState(function(a) {
- console.log(a)
- });
- }, 1000);
});
diff --git a/Web12306/js/ui/ui-login.js b/Web12306/js/ui/ui-login.js
index a5408be..0498cb1 100644
--- a/Web12306/js/ui/ui-login.js
+++ b/Web12306/js/ui/ui-login.js
@@ -1,26 +1,186 @@
define(function (require, exports, module) {
var sessionMgr = require("../account/sessionMgr.js");
var ev = require("../platform/EventObject.js");
+ var mp = require("./widget_message_popup.js");
+ var parser = require("../platform/parser.js");
+ var ajax = require("../platform/webRequest.js");
+ var vc = require("../ui/widget_verifycode.js");
+ var widget = require("../ui/widget.js");
+ var storage = require("../platform/storage.js");
+
var UiWidgetLogin = function () {
+ var that = this;
+ var dlg = $("#user-login-dialog");
+ var btn = dlg.find("button.button-primary");
+
ev.apply(this, arguments);
+ this.showLoginDialog = function () {
+ var info = storage.obj("12306_lastUser");
+ if (info) {
+ $("#user-login-dialog input:password").val(info.pwd);
+ $("#user-login-dialog input:text:first").val(info.name);
+ }
+ $("#user-login-dialog input:text:last").val("");
+ vc.autoLoad(dlg);
+ widget.showFloatDialog(dlg);
+ };
+ this.closeLoginDialog = function () {
+ widget.hideFloatDialog(dlg);
+ };
+ that.logout = function() {
+ var tip = new mp.MessagePopup("loading", "正在退出中...");
+ tip.show();
+
+ ajax.sendGet("login/loginOut", "login/init", null, "text", function () {
+ sessionMgr.resetProfile();
+ tip.setState("ok", "您已经成功注销了登录...");
+ tip.delayClose();
+ }, function () {
+ mp.alert("提示", "似乎看起来出了点网络错误...请刷新页面.....", function() {
+ self.location.reload();
+ });
+ });
+ };
+ this.loginAsync = function (preventError) {
+ var inputs = $("#user-login-dialog input:text");
+ var un = inputs[0].value;
+ var pwd = $("#user-login-dialog input:password").val();
+ var randcode = inputs[1].value;
+
+ if (!un) {
+ if (!preventError)
+ mp.showMessagePopup("warn", "客官您的【用户名】没填...");
+ return;
+ }
+ if (!pwd) {
+ if (!preventError)
+ mp.showMessagePopup("warn", "客官您的【密码】没填...");
+ return;
+ }
+ if (!randcode) {
+ if (!preventError)
+ mp.showMessagePopup("warn", "客官您的【验证码】没填...");
+ return;
+ }
+ btn.prop("disabled", true);
+ var loginTip = new mp.MessagePopup("loading", "正在登录中...");
+ loginTip.show();
+
+ var loadLoginAsyncSuggest = function () {
+ that.fireEvent("loadSuggest");
+
+ loginTip.setState("loading", "正在获得登录随机码...");
+ ajax.sendPost("login/loginAysnSuggest", "login/init", {
+ "loginUserDTO.user_name": un,
+ "userDTO.password": pwd,
+ "randCode": randcode
+ }, "json", function () {
+ if (this.model.data === null || this.model.data.loginCheck !== 'Y') {
+ loginFailed(parser.getError(this.model).message);
+ } else {
+ submitLoginInfo();
+ }
+ }, function () {
+ loginFailed("无法加载随机码");
+ });
+ };
+ var submitLoginInfo = function () {
+ that.fireEvent("checkUserInfo");
+
+ loginTip.setState("loading", "正在登录中...");
+
+ ajax.sendPost("login/checkUser", "login/init", null, "json", function () {
+ var m = this.model;
+
+ if (!m || !m.status || !m.data || !m.data.flag)
+ loginFailed(parser.getError(m).message);
+ else {
+ ajax.userAtts = m.data.attributes;
+ postLogin();
+ }
+ }, function () {
+ loginFailed("无法获得用户信息");
+ });
+ };
+ var postLogin = function () {
+ that.fireEvent("postLogin");
+
+ loginTip.setState("loading", "正在登录中...");
+
+ ajax.sendPost("login/userLogin", "login/init", null, "json", function () {
+ loginSuccess();
+ }, function () {
+ loginSuccess();
+ });
+
+ };
+ var loginFailed = function (msg) {
+ btn.prop("disabled", false);
+ vc.refresh(dlg);
+ $("#user-login-dialog input:text:last").val("");
+
+ loginTip.content = msg;
+ loginTip.type = "error";
+
+ loginTip.delayClose();
+ };
+ var loginSuccess = function () {
+ btn.prop("disabled", false);
+ that.closeLoginDialog();
+ loginTip.setState("ok", "您已成功登录!");
+ loginTip.delayClose();
+
+ //TODO 没有保存密码的选项!
+ storage.put("12306_lastUser", {
+ name: un,
+ pwd: pwd
+ });
+ sessionMgr.loadProfile(un);
+ };
+
+ loadLoginAsyncSuggest();
+ };
sessionMgr.on("sessionChanged", function () {
if (sessionMgr.current) {
$(".non-login").hide();
$(".user-logined").show();
+ $("div.user-nav-user").html(sessionMgr.current.dispname);
} else {
$(".non-login").show();
$(".user-logined").hide();
+ $("div.user-nav-user").html("未登录");
}
});
+ sessionMgr.on("userInfoUpdated", function() {
+ $("div.user-nav-user").html(sessionMgr.current.dispname);
+ });
+ sessionMgr.on("userNotChecked", function () {
+ mp.alert("用户尚未通过审核!");
+ });
//界面事件绑定
$("#acc_login").click(function () {
- sessionMgr.showLogin();
+ that.showLoginDialog();
+ });
+ $("#user-login-dialog button.button-primary").click(function () {
+ that.loginAsync();
+ });
+ $("#user-login-dialog input:text:last").keyup(function () {
+ if (this.value.length === 4)
+ that.loginAsync(true);
+ });
+ $("#acc_logout").click(function () {
+ mp.confirm("退出?", "确定要退出登录吗?", function() {
+ that.logout();
+ }, function() {
+
+ });
+
});
//初始化显示
diff --git a/Web12306/js/ui/widget_message_popup.js b/Web12306/js/ui/widget_message_popup.js
new file mode 100644
index 0000000..c957701
--- /dev/null
+++ b/Web12306/js/ui/widget_message_popup.js
@@ -0,0 +1,111 @@
+define(function (require, exports, module) {
+ var htmlTemplate = '';
+ var iconMap = {
+ warn: "warning",
+ ok: "check",
+ error: "times-circle",
+ loading: "spinner"
+ };
+
+ var MessagePopup = function (type, content, options) {
+ var that = this;
+ var html = $(htmlTemplate);
+ var shown = false;
+ options = $.extend({ closeAfter: null }, options);
+ $("body").append(html);
+
+ var centerElement = function (ele) {
+ ele = ele || html;
+
+ var width = ele.width();
+ var height = ele.height();
+
+ ele.css({
+ "margin-left": "-" + (width / 2) + "px",
+ "margin-top": "-" + (height / 2 - 40) + "px"
+ });
+ };
+ this.show = function () {
+ html.animate({ "margin-top": "-=20px", opacity: "1" }, "fast", "linear", function () {
+ shown = true;
+ if (options.closeAfter) {
+ that.delayClose();
+ }
+ });
+ };
+ this.close = function () {
+ html.animate({ "margin-top": "-=20px", opacity: "hide" }, "fast", "linear", function () {
+ html.remove();
+ });
+ };
+ this.delayClose = function (timeout) {
+ if (that.shown)
+ setTimeout(that.close, timeout || options.closeAfter);
+ else {
+ options.closeAfter = timeout || options.closeAfter || 1000;
+ }
+ };
+ that.setState = function (type, content) {
+ that.type = type;
+ that.content = content;
+ };
+ Object.defineProperties(this, {
+ "content": {
+ get: function () {
+ return html.find("span").html();
+ },
+ set: function (value) {
+ html.find("span").html(value);
+ centerElement();
+ }
+ },
+ "type": {
+ get: function () {
+ return html.attr("data-type");
+ },
+ set: function (value) {
+ html.attr("data-type", value);
+ html.removeClass().addClass("message-popup message-popup-" + value);
+ html.find("b").removeClass().addClass("fa fa-" + iconMap[value] + (value === "loading" ? " fa-spin" : ""));
+ }
+ },
+ "shown": {
+ get: function () {
+ return shown;
+ }
+ }
+ });
+ this.content = content;
+ this.type = type;
+
+ return this;
+ };
+
+ exports.MessagePopup = MessagePopup;
+
+ exports.showMessagePopup = function (icon, content, options) {
+ options = $.extend({ closeAfter: 1000 }, options);
+
+ var popup = new MessagePopup(icon, content, options);
+ popup.show();
+ return popup;
+ };
+
+ exports.confirm = function (title, content, yes, no) {
+ //TODO 确认界面
+ if (confirm(content))
+ yes && yes();
+ else no && no();
+ };
+ exports.alert = function (title, content, callback) {
+ //TODO 确认界面
+ alert(content);
+ callback && callback();
+ };
+
+ //捕捉一些通用的事件
+ document.addEventListener("requestSupportError", function () {
+ exports.showMessagePopup("error", "无法执行网络请求,似乎没有安装12306订票助手哦。", { closeAfter: null });
+ });
+
+});
diff --git a/Web12306/js/ui/widget_verifycode.js b/Web12306/js/ui/widget_verifycode.js
new file mode 100644
index 0000000..934313f
--- /dev/null
+++ b/Web12306/js/ui/widget_verifycode.js
@@ -0,0 +1,45 @@
+define(function (require, exports, module) {
+ var ajax = require("../platform/webRequest.js");
+
+ exports.data = {
+ login: {
+ url: "passcodeNew/getPassCodeNew?module=login&rand=sjrand",
+ refer: "login/init"
+ },
+ order: {
+ url: "",
+ refer: ""
+ }
+ };
+ exports.autoLoad = function(target) {
+ if (target.is(".verify-code"))
+ exports.load(target);
+ else {
+ target.find(".verify-code").each(function() { exports.load($(this)); });
+ }
+ };
+ exports.load = function (target) {
+ var type = target.attr("data-target");
+ var config = exports.data[type];
+ var imageEle = target.find("span");
+
+ imageEle.css({ "background-image": "url(../../images/loading.gif", "background-size": "auto" });
+ ajax.loadImage(config.url, config.refer, function (e) {
+ imageEle.css({ "background-image": "url(" + this.url + ")", "background-size": "100% 100%" });
+ }, function () {
+ imageEle.css({ "background-image": "url(../../images/loading.gif", "background-size": "auto" });
+ });
+ };
+ exports.refresh = function (target) {
+ if(target.is(".verify-code"))
+ exports.load(target);
+ else {
+ target.find(".verify-code").each(function() { exports.load($(this)); });
+ }
+ };
+ exports.init = function () {
+ $(document).on("click", ".verify-code span, .verify-code .refresh-vc", function () {
+ exports.load($(this).closest(".verify-code"));
+ });
+ };
+});
\ No newline at end of file