ackLib.js 12.7 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
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
//常用公用方法
commonModule.factory('ackLib', ['$translate', function ($translate) {
    'use strict';

    var ackLib = {};

    //辅助方法
    ackLib.Helper = {
        //翻译,仅此一个入口
        translate: function (value) {
            return $translate.instant(value);
        }
    }

    //验证
    ackLib.validation = {
        //是否为日期验证,如果验证成功,返回true,否则返回false
        isDate: function (value, dateSeparator) {
            try {
                var dateSplit = dateSeparator || constant.date.dateFormatSeparator;
                var arr = value.split(dateSplit);
                var year = parseInt(arr[0]);
                var month = parseInt(arr[1]);
                var day = parseInt(arr[2]);
                var date = new Date(year, month - 1, day);
                if (date.getFullYear() == year && date.getMonth() + 1 == month && date.getDate() == day) {
                    return true;
                } else {
                    return false;
                }
            }
            catch (e) {
                return false;
            }
        },
        isEmpty: function (str) {
            if (!str || str === '') {
                return true;
            }
            else {
                return false;
            }
        }
    };

    //翻译,所有的翻译都放这,到时直接调用,不用每个页面都去做翻译
    ackLib.translation = {};
    //常用翻译
    ackLib.translation.common = {
        Remark: ackLib.Helper.translate('Remark'), //备注
        DownloadTemplateFail: ackLib.Helper.translate('DownloadTemplateFail'), //下载模板失败
        FileUploadSuccess: ackLib.Helper.translate('FileUploadSuccess'), //文件上传成功
        SelectAtLeastOneRecord: ackLib.Helper.translate('SelectAtLeastOneRecord'), //请至少选择一条记录
        OperateSuccess: ackLib.Helper.translate('OperateSuccess'), //操作成功
        UploadSuccess: ackLib.Helper.translate('UploadSuccess'), //上传成功
        PleaseSelectFile: ackLib.Helper.translate('PleaseSelectFile'), //请先选择文件
        NoDataText: ackLib.Helper.translate('NoDataText'), //没有数据
        pleaseSelect: ackLib.Helper.translate('ChoosePlaceholder'), //-请选择-
        pleaseInput: ackLib.Helper.translate('InputPlaceholder'), //-请输入-
        PleaseSelectRecordToDelete: ackLib.Helper.translate('PleaseSelectRecordToDelete'), //请选择需要删除的记录
        FileName: ackLib.Helper.translate('FileNameTitle'), //文件名称
        ErrorMessage: ackLib.Helper.translate('ErrorMessage'), //错误消息
        Confirm: ackLib.Helper.translate('Confirm'), //错误消息
        Cancel: ackLib.Helper.translate('Cancel'), //错误消息
    };

    //发票相关
    ackLib.translation.inputInvoice = {
        AllowRefundInvoiceInfo: ackLib.Helper.translate('AllowRefundInvoiceInfo'), //只有状态为识别成功,识别失败,无法识别,待退票或待匹配的发票才可以做退票操作
        SpecialToInvoiceRecognizeSuccessInfo: ackLib.Helper.translate('SpecialToInvoiceRecognizeSuccessInfo'), //对于发票来源为“发票章错误,待退票”且发票状态为【待退票】的发票,只支持单张退票,不支持与其他发票一起批量退票
        SpecialToInvoicePendingRefundInfo: ackLib.Helper.translate('SpecialToInvoicePendingRefundInfo'), //对于发票来源为“发票章错误,待退票”且发票状态为【识别成功】的发票,支持批量退票,不支持将此类发票与其他发票一起批量退票。
        AllowInvoiceRecognizeWithUpload: ackLib.Helper.translate('AllowInvoiceRecognizeWithUpload'), //只有已上传或者已补录的发票可以做发票识别
        RecognizeInvoiceSuccess: ackLib.Helper.translate('RecognizeInvoiceSuccess'), //发票识别成功
        VerifyInvoiceSuccess: ackLib.Helper.translate('VerifyInvoiceSuccess'), //发票验真成功
        CannotExpireInvoice: ackLib.Helper.translate('CannotExpireInvoice'), //已认证、已失效、已清理、已匹配、已退票的发票不能做失效操作
        ConfirmToExpireInvoice: ackLib.Helper.translate('ConfirmToExpireInvoice'), //确认要把选中的发票进行失效处理吗
    }

    //浏览器检测
    ackLib.Browser = {
        isIE: (function () {
            if (navigator.userAgent.match(/msie/i) || navigator.userAgent.match(/trident/i)) {
                return true;
            }
            return false;
        })(),
    };

    //小数点位数
    var fixedNumber = constant.toFixedNumber;
    //数字的格式化
    ackLib.Number = {
        toFixed: function (num) {
            return num.toFixed(fixedNumber);
        },
    }


    //阻止冒泡
    ackLib.stopPropagation = function (event) {
        event = event || window.event;
        if (event.stopPropagation) {
            event.stopPropagation();
        } else {
            event.cancelBubble = true;
        }
    };

    //动态构造table
    ackLib.populateTable = function (columnList, valueList) {

        var table = '';
        var tableHeader = '<table class="table table-striped table-hover table-bordered" style="max-height:200px; overflow:auto; text-align:left">';
        var tbody = '';
        var th = '';

        columnList.forEach(function (item) {
            th += '<th>' + item + '</th>';
        });
        var thead = '<tr>' + th + '</tr>';

        valueList.forEach(function (row) {
            var tr = '';
            columnList.forEach(function (item) {
                tr += '<td>' + row[item] + '</td>';
            });

            tbody += '<tr>' + tr + '</tr>';
        });

        table = tableHeader + thead + tbody + '</table>';

        // console.log(table);

        return table;
    };


    return ackLib;

}]);


