var WIN = window; var DOC = document; var SKIPVC = true; var hostApi = "12306.cn/otsweb"; var baseProxyUrl = "/12306/proxy.php"; // var baseProxyUrl = "/12306/"; var isMobile = true; var isIos = typeof(window.__gChrome) != 'undefined' || (typeof(liebaoExtentions) != 'undefined' && liebaoExtentions.isWebviewLiebao && liebaoExtentions.isWebviewLiebao()); var isAndLiebao = typeof(liebaoExtentions) != 'undefined'; var isWebApp = false; DOC.addEventListener("mobileSupportInitialized", function() { isWebApp = true; }); var Public = { init: function() { window.onbeforeunload = function() { Client.refreshEnd(); } window.onunload = function() { Client.refreshEnd(); } Public.setHis(); cn12306.getLocalTrainNos(); // 刷新验证码 $('#randcodeimg').live('click', function(e) { cn12306.recode(); return false; }); $('.query_box .query_bigcheck').live('click', function() { $('.query_bigcheck', $(this).parent('.query_box')).removeClass('query_bigcheck_checked'); // $('.query_bigcheck input[type=radio]',$(this).parent('.query_box')).removeAttr('checked'); $('input[type=radio]', this).attr('checked', 'checked'); $(this).addClass('query_bigcheck_checked'); }); $('[data-fn="back"]').on('click', function() { window.history.back(); }); $('.query_box [type=checkbox]').on('change', function(e) { 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"]'); // alert('change'); 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); } } }); $('.loginout').live('click', function() { var url = $(this).attr('data-url'); // alert(url); if (Public.isLogin) { Public.confirm('确定退出登录?', function() { cn12306.loginOut(function() { location.href = url; }); }); } else { location.href = url; } }); $('.checklogin').live('click', function() { var url = $(this).attr('data-url'), text = $(this).attr('data-text') || '请先登录'; if (!Public.isLogin) { Public.confirm(text, function() { location.href = url; }); return false; } }); }, user: '', isLogin: false, checkNum: 0, checkLogin: function(loginCB, noLoginCB, errorCB) { Public.checkNum++; cn12306.isUserLogined(function() { Public.isLogin = true; $('.loginout').show(); 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; $('.loginout').hide(); $('body').addClass('nologin'); $('#login_tip').show(); if (typeof noLoginCB == 'function') { noLoginCB(); } }, function() { Public.isLogin = false; $('.loginout').hide(); $('body').addClass('nologin'); $('#login_tip').show(); if (Public.checkNum < 2) { Public.checkLogin(loginCB, noLoginCB, errorCB); } else { errorCB(); } }); }, his: { querys: [], queryObjs: {} }, setHis: function() { 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]); }; }, /* * 弹出提示 * * @param {string} 弹出提示文字 * */ toast: function(text, ms) { if (!text) { return false; } var dom = $('
' + text + '
'); var ms = ms || 1500; $('body').append(dom); setTimeout(function() { dom.addClass('public_toast_show'); }, 10); setTimeout(function() { dom.removeClass('public_toast_show'); dom.on('webkitTransitionEnd', function() { dom.remove(); }); }, ms); }, /* * 弹出层 * @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 += '' + btns[k] + ''; } } if (btnsHtml == '') { btnsHtml = '
确定
'; } else { btnsHtml = '
' + btnsHtml + '
' } dom['className'] = 'public_layer'; html += '

' + text + '

' + btnsHtml + '
'; 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(); } }); }, showLoading: function(text) { var text = !!text ? '  ' + text : '', dom = $('
' + text + '
');; $('body').append(dom); return dom; }, hideLoading: function(dom) { if (!!dom) { dom.remove(); } else { $('.loadingBox').remove(); } }, /* * 模拟 confirm 弹出层 * 依赖 Public.popHtml() * * @param {string} 弹出提示文字 * @param {function} 点击确定后的回调 (可选) * */ confirm: function(text, callback, sureBtn) { var dom = Public.popHtml(text, { 'cancel': '取消', 'sure': sureBtn || '确定' }); $('.public_btns a', dom).on('click', function() { var type = $(this).attr('data-val'); dom.remove(); if (typeof callback == 'function' && type == "sure") { callback(); } }); }, /** * 根据传入的query字符串返回键值对形式的对象 * @param {String} queryString query字符串 * @return {KeyValueObject} */ getQueryData: function(queryString) { /* 去掉字符串前面的"?",并把&转换为& */ queryString = queryString.replace(/^\?+/, '').replace(/&/, '&'); 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) { var b = -1; for (var i = 0, len = arr.length; i < len; i++) { if (arr[i][key] == val) { b = i; break; } }; return b; }, getHisQuery: function() { var hisList = localStorage.getItem(Public.user + 'hisQuery'); if (!hisList) { return []; } return JSON.parse(hisList); }, setHisQuery: function(key, val) { var hisList = Public.getHisQuery(), idx = Public.hasArrObjKey(hisList, 0, key); if (idx != -1) { hisList.splice(idx, 1); } hisList.unshift([key, val]); if (hisList.length > 6) { hisList.length == 6; } localStorage.setItem(Public.user + 'hisQuery', JSON.stringify(hisList)); }, showInnerPage: function(id) { $('.fixed_box').removeClass('fixed_box_show'); $('#' + id).addClass('fixed_box_show'); $('#wrap').hide(); window.scrollTo(0, 0); //切换页面后,默认执行一次返回顶部 }, hideInnerPage: function() { $('.fixed_box').removeClass('fixed_box_show'); $('#wrap').show(); }, formatString: 'yyyy年M月d日', setDate: function(date) { if ($('#calendar').length == 0) { 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; Public.calendar.checkCallback(Public.calendar.curDate); // Public.dateChangeCallback(Public.calendar.curDate); } 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 { var offset = $('a.cur', Public.calendar.wrap).offset(), str = ''; str = '

' + date.format('M月d日') + '不在预售期

预约提醒'; $('#date_tip').css({ 'left': offset.left, 'top': offset.top, 'display': 'block' }).html(str); 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'); } } } }