frame.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. $.LoadingOverlaySetup({});
  2. var app = angular.module('app', [ 'ui.router', 'ui.router.state.events', 'oc.lazyLoad' ]).factory('frameAjaxInterceptor', function($rootScope, $q, $location) {
  3. // 定义通用函数
  4. $rootScope.msg = function(msg, title, callback) {
  5. var msgTitle = '提示消息';
  6. if (title !== undefined) {
  7. var type = typeof (title);
  8. if (type == 'string' && title.length > 0)
  9. msgTitle = title;
  10. else if (type == 'function') {
  11. callback = title;
  12. }
  13. }
  14. if (callback == undefined)
  15. callback = function() {
  16. }
  17. var d = dialog({
  18. title : msgTitle,
  19. width : '20em',
  20. content : msg,
  21. cancel : false,
  22. ok : callback,
  23. cancelDisplay : false,
  24. okValue : '确 定'
  25. });
  26. d.showModal();
  27. }
  28. $rootScope.confirm = function(msg, callback) {
  29. var msgTitle = '请进行操作操作';
  30. var d = dialog({
  31. title : msgTitle,
  32. width : '20em',
  33. content : msg,
  34. cancelValue : '取消操作',
  35. cancel : function() {
  36. },
  37. ok : callback,
  38. cancelDisplay : true,
  39. okValue : '确定操作'
  40. });
  41. d.showModal();
  42. }
  43. return {
  44. request : function(config) {
  45. // 判断是否弹出等待框
  46. var wait = config.RequestWithLoading || 0;
  47. if (wait > 0) {
  48. // 需要弹等待框
  49. $.LoadingOverlay("show");
  50. }
  51. return config;
  52. },
  53. requestError : function(rejection) {
  54. return $q.reject(rejection);
  55. },
  56. response : function(response) {
  57. var wait = (response.config || {}).RequestWithLoading || 0;
  58. if (wait > 0) {
  59. // 需要弹等待框
  60. $.LoadingOverlay("hide");
  61. }
  62. if (response.data.success == undefined)
  63. return response;
  64. if (response.data.success == false) {
  65. // 登录超时
  66. if (response.data.login == false) {
  67. $rootScope.msg('登录超时,为保证数据安全,请重新登录!', function() {
  68. window.location = "./login/login.jsp";
  69. })
  70. return $q.reject(response);
  71. } else {
  72. // 其他错误信息
  73. $rootScope.msg(response.data.errorMsg ? response.data.errorMsg : '服务器内部错误,请稍后重试!', response.config.error);
  74. return $q.reject(response);
  75. }
  76. }
  77. return response;
  78. },
  79. responseError : function(response) {
  80. $.LoadingOverlay("hide");
  81. $rootScope.msg('服务器内部错误,请稍后重试!', '错误提示');
  82. // 非业务错误,系统层错误 如404这些错误
  83. return $q.reject(response);
  84. }
  85. };
  86. }).config(function($httpProvider, $stateProvider, $urlRouterProvider) {
  87. // 设置http默认信息
  88. $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
  89. $httpProvider.defaults.headers.post['Accept'] = 'application/json, text/javascript, */*; q=0.01';
  90. $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
  91. // 修改提交方式,全部用json处理
  92. $httpProvider.defaults.transformRequest = [ function(data) {
  93. var param = function(obj) {
  94. var query = '';
  95. var name, value, fullSubName, subName, subValue, innerObj, i;
  96. for (name in obj) {
  97. value = obj[name];
  98. if (value instanceof Array) {
  99. for (i = 0; i < value.length; ++i) {
  100. subValue = $.trim(value[i]);
  101. fullSubName = name + '[]';
  102. innerObj = {};
  103. innerObj[fullSubName] = subValue;
  104. query += param(innerObj) + '&';
  105. }
  106. } else if (value instanceof Date) {
  107. query += encodeURIComponent(name) + '=' + encodeURIComponent($.trim(value.toJSON())) + '&';
  108. } else if (value instanceof Object) {
  109. for (subName in value) {
  110. subValue = $.trim(value[subName]);
  111. fullSubName = name + '[' + subName + ']';
  112. innerObj = {};
  113. innerObj[fullSubName] = subValue;
  114. query += param(innerObj) + '&';
  115. }
  116. } else if (value !== undefined && value !== null) {
  117. query += encodeURIComponent(name) + '=' + encodeURIComponent($.trim(value)) + '&';
  118. }
  119. }
  120. return query.length ? query.substr(0, query.length - 1) : query;
  121. };
  122. return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
  123. } ];
  124. $httpProvider.interceptors.push('frameAjaxInterceptor');
  125. });
  126. ;