main.js
9.05 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// angular 主程序js,必须先导入,配置一些全局设置
var ScheduleApp = angular.module('ScheduleApp', [
'ui.router', // ui-route跳转
'ngAnimate', // ng动画
'ui.bootstrap', // ui bootstrap封装
'oc.lazyLoad', // 动态加载模块(html,js,css等)
'ngSanitize', // 净化html标签,配合ng-bind-html使用
'ngResource', // resource服务
'ngHandsontable',
'ng-sweet-alert'
]);
ScheduleApp.factory('UserPrincipal', [
'$http',
function($http) {
// 登录后获取用户公司信息
var gsinfos = []; // 原始返回的对象数据
var gsinfo_strs = []; // 拼装以后的(公司代码_分公司代码)
var gsinfo_strs_u = []; // 拼装以后的(分公司代码_公司代码)
$http({
method: 'GET',
url: '/user/companyData'
}).then(function(result) {
if (angular.isArray(result.data)) {
gsinfos = result;
angular.forEach(result.data, function(obj) {
var gsbm = obj.companyCode;
angular.forEach(obj.children, function(obj2) {
var fgsbm = obj2.code;
gsinfo_strs.push(gsbm + "_" + fgsbm);
gsinfo_strs_u.push(fgsbm + "_" + gsbm);
});
});
}
console.log(gsinfos);
console.log(gsinfo_strs);
});
return {
getGsStrs: function() {
return gsinfo_strs;
},
getGsStrsU: function() {
return gsinfo_strs_u;
}
};
}
]);
ScheduleApp.factory('DataStore', [
'$http',
function($http) {
// 本地数据存储,如车辆数据,人员数据
var dataMap = {
"cl": [],
"ry": []
};
$http({
method: 'GET',
url: '/cars_sc/all'
}).then(function(result) {
// 简拼数据
dataMap.cl = result.data.data;
angular.forEach(result.data.data, function(obj) {
// 全拼
obj["$fullChars"] = pinyin.getFullChars(obj.insideCode ? obj.insideCode: "");
// 简拼
obj["$camelChars"] = pinyin.getCamelChars(obj.insideCode ? obj.insideCode: "");
// 原值
obj["$calcu_str"] = obj.insideCode? obj.insideCode: "";
});
});
$http({
method: 'GET',
url: '/ee/all'
}).then(function(result) {
// 简拼数据
dataMap.ry = result.data.data;
angular.forEach(result.data.data, function(obj) {
// 全拼
obj["$fullChars"] = pinyin.getFullChars(obj.personnelName + "-" + obj.jobCode);
// 简拼
obj["$camelChars"] = pinyin.getCamelChars(obj.personnelName + "-" + obj.jobCode);
// 原值
obj["$calcu_str"] = obj.personnelName + "-" + obj.jobCode;
});
});
return {
getData: function(type) {
if (dataMap[type]) {
return dataMap[type];
} else {
return [];
}
}
}
}
]);
/**
* 用于请求通知。
*/
ScheduleApp.factory('requestNotificationChannel', ['$rootScope', function($rootScope) {
// 通知消息常量
var _START_REQUEST_ = '_START_REQUEST_'; // 开始请求通知message
var _END_REQUEST_ = '_END_REQUEST_'; // 请求结束通知message
// 计数器
var activeCalls = 0;
// 发布开始请求通知
var requestStarted = function(requestInfo) {
activeCalls += 1;
console.log("activeCalls=" + activeCalls);
$rootScope.$broadcast(_START_REQUEST_, requestInfo);
};
// 发布请求结束通知
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, requestInfo) {
handler(requestInfo);
});
};
/**
* 订阅请求结束通知。
* @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(config);
return config;
},
requestError: function(rejection) {
requestNotificationChannel.requestEnded();
return rejection;
},
response: function(response) {
requestNotificationChannel.requestEnded();
var data = response.data;
var output = [];
if (data.status == '407') {
alert("请重新登录!");
return $q.reject(response);
} else if (data.status == '500') {
output.push("状态编码:" + data.status);
output.push("访问路径:" + data.path);
output.push("错误消息:" + data.message);
alert("服务端错误:" + "\n" + output.join("\n"));
return $q.reject(response);
} else {
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 output = [];
if (!rejection.status) {
alert("我擦,后台返回连个状态码都没返回,见鬼了,服务器可能重启了");
} else if (rejection.status == -1) {
// 服务器断开了
alert("貌似服务端连接不上");
} else {
output.push("状态编码:" + rejection.status);
output.push("错误内容:" + angular.toJson(rejection.data));
if (rejection.status == 500) {
alert("服务端错误:" + "\n" + output.join("\n"));
} else if (rejection.status == 407) {
alert("请重新登录:" + "\n" + output.join("\n"));
} else {
alert("其他错误:" + "\n" + output.join("\n"));
}
}
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',
'UserPrincipal',
'DataStore',
function($scope, UserPrincipal, DataStore) {
$scope.$on('$viewContentLoaded', function(event) {
console.log("子页面已载入:" + event);
});
// 获取一次用户身份信息
UserPrincipal.getGsStrs();
// 获取本地数据
DataStore.getData("cl");
DataStore.getData("ry");
}
]);