angular 拦截器 和 $http公共方法

/*
* 定义拦截器,解决session过期后不自动退出的问题 added by Peter 2017.7.5
*/
/*-------------------------------begin-------------------------------------------------*/

myApp.factory("httpInterceptor", ['urlConfig',
'$q',
'$window',
function(urlConfig,$q, $window) {
return {
responseError : function(response) {
if (response.status == 500) {
showtips({
message : 'service fail!',
position : center//显示的位置,top:在顶部显示,centger:居中显示。默认top
});// 服务器异常处理
}
return $q.reject(response);
},
response : function(response) {
return response;
},
request : function(config) {
var arr, reg = new RegExp("(^|)" + "hwsso_login"
+ "=([^;]*)(;|$)");
var hwssoCookie = "";
if (arr = document.cookie.match(reg)) {
hwssoCookie = arr[2];
}
if (hwssoCookie == null || hwssoCookie == ""
|| hwssoCookie == "\"\"") {
sessionStorage.setItem('resUserROles','');//清除缓存的用户角色信息
$window.location.href = urlConfig.base+"/logout";
return;
}
return config;
},
requestError : function(config) {
return $q.reject(config);
}
};
} ]);

myApp.config([ '$httpProvider','urlConfig', function($httpProvider,urlConfig) {
console.log(urlConfig.guardingUrl);
if($ && $.urlConfig){
$.urlConfig.base=urlConfig.guardingUrl+'/';
$.urlConfig.upload=urlConfig.guardingUrl+'/';
}
$httpProvider.interceptors.push('httpInterceptor');
} ]);
/*-------------------------------end-------------------------------------------------*/

myApp.factory('guardingService', function($http) {
var factory = {};
// 公用的请求方法
factory._get = function(urlPath, params) {
return $http({
method : "GET",
url : urlPath,
params : params
});
}

factory._post = function(urlPath, data) {
return $http({
method : "POST",
url : urlPath,
data : data,
headers : {
'Content-Type' : 'application/json;charset=UTF-8'
}
});
}

factory._put = function(urlPath, data) {
return $http({
method : "PUT",
url : urlPath,
data : data,
headers : {
'Content-Type' : 'application/json;charset=UTF-8'
}
});
}

factory._delete = function(urlPath, data) {
return $http({
method : "DELETE",
url : urlPath,
data : data,
headers : {
'Content-Type' : 'application/json;charset=UTF-8'
}
});
}
factory.getSynchData = function(url, data, type) {
return $.ajax({
method : type,
data : JSON.stringify(data),
async : false,
dataType : "json",
url : url,
contentType : 'application/json;charset=UTF-8'
});
}
factory.getAynchData = function(url, data, type) {
return $.ajax({
method : type,
data : JSON.stringify(data),
async : true,
dataType : "json",
url : url,
contentType : 'application/json;charset=UTF-8'
});
}
return factory;
});

// var permissionList;
// angular.element(document).ready(function() {
// $.get("/guarding-web/userRoles", function(data) {
// permissionList = data.data;
// console.log(permissionList);
// });
// });

/*
* angular.module('AG.app').value("permissionList","").factory('abc',f){
* }
*/

/*
* 获取用户信息 sky 2017-3-25 请求范湖的数据包含 code message page
* data{UserRoles:[],userinfo:用户相关信息createTime: 1489462679000 email:
* "yaochonglin1@huawei.com" empFullNumber: "ywx459298" empNumber: "wx459298"
* fullName: null id: 175}
*/
myApp.factory('loginUserService', function($http, $rootScope,
guardingService, $state, $q, urlConfig) {
return {
queryUserInfo : function() {
var deferred = $q.defer(); // 声明延后执行,表示要去监控后面的执行
$http({
method : 'GET',
url : urlConfig.base + '/userRoles'
}).success(function(res) {
deferred.resolve(res); // 声明执行成功,即http请求数据成功,可以返回数据了
}).error(function(res) {
deferred.reject(res); // 声明执行失败,即服务器返回错误
});
return deferred.promise; // 返回承诺,这里并不是最终数据,而是访问最终数据的API
} // end query
};
});

posted @ 2017-07-25 17:12  duguangyan  阅读(239)  评论(0编辑  收藏  举报