/*文件上传:目前支持单文件上传,支持跨域上传
创建于:2017/09/08
*/
commonModule.factory('ackFileUploader', ['Upload', '$q', 'apiInterceptor', 'ackLib', function (Upload, $q, apiInterceptor, ackLib) {
    'use strict';

    var defaultOptions = {
        chunkSize: '10MB',
        resumable: true,
        token: $('input[name="__RequestVerificationToken"]').val(),
        url: '',
        cancel: false,
        maximumSize: 1024 * 1024 * 10,  //10MB,
        isAppend: true, //默认为追加
    };

    defaultOptions.headers = {
        'Access-Control-Allow-Origin': '*',
162
        Authorization: apiInterceptor.tokenType + ' ' + apiInterceptor.apiToken(),
eddie.woo's avatar
eddie.woo committed
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
        __RequestVerificationToken: defaultOptions.token,
        withCredentials: true
    }


    var ackFileUploader = {

        //上传单个文件
        upload: function (myfile, options) {
            var deferred = $q.defer();
            options = $.extend(true, {}, defaultOptions, options);
            var fileName = myfile.name;

            var self = ackFileUploader;

            if (!fileName || fileName.size == 0) {
                deferred.reject("FileIsEmpty");
            }
            if (myfile.size > options.maximumSize) {
                deferred.reject("FileSizeExceedMaximum");
            }
            var optionsData = {
                cancel: options.cancel,
                filename: fileName,
            }

            optionsData = $.extend(true, {}, optionsData, options.data);
            Upload.upload({
                url: options.url,
                data:optionsData,
                file: myfile,
                resumeChunkSize: options.resumable ? options.chunkSize : null,
                headers: options.headers,
                withCredentials: true
            }).then(function (resp) {
                deferred.resolve(resp);
            });
            return deferred.promise;
        },

        //uploadMultiple: function (files, options) {
        //    if (files && files.length > 0) {
        //        for (var i = 0; i < files.length; i++) {
        //            ackFileUploader.upload(files[i], options);
        //        }
        //    }
        //}
    };

    return ackFileUploader;

}]);


