AngularJS有种机制叫做拦截器(interceptor),它是$http扩展点,类似ASP.NET MVC的过滤器filter机制,对每个$http请求的发送和接收过程进行过滤。

  $httpProvider 中有一个 interceptors 数组,定义一个工厂服务,并添加到该数组中。

 module.config(['$httpProvider', function($httpProvider) {

    $httpProvider.interceptors.push('myInterceptor');

  }]);

  定义factory,返回一个对象,有属性request,requestError,response,responseError属性,对每个请求及其进行统一处理,对每次请求都添加上身份认证信息,构造附加的请求地址前缀等,对响应如果有错误或异常,进行统一处理,或弹出对话框。

module.factory('myInterceptor',
['$q', '$log', '$injector', 'loginContext', 'eventAggregator', 'maintainUtil',
function ($q, $log, $injector, loginContext, eventAggregator, maintainUtil) {
'use strict'; var apiToken = loginContext.apiToken;
var tokenType = loginContext.tokenType;
var webApiHostUrl = loginContext.apiHost + "/api/v1"; return {
//token save to services for further usage
tokenType: tokenType,
apiToken: apiToken,
webApiHostUrl: webApiHostUrl, // On request success
request: function (config) {
if (config.isWebApiRequest && !config.isPlugin) {
config.url = (config.mkApiUrl || webApiHostUrl) + config.url;
config.headers = config.headers || {};
config.headers.Authorization = tokenType + ' ' + (config.mkToken || apiToken);
var specificOfficeId = Mres.specificOfficeUtil.getOfficeId();
if (specificOfficeId) {
config.headers["specific-office-id"] = specificOfficeId;
}
} else if (config.handleApiRequest) {
config = config.handleApiRequest(config);
}
return config;
},
// On request failure
requestError: function (rejection) {
$log.error(rejection); // Contains the data about the error on the request.
// Return the promise rejection.
return $q.reject(rejection);
},
// On response failture
responseError: function (response) {
$log.error(response); // Contains the data about the error.
if (response.status === 401) {
//window.location = '/logoff';
Mres.logOff();
} else if (response.data) {
if (response.data.name == 'MenantInactiveException') { aresMaintainUtil.goToMenantInactivePage();
}
eventAggregator.publish(eventAggregator.events.ApiErrorHappened, response, 'myInterceptor');
} else if (response.status === 0) {
var isSaasApi = true;
if (response.config && response.config.url.indexOf('//marketcenter') > -1) {
isSaasApi = false;
}
if (isSaasApi) {
aresMaintainUtil.ensureInMaintainMode().then(function (isInMaintainMode) {
if (isInMaintainMode) {
mresMaintainUtil.goToMaintainPage();
}
});
}
}
// Return the promise rejection.
return $q.reject(response);
}
};
}])

  适用于对每次请求和响应附加统一功能或数据。

AngularJS AOP 实例的更多相关文章

  1. angularjs directive 实例 详解

    前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的mo ...

  2. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  3. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  4. 第一个AngularJS表达式实例

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. Spring AOP实例——异常处理和记录程序执行时间

    实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...

  6. Spring Boot1.5.4 AOP实例

    原文:https://github.com/x113773/testall/issues/12 1. 还是首先添加依赖(使用当前springboot的默认版本)```<dependency> ...

  7. Java框架spring 学习笔记(十二):aop实例操作

    使用aop需要在网上下载两个jar包: aopalliance.jar aspectjweaver.jar 为idea添加jar包,快捷键ctrl+shift+alt+s,打开添加jar包的对话框,将 ...

  8. AngularJS:实例

    ylbtech-AngularJS:实例 1.返回顶部 1. AngularJS 实例 实例 您可以在线编辑实例,然后点击按钮查看结果. AngularJS 实例 <div ng-app=&qu ...

  9. Spring学习十四----------Spring AOP实例

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

随机推荐

  1. MySQL中varchar与char区别

    MySQL中varchar与char区别(转) MySQL中varchar最大长度是多少? 一. varchar存储规则: 4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字 ...

  2. Snownlp

    from snownlp import SnowNLP text='宝贝自拍很帅!!!注意休息-' s=SnowNLP(text) #分词print(s.words) #词性for tag in s. ...

  3. 状态机学习(三)解析JSON1

    来自 从零开始的 JSON 库教程 从零开始教授如何写一个符合标准的 C 语言 JSON 库 作者 Milo Yip https://zhuanlan.zhihu.com/json-tutorial ...

  4. 关于部署传统的Dynamic Web项目

    现在大部分都是采用maven构建的项目,但是偶尔也会遇到一些较老的项目,采用的是传统的动态Web项目. 我最近碰到这样一个项目,项目用的jar包都放在了WEB-INF/lib目录下.之前的人采用的部署 ...

  5. Python多进程并发操作进程池Pool

    目录: multiprocessing模块 Pool类 apply apply_async map close terminate join 进程实例 multiprocessing模块 如果你打算编 ...

  6. python中将两个数组压缩成一个数组

    我们有时候会遇到一个问题将两个数组一一对应的压缩起来: 两个都是字符串: 列表解析[''.join(i) for i in zip(list_1, list_2)] map(lambda x,y:x+ ...

  7. python第一类对象,闭包,迭代器

    一.第一类对象 第一类对象 -> 函数名 -> 变量名 1.特征:     函数对象可以像变量一样进行赋值     还可以作为列表的元素进行使用     还可以作为返回值返回     还可 ...

  8. WPF中的路由事件(转)

    出处:https://www.cnblogs.com/JerryWang1991/archive/2013/03/29/2981103.html 最近因为工作需要学习WPF方面的知识,因为以前只关注的 ...

  9. jq无法获取a标签动态id

    起初a标签是这样写的<a href="javascript:void(0)"  id="${menu.id}" value="${menu.na ...

  10. linux复制文件并修改文件名

    #!/bin/bash #复制/casnw/backup/db203oradata/目录下的所有后缀名为dmp的文件拷贝到/casnw/backup/dbmonthbak 目录下cp -f /casn ...