顿搜
飞过闲红千叶,夕岸在哪
类目归类
本模板需要NProgress和Angular-MsgBox的支持,GIthub下载地址:
https://github.com/dunso/nprogress
https://github.com/dunso/Angular-MsgBox
'use strict';
var app = angular.module('app', [
'ngAnimate',
'ngCookies',
'ngStorage',
'app.controllers',
'msgbox'
])
angular.module('app.controllers', ['ngCookies'])
.factory('Dunso', function ($http, $rootScope, $state) {
var DUNSO_SERVER_URL = "https://dun.so/";
return {
//POST请求(通用AJAX方式)
post_jquery: function (api, data, success, error) {
var url = DUNSO_SERVER_URL + api;
this.isDebug(url, data, 'POST');
NProgress.start();
$.ajax({
method: "POST",
url: url,
data: JSON.stringify(data),
contentType: "application/json",
dataType: 'json',
async: false,
success: function (response) {
NProgress.done();
success(response);
},
error: function (response) {
NProgress.done();
error(response);
},
timeout: function () {
NProgress.done();
}
});
},
//POST请求(Angular方式)
post: function (api, data, success, error) {
var url = DUNSO_SERVER_URL + api;
this.isDebug(url, data, 'POST');
NProgress.start();
var request = {
'method': 'POST',
'url': url,
'headers': {
'Content-Type': 'application/json'
},
'data': data
}
$http(request).success(function (response) {
if (response.err_code == undefined) {
this.notify('服务器繁忙,请稍后重试! ','error');
}
if (success != undefined) {
NProgress.done();
success(response);
}
}).error(function (response) {
if (response.err_code != undefined) {
this.notify('网络不给力哟,请休息片刻再试试! ','warn');
}
if (error != undefined) {
NProgress.done();
error(response);
}
});
},
//GET请求(Angular方式)
get: function (api, success, error) {
var url = DUNSO_SERVER_URL + api;
this.isDebug(url,null,'GET')
NProgress.start();
$http.get(url).success(function (response) {
if (response.err_code != undefined) {
this.notify('服务器繁忙,请稍后重试! ','error');
}
if (success != undefined) {
NProgress.done();
success(res);
}
}).error(function (response) {
if (response.err_code != undefined) {
this.notify('网络不给力哟,请休息片刻再试试! ','warn');
}
if (error != undefined) {
NProgress.done();
error(response);
}
});
},
isDebug: function (url, data, method) {
if ($rootScope.DEBUG) {
console.log(method + ':' + url);
console.log('Data: ' + JSON.stringify(data));
}
return;
},
notify:function(msg,type){
$.notify({
message: msg
}, {
placement: {
from: "top",
align: "center"
},
type: type
});
}
};
})