//消息弹框
commonModule.factory('ackMessageBox', ['$translate', 'SweetAlert', '$q', 'ackLib', function ($translate, SweetAlert, $q, ackLib) {
    'use strict';
    //只负责弹框,保持独立性
    var messageBox = {
        success: function (title, message) {
            if (arguments.length == 1) { // message is undefined
                SweetAlert.success(title, '');
            }
            else if (arguments.length == 2) { // message is undefined
                SweetAlert.success(title, message);
            }
        },

        warning: function (title, message) {
            if (arguments.length == 1) { // message is undefined
                SweetAlert.warning(title, '');
            }
            else if (arguments.length == 2) { // message is undefined
                SweetAlert.warning(title, message);
            }
        },

        close: function () {
            swal.close();
        },

        //有确认和取消按钮
        confirm: function (title, text, customClass) {
            var deferred = $q.defer();
            SweetAlert.swal({
                title: title,
                text: text,
                html: true,
                type: "warning",
                customClass: customClass || '',
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                allowOutsideClick: false,
                confirmButtonText: ackLib.translation.common.Confirm,
                cancelButtonText: ackLib.translation.common.Cancel,
                closeOnConfirm: true,
                closeOnCancel: true
            },
              function (isConfirm) {
                  deferred.resolve(isConfirm);
              });

            return deferred.promise;
        },

        //没有取消按钮
        info: function (title, text, customClass) {
            var deferred = $q.defer();

            SweetAlert.swal({
                title: title,
                text: text,
                html: true,
                type: "warning",
                customClass: customClass || 'swal-info',
                showCancelButton: false,
                confirmButtonColor: "#DD6B55",
                allowOutsideClick: false,
                confirmButtonText: ackLib.translation.common.Confirm,
                cancelButtonText: ackLib.translation.common.Cancel,
                closeOnConfirm: true,
                closeOnCancel: false
            },
              function (isConfirm) {
                  deferred.resolve(isConfirm);
              });

            return deferred.promise;
        },
    };

    return messageBox;

}]);


//UIModal模态框
commonModule.factory('ackUibModal', ['$translate', 'SweetAlert', '$q', '$uibModal', '$document', function ($translate, SweetAlert, $q, $uibModal, $document) {
    'use strict';


    //模态框
    /*参数currentScope:页面的scope
    templateUrl:定义弹框的模板,
    windowClass:定义弹框的css,
    parentSelector:父框,
    isbackrop:,‘static’, or 'trye'
    resetCallbackFunc: 回调函数
    */
    var createModalInstance = function (currentScope, templateUrl, windowClass, parentSelector, isbackrop, resetCallbackFunc) {
        var thisModalService = new Object();
        isbackrop = isbackrop ? isbackrop : true;

        var parentElem = parentSelector ? angular.element($document[0].querySelector(parentSelector)) : undefined;

        thisModalService.open = function () {
            var modalInstance = $uibModal.open({
                animation: true,
                ariaLabelledBy: 'modal-title',
                ariaDescribedBy: 'modal-body',
                backdrop: isbackrop,  //点击父页面不会自动关闭 
                templateUrl: templateUrl, //script模板
                windowClass: windowClass, //弹框页面的css
                scope: currentScope,
                appendTo: parentElem,
            });

            thisModalService.modalInstance = modalInstance;

            //modal关闭之后接收返回值的函数
            modalInstance.result.then(function (data) {
                //call close的时候调用这里

            }, function () {
                //取消的时候触发这里

            }).finally(function () {
                resetCallbackFunc(); //回调函数
                thisModalService = null;
            });
        };

        thisModalService.close = function (result) {
            if (thisModalService.modalInstance) {
                thisModalService.modalInstance.close(result);
            }
        };

        thisModalService.cancel = function () {
            if (thisModalService.modalInstance) {
                thisModalService.modalInstance.dismiss('cancel');
            }
        }
        return thisModalService;
    };

    return createModalInstance;


}]);