Browse Source

权限:界面逻辑更新2

wangdy 8 years ago
parent
commit
66b0090b40

+ 5 - 1
src/main/webapp/resources/js/common/query/user.js

@@ -77,5 +77,9 @@ define([ 'angular', 'ui-bootstrap', 'ngResource' ], function(angular) {
 				method: 'POST'
 			}
 		});
-	}]);
+	}]).factory('AccountResource', ['$resource', function($resource) {
+        return $resource('account/resource', {});
+    }]).factory('AccountRole', ['$resource', function($resource) {
+        return $resource('account/role/:id');
+    }]);
 });

+ 135 - 15
src/main/webapp/resources/js/vendor/controllers/forstore/vendor_account_management_ctrl.js

@@ -275,21 +275,36 @@ define(['app/app'], function(app) {
 			});
 
 		};
-		$scope.editRole = function (role) {
-			var modalInstance = $modal.open({
-				animation: true,
-				templateUrl: $rootScope.rootPath + '/static/view/vendor/modal/role_detail.html',
-				controller: 'RoleEditCtrl',
-				resolve: {
-					role: function () {
-						return role;
-					}
-				}
-			});
-			// modalInstance.result.then(function (added) {
-			// 	added && (getData());
-			// });
-		};
+		//角色控制
+        $scope.roles = {};
+        var getData = function () {
+            AccountRole.query({}, function (data) {
+                var defaults = [], custom = [];
+                angular.forEach(data, function (d) {
+                    if (d.isdefault == 1)
+                        defaults.push(d);
+                    else
+                        custom.push(d);
+                });
+                $scope.roles = {defaults: defaults, custom: custom};
+            });
+        };
+        getData();
+        $scope.editRole = function (role) {
+            var modalInstance = $modal.open({
+                animation: true,
+                templateUrl: $rootScope.rootPath + '/static/view/vendor/modal/role_detail.html',
+                controller: 'RoleEditCtrl',
+                resolve: {
+                    role: function () {
+                        return role;
+                    }
+                }
+            });
+            modalInstance.result.then(function (added) {
+                added && (getData());
+            });
+        };
     }]);
 
 	/*
@@ -369,6 +384,7 @@ define(['app/app'], function(app) {
 		};
 	}]);
 
+
 	// 修改密码Controller
 	app.register.controller('PasswordCtrl', ['$scope', '$modalInstance', 'user', 'User', 'toaster', function($scope, $modalInstance, user, User, toaster){
 		$scope.user = user;
@@ -430,4 +446,108 @@ define(['app/app'], function(app) {
 		};
 
 	}]);
+
+    app.register.controller('RoleEditCtrl', ['$scope', '$modalInstance', '$timeout', 'AccountResource', 'AccountRole', 'toaster', 'role', 'ngAlert', 'BaseService', function ($scope, $modalInstance, $timeout, AccountResource, AccountRole, toaster, role, ngAlert, BaseService) {
+        BaseService.scrollBackToTop();
+        $scope.role = role;
+        $scope.master = angular.copy($scope.role);
+        var isNew = role == null;
+        $scope.cancel = function () {
+            $modalInstance.close(false);
+        };
+        AccountResource.query({}, function (data) {
+            if (data && data.length > 0) {
+                if (role && role.resourceItems) {
+                    var rs = [];// 已分配的资源的id
+                    angular.forEach(role.resourceItems, function (item) {
+                        rs.push(item.id);
+                    });
+                    angular.forEach(data, function (resource) {
+                        var c = 0;
+                        angular.forEach(resource.items, function (item) {
+                            if (rs.indexOf(item.id) > -1) {
+                                item.$checked = true;
+                                c += 1;
+                            }
+                        });
+                        resource.$checked = c == resource.items.length;
+                    });
+                }
+                $scope.resourceItems = data[0].items;
+                data[0].$active = true;
+            }
+            $scope.resources = data;
+            $timeout(function () {
+                angular.forEach($scope.resources, function (resource) {
+                    var c = 0;
+                    angular.forEach(resource.items, function (item) {
+                        item.$checked && (c += 1);
+                    });
+                    angular.element(document.getElementById('check_' + resource.id)).prop('indeterminate', (c > 0 && !resource.$checked));
+                });
+            }, 100);
+        });
+        $scope.onItemClick = function (resource) {
+            $scope.resourceItems = resource.items;
+            angular.forEach($scope.resources, function (r) {
+                r.$active = false;
+            });
+            resource.$active = true;
+        };
+        var getChecked = function () {
+            var items = [];
+            angular.forEach($scope.resources, function (resource) {
+                angular.forEach(resource.items, function (item) {
+                    item.$checked && (items.push(item));
+                });
+            });
+            $scope.role.resourceItems = items;
+        };
+        $scope.onResourceChange = function (resource) {
+            angular.forEach(resource.items, function (item) {
+                item.$checked = resource.$checked;
+            });
+            getChecked();
+        };
+        $scope.onItemChange = function () {
+            angular.forEach($scope.resources, function (resource) {
+                if (resource.$active) {
+                    var c = 0;
+                    angular.forEach(resource.items, function (item) {
+                        item.$checked && (c += 1);
+                    });
+                    resource.$checked = (c == resource.items.length);
+                    angular.element(document.getElementById('check_' + resource.id)).prop('indeterminate', (c > 0 && !resource.$checked));
+                }
+            });
+            getChecked();
+        };
+        $scope.isChanged = function (formData) {
+            if (!$scope.master)
+                return true;
+            return !angular.equals(formData, $scope.master);
+        };
+        $scope.save = function () {
+            $scope.master = angular.copy($scope.role);
+            AccountRole.save($scope.master, function () {
+                toaster.pop('success', '提示', '角色:' + $scope.role.desc + ' 资料' + (isNew ? '添加' : '修改') + '成功');
+                $modalInstance.close(true);
+            }, function (response) {
+                toaster.pop('error', '错误', response.data);
+            });
+        };
+        $scope.del = function () {
+            new ngAlert({
+                title: '提示',
+                content: '确定删除角色(' + $scope.role.desc + ')吗?'
+            }, function () {
+                AccountRole.remove({id: role.id}, function () {
+                    toaster.pop('success', '提示', '角色:' + $scope.role.desc + ' 删除成功');
+                    $modalInstance.close(true);
+                }, function (response) {
+                    toaster.pop('error', '错误', response.data);
+                });
+            })
+        };
+    }]);
 });