(function (factory) {
if (typeof exports == 'object') {
module.exports = factory(
require('jquery'),
require('common'),
require('/static/asset/common/js/constant')
);
} else if (typeof define == 'function' && define.amd) {
define(["jquery", "common", "constant"], factory);
} else {
factory(window.jQuery);
}
}(function ($, common, CONSTANT) {
"use strict";
var defaults = {
root: '',
url: "",
cache: false,
data: {},
dataType: "json",
type: "GET",
lock: true,
lockTarget: "body",
success: null,
error: null,
onSuccess: null,
onError: null,
successMessage: null,
errorMessage: null
};
function RESTAjax(option) {
this.option = $.extend(option || {}, defaults);
}
function createMessage(message, data) {
return $.isFunction(message) ? message(data) : message;
}
RESTAjax.fn = RESTAjax.prototype = {
ajax: function (option) {
var self = this;
return $.Deferred(function (defer) {
option = $.extend({}, self.option, option);
if (option.type == "POST") {
option.data = JSON.stringify(option.data);
}
$.ajax({
type: option.type,
url: option.root + option.url,
cache: option.cache,
data: option.data,
beforeSend: function (xhr) {},
headers: {
'x-authorization': "Bearer " + localStorage.getItem("token")
},
contentType: "application/json; charset=UTF-8",
dataType: option.dataType,
success: function (data, textStatus, jqXHR) {
if (data.message) {
if (data.success) {
option.success && option.success(data);
defer.resolve(data);
} else {
option.error(data);
}
} else {
option.success && option.success(data);
defer.resolve(data);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
var errMsg = "服务器连接异常!";
switch (XMLHttpRequest.status) {
case 404:
errMsg = "请求地址不存在!";
break;
case 500:
errMsg = "服务器内部错误!";
default:
break;
}
var data = {
"success": false,
"errMsg": "(" + XMLHttpRequest.status + ")" + errMsg,
"data": {
"XMLHttpRequest": XMLHttpRequest,
"textStatus": textStatus,
"errorThrown": errorThrown
}
};
if (option.error) {
option.error(data);
defer.reject(data);
} else {
console.log(JSON.stringify(data));
}
}
});
}).promise();
},
query: function (option) {
var self = this;
return $.Deferred(function (defer) {
self.ajax($.extend({}, self.option, {
type: "GET",
success: function (data) {
option.onSuccess && option.onSuccess(data);
defer.resolve(data);
},
error: function (data) {
option.onError && option.onError(data);
defer.reject(data);
}
}, option));
}).promise();
},
add: function (option) {
var self = this;
return $.Deferred(function (defer) {
self.ajax($.extend({}, self.option, {
type: "POST",
success: function (data) {
option.onSuccess && option.onSuccess(data);
defer.resolve(data);
},
error: function (data) {
option.onError && option.onError(data);
defer.reject(data);
}
}, option));
}).promise();
},
update: function (option) {
var self = this;
return $.Deferred(function (defer) {
self.ajax($.extend({}, self.option, {
type: "POST",
success: function (data) {
option.onSuccess && option.onSuccess(data);
defer.resolve(data);
},
error: function (data) {
option.onError && option.onError(data);
defer.reject(data);
}
}, option));
}).promise();
},
del: function (option) {
var self = this;
return $.Deferred(function (defer) {
self.ajax($.extend({}, self.option, {
type: "POST",
success: function (data) {
option.onSuccess && option.onSuccess(data);
defer.resolve(data);
},
error: function (data) {
option.onError && option.onError(data);
defer.reject(data);
}
}, option));
}).promise();
},
action: function (option) {
var self = this;
return $.Deferred(function (defer) {
self.ajax($.extend({}, self.option, {
success: function (data) {
option.onSuccess && option.onSuccess(data);
defer.resolve(data);
},
error: function (data) {
option.onError && option.onError(data);
defer.reject(data);
}
}, option));
}).promise();
},
custom: function (option) {
var self = this;
return $.Deferred(function (defer) {
self.ajax($.extend({}, self.option, {
success: function (data) {
option.onSuccess && option.onSuccess(data);
defer.resolve(data);
},
error: function (data) {
option.onError && option.onError(data);
defer.reject(data);
}
}, option));
}).promise();
}
};
window.RESTAjax = $.restAjax = new RESTAjax;
return $;
}));