ng1附件下载指令

/**
 *只支持下载,有部分是流程上的或者是微信上需要的代码,可省略
 *messager是自定义封装弹框消息
 *bpm.Service是ajax请求的封装
 *showObj.attachIdCardLit是在模块的控制器中获取到附件的相关文件说明字段(具体文件字段在指令调用的方法参数中有体现出来)
 *下载地址跟文件相关内容通过外部双向绑定 
 * <outside-attachment options="commonAttachment" ng-model="showObj.attachIdCardLit" ng-if="showObj.attachIdCardLit.length>0"></outside-attachment>
 */
m.directive('outsideAttachment',['$state','messager','bpm.Service','$window','$timeout','$ionicActionSheet',
    function($state,messager,bpmSvc,$window,$timeout,$ionicActionSheet){
        //先签名
        if (!window.isWxLoaded){
            bpmSvc.getSignature({"url":window.__G_MOSS_Download+"/wechat/"},function(data, successful){
                if (successful){
                    wx.config({
                        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                        appId:data.appid, // 必填,企业号的唯一标识,此处填写企业号corpid
                        timestamp: data.timestamp, // 必填,生成签名的时间戳
                        nonceStr: data.noncestr, // 必填,生成签名的随机串
                        signature: data.signature,// 必填,签名,见附录1
                        jsApiList: ['previewImage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
                    }); 
                    window.isWxLoaded = false;
                }
            });
        }

        wx.error(function(res){
            window.isWxLoaded = false;
        });
        return {
            restrict: 'E',
            require: '^ngModel',
            scope: {
                ngModel: "=",//双向绑定外部文件内容
                options: "=",//双向绑定的下载地址
            },
            templateUrl: 'tpl/outsideAttachment.html',
            controller:['$scope',function(scope){

                var _options = scope.options||{};//外部绑定的下载地址
                var _defaults_ = {
                fileMaxSize: 20,
                permission: ['L', 'D', 'U', 'C'] //未进入流程默认拥有所有权限
                };

                function fFileSize(inputValue) {
                    if (!inputValue) {
                        return "无";
                    };
                    var mCount = inputValue / 1024 / 1024;
                    if (mCount > 1) {
                        return mCount.toFixed(2).toString() + "M";
                    }
                    var kCount = inputValue / 1024;
                    if (kCount > 1) {
                        return kCount.toFixed(2).toString() + "K";
                    }
                    return inputValue + "字节";
                }
                //判断下载的文件类型
                var showAbled = function(fileSuffix) {
                    if (fileSuffix == ".docx") return true;
                    if (fileSuffix == ".doc") return true;
                    if (fileSuffix == ".xlsx") return true;
                    if (fileSuffix == ".xls") return true;
                    if (fileSuffix == ".pdf") return true;
                    if (fileSuffix == ".png") return true;
                    if (fileSuffix == ".jpg") return true;
                    if (fileSuffix == ".gif") return true;
                    return false;
                }
                            // 执行下载操作
                scope.showSheet = function(attachment) {
                    scope.titleShow = '下载';
                    scope.isImageType = false;
                    function doShowAttach() {
                        bizId =attachment.businessId;
                        aid = attachment.id;
                        aSize = attachment.attachment.fileSize;
                        aFix = attachment.attachment.fileSuffix;
                        var altTxt = "您将要" + scope.titleShow + "的文件体积是" + fFileSize(aSize) + ",";
                        var contentShow = altTxt + "是否要继续下载?(将使用内置浏览器打开,下载后可返回微信,请注意您的流量使用情况!)";
                        messager.confirm("是否继续" + scope.titleShow, contentShow, function() {
                                var imgArray = [];
                                curImageSrc = {};
                                curImageSrc = _options.downloadUrl+"?bizId="+bizId + "&fileSource=APP&id=" + attachment.id + "&taskId=" + (scope.taskId || '') + "&openid=" + (window.loginToken || '');
                                    $window.location.href = curImageSrc;
                            },
                            "继续" + scope.titleShow,
                            "取消"
                        );

                    }
                    doShowAttach();
                };
            }]
        }   
}])

指令html模板代码↓

<div class="card padding">
<li ng-repeat="attachment in ngModel" style="list-style-type: none;" ng-if="ngModel">
            <span class="col col-80">
                <a  ng-click="showSheet(attachment);">
                    <span class="lineheight-30" ng-bind="attachment.fileName | cut:true:10:'...'"></span>
                    <span class="lineheight-30" ng-bind="'('+(attachment.attachment.fileSize/1024|number:0) + 'K)'"></span>
                </a>
            </span>
        </li>
    <label ng-if="!ngModel">暂无附件</label>
</div>

在使用该指令模块的控制器代码部分↓

//附件下载的地址
        $scope.commonAttachment = {};
        $scope.commonAttachment = {
            "downloadUrl": window.__rm("/scm/supplierInfoAppl/download.do"),
            "previewUrl": window.__rm("/scm/supplierInfoAppl/preview.do")
        };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值