angular.init.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. angular.module('app', [ 'ngSanitize' ]);
  3. var app = angular.module('app').config([ '$controllerProvider', '$compileProvider', '$filterProvider', '$provide', function($controllerProvider, $compileProvider, $filterProvider, $provide) {
  4. app.controller = $controllerProvider.register;
  5. app.directive = $compileProvider.directive;
  6. app.filter = $filterProvider.register;
  7. app.factory = $provide.factory;
  8. app.service = $provide.service;
  9. app.constant = $provide.constant;
  10. app.value = $provide.value;
  11. } ]);
  12. app.factory('frameAjaxInterceptor', function($rootScope, $q, $location) {
  13. return {
  14. 'request' : function(config) {
  15. // 判断是否弹出等待框
  16. var wait = config.wait || 0;
  17. if (wait > 0) {
  18. // todo 显示提交等待框
  19. }
  20. return config;
  21. },
  22. 'requestError' : function(rejection) {
  23. return $q.reject(rejection);
  24. },
  25. 'response' : function(response) {
  26. var wait = (response.config || {}).wait || 0;
  27. // todo 隐藏等待框
  28. if (response.data.success == undefined)
  29. return response;
  30. if (response.data.success == false) {
  31. // todo 用更友好的提示框显示
  32. alert(response.data.errorMsg);
  33. if (response.config.error) {
  34. response.config.error.call(undefined);
  35. }
  36. return $q.reject(response);
  37. }
  38. return response;
  39. },
  40. 'responseError' : function(rejection) {
  41. var wait = (rejection.config || {}).wait || 0;
  42. if (wait > 0) {
  43. // todo 隐藏等待框
  44. }
  45. // todo 用更友好的提示框显示
  46. alert('无法访问服务器!');
  47. if (rejection.config.error) {
  48. rejection.config.error.call(undefined);
  49. }
  50. return $q.reject(rejection);
  51. }
  52. };
  53. }).config([ '$httpProvider', function($httpProvider) {
  54. // 设置http默认信息
  55. $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
  56. $httpProvider.defaults.headers.post['Accept'] = 'application/json, text/javascript, */*; q=0.01';
  57. $httpProvider.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
  58. $httpProvider.defaults.transformRequest = [ function(data) {
  59. var param = function(obj) {
  60. var query = '';
  61. var name, value, fullSubName, subName, subValue, innerObj, i;
  62. for (name in obj) {
  63. value = obj[name];
  64. if (value instanceof Array) {
  65. for (i = 0; i < value.length; ++i) {
  66. subValue = $.trim(value[i]);
  67. fullSubName = name + '[]';
  68. innerObj = {};
  69. innerObj[fullSubName] = subValue;
  70. query += param(innerObj) + '&';
  71. }
  72. } else if (value instanceof Date) {
  73. query += encodeURIComponent(name) + '=' + encodeURIComponent($.trim(value.toJSON())) + '&';
  74. } else if (value instanceof Object) {
  75. for (subName in value) {
  76. subValue = $.trim(value[subName]);
  77. fullSubName = name + '[' + subName + ']';
  78. innerObj = {};
  79. innerObj[fullSubName] = subValue;
  80. query += param(innerObj) + '&';
  81. }
  82. } else if (value !== undefined && value !== null) {
  83. query += encodeURIComponent(name) + '=' + encodeURIComponent($.trim(value)) + '&';
  84. }
  85. }
  86. return query.length ? query.substr(0, query.length - 1) : query;
  87. };
  88. return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
  89. } ];
  90. $httpProvider.interceptors.push('frameAjaxInterceptor');
  91. } ]);