abp.ng.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. (function (abp, angular) {
  2. if (!angular) {
  3. return;
  4. }
  5. abp.ng = abp.ng || {};
  6. abp.ng.http = {
  7. defaultError: {
  8. message: 'An error has occurred!',
  9. details: 'Error detail not sent by server.'
  10. },
  11. defaultError401: {
  12. message: 'You are not authenticated!',
  13. details: 'You should be authenticated (sign in) in order to perform this operation.'
  14. },
  15. defaultError403: {
  16. message: 'You are not authorized!',
  17. details: 'You are not allowed to perform this operation.'
  18. },
  19. defaultError404: {
  20. message: 'Resource not found!',
  21. details: 'The resource requested could not be found on the server.'
  22. },
  23. logError: function (error) {
  24. abp.log.error(error);
  25. },
  26. showError: function (error) {
  27. if (error.details) {
  28. return abp.message.error(error.details, error.message || abp.ng.http.defaultError.message);
  29. } else {
  30. return abp.message.error(error.message || abp.ng.http.defaultError.message);
  31. }
  32. },
  33. handleTargetUrl: function (targetUrl) {
  34. if (!targetUrl) {
  35. location.href = abp.appPath;
  36. } else {
  37. location.href = targetUrl;
  38. }
  39. },
  40. handleNonAbpErrorResponse: function (response, defer) {
  41. if (response.config.abpHandleError !== false) {
  42. switch (response.status) {
  43. case 401:
  44. abp.ng.http.handleUnAuthorizedRequest(
  45. abp.ng.http.showError(abp.ng.http.defaultError401),
  46. abp.appPath
  47. );
  48. break;
  49. case 403:
  50. abp.ng.http.showError(abp.ajax.defaultError403);
  51. break;
  52. case 404:
  53. abp.ng.http.showError(abp.ajax.defaultError404);
  54. break;
  55. default:
  56. abp.ng.http.showError(abp.ng.http.defaultError);
  57. break;
  58. }
  59. }
  60. defer.reject(response);
  61. },
  62. handleUnAuthorizedRequest: function (messagePromise, targetUrl) {
  63. if (messagePromise) {
  64. messagePromise.done(function () {
  65. abp.ng.http.handleTargetUrl(targetUrl || abp.appPath);
  66. });
  67. } else {
  68. abp.ng.http.handleTargetUrl(targetUrl || abp.appPath);
  69. }
  70. },
  71. handleResponse: function (response, defer) {
  72. var originalData = response.data;
  73. if (originalData.success === true) {
  74. response.data = originalData.result;
  75. defer.resolve(response);
  76. if (originalData.targetUrl) {
  77. abp.ng.http.handleTargetUrl(originalData.targetUrl);
  78. }
  79. } else if (originalData.success === false) {
  80. var messagePromise = null;
  81. if (originalData.error) {
  82. if (response.config.abpHandleError !== false) {
  83. messagePromise = abp.ng.http.showError(originalData.error);
  84. }
  85. } else {
  86. originalData.error = defaultError;
  87. }
  88. abp.ng.http.logError(originalData.error);
  89. response.data = originalData.error;
  90. defer.reject(response);
  91. if (response.status == 401 && response.config.abpHandleError !== false) {
  92. abp.ng.http.handleUnAuthorizedRequest(messagePromise, originalData.targetUrl);
  93. }
  94. } else { //not wrapped result
  95. defer.resolve(response);
  96. }
  97. }
  98. }
  99. var abpModule = angular.module('abp', []);
  100. abpModule.config([
  101. '$httpProvider', function ($httpProvider) {
  102. $httpProvider.interceptors.push(['$q', function ($q) {
  103. return {
  104. 'request': function (config) {
  105. if (config.url.indexOf('.cshtml') !== -1) {
  106. config.url = abp.appPath + 'AbpAppView/Load?viewUrl=' + config.url + '&_t=' + abp.pageLoadTime.getTime();
  107. }
  108. return config;
  109. },
  110. 'response': function (response) {
  111. if (!response.data || !response.data.__abp) {
  112. //Non ABP related return value
  113. return response;
  114. }
  115. var defer = $q.defer();
  116. abp.ng.http.handleResponse(response, defer);
  117. return defer.promise;
  118. },
  119. 'responseError': function (ngError) {
  120. var defer = $q.defer();
  121. if (!ngError.data || !ngError.data.__abp) {
  122. abp.ng.http.handleNonAbpErrorResponse(ngError, defer);
  123. } else {
  124. abp.ng.http.handleResponse(ngError, defer);
  125. }
  126. return defer.promise;
  127. }
  128. };
  129. }]);
  130. }
  131. ]);
  132. abp.event.on('abp.dynamicScriptsInitialized', function () {
  133. abp.ng.http.defaultError.message = abp.localization.abpWeb('DefaultError');
  134. abp.ng.http.defaultError.details = abp.localization.abpWeb('DefaultErrorDetail');
  135. abp.ng.http.defaultError401.message = abp.localization.abpWeb('DefaultError401');
  136. abp.ng.http.defaultError401.details = abp.localization.abpWeb('DefaultErrorDetail401');
  137. abp.ng.http.defaultError403.message = abp.localization.abpWeb('DefaultError403');
  138. abp.ng.http.defaultError403.details = abp.localization.abpWeb('DefaultErrorDetail403');
  139. abp.ng.http.defaultError404.message = abp.localization.abpWeb('DefaultError404');
  140. abp.ng.http.defaultError404.details = abp.localization.abpWeb('DefaultErrorDetail404');
  141. });
  142. })((abp || (abp = {})), (angular || undefined));