1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 'use strict';
- angular.module('app', [ 'ngSanitize' ]);
- var app = angular.module('app').config([ '$controllerProvider', '$compileProvider', '$filterProvider', '$provide', function($controllerProvider, $compileProvider, $filterProvider, $provide) {
- app.controller = $controllerProvider.register;
- app.directive = $compileProvider.directive;
- app.filter = $filterProvider.register;
- app.factory = $provide.factory;
- app.service = $provide.service;
- app.constant = $provide.constant;
- app.value = $provide.value;
- } ]);
- app.factory('frameAjaxInterceptor', function($rootScope, $q, $location) {
- return {
- 'request' : function(config) {
- // 判断是否弹出等待框
- var wait = config.wait || 0;
- if (wait > 0) {
- // todo 显示提交等待框
- }
- return config;
- },
- 'requestError' : function(rejection) {
- return $q.reject(rejection);
- },
- 'response' : function(response) {
- var wait = (response.config || {}).wait || 0;
- // todo 隐藏等待框
- if (response.data.success == undefined)
- return response;
- if (response.data.success == false) {
- // todo 用更友好的提示框显示
- alert(response.data.errorMsg);
- if (response.config.error) {
- response.config.error.call(undefined);
- }
- return $q.reject(response);
- }
- return response;
- },
- 'responseError' : function(rejection) {
- var wait = (rejection.config || {}).wait || 0;
- if (wait > 0) {
- // todo 隐藏等待框
- }
- // todo 用更友好的提示框显示
- alert('无法访问服务器!');
- if (rejection.config.error) {
- rejection.config.error.call(undefined);
- }
- return $q.reject(rejection);
- }
- };
- }).config([ '$httpProvider', function($httpProvider) {
- // 设置http默认信息
- $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
- $httpProvider.defaults.headers.post['Accept'] = 'application/json, text/javascript, */*; q=0.01';
- $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
- $httpProvider.defaults.transformRequest = [ function(data) {
- var param = function(obj) {
- var query = '';
- var name, value, fullSubName, subName, subValue, innerObj, i;
- for (name in obj) {
- value = obj[name];
- if (value instanceof Array) {
- for (i = 0; i < value.length; ++i) {
- subValue = $.trim(value[i]);
- fullSubName = name + '[]';
- innerObj = {};
- innerObj[fullSubName] = subValue;
- query += param(innerObj) + '&';
- }
- } else if (value instanceof Date) {
- query += encodeURIComponent(name) + '=' + encodeURIComponent($.trim(value.toJSON())) + '&';
- } else if (value instanceof Object) {
- for (subName in value) {
- subValue = $.trim(value[subName]);
- fullSubName = name + '[' + subName + ']';
- innerObj = {};
- innerObj[fullSubName] = subValue;
- query += param(innerObj) + '&';
- }
- } else if (value !== undefined && value !== null) {
- query += encodeURIComponent(name) + '=' + encodeURIComponent($.trim(value)) + '&';
- }
- }
- return query.length ? query.substr(0, query.length - 1) : query;
- };
- return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
- } ];
- $httpProvider.interceptors.push('frameAjaxInterceptor');
- } ]);
|