$.LoadingOverlaySetup({});

var app = angular.module('app', [ 'ui.router', 'ui.router.state.events', 'oc.lazyLoad' ]).factory('frameAjaxInterceptor', function($rootScope, $q, $location) {
	// 定义通用函数
	$rootScope.msg = function(msg, title, callback) {
		var msgTitle = '提示消息';
		if (title !== undefined) {
			var type = typeof (title);
			if (type == 'string' && title.length > 0)
				msgTitle = title;
			else if (type == 'function') {
				callback = title;
			}
		}

		if (callback == undefined)
			callback = function() {

			}
		var d = dialog({
			title : msgTitle,
			width : '20em',
			content : msg,
			cancel : false,
			ok : callback,
			cancelDisplay : false,
			okValue : '确 定'
		});
		d.showModal();
	}

	$rootScope.confirm = function(msg, callback) {
		var msgTitle = '请进行操作操作';
		var d = dialog({
			title : msgTitle,
			width : '20em',
			content : msg,
			cancelValue : '取消操作',
			cancel : function() {
			},
			ok : callback,
			cancelDisplay : true,
			okValue : '确定操作'
		});
		d.showModal();
	}

	return {
		request : function(config) {
			// 判断是否弹出等待框
			var wait = config.RequestWithLoading || 0;
			if (wait > 0) {
				// 需要弹等待框
				$.LoadingOverlay("show");
			}
			return config;
		},
		requestError : function(rejection) {
			return $q.reject(rejection);
		},
		response : function(response) {
			var wait = (response.config || {}).RequestWithLoading || 0;
			if (wait > 0) {
				// 需要弹等待框
				$.LoadingOverlay("hide");
			}
			if (response.data.success == undefined)
				return response;
			if (response.data.success == false) {
				// 登录超时
				if (response.data.login == false) {
					$rootScope.msg('登录超时，为保证数据安全，请重新登录！', function() {
						window.location = "./login/login.jsp";
					})
					return $q.reject(response);
				} else {
					// 其他错误信息
					$rootScope.msg(response.data.errorMsg ? response.data.errorMsg : '服务器内部错误，请稍后重试！', response.config.error);
					return $q.reject(response);
				}
			}
			return response;
		},
		responseError : function(response) {
			$.LoadingOverlay("hide");
			$rootScope.msg('服务器内部错误，请稍后重试！', '错误提示');
			// 非业务错误，系统层错误 如404这些错误
			return $q.reject(response);
		}
	};
}).config(function($httpProvider, $stateProvider, $urlRouterProvider) {
	// 设置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';

	// 修改提交方式，全部用json处理
	$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');
});
;
