main.js 14.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
// angular 主程序js,必须先导入,配置一些全局设置

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

ScheduleApp.factory('UserPrincipal', [
    '$http',
    function($http) {
        // 登录后获取用户公司信息

        var gsinfos = []; // 原始返回的对象数据
        var gsinfo_strs = []; // 拼装以后的(公司代码_分公司代码)
        var gsinfo_strs_u = []; // 拼装以后的(分公司代码_公司代码)
        var gsinfo_strs_query = []; // 拼装以后的(公司代码_分公司代码),带单引号

        $http({
            method: 'GET',
            url: '/user/companyData'
        }).then(function(result) {
            if (angular.isArray(result.data)) {
                gsinfos = result;
                angular.forEach(result.data, function(obj) {
                    var gsbm = obj.companyCode;
                    angular.forEach(obj.children, function(obj2) {
                        var fgsbm = obj2.code;
                        gsinfo_strs.push(gsbm + "_" + fgsbm);
                        gsinfo_strs_u.push(fgsbm + "_" + gsbm);
                        gsinfo_strs_query.push("'" + gsbm + "_" + fgsbm + "'");
                    });
                });

            }
            console.log(gsinfos);
            console.log(gsinfo_strs);
        });

        return {
            getGsStrs: function() {
                return gsinfo_strs;
            },
            getGsStrsU: function() {
                return gsinfo_strs_u;
            },
            getGsStrsQuery: function() {
                return gsinfo_strs_query;
            }
        };
    }
]);

ScheduleApp.factory('DataStore', [
    '$http',
    '$q',
    function($http, $q) {
        // 本地数据存储,如车辆数据,人员数据
        var dataMap = {
            "cl": [], // 车辆信息
            "ry": [], // 人员信息
            "ddreasons": [ // 调度原因(调度执勤日报里的)
                {code: "0", name: "营运"},
                {code: "1", name: "缺车"},
                {code: "2", name: "气候"},
                {code: "3", name: "肇事"},
                {code: "4", name: "纠纷"},
                {code: "5", name: "抽减"},
                {code: "6", name: "配车"},
                {code: "7", name: "故障"},
                {code: "8", name: "路阻"},
                {code: "9", name: "客稀"},
                {code: "10", name: "吊慢"},
                {code: "11", name: "其他"},
                {code: "12", name: "保养"},
                {code: "13", name: "缺人"},
                {code: "14", name: "援外"}
            ],
            "zdlytype" : [ // 站点路由类型
                {name: "当前版本", value: 1},
                {name: "所有版本(待更新,当前,历史)", value: 2}
            ]
        };

        var dataPromise_cars = function() {
            var deferred = $q.defer();
            $http({
                method: 'GET',
                url: '/cars_sc/all'
            }).then(function(result) {
                // 简拼数据
                var dd = result.data.data;
                angular.forEach(result.data.data, function(obj) {
                    //// 全拼
                    //obj["$fullChars"] = pinyin.getFullChars(obj.insideCode ? obj.insideCode: "");
                    //// 简拼
                    //obj["$camelChars"] = pinyin.getCamelChars(obj.insideCode ? obj.insideCode: "");
                    // 全拼
                    obj["$fullChars"] = obj.insideCode? obj.insideCode: "";
                    // 简拼
                    obj["$camelChars"] = obj.insideCode? obj.insideCode: "";
                    // 原值
                    obj["$calcu_str"] = obj.insideCode? obj.insideCode: "";
                });
                deferred.resolve(dd);
            });
            return deferred.promise;
        };

        var dataPromise_ees = function() {
            var deferred = $q.defer();

            $http({
                method: 'GET',
                url: '/ee/all_py'
            }).then(function(result) {
                // 简拼数据
                var dd = result.data;
                angular.forEach(result.data, function(obj) {
                    // 全拼
                    obj["$fullChars"] = obj.fullChars;
                    // 简拼
                    obj["$camelChars"] = obj.camelChars;
                    // 原值
                    obj["$calcu_str"] = obj.name + "-" + obj.workId;
                });
                deferred.resolve(dd);
            });

            return deferred.promise;
        };

        var refreshCars = function() {
            dataPromise_cars().then(function(res) {
                console.log("refreshCars");
                dataMap.cl = [];
                angular.forEach(res, function(obj) {
                    dataMap.cl.push(obj);
                });
            });
        };

        var refreshEes = function() {
            dataPromise_ees().then(function(res) {
                console.log("refreshEes");
                dataMap.ry = [];
                angular.forEach(res, function(obj) {
                    dataMap.ry.push(obj);
                });
            });
        };

        // 初始化
        refreshCars();
        refreshEes();

        return {
            getData: function(type) {
                if (dataMap[type]) {
                    return dataMap[type];
                } else {
                    return [];
                }
            },
            refreshData: function(type) {
                if (type == 'cl') {
                    refreshCars();
                } else if (type == 'ry') {
                    refreshEes();
                }
            }
        }

    }
]);

ScheduleApp.factory('UserInfo', [
    '$http',
    function($http) {
        var user;
        $http({
            method: 'GET',
            url: '/user/getCurrentUser'
        }).then(function(result) {
            user = result.data;
            var roles = [];
            angular.forEach(user.roles, function(obj) {
                roles.push(obj.codeName);
            });
            user.roles = roles.join(',');
        });

        return {
            getRole: function() {
                return user.roles;
            }
        }
    }
]);

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

    // 计数器
    var activeCalls = 0;

    // 发布开始请求通知
    var requestStarted = function(requestInfo) {
        activeCalls += 1;
        console.log("activeCalls=" + activeCalls);
        $rootScope.$broadcast(_START_REQUEST_, requestInfo);
    };
    // 发布请求结束通知
    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, requestInfo) {
            handler(requestInfo);
        });
    };
    /**
     * 订阅请求结束通知。
     * @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(config);
                    return config;
                },
                requestError: function(rejection) {
                    requestNotificationChannel.requestEnded();
                    return rejection;
                },
                response: function(response) {
                    requestNotificationChannel.requestEnded();

                    var data = response.data;
                    var output = [];
                    if (data.status == '407') {
                        alert("请重新登录!");
                        return $q.reject(response);
                    } else if (data.status == '500') {
                        output.push("状态编码:" + data.status);
                        output.push("访问路径:" + data.path);
                        output.push("错误消息:" + data.message);
                        alert("服务端错误:" + "\n" + output.join("\n"));
                        return $q.reject(response);
                    } else {
                        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 output = [];
                    if (!rejection.status) {
                        // alert("我擦,后台返回连个状态码都没返回,见鬼了,服务器可能重启了");
                    } else if (rejection.status == -1) {
                        // 服务器断开了
                        // alert("貌似服务端连接不上");
                        console.log("貌似服务端连接不上");
                    } else {
                        output.push("状态编码:" + rejection.status);
                        // output.push("错误内容:" + angular.toJson(rejection.data));
                        if (rejection.data) {
                            output.push("错误内容:" + rejection.data.message);
                        } else {
                            output.push("错误内容:" + rejection.message);
                        }

                        if (rejection.status == 500) {
                            alert("服务端错误:" + "\n" + output.join("\n"));
                        } else if (rejection.status == 407) {
                            alert("请重新登录:" + "\n" + output.join("\n"));
                        } else {
                            alert("其他错误:" + "\n" + output.join("\n"));
                        }
                    }

                    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',
    'UserPrincipal',
    'DataStore',
    function($scope, UserPrincipal, DataStore) {
        $scope.$on('$viewContentLoaded', function(event) {
            console.log("子页面已载入:" + event);
        });

        // 获取一次用户身份信息
        UserPrincipal.getGsStrs();
        // 获取本地数据
        DataStore.getData("cl");
        DataStore.getData("ry");
    }
]);

// JQuery插件,使$ajax支持resposetype=arraybuffer,二进制输出,html5的特性
// use this transport for "binary" data type
$.ajaxTransport("+binary", function(options, originalOptions, jqXHR){
    // check for conditions and support for blob / arraybuffer response type
    if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob)))))
    {
        return {
            // create new XMLHttpRequest
            send: function(headers, callback){
                // setup all variables
                var xhr = new XMLHttpRequest(),
                    url = options.url,
                    type = options.type,
                    async = options.async || true,
                // blob or arraybuffer. Default is blob
                    dataType = options.responseType || "blob",
                    data = options.data || null,
                    username = options.username || null,
                    password = options.password || null;

                xhr.addEventListener('load', function(){
                    var data = {};
                    data[options.dataType] = xhr.response;
                    // make callback and send data
                    callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
                });

                xhr.open(type, url, async, username, password);

                // setup custom headers
                for (var i in headers ) {
                    xhr.setRequestHeader(i, headers[i] );
                }

                xhr.responseType = dataType;
                xhr.send(data);
            },
            abort: function(){
                jqXHR.abort();
            }
        };
    }
});