Light12306/Mobile12306New/js/public.js

482 lines
13 KiB
JavaScript
Raw Normal View History

2014-08-19 16:11:37 +08:00
var WIN = window;
var DOC = document;
2014-09-09 12:41:28 +08:00
var IsTouch = 'ontouchstart' in WIN,
UA = WIN.navigator.userAgent,
IsAndroid = (/Android|HTC/i.test(UA) || !!(WIN.navigator['platform'] + '').match(/Linux/i)), /* HTC Flyer平板的UA字符串中不包含Android关键词 */
IsIPad = !IsAndroid && /iPad/i.test(UA),
IsIPhone = !IsAndroid && /iPod|iPhone/i.test(UA),
IsIOS = IsIPad || IsIPhone,
IsWindowsPhone = /Windows Phone/i.test(UA),
IsIEMobile = /IEMobile/i.test(UA),
IsIE = !!DOC.all;
2014-08-19 16:11:37 +08:00
var SKIPVC = true;
var hostApi = "12306.cn/otsweb";
var baseProxyUrl = "/12306/proxy.php";
// var baseProxyUrl = "/12306/";
var isMobile = true;
2014-08-21 13:42:42 +08:00
var isIos = typeof(window.__gChrome) != 'undefined' || (typeof(liebaoExtentions) != 'undefined' && liebaoExtentions.isWebviewLiebao && liebaoExtentions.isWebviewLiebao());
var isAndLiebao = typeof(liebaoExtentions) != 'undefined';
2014-08-19 16:11:37 +08:00
var isWebApp = false;
2014-08-21 13:42:42 +08:00
DOC.addEventListener("mobileSupportInitialized", function() {
2014-08-19 16:11:37 +08:00
isWebApp = true;
});
var Public = {
2014-08-21 13:42:42 +08:00
init: function() {
2014-09-03 19:25:32 +08:00
window.onbeforeunload = function() {
2014-09-02 19:09:24 +08:00
Client.refreshEnd();
}
2014-09-03 19:25:32 +08:00
window.onunload = function() {
Client.refreshEnd();
2014-09-02 19:09:24 +08:00
}
2014-09-01 13:50:43 +08:00
Public.setHis();
cn12306.getLocalTrainNos();
2014-08-22 18:00:59 +08:00
// 刷新验证码
2014-09-05 14:29:49 +08:00
$('#randcodeimg').on('click', function(e) {
2014-08-22 18:00:59 +08:00
cn12306.recode();
return false;
});
2014-08-21 13:42:42 +08:00
$('.query_box .query_bigcheck').live('click', function() {
$('.query_bigcheck', $(this).parent('.query_box')).removeClass('query_bigcheck_checked');
2014-08-19 16:11:37 +08:00
// $('.query_bigcheck input[type=radio]',$(this).parent('.query_box')).removeAttr('checked');
2014-08-21 13:42:42 +08:00
$('input[type=radio]', this).attr('checked', 'checked');
2014-08-19 16:11:37 +08:00
$(this).addClass('query_bigcheck_checked');
});
2014-08-21 13:42:42 +08:00
2014-09-01 13:50:43 +08:00
$('[data-fn="back"]').on('click', function() {
2014-08-21 13:42:42 +08:00
window.history.back();
});
2014-09-02 12:03:50 +08:00
$('.query_box [type=checkbox]').on('change', function(e) {
2014-09-01 13:50:43 +08:00
if ($(this).closest('.query_box').find('[type=checkbox][value="all"]').length == 0) {
return;
}
var $checkbox = $(this).closest('.query_box').find('[type=checkbox]'),
$allBtn = $(this).closest('.query_box').find('[type=checkbox][value="all"]');
2014-09-02 12:03:50 +08:00
// alert('change');
2014-09-01 13:50:43 +08:00
if ($(this).val() == 'all') {
if ($(this).prop('checked')) {
$checkbox.prop('checked', true);
} else {
$checkbox.prop('checked', false);
}
} else {
var num = $(this).closest('.query_box').find('[type=checkbox]:checked').length;
if ($allBtn.prop('checked')) {
num--;
}
if (num == $checkbox.length - 1) {
$allBtn.prop('checked', true);
} else {
$allBtn.prop('checked', false);
}
}
2014-09-02 20:02:55 +08:00
});
2014-09-03 19:25:32 +08:00
$('.loginout').live('click', function() {
2014-09-02 20:02:55 +08:00
var url = $(this).attr('data-url');
// alert(url);
2014-09-03 19:25:32 +08:00
if (Public.isLogin) {
Public.confirm('确定退出登录?', function() {
cn12306.loginOut(function() {
2014-09-02 22:52:30 +08:00
location.href = url;
});
});
2014-09-03 19:25:32 +08:00
} else {
2014-09-02 20:02:55 +08:00
location.href = url;
}
});
2014-09-03 19:25:32 +08:00
$('.checklogin').live('click', function() {
2014-09-02 22:52:30 +08:00
var url = $(this).attr('data-url'),
2014-09-03 19:25:32 +08:00
text = $(this).attr('data-text') || '请先登录';
if (!Public.isLogin) {
Public.confirm(text, function() {
2014-09-02 22:52:30 +08:00
location.href = url;
});
return false;
}
});
2014-09-04 20:27:55 +08:00
//全局外部链接跳转
if(bootStrap.isAndroid || bootStrap.isIos){
$(".jump_link").live('click', function(){
2014-09-04 20:27:55 +08:00
bootStrap.open_url($(this).attr('rel'));
});
2014-09-04 20:27:55 +08:00
}else{
$(".jump_link").hide();
}
2014-09-01 13:50:43 +08:00
},
user: '',
isLogin: false,
checkNum: 0,
checkLogin: function(loginCB, noLoginCB, errorCB) {
Public.checkNum++;
cn12306.isUserLogined(function() {
Public.isLogin = true;
2014-09-02 20:02:55 +08:00
$('.loginout').show();
2014-09-01 13:50:43 +08:00
if (sessionStorage.getItem('user')) {
Public.user = sessionStorage.getItem('user');
} else if (localStorage.getItem('useraccount')) {
Public.user = (JSON.parse(localStorage.getItem('useraccount'))['username']);
}
Public.setHis();
$('body').removeClass('nologin');
$('#login_tip').hide();
if (typeof loginCB == 'function') {
loginCB();
}
}, function() {
sessionStorage.clear();
Public.isLogin = false;
2014-09-02 20:02:55 +08:00
$('.loginout').hide();
2014-09-01 13:50:43 +08:00
$('body').addClass('nologin');
$('#login_tip').show();
if (typeof noLoginCB == 'function') {
noLoginCB();
}
}, function() {
Public.isLogin = false;
2014-09-03 19:25:32 +08:00
2014-09-02 20:02:55 +08:00
$('.loginout').hide();
2014-09-01 13:50:43 +08:00
$('body').addClass('nologin');
$('#login_tip').show();
if (Public.checkNum < 2) {
Public.checkLogin(loginCB, noLoginCB, errorCB);
} else {
errorCB();
}
});
2014-08-21 13:42:42 +08:00
},
his: {
2014-09-01 13:50:43 +08:00
querys: [],
queryObjs: {}
2014-08-21 13:42:42 +08:00
},
2014-09-01 13:50:43 +08:00
setHis: function() {
2014-08-21 13:42:42 +08:00
Public.his.querys = Public.getHisQuery();
for (var i = 0, len = Public.his.querys.length; i < len; i++) {
Public.his.queryObjs[Public.his.querys[i][0]] = Public.getQueryData(Public.his.querys[i][1]);
};
2014-08-19 16:11:37 +08:00
},
/*
* 弹出提示
2014-08-21 13:42:42 +08:00
*
2014-08-19 16:11:37 +08:00
* @param {string} 弹出提示文字
*
*/
2014-08-21 13:42:42 +08:00
toast: function(text, ms) {
2014-08-19 16:11:37 +08:00
if (!text) {
return false;
}
2014-08-21 13:42:42 +08:00
var dom = $('<div class="public_toast">' + text + '</div>');
2014-08-19 16:11:37 +08:00
var ms = ms || 1500;
$('body').append(dom);
2014-08-21 13:42:42 +08:00
setTimeout(function() {
2014-08-19 16:11:37 +08:00
dom.addClass('public_toast_show');
2014-08-21 13:42:42 +08:00
}, 10);
setTimeout(function() {
2014-08-19 16:11:37 +08:00
dom.removeClass('public_toast_show');
2014-08-21 13:42:42 +08:00
dom.on('webkitTransitionEnd', function() {
2014-08-19 16:11:37 +08:00
dom.remove();
});
2014-08-21 13:42:42 +08:00
}, ms);
2014-08-19 16:11:37 +08:00
},
/*
* 弹出层
* @param {string} 弹出提示文字
* @param {object} 弹层按钮 {'sure':'确定','cancel':'取消'}
*
*/
popHtml: function(text, btns) {
var html = "",
dom = DOC['createElement']('div'),
btnsHtml = '';
if (typeof btns == 'object') {
for (var k in btns) {
btnsHtml += '<a href="javascript:;" data-val="' + k + '">' + btns[k] + '</a>';
}
}
if (btnsHtml == '') {
btnsHtml = '<div class="public_btns"><a href="javascript:;" data-type="sure">确定</a></div>';
} else {
btnsHtml = '<div class="public_btns">' + btnsHtml + '</div>'
}
dom['className'] = 'public_layer';
html += '<div class="public_pop"><p>' + text + '</p>' + btnsHtml + '</div>';
dom.innerHTML = html;
document.getElementsByTagName('body')[0].appendChild(dom);
setTimeout(function() {
$('.public_pop', dom).addClass('public_pop_show');
}, 0);
$(dom).on('touchmove', function(e) {
e.preventDefault();
return false;
});
return $(dom);
},
/*
* 模拟 alert 弹出层
* 依赖 Public.popHtml()
*
* @param {string} 弹出提示文字
* @param {function} 点击确定后的回调 可选
*
*/
alert: function(text, callback) {
var dom = Public.popHtml(text, {
'sure': '确定'
});
$('.public_btns a', dom).on('click', function() {
dom.remove();
if (typeof callback == 'function') {
callback();
}
});
},
2014-09-01 13:50:43 +08:00
showLoading: function(text) {
var text = !!text ? '&nbsp;&nbsp;' + text : '',
dom = $('<div class="loadingBox"><div class="loading"><i class="icon_loading"></i><span>' + text + '</span></div></div>');;
2014-08-21 16:32:56 +08:00
$('body').append(dom);
return dom;
},
2014-09-01 13:50:43 +08:00
hideLoading: function(dom) {
if (!!dom) {
2014-08-21 16:32:56 +08:00
dom.remove();
2014-09-01 13:50:43 +08:00
} else {
2014-08-21 16:32:56 +08:00
$('.loadingBox').remove();
}
},
2014-08-19 16:11:37 +08:00
/*
* 模拟 confirm 弹出层
* 依赖 Public.popHtml()
*
* @param {string} 弹出提示文字
* @param {function} 点击确定后的回调 可选
*
*/
2014-09-09 12:41:28 +08:00
confirm: function(text, callback, sureBtn,cancelCallback) {
2014-08-19 16:11:37 +08:00
var dom = Public.popHtml(text, {
'cancel': '取消',
2014-09-01 13:50:43 +08:00
'sure': sureBtn || '确定'
2014-08-19 16:11:37 +08:00
});
$('.public_btns a', dom).on('click', function() {
var type = $(this).attr('data-val');
dom.remove();
if (typeof callback == 'function' && type == "sure") {
callback();
2014-09-09 12:41:28 +08:00
}else if(typeof cancelCallback == 'function' && type == "cancel"){
cancelCallback();
2014-08-19 16:11:37 +08:00
}
});
2014-08-21 13:42:42 +08:00
},
/**
* 根据传入的query字符串返回键值对形式的对象
* @param {String} queryString query字符串
* @return {KeyValueObject}
*/
getQueryData: function(queryString) {
/* 去掉字符串前面的"?",并把&amp;转换为& */
queryString = queryString.replace(/^\?+/, '').replace(/&amp;/, '&');
var querys = queryString.split('&'),
i = querys.length,
_URLParms = {};
while (i--) {
item = querys[i].split('=');
if (item[0]) {
var value = item[1] || '';
try {
value = decodeURIComponent(value);
} catch (e) {
value = unescape(value);
}
_URLParms[decodeURIComponent(item[0])] = value;
}
}
return _URLParms;
},
/**
* 参数对象转为查询字符串片段
*/
objToQueryString: function(obj) {
var result = [],
key, value, i;
for (key in obj) {
value = obj[key];
if (value instanceof Array) {
for (i = value.length; i--;) {
result.push(key + '[]=' + encodeURIComponent(value[i]));
}
} else {
result.push(key + ('' === value ? '' : ('=' + encodeURIComponent(value))));
}
}
return result.join('&');
},
hasArrObjKey: function(arr, key, val) {
2014-09-01 13:50:43 +08:00
var b = -1;
2014-08-21 13:42:42 +08:00
for (var i = 0, len = arr.length; i < len; i++) {
2014-09-01 13:50:43 +08:00
if (arr[i][key] == val) {
b = i;
2014-08-21 13:42:42 +08:00
break;
}
};
return b;
},
getHisQuery: function() {
2014-09-01 13:50:43 +08:00
var hisList = localStorage.getItem(Public.user + 'hisQuery');
2014-08-21 13:42:42 +08:00
if (!hisList) {
return [];
}
return JSON.parse(hisList);
},
2014-09-01 13:50:43 +08:00
setHisQuery: function(key, val) {
2014-08-21 13:42:42 +08:00
var hisList = Public.getHisQuery(),
2014-09-01 13:50:43 +08:00
idx = Public.hasArrObjKey(hisList, 0, key);
if (idx != -1) {
hisList.splice(idx, 1);
2014-08-21 13:42:42 +08:00
}
2014-09-01 13:50:43 +08:00
hisList.unshift([key, val]);
if (hisList.length > 6) {
2014-08-21 13:42:42 +08:00
hisList.length == 6;
}
2014-09-01 13:50:43 +08:00
localStorage.setItem(Public.user + 'hisQuery', JSON.stringify(hisList));
2014-08-21 13:42:42 +08:00
},
showInnerPage: function(id) {
2014-09-01 13:50:43 +08:00
$('.fixed_box').removeClass('fixed_box_show');
2014-08-21 13:42:42 +08:00
$('#' + id).addClass('fixed_box_show');
$('#wrap').hide();
2014-09-03 19:25:32 +08:00
window.scrollTo(0, 0); //切换页面后,默认执行一次返回顶部
2014-08-21 13:42:42 +08:00
},
hideInnerPage: function() {
$('.fixed_box').removeClass('fixed_box_show');
$('#wrap').show();
},
2014-08-19 16:11:37 +08:00
2014-09-01 13:50:43 +08:00
formatString: 'yyyy年M月d日',
2014-08-21 13:42:42 +08:00
setDate: function(date) {
2014-09-01 13:50:43 +08:00
if ($('#calendar').length == 0) {
2014-08-21 13:42:42 +08:00
return false;
}
var date = date || (new Date()).date;
$('#start_date').html(date.format(Public.formatString) + ' ' + date.day(3));
$('#start_date_val').val(date.format('yyyy-MM-dd'));
if (!Public.calendar) {
Public.calendar = new DateComponent({
wrap: $('#calendar'),
curDate: date,
checkCallback: Public.dateChangeCallback
});
}
$('#check_left,#check_right').on('click', function() {
var checkDate;
if ($(this).attr('id') == 'check_left') {
checkDate = Public.calendar.dayPrev();
} else {
checkDate = Public.calendar.dayNext();
}
if (checkDate) {
$('#start_date').html(checkDate.format(Public.formatString) + ' ' + checkDate.day(3));
$('#start_date_val').val(checkDate.format('yyyy-MM-dd'));
}
});
$('#start_date').on('click', function() {
Public.showInnerPage('date_box');
});
$('[name=type]').on('change', function() {
var val = $('[name=type]:checked').val();
if (val == 1) {
Public.calendar.maxDate = Public.calendar._TODAY.addDays(19);
if (Public.calendar.curDate.date.getTime() > Public.calendar.maxDate.date.getTime()) {
Public.calendar.curDate = Public.calendar.maxDate;
2014-08-21 16:32:56 +08:00
Public.calendar.checkCallback(Public.calendar.curDate);
// Public.dateChangeCallback(Public.calendar.curDate);
2014-08-21 13:42:42 +08:00
}
Public.calendar.setBasic();
} else if (val == 2) {
Public.calendar.maxDate = Public.calendar._TODAY.addDays(29);
Public.calendar.setBasic();
}
});
},
dateChangeCallback: function(date) {
var time = date.date.getTime();
if (time >= Public.calendar.minDate.date.getTime() && time <= Public.calendar.maxDate.date.getTime()) {
$('#date_tip').hide();
Public.hideInnerPage('date_box');
$('#start_date').html(Public.calendar.curDate.format(Public.formatString) + ' ' + Public.calendar.curDate.day(3));
$('#start_date_val').val(Public.calendar.curDate.format('yyyy-MM-dd'));
} else {
2014-09-03 19:25:32 +08:00
var offset = $('a.cur', Public.calendar.wrap).offset(),
str = '';
str = '<p>' + date.format('M月d日') + '不在预售期</p><a href="remind.html?date=' + encodeURIComponent(date.format('yyyy-MM-dd'));
if($('#startStationCode').length > 0 && $('#startStationCode').val()!=''){
str += '&from='+$('#startStationCode').val();
}
if($('#endStationCode').length > 0 && $('#endStationCode').val()!=''){
str += '&to='+$('#endStationCode').val();
}
str += '" class="btn btn_m btn_success">预约提醒</a>';
2014-08-21 13:42:42 +08:00
$('#date_tip').css({
'left': offset.left,
'top': offset.top,
'display': 'block'
2014-09-03 19:25:32 +08:00
}).html(str);
2014-08-21 13:42:42 +08:00
if (offset.left / window.innerWidth > 0.75) {
$('#date_tip').attr('class', 'tip_small tip_right');
} else if (offset.left / window.innerWidth < 0.15) {
$('#date_tip').attr('class', 'tip_small tip_left');
} else {
$('#date_tip').attr('class', 'tip_small');
}
}
},
updateIos : function(url){
window.location.href = url || 'https://itunes.apple.com/cn/app/id641522896' ;
},
updateAndroid : function(url){
window.location.href = url || 'http://dl.liebao.cn/android/fast/cheetah_2.11.0.apk';
2014-08-21 13:42:42 +08:00
}
2014-08-21 13:42:42 +08:00
}