header.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @ngdoc directive
  3. * @name izzyposWebApp.directive:adminPosHeader
  4. * @description
  5. * # adminPosHeader
  6. */
  7. angular.module('sentinelDashboardApp')
  8. .directive('header', ['VersionService', 'AuthService', function () {
  9. return {
  10. templateUrl: 'app/scripts/directives/header/header.html',
  11. restrict: 'E',
  12. replace: true,
  13. controller: function ($scope, $state, $window, VersionService, AuthService) {
  14. VersionService.version().success(function (data) {
  15. if (data.code == 0) {
  16. $scope.dashboardVersion = data.data;
  17. }
  18. });
  19. if (!$window.localStorage.getItem("session_sentinel_admin")) {
  20. AuthService.check().success(function (data) {
  21. if (data.code == 0) {
  22. $window.localStorage.setItem('session_sentinel_admin', JSON.stringify(data.data));
  23. handleLogout($scope, data.data.id)
  24. } else {
  25. $state.go('login');
  26. }
  27. });
  28. } else {
  29. try {
  30. var id = JSON.parse($window.localStorage.getItem("session_sentinel_admin")).id;
  31. handleLogout($scope, id);
  32. } catch (e) {
  33. // Historical version compatibility processing, fixes issue-1449
  34. // If error happens while parsing, remove item in localStorage and redirect to login page.
  35. $window.localStorage.removeItem("session_sentinel_admin");
  36. $state.go('login');
  37. }
  38. }
  39. function handleLogout($scope, id) {
  40. if (id == 'FAKE_EMP_ID') {
  41. $scope.showLogout = false;
  42. } else {
  43. $scope.showLogout = true;
  44. }
  45. }
  46. $scope.logout = function () {
  47. AuthService.logout().success(function (data) {
  48. if (data.code == 0) {
  49. $window.localStorage.removeItem("session_sentinel_admin");
  50. $state.go('login');
  51. } else {
  52. alert('logout error');
  53. }
  54. });
  55. }
  56. }
  57. }
  58. }]);