MCSManager/public/common/js/response.js
2020-01-27 14:27:38 +08:00

156 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//响应层
(function () {
var DEBUG = false;
var TOKEN_NAME = '_T0K_N';
var RES = window.RES = function () {
return null;
};
RES.TOKEN_NAME = TOKEN_NAME;
RES.TOKEN = null;
RES.getToken = function (callback) {
//同源策略可以防止其他域对这里发送一个Ajax请求.
var _url = MCSERVER.URL("./token?_LoveYouMaster_Time=" + Date.parse(new Date()));
$.get(_url, function (data, status) {
data = JSON.parse(data);
if (data.hasOwnProperty('ResponseValue')) {
MCSERVER.username = data['ResponseValue'].username;
callback(data['ResponseValue'].token);
} else {
callback(undefined);
}
});
}
RES.tokenAjax = function (parameter) {
window.RES.getToken(function (token) {
parameter['token'] = token;
});
}
RES.ajax = function (parameter) {
if (!parameter['data']) parameter['data'] = {};
var tokenV = parameter['token'] || RES.TOKEN;
if (tokenV != null) parameter['url'] += '?' + TOKEN_NAME + '=' + tokenV;
$.ajax({
type: parameter['type'] || "POST",
url: MCSERVER.URL(parameter['url']),
data: parameter['data'], //具体实例化
timeout: parameter['timeout'] || 10000,
success: function (data, textStatus) {
try {
data = JSON.parse(data);
if (typeof (data) == 'object') {
if (data['ResponseKey']) {
window.MI.routeOn(data['ResponseKey'], data['ResponseValue']);
}
}
} catch (e) {
DEBUG && console.log("$.ajax 响应数据非一个JSON对象");
} finally {
if (parameter['success']) parameter['success'](data, textStatus);
}
},
error: function (XML, textStatus, errorThrown) {
if (parameter['error']) parameter['error'](XML);
DEBUG && console.log('Ajax ERROR 回调触发');
DEBUG && console.log(XML);
DEBUG && console.log(textStatus);
DEBUG && console.log(errorThrown);
DEBUG && console.log(XML.responseText);
window.MI.routeOn('ajax/error', XML);
},
processData: parameter['processData'] == false || true,
traditional: parameter['traditional'] == false || true,
cache: parameter['cache'] || false
});
}
RES.redirectHTML = function (url, key, body, callback) {
//静态文件均在 public 目录下动态文件则在不同API接口
var _url = MCSERVER.URL('./public/' + url);
//响应事件函数
function responseCallback(response, status, xhr) {
if (status != 'success')
TOOLS.pushMsgWindow("[ " + status + " ] 由于网络或权限问题,请求的网页无法成功!");
callback && callback();
}
window.scrollTo(0, 0);
if (key == null) {
//不需要API数据的单页
$("#ConsoleMain").load(_url, responseCallback);
} else {
WS.sendMsg(key, body || '', function () {
$("#ConsoleMain").load(_url, responseCallback);
});
}
}
MI.listener("RedirectPage", function () {
//自动菜单更改
MCSERVER.autoColmDo();
});
RES.redirectPage = function (url, key, body, callback) {
var showUrl = url.replace(".", "");
var showKey = key || "Null"
TOOLS.setHeaderTitle(["正在加载...."].join(" "));
ToolsLoadingStart(function () {
MI.rOn('onend');
PageLoading();
//替換掉原先存在的函数 防止新的单页没有这些函数而导致代码二次执行
MI.rListener('onend', function () { });
MI.rListener('onload', function () { });
//触发页面切换事件
MI.on('RedirectPage', url);
RES.redirectHTML(url, key, body, function () {
TOOLS.setHeaderTitle(["", showUrl, "|", showKey].join(" "));
MI.on('page/live');
//赋予的单页刷新
PAGE.refresh = function () {
RES.redirectPage(url, key, body, callback);
}
ToolsLoadingEnd();
MI.rOn('onload');
callback && callback();
});
});
}
var PageMain = $('#ConsoleMain');
var ToolsLoading = $('#ToolsLoading'); //进度条
var ToolsPageLoading = $("#ToolsPageLoading"); //进度条容器
function ToolsLoadingStart(callback) {
ToolsLoading.css("width", "0%");
ToolsPageLoading.css("display", "block");
PageMain.stop(true, true).animate({
'opacity': '0'
}, 150, callback);
}
function PageLoading() {
ToolsLoading.css("width", "70%");
}
function ToolsLoadingEnd() {
ToolsLoading.css("width", "100%");
PageMain.stop(true, true).animate({
'opacity': '1'
}, 150, function () {
setTimeout(function () {
ToolsPageLoading.css("display", "none");
}, 150);
});
}
})();