85 lines
3.0 KiB
JavaScript
85 lines
3.0 KiB
JavaScript
(function ($, oldFunction) {
|
|
$.param = function (a, traditional) {
|
|
var s = oldFunction.apply(oldFunction, [a, traditional]);
|
|
return s.replace(/\+/g, '%20');
|
|
};
|
|
})(jQuery, jQuery.param);
|
|
|
|
(function () {
|
|
'use strict';
|
|
angular
|
|
.module('smart.request', [])
|
|
.service('smartRequest', smartRequest);
|
|
|
|
smartRequest.$inject = ['$http', 'Notification', '$rootScope'];
|
|
|
|
function smartRequest($http, Notification, $rootScope) {
|
|
|
|
this.post = function (method, data, callback, error_callback, server_error_callback) {
|
|
var xsrf = $.param(data, true);
|
|
|
|
$http({
|
|
method: 'POST',
|
|
url: $rootScope.apiUrl + method,
|
|
timeout: 10 * 60 * 1000,
|
|
data: xsrf,
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Token': $rootScope.globals.currentUser ? $rootScope.globals.currentUser.token : '' }
|
|
}).then(function successCallback(response) {
|
|
if (response.data.status !== 'success') {
|
|
if (response.data.more) {
|
|
Notification.error(response.data.more);
|
|
}
|
|
if (response.data.message) {
|
|
Notification.error(response.data.message);
|
|
}
|
|
if (typeof error_callback !== typeof undefined) {
|
|
error_callback(response.data);
|
|
}
|
|
}
|
|
else {
|
|
if (typeof callback !== typeof undefined) {
|
|
callback(response.data);
|
|
}
|
|
}
|
|
}, function errorCallback(response) {
|
|
if (typeof server_error_callback !== typeof undefined) {
|
|
server_error_callback(response);
|
|
}
|
|
else {
|
|
Notification.error('Сервер недоступен');
|
|
}
|
|
});
|
|
};
|
|
|
|
this.get = function (method, callback, error_callback, server_error_callback) {
|
|
|
|
$http({
|
|
method: 'GET',
|
|
url: $rootScope.apiUrl + method,
|
|
timeout: 10 * 60 * 1000,
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Token': $rootScope.globals.currentUser ? $rootScope.globals.currentUser.token : '' }
|
|
}).then(function successCallback(response) {
|
|
if (response.data.status !== 'success') {
|
|
Notification.error(response.data.more);
|
|
|
|
if (typeof error_callback !== typeof undefined) {
|
|
error_callback(response.data);
|
|
}
|
|
}
|
|
else {
|
|
if (typeof callback !== typeof undefined) {
|
|
callback(response.data);
|
|
}
|
|
}
|
|
}, function errorCallback(response) {
|
|
if (typeof server_error_callback !== typeof undefined) {
|
|
server_error_callback(response);
|
|
}
|
|
else {
|
|
Notification.error('Сервер недоступен');
|
|
}
|
|
});
|
|
};
|
|
|
|
}
|
|
})(); |