(function () { 'use strict'; angular.module('ylmis').controller("homeCtrl", function ($scope, $rootScope, $state, $http, AuthUser, $alert, $interval, $loading, $modal ,$bsRouterState) { $scope.User = { name: AuthUser.getUser().UserName, roleName:AuthUser.getUser().RoleName }; $scope.panels = []; $scope.roleName = ''; $scope.roleList = []; ////站点ID $scope.menudatas = { getMenuUrl: '../../api/systemsetting/Menu/GetMenu', toggle: true }; $scope.now = new Date(); $scope.weekes = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; $scope.week = $scope.weekes[$scope.now.getDay()]; var timer = $interval(function () { $scope.now = new Date(); }, 1000); $scope.$on("menudatas.toggle", function (event, data) { $scope.menudatas.toggle = data; }); //$scope.$watch('roleList.selectedId', function (newVal, oldVal) { // if (newVal != undefined && newVal != '') { // //修改选择角色 // $scope.User.curRoleId = newVal; // AuthUser.setUser($scope.User); // $bsRouterState.tabs.length=0; // $scope.getMenuList(newVal); // $scope.getUserDataScope(); // } //}); $scope.getMenuList = function () { $scope.menuList = []; $scope.panels.length = 0; $http({ method: 'post', url: $scope.menudatas.getMenuUrl, data: {} }).then(function (result) { if (result.data.IsSuccess) { $scope.menuList = result.data.Data; //$state.get('home');若路由不存在,返回null //$state.is('home');若路由不存在,返回undifined; if ($scope.menuList.length > 0) { $scope.panels = $scope.buildMenuTree($scope.menuList, 'mainMenu'); $scope.initActive($scope.panels); } $bsRouterState.buildTabRoute($scope.menuList); } else { $alert({ title: '错误:', content: result.data.Message, placement: 'centre', type: 'info', show: true, duration: 3 }); } }); }; //begin菜单树方法 { //构建菜单树 $scope.buildMenuTree = function (menuList, parentId) { var returnVal = []; angular.forEach(menuList, function (menu) { if (menu.IsVisible != 1) { return true; } var pmenu = { id: menu.MenuId, title: menu.Title, icon: menu.Icon, name: menu.Url, url: menu.Url, cNodes: [], //子节点 childShow: 0, //子节点是否显示,0不显示,1显示 parentIds: parentId }; //遍历插入子节点 pmenu.cNodes = $scope.buildMenuTree(menu.Childrens, parentId + '.' + pmenu.id); returnVal.push(pmenu); }); return returnVal; }; //点击菜单事件 $scope.menutoggle = function (node) { var childShow = node.cNodes.length > 0 ? (node.childShow > 0 ? 0 : 1) : 1; var codes = node.parentIds.split('.'); codes.push(node.id); $scope.resetActive($scope.panels, codes); node.childShow = childShow; //$bsRouterState.$showTab(node.name, {}); }; //重置菜单选中项 $scope.resetActive = function (list, codes) { angular.forEach(list, function (node, index) { var index = $.inArray(node.id, codes); node.childShow = index >= 0 ? 1 : 0; if (node.cNodes != null && node.cNodes.length > 0) { $scope.resetActive(node.cNodes, codes); } }); }; //刷新页面时默认打开菜单 $scope.initActive = function (menuList) { //当前路由name var statename = $state.current.name; angular.forEach(menuList, function (value, index) { var p_realState = $scope.sliceStateName(value.name); if (p_realState == statename) { $scope.menutoggle(value); } $scope.initActive(value.cNodes); }); }; } //end var editPasswordCtrl = function ($scope, $http, AuthUser) { $scope.userinfovm = AuthUser.getUser(); $scope.pwd = { pwd_default: '', pwd_confirm: '' }; $scope.submitUser = function (isflag) { if (isflag) { if ($scope.pwd.pwd_default != $scope.pwd.pwd_confirm) { $scope.showMsg('失败', "两次输入的密码不一致!"); return; } //$scope.uservm.userpwd = $scope.pwd.pwd_default; //var data = $desData.GetToken( $scope.pwd.pwd_default); $loading.show(); $http({ method: "post", url: "../../api/systemsetting/User/UpdatePwd", data: { newPassword: $.md5($scope.pwd.pwd_default) } }).then(function (result) { $loading.hide(); if (result.data.IsSuccess) { $scope.showMsg('成功', '修改成功'); $scope.$hide(); } else { $scope.showMsg('失败', result.data.Message); } }), function (resp) { $scope.showMsg('错误', '服务器错误'); } } } }; editPasswordCtrl.$inject = ['$scope', '$http','AuthUser']; //定义模态框 var passwordModal = $modal({ resolve: { load: ['$ocLazyLoad', function ($ocLazyLoad) { //return [$ocLazyLoad.load('../main/user/edituser.css')]; }] }, scope: $scope, controller: editPasswordCtrl, templateUrl: '../main/home/editPwd.html', show: false, animation: 'am-fade-and-slide-top' }); $scope.updatePwd = function (id) { passwordModal.$promise.then(passwordModal.show); }; $scope.logout = function () { if (confirm('是否退出登录?')) { //$http.get("../../api/home/logout").then(function (req) { // if (req.data.success) { // AuthUser.clearUser(); // $bsRouterState.tabs.length = 0; // $state.go("login"); // //window.open("../public/index.html", "_self"); // } else { // $alert({ // title: '错误:', // content: '退出失败,请重试:' + req.data.msg, // placement: 'top', // type: 'info', // show: true, // duration: 3 // }); // } //}, function (err) { // $alert({ // title: '错误:', // content: '退出失败,请重试:' + err, // placement: 'top', // type: 'info', // show: true, // duration: 3 // }); //}); AuthUser.clearUser(); $bsRouterState.tabs.length = 0; if (angular.isDefined($rootScope.refreshTokenInterval)) { $interval.cancel($rootScope.refreshTokenInterval); $rootScope.refreshTokenInterval = undefined; } $state.go("login"); } }; $scope.showMsg = function (title, content) { $alert({ title: title + ':', content: content, placement: 'top', type: 'info', show: true, duration: 3 }); }; $scope.sliceStateName = function (statename) { if (statename == null) return null; var index = statename.indexOf('('); if (index >= 0) { return statename.slice(0, index); } return statename; }; $scope.getMenuList(); $bsRouterState.$showTab($bsRouterState.defaults.initTabName, {}); ////Set Waves //Waves.attach('.menu .list a', ['waves-block']); //Waves.init(); }); })();