main.js
5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// angular 主程序js,必须先导入,配置一些全局设置
var ScheduleApp = angular.module('ScheduleApp', [
'ui.router', // ui-route跳转
'ui.bootstrap', // ui bootstrap封装
'oc.lazyLoad', // 动态加载模块(html,js,css等)
'ngSanitize', // 净化html标签,配合ng-bind-html使用
'ngResource' // resource服务
]);
/**
* 用于请求通知。
*/
ScheduleApp.factory('requestNotificationChannel', ['$rootScope', function($rootScope) {
// 通知消息常量
var _START_REQUEST_ = '_START_REQUEST_'; // 开始请求通知message
var _END_REQUEST_ = '_END_REQUEST_'; // 请求结束通知message
// 计数器
var activeCalls = 0;
// 发布开始请求通知
var requestStarted = function() {
activeCalls += 1;
console.log("activeCalls=" + activeCalls);
$rootScope.$broadcast(_START_REQUEST_);
};
// 发布请求结束通知
var requestEnded = function() {
activeCalls -= 1;
console.log("activeCalls=" + activeCalls);
$rootScope.$broadcast(_END_REQUEST_);
};
/**
* 订阅开始请求通知。
* @param $scope 作用域
* @param handler 通知处理器函数
*/
var onRequestStarted = function($scope, handler) {
$scope.$on(_START_REQUEST_, function(event) {
handler();
});
};
/**
* 订阅请求结束通知。
* @param $scope 作用域
* @param handler 通知处理器函数
*/
var onRequestEnded = function($scope, handler) {
$scope.$on(_END_REQUEST_, function(event) {
handler();
});
};
return {
requestStarted : requestStarted,
requestEnded : requestEnded,
onRequestStarted : onRequestStarted,
onRequestEnded : onRequestEnded
};
}]);
// http 拦截器
ScheduleApp.factory(
'myInterceptor',
[
'requestNotificationChannel',
'$q',
function(requestNotificationChannel, $q) {
return {
request: function(config) {
requestNotificationChannel.requestStarted();
return config;
},
requestError: function(rejection) {
requestNotificationChannel.requestEnded();
return rejection;
},
response: function(response) {
requestNotificationChannel.requestEnded();
return response;
},
responseError: function(rejection) {
requestNotificationChannel.requestEnded();
// 处理错误,springboot会包装返回的错误数据
// 如:{"timestamp":1478674739246,"status":500,"error":"Internal Server Error","exception":"java.lang.ClassCastException","message":"java.lang.String cannot be cast to java.lang.Long","path":"/tidc/importfile"}
var status = rejection.status;
var path = rejection.data.path;
var message = rejection.data.message;
var output = [];
output.push("状态编码:" + status);
output.push("访问路径:" + path);
output.push("错误消息:" + message);
if (status) {
if (status == 500) {
alert("服务端错误:" + "\n" + output.join("\n"));
} else if (status == 407) {
alert("请重新登录:" + "\n" + output.join("\n"));
} else if (status == -1) {
alert("貌似服务端连接不上");
} else {
alert("其他错误:" + "\n" + output.join("\n"));
}
} else {
alert("我擦,后台返回连个状态码都没返回,见鬼了!");
}
return $q.reject(rejection);
}
};
}
]
);
ScheduleApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
$httpProvider.interceptors.push('myInterceptor');
}]);
/** ocLazyLoader 配置 */
ScheduleApp.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
// TODO:全局配置在这里
});
}]);
/** 配置全局配置信息 */
ScheduleApp.factory('settings', ['$rootScope', function($rootScope) {
// 封装 settings服务,并设置给 $rootScope同名对象
var settings = {
// TODO:
};
$rootScope.settings = settings;
return settings;
}]);
/** Schedule App 主应用控制器 */
ScheduleApp.controller('ScheduleAppController', ['$scope', function($scope) {
$scope.$on('$viewContentLoaded', function(event) {
console.log("子页面已载入:" + event);
});
}]);