SysResourceController.js
16.2 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
"use strict";
var sysResourceController = angular.module('sysResourceController',[]);
sysResourceController.controller('sysResourceController', [
'$rootScope',
'$scope',
'SysResourceService',
function($rootScope, $scope, SysResourceService) {
$scope.parantResource = {};
$scope.childResource = {};
$scope.name = null;
$scope.resource={
id:0,
name:'',
type:0,
parentId:0,
resourceUrl:'',
target:'',
iconImg:'',
sort:255,
isShow:1
};
$scope.changeClassification = function (selected1) {
SysResourceService.getParentSysResource(selected1.id, function(res) {
$scope.childResource = res.data;
});
};
$scope.changeChildClassification = function (selected2) {
$scope.resource.parentId = selected2.id;
}
$scope.onSubmitSysResource= function () {
SysResourceService.addSysResource($scope.resource, function(res) {
if(res.code == 0) {
$scope.resource={
id:0,
name:'',
type:0,
parentId:0,
url:'',
target:'',
iconImg:'',
sort:255,
isShow:1
};
if($scope.resource.parentId == 0) {
SysResourceService.getParentSysResource(0, function(res) {
$scope.parantResource = res.data;
});
}
loadTree();
}
});
}
//删除
$scope.deleteSysResource = function(){
SysResourceService.deleteSysResource($scope.resource.id, function(res) {
if(res.code == 0) {
$scope.resource={
id:0,
name:'',
type:0,
parentId:0,
url:'',
target:'',
iconImg:'',
sort:255,
isShow:1
};
loadTree();
if($scope.resource.parentId == 0) {
SysResourceService.getParentSysResource(0, function(res) {
$scope.parantResource = res.data;
});
}
}
});
}
SysResourceService.getParentSysResource(0, function(res) {
$scope.parantResource = res.data;
});
//重新加载资源树
function loadTree() {
var setting = {
check: {
enable: true
},
data: {
key : {
checked : 'checked'
},
simpleData: {
enable: true,
idKey: "id",
pIdKey: "parentId",
rootPId: 0
}
},
callback: {
onClick:function(event, treeId, treeNode, clickFlag) {
$scope.resource={
id:treeNode.id,
name:treeNode.name,
type:treeNode.type,
parentId:treeNode.parentId,
resourceUrl:treeNode.resourceUrl,
target:treeNode.target,
iconImg:treeNode.iconImg,
sort:treeNode.sort,
isShow:1
};
$rootScope.$apply();
}
}
};
SysResourceService.getTreeSysResource(function (res) {
if(res.code == 0) {
$.fn.zTree.init($("#treeDemo"), setting, res.data);
}
});
}
}]
);
sysResourceController.controller('sysRoleController', [
'$rootScope',
'$scope',
'SysResourceService',
function($rootScope, $scope, SysResourceService) {
var id=$rootScope.$stateParams.id ? $rootScope.$stateParams.id:0;
$scope.roleList = {};
$scope.role={
id:id,
name:'',
cnname:'',
resourceIds:''
};
SysResourceService.listSysRole(function(res) {
$scope.roleList = res.data;
});
if(id){
SysResourceService.getSysRole(id, function(res) {
$scope.role = res.data;
});
}
$scope.onSubmitSysRole = function () {
SysResourceService.addSysRole($scope.role);
}
//删除角色
$scope.deleteSysRole=function(id){
if(confirm("确定要删除吗?")){
SysResourceService.deleteSysRole(id,function(){
SysResourceService.listSysRole(function(res) {
$scope.roleList = res.data;
});
});
}
}
}]
);
sysResourceController.controller('allocationSysRoleController', [
'$rootScope',
'$scope',
'SysResourceService',
function($rootScope, $scope, SysResourceService) {
var id=$rootScope.$stateParams.id;
var resources = new Array();
$scope.roleResources = {};
$scope.role={
id:id,
name:'',
cnname:'',
resourceIds:''
};
SysResourceService.getSysRole(id, function(res) {
if(res.code == 0) {
$scope.role = res.data;
if($scope.role.resourceIds) {
resources = $scope.role.resourceIds.split(",");
}
if(!$scope.role.resourceIds) {
SysResourceService.getTreeSysResource(function (res) {
if(res.code == 0) {
$scope.roleResources = res.data;
loadRoleTree();
}
});
} else {
SysResourceService.getRoleResource(id, function(res){
if(res.code == 0) {
$scope.roleResources = res.data;
loadRoleTree();
}
});
}
}
});
//分配权限
$scope.allocationRoleResource = function () {
$scope.role.resourceIds = resources.join(",");
SysResourceService.allocationRoleResource($scope.role);
}
//重新加载资源树
function loadRoleTree() {
var setting = {
check: {
enable: true
},
data: {
key : {
checked : 'checked'
},
simpleData: {
enable: true,
idKey: "id",
pIdKey: "parentId",
rootPId: 0
}
},
callback: {
onCheck: function (event, treeId, treeNode, clickFlag) {
//选中状态
if(treeNode.checked) {
resources.push(treeNode.id);
if(treeNode.isParent) {
//将子元素加入到数组中
for(var i=0; i<treeNode.children.length; i++) {
//添加父级的时候要判断里面是否有这个父级了
pushResource(treeNode.children[i].id);
//如果这个子节点还是父级
if(treeNode.children[i].isParent) {
//将子节点下的节点加入到数组中
for(var j=0; j<treeNode.children[i].children.length; j++) {
pushResource(treeNode.children[i].children[j].id);
}
}
}
//如果这个元素也有父级节点,将父级节点删除
var preParentNode = treeNode.getParentNode();
if(preParentNode) {
if(preParentNode.checked) {
pushResource(preParentNode.id);
}
}
} else {
//如果是子节点,将他的父节点添加进去
var preParentNode = treeNode.getParentNode();
if(preParentNode) {
if(preParentNode.checked) {
pushResource(preParentNode.id);
}
if(preParentNode.getParentNode() && preParentNode.getParentNode().checked) {
pushResource(preParentNode.getParentNode().id);
}
}
}
} else {
//取消选中
deleteResource(treeNode.id);
//如果这个节点是父级节点,将下面的所有子级节点删除
if(treeNode.isParent && treeNode.children) {
//将子元素从数组删除
for(var i=0; i<treeNode.children.length; i++) {
deleteResource(treeNode.children[i].id);
//如果这个子节点还是父级
if(treeNode.children[i].isParent && treeNode.children[i].children) {
//将子节点下的节点从数组删除
for(var j=0; j<treeNode.children[i].children.length; j++) {
deleteResource(treeNode.children[i].children[j].id);
}
}
}
//如果这个元素也有父级节点,将父级节点删除
var preParentNode = treeNode.getParentNode();
if(preParentNode) {
if(!preParentNode.checked) {
deleteResource(preParentNode.id);
}
}
} else {
//如果是子级节点,判断父级节点是否选中
var preParentNode = treeNode.getParentNode();
if(preParentNode) {
if(!preParentNode.checked) {
deleteResource(preParentNode.id);
}
if(preParentNode.getParentNode() && !preParentNode.getParentNode().checked) {
deleteResource(preParentNode.getParentNode().id);
}
}
}
}
}
}
};
$.fn.zTree.init($("#treeDemo"), setting, $scope.roleResources);
}
function deleteResource(delVal) {
for(var j=0; j<resources.length; j++) {
if(resources[j] == delVal) {
resources.splice(j, 1);
break;
}
}
}
function pushResource(pushVal) {
var flag = 1;
for(var j=0; j<resources.length; j++) {
if(resources[j] == pushVal) {
flag = 0;
break;
}
}
if(flag) {
resources.push(pushVal);
}
}
}
]);
sysResourceController.controller('sysUserController', [
'$rootScope',
'$scope',
'SysResourceService',
function($rootScope, $scope, SysResourceService) {
$scope.currentPage = 1;
$scope.numPages = 1;
$scope.pageSize = 15;
$scope.totalRecord = 0;
$scope.pages = [];
$scope.user={
id:0,
locked:0
};
$scope.userQuery={
supplierName:'',
userName:'',
nickName:'',
locked:'',
pageNow:1,
pageSize:10,
};
$scope.userQuery.locked=0;
SysResourceService.listUser($scope.userQuery);
$scope.$on('user.list', function() {
$scope.users = SysResourceService.users.datas;
$scope.numPages = SysResourceService.users.totalPage;
$scope.totalRecord = SysResourceService.users.totalRecord;
});
//搜索
$scope.search=function(){
$scope.userQuery.pageNow=1;
SysResourceService.listUser($scope.userQuery);
}
$scope.onSelectPage = function(page) {
$scope.currentPage=page;
SysResourceService.listUser($scope.currentPage, $scope.pageSize);
};
$scope.isActive = function(page) {
return $scope.currentPage === page;
};
$scope.lockedUser = function(id, userName, flag) {
var title = null;
if(flag == 1) {
title = "您确定要将用户“"+userName+"”禁止登录吗";
} else {
title = "您确定要将用户“"+userName+"”允许登录吗"
}
if(confirm(title)) {
$scope.user.id = id;
$scope.user.locked = flag;
SysResourceService.lockedUser($scope.user, function(res){
if(res.code == 0) {
for(var i=0; i<$scope.users.length; i++) {
if($scope.users[i].id == id) {
$scope.users[i].locked = flag;
break;
}
}
}
});
}
}
}]
);
sysResourceController.controller('logRecordController', [
'$rootScope',
'$scope',
'SysResourceService',
function($rootScope, $scope, SysResourceService) {
$scope.logType=$rootScope.$stateParams.logType;
$scope.currentPage = 1;
$scope.numPages = 1;
$scope.pageSize = 15;
$scope.totalRecord = 0;
$scope.pages = [];
SysResourceService.listLog($scope.currentPage, $scope.pageSize, $scope.logType);
$scope.$on('log.list', function() {
$scope.logs = SysResourceService.logs.datas;
$scope.numPages = SysResourceService.logs.totalPage;
$scope.totalRecord = SysResourceService.logs.totalRecord;
});
$scope.onSelectPage = function(page) {
$scope.currentPage=page;
SysResourceService.listLog($scope.currentPage, $scope.pageSize, $scope.logType);
};
$scope.isActive = function(page) {
return $scope.currentPage === page;
};
}]
);
sysResourceController.controller('addSysUserController', [
'$rootScope',
'$scope',
'SysResourceService',
'SupplierService',
'BaseService',
function($rootScope, $scope, SysResourceService,SupplierService,BaseService) {
var id=$rootScope.$stateParams.id ? $rootScope.$stateParams.id:0;
$scope.supplierObj=[];
$scope.user={
id:id,
userName:'',
phone:'',
password:'',
locked:0,
roleId:0,
nickName:'',
supplierId:'',
platformFlag:''
};
$scope.roleList = {};
SysResourceService.listSysRole(function(res) {
$scope.roleList = res.data;
});
function init(){
if($scope.user.platformFlag == 0){
$("#supplierDiv").show();
}else{
$scope.supplierObj = null;
$("#supplierDiv").hide();
}
}
if(id) {
SysResourceService.getSysUser(id, function(res){
$scope.user = res.data;
init();
getSupplierName();
});
}
$scope.platformOrSupplier =function (){
init();
}
$scope.onSubmitSysUser = function() {
if($scope.user.platformFlag==true || $scope.user.platformFlag==1){
$scope.user.platformFlag=1;
$scope.user.supplierId="";
}else {
$scope.user.platformFlag=0;
}
if($scope.user.platformFlag == 0){
if($scope.user.supplierId == null || $scope.user.supplierId ==""){
BaseService.updateAlert("请选择商家");
return false;
}
}
if($scope.user.roleId == 0){
BaseService.updateAlert("请选择用户角色");
return false;
}
SysResourceService.addSysUser($scope.user);
}
function getSupplierName(){
if($scope.suppliers&&$scope.suppliers.length){
var type = $scope.user.supplierId;
var list = $scope.suppliers;
for(var i=0;i<list.length;i++){
if(list[i]['id'] == type){
$scope.supplierObj = $scope.suppliers[i];
break;
}
if(type == '') {
$scope.supplierObj = null;
break;
}
}
}
}
SupplierService.listSupplier();
$scope.$on('supplier.list',function(){
$scope.suppliers=SupplierService.suppliers;
getSupplierName();
});
//选择供应商
$scope.onChangeSupplier=function () {
if($scope.user.platformFlag==true || $scope.user.platformFlag==1){
$scope.user.supplierId="";
}else {
if($scope.supplierObj != null) {
$scope.user.supplierId = $scope.supplierObj.id;
}else{
$scope.user.supplierId = "";
}
}
};
}]
);
sysResourceController.controller('resetUserPasswordController', [
'$rootScope',
'$scope',
'SysResourceService',
'HomeService',
function($rootScope, $scope, SysResourceService,HomeService) {
var id=$rootScope.$stateParams.id;
$scope.user={
id:id,
password:'',
againPassword:''
};
var userId='';
HomeService.getCurrentUserInfo();
$scope.$on('user.info', function() {
$scope.userinfo = HomeService.userinfo;
userId=$scope.userinfo.id;
});
$scope.resetUserPassword = function() {
$scope.user.id=userId;
SysResourceService.resetUserPassword($scope.user);
}
}]
);
sysResourceController.controller('resetUserPasswordBySupplierController', [
'$rootScope',
'$scope',
'SysResourceService',
'HomeService',
function($rootScope, $scope, SysResourceService,HomeService) {
$scope.user={
id:'',
password:'',
againPassword:''
};
var userId='';
HomeService.getCurrentUserInfo();
$scope.$on('user.info', function() {
$scope.userinfo = HomeService.userinfo;
userId=$scope.userinfo.id;
});
$scope.resetUserPassword = function() {
$scope.user.id=userId;
SysResourceService.resetUserPassword($scope.user);
}
}]
);