react-admin-lte
Version:
简单封装的 AdminLTE react 类库,并包含一个编译配置。
127 lines (123 loc) • 3.18 kB
JavaScript
var Tools = {
getCloudeer: function (service, url, params, callback, onError) {
$.post('/cloudeer/get', {
service: service,
url: url,
params: params
}, function (res) {
if (res && res.errno) {
if (onError) {
onError(res);
} else {
alert(res.errText);
}
return;
}
callback(res);
}, 'json');
},
postCloudeer: function (service, url, params, callback, onError) {
$.ajax({
type: 'POST',
url: '/cloudeer/post',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify({
service: service,
url: url,
params: params
}),
success: function (res) {
if (res && res.errno) {
if (onError) {
onError(res);
} else {
alert(res.errText);
}
return;
}
callback(res);
}
});
},
formatDate: function (xdate) {
if (!xdate) return null;
if (typeof xdate === 'string') {
xdate = new Date(xdate);
}
if (typeof xdate.getDate === 'function') {
var dd = xdate.getDate();
var mm = xdate.getMonth() + 1;
var yyyy = xdate.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
return yyyy + '-' + mm + '-' + dd;
}
return null;
},
parseQuery(pkey) {
var search = window.location.search;
var args = search.substring(1).split('&');
var argsParsed = {};
var i, arg, kvp, key, value;
for (i = 0; i < args.length; i++) {
arg = args[i];
if (-1 === arg.indexOf('=')) {
argsParsed[decodeURIComponent(arg).trim()] = true;
} else {
kvp = arg.split('=');
key = decodeURIComponent(kvp[0]).trim();
value = decodeURIComponent(kvp[1]).trim();
argsParsed[key] = value;
}
}
if (pkey) {
return argsParsed[pkey];
}
return argsParsed;
},
parseRouter: function () {
var rtn = {};
var hash = window.location.hash;
rtn.path = hash.substr(1, hash.indexOf("?") > 0 ? hash.indexOf("?") - 1 : hash.length);
rtn.query = {};
if (hash.indexOf('?') > 0) {
var search = hash.substr(hash.indexOf("?"));
console.log(search);
var args = search.substring(1).split('&');
var i, arg, kvp, key, value;
for (i = 0; i < args.length; i++) {
arg = args[i];
if (-1 === arg.indexOf('=')) {
argsParsed[decodeURIComponent(arg).trim()] = true;
} else {
kvp = arg.split('=');
key = decodeURIComponent(kvp[0]).trim();
value = decodeURIComponent(kvp[1]).trim();
rtn.query[key] = value;
}
}
}
return rtn;
}
};
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
export default Tools;