roleCtrl.js 4.8 KB
(function(angular) {
	function roleCtrl($scope, DTOptionsBuilder, DTColumnBuilder, systemService, $uibModal, $filter, $state) {
		$scope.fieldName='merchantId';
		$scope.createTime = {};
		$scope.drOptions = {
			opens: 'right',
	   		eventHandlers: {
                'apply.daterangepicker':function(ev, picker){
                	$scope.createTime.startDate = picker.startDate;
                	$scope.createTime.endDate = picker.endDate;
                },
                'cancel.daterangepicker': function (ev, picker) {
                	$scope.createTime = {}
                    $scope.createTime.startDate = null
                    $scope.createTime.endDate = null
                    $scope.$apply()
                  }
            }
		}
		$scope.dtColumns = [
            DTColumnBuilder.newColumn('name').withOption('width',140).withTitle('角色'),
            DTColumnBuilder.newColumn('description').withOption('width',200).withTitle('角色说明'),
            DTColumnBuilder.newColumn('merchantName').withOption('width',200).withTitle('创建商户名称'),
            DTColumnBuilder.newColumn('createdDate').withOption('width',140).withTitle('创建时间').renderWith(function(e, dt, node, config){
            	return $filter('date')(new Date(node.createdDate), 'yyyy-MM-dd HH:mm:ss');
            }),
            DTColumnBuilder.newColumn(null).withTitle(function(){
            	return '操作';
            }).withOption('width', 140).withClass('text-center').renderWith(function(data, type, full, oSettings) {
            	if(data.isSuper){
            		return "";
            	}
            	var str = '<div class="form-inline">'
	                +'<div class="form-group"><a class="btn btn-link" ng-click="editRole('+ data.id +')">编辑</a></div>'
	                +'<div class="form-group"><a class="btn btn-link" ng-click="deleteRole('+ data.id +')">删除</a></div>';
	            str += '</div>';
                return str;
            })
        ];
		$scope.dtOptions = DTOptionsBuilder.newOptions()
		.withOption('id','id')
		.withFixedColumns({
            leftColumns: 2,
            rightColumns: 0
        })
        
		$scope.dtInstance = {
			serverData : systemService.getRoleList
		};

		function getParams(){
			var obj = {};
			if($scope.name){
				obj.name = $scope.name;
			}
			if($scope.fieldName){
                if($scope.fieldValue){
                	if($scope.fieldName == 'merchantId'){
                		var m = -1;
                		if(/^[1-9]\d*$/.test($scope.fieldValue)){
                			m = parseInt($scope.fieldValue) || -1;
                    		if(m > 2147483647){
                    			m = -1;
                    		}
                		}
                		obj[$scope.fieldName] = m;
                	}else{
                		obj[$scope.fieldName] = $scope.fieldValue;
                	}
                    
                }else {
                    obj[$scope.fieldName] = '';
                }
            }
			if($scope.createTime.startDate && $scope.createTime.endDate){
				obj.startTime = $scope.createTime.startDate.format('YYYY-MM-DD HH:mm:ss');
				obj.endTime = $scope.createTime.endDate.format('YYYY-MM-DD HH:mm:ss');
			}
			
			return obj;
		}

		//查询列表
		$scope.roleListForm = function(){
			$scope.dtInstance.query(getParams());
		}

		$scope.addRole = function(){
			$state.go('system.userAdmin.roleEdit');
		}

		$scope.editRole = function(id){
			$state.go('system.userAdmin.roleEdit',{id:id});
		}
		
		$scope.batchAdd = function(){
			$state.go('system.userAdmin.roleBatchEdit',{batchOper:'0'});
		}
		$scope.batchSub = function(){
			$state.go('system.userAdmin.roleBatchEdit',{batchOper:'1'});
		}
	   	$scope.deleteRole = function(id){
	   		var modalInstance = $uibModal.open({
		    	animation: true,
		        templateUrl: 'deleteRoleModal',
		        controller: 'deleteRoleCtrl',
		        size: 'sm',
		        resolve: {
		        	id: id,
		        	dtInstance: $scope.dtInstance,
		        	queryParams: getParams()
		        }
		    });
	   	}

		
	}
	function deleteRoleCtrl($scope, $uibModalInstance, systemService, id, dtInstance, queryParams){
		$scope.confirm = function() {
	    	$scope.isWorking = true;
	    	systemService.deleteRole({id: id}, function(result){
	    		$uibModalInstance.close();
	    		if(result.returnCode == '0'){
    				swal({type: 'success',title: '',text: result.message,closeOnConfirm: false,confirmButtonText: "确定",timer: 3000});
    				dtInstance.query(queryParams);
    			}else{
    				swal({type: 'error',title: '',text: result.message,closeOnConfirm: false,confirmButtonText: "确定"});
    			}
    			$scope.isWorking = false;
	    	});
	    };

	    $scope.cancel = function() {
	        $uibModalInstance.dismiss('cancel');
	    };
	}

	angular.module('system').controller('roleCtrl', roleCtrl)
	.controller('deleteRoleCtrl', deleteRoleCtrl)
})(angular);