You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
6.5 KiB

4 years ago
/*
* EasyCode EC管理后台(http://www.learun.cn)
* Copyright (c) 2019-present EC管理有限公司
* 创建人tobin
* 2017.03.16
* ajax操作方法
*/
(function ($, learun) {
"use strict";
var httpCode = {
success: 200,
fail: 400,
exception: 500,
nologin: 410 // 没有登录者信息
};
var exres = { code: httpCode.exception, info: '通信异常,请联系管理员!' };
function isLogin(res) {
if (res.code == learun.httpCode.nologin) {
var _topUrl = top.$.rootUrl + '/Login/Index';
switch (res.info) {
case 'nologin':
break;
case 'noip':
_topUrl += '?error=ip';
break;
case 'notime':
_topUrl += '?error=time';
break;
case 'other':
_topUrl += '?error=other';
break;
}
top.window.location.href = _topUrl;
return false;
}
return true;
}
function httpHeaders() {
var headers = {
token: $.lcoreUser.token
}
return headers;
}
$.extend(learun, {
// http 通信异常的时候调用此方法
httpErrorLog: function (msg) {
learun.log(msg);
},
// http请求返回数据码
httpCode: httpCode,
// get请求方法(异步):url地址,callback回调函数
httpAsyncGet: function (url, callback) {
$.ajax({
url: url,
headers: httpHeaders(),
type: "GET",
dataType: "json",
async: true,
cache: false,
success: function (res) {
isLogin(res);
if (res.code == learun.httpCode.exception) {
learun.httpErrorLog(res.info);
}
callback(res);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
learun.httpErrorLog(textStatus);
callback(exres);
},
beforeSend: function () {
},
complete: function () {
}
});
},
// get请求方法(异步):url地址,callback回调函数
httpAsyncGet2: function (url, param, callback) {
$.ajax({
url: url,
headers: httpHeaders(),
data: param,
type: "GET",
dataType: "json",
async: true,
cache: false,
success: function (res) {
isLogin(res);
if (res.code == learun.httpCode.exception) {
learun.httpErrorLog(res.info);
}
callback(res);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
learun.httpErrorLog(textStatus);
callback(exres);
},
beforeSend: function () {
},
complete: function () {
}
});
},
// get请求方法(同步):url地址,param参数
httpGet: function (url, param) {
var _res = {};
$.ajax({
url: url,
headers: httpHeaders(),
data: param,
type: "GET",
dataType: "json",
async: false,
cache: false,
success: function (res) {
isLogin(res);
if (res.code == learun.httpCode.exception) {
learun.httpErrorLog(res.info);
}
_res = res;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
learun.httpErrorLog(textStatus);
},
beforeSend: function () {
},
complete: function () {
}
});
return _res;
},
// post请求方法(异步):url地址,param参数,callback回调函数
httpAsyncPost: function (url, param, callback) {
$.ajax({
url: url,
headers: httpHeaders(),
data: param,
type: "POST",
dataType: "json",
async: true,
cache: false,
success: function (res) {
isLogin(res);
if (res.code == learun.httpCode.exception) {
learun.httpErrorLog(res.info);
}
callback && callback(res);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
learun.httpErrorLog(textStatus);
callback && callback(exres);
},
beforeSend: function () {
},
complete: function () {
}
});
},
// post请求方法(同步步):url地址,param参数,callback回调函数
httpPost: function (url, param, callback) {
$.ajax({
url: url,
headers: httpHeaders(),
data: param,
type: "POST",
dataType: "json",
async: false,
cache: false,
success: function (res) {
isLogin(res);
if (res.code == learun.httpCode.exception) {
learun.httpErrorLog(res.info);
}
callback(res);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
learun.httpErrorLog(textStatus);
callback(exres);
},
beforeSend: function () {
},
complete: function () {
}
});
},
// ajax 异步封装
httpAsync: function (type, url, param, callback) {
$.ajax({
url: url,
headers: httpHeaders(),
data: param,
type: type,
dataType: "json",
async: true,
cache: false,
success: function (res) {
if (!isLogin(res)) {
callback(null);
}
else if (res.code == learun.httpCode.success) {
callback(res.data);
}
else {
learun.httpErrorLog(res.info);
callback(null);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
learun.httpErrorLog(textStatus);
callback(null);
},
beforeSend: function () {
},
complete: function () {
}
});
},
// ajax 同步封装
httpSync: function (type, url, param, callback) {
$.ajax({
url: url,
headers: httpHeaders(),
data: param,
type: type,
dataType: "json",
async: false,
cache: false,
success: function (res) {
if (!isLogin(res)) {
callback(null);
}
else if (res.code == learun.httpCode.success) {
callback(res.data);
}
else {
learun.httpErrorLog(res.info);
callback(null);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
learun.httpErrorLog(textStatus);
callback(null);
},
beforeSend: function () {
},
complete: function () {
}
});
},
deleteForm: function (url, param, callback) {
learun.loading(true, '正在删除数据');
learun.httpAsyncPost(url, param, function (res) {
learun.loading(false);
if (res.code == learun.httpCode.success) {
if (!!callback) {
callback(res);
}
learun.alert.success(res.info);
}
else {
learun.alert.error(res.info);
learun.httpErrorLog(res.info);
}
layer.close(layer.index);
});
},
postForm: function (url, param, callback, msg) {
learun.loading(true, msg || '正在提交数据');
learun.httpAsyncPost(url, param, function (res) {
learun.loading(false);
if (res.code == learun.httpCode.success) {
if (!!callback) {
callback(res);
}
learun.alert.success(res.info);
layer.close(layer.index);
}
else {
learun.alert.error(res.info);
learun.httpErrorLog(res.info);
}
});
}
});
})(window.jQuery, top.learun);