main.js 5.09 KB
// angular 主程序js,必须先导入,配置一些全局设置

var ScheduleApp = angular.module('ScheduleApp', [
    'ui.router', // ui-route跳转
    'ui.bootstrap', // ui bootstrap封装
    'oc.lazyLoad', // 动态加载模块(html,js,css等)
    'ngSanitize', // 净化html标签,配合ng-bind-html使用
    'ngResource' // resource服务
]);

/**
 * 用于请求通知。
 */
ScheduleApp.factory('requestNotificationChannel', ['$rootScope', function($rootScope) {
    // 通知消息常量
    var _START_REQUEST_ = '_START_REQUEST_'; // 开始请求通知message
    var _END_REQUEST_ = '_END_REQUEST_'; // 请求结束通知message

    // 计数器
    var activeCalls = 0;

    // 发布开始请求通知
    var requestStarted = function() {
        activeCalls += 1;
        console.log("activeCalls=" + activeCalls);
        $rootScope.$broadcast(_START_REQUEST_);
    };
    // 发布请求结束通知
    var requestEnded = function() {
        activeCalls -= 1;
        console.log("activeCalls=" + activeCalls);
        $rootScope.$broadcast(_END_REQUEST_);
    };

    /**
     * 订阅开始请求通知。
     * @param $scope 作用域
     * @param handler 通知处理器函数
     */
    var onRequestStarted = function($scope, handler) {
        $scope.$on(_START_REQUEST_, function(event) {
            handler();
        });
    };
    /**
     * 订阅请求结束通知。
     * @param $scope 作用域
     * @param handler 通知处理器函数
     */
    var onRequestEnded = function($scope, handler) {
        $scope.$on(_END_REQUEST_, function(event) {
            handler();
        });
    };

    return {
        requestStarted : requestStarted,
        requestEnded : requestEnded,
        onRequestStarted : onRequestStarted,
        onRequestEnded : onRequestEnded
    };

}]);

// http 拦截器
ScheduleApp.factory(
    'myInterceptor',
    [
        'requestNotificationChannel',
        '$q',
        function(requestNotificationChannel, $q) {
            return {
                request: function(config) {
                    requestNotificationChannel.requestStarted();
                    return config;
                },
                requestError: function(rejection) {
                    requestNotificationChannel.requestEnded();
                    return rejection;
                },
                response: function(response) {
                    requestNotificationChannel.requestEnded();

                    return response;
                },
                responseError: function(rejection) {
                    requestNotificationChannel.requestEnded();

                    // 处理错误,springboot会包装返回的错误数据
                    // 如:{"timestamp":1478674739246,"status":500,"error":"Internal Server Error","exception":"java.lang.ClassCastException","message":"java.lang.String cannot be cast to java.lang.Long","path":"/tidc/importfile"}

                    var status = rejection.status;
                    var path = rejection.data.path;
                    var message = rejection.data.message;
                    var output = [];
                    output.push("状态编码:" + status);
                    output.push("访问路径:" + path);
                    output.push("错误消息:" + message);
                    if (status) {
                        if (status == 500) {
                            alert("服务端错误:" + "\n" + output.join("\n"));
                        } else if (status == 407) {
                            alert("请重新登录:" + "\n" + output.join("\n"));
                        } else if (status == -1) {
                            alert("貌似服务端连接不上");
                        } else {
                            alert("其他错误:" + "\n" + output.join("\n"));
                        }
                    } else {
                        alert("我擦,后台返回连个状态码都没返回,见鬼了!");
                    }

                    return $q.reject(rejection);
                }
            };
        }
    ]
);

ScheduleApp.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
    $httpProvider.interceptors.push('myInterceptor');
}]);

/** ocLazyLoader 配置 */
ScheduleApp.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
    $ocLazyLoadProvider.config({
        // TODO:全局配置在这里
    });
}]);

/** 配置全局配置信息 */
ScheduleApp.factory('settings', ['$rootScope', function($rootScope) {
    // 封装 settings服务,并设置给 $rootScope同名对象
    var settings = {
        // TODO:
    };

    $rootScope.settings = settings;

    return settings;
}]);

/** Schedule App 主应用控制器 */
ScheduleApp.controller('ScheduleAppController', ['$scope', function($scope) {
    $scope.$on('$viewContentLoaded', function(event) {
        console.log("子页面已载入:" + event);
    });
}]);