$(function(){ let n = getN() if(n.u){ getFileObj(n.u,function(obj){ $(".name").html(obj.title) $(".size").html(obj.size) $(".btn").attr("data-url",obj.fileUrl).attr("data-name",obj.title) }) }else if(n.file){ let fileName = n.fileName || "未知文件名" fileName = decodeURIComponent(fileName) $(".name").html(fileName) $(".size").hide() $(".btn").attr("data-url",n.file).attr("data-name",fileName) }else{ $(".name").html("无法识别文件") $(".size").hide() $(".btn").hide() } $("body").on("click",".btn",function(){ let url = $(this).attr("data-url") let name = $(this).attr("data-name") if(url){ fileDownloadFun(url,name,true) } }) }) function getFileObj(u,callback){ let url = "/nportal/fwebapi/cms/lowcode/file/getFileById?id="+u timedGetText(url,function(res){ res = JSON.parse(res) let fileObj = res.data.data if(fileObj){ $require(["pl_util"], function () { fileObj.fileUrl = $.handleDataFile(fileObj.fileUrl) callback(fileObj) }) }else{ $(".name").html("无法识别文件") $(".btn").hide() } }) } function timedGetText( url, callback ){ var request = new XMLHttpRequest(); request.open( "GET", url ,false); request.send( null ); if(request.status===200){ callback( request.responseText ); } } function getSearch(){ let theRequest = new Object(); let wholeUrl = decodeURIComponent(location.href) let cArr = wholeUrl.match(/c-.*?\.html/) if(cArr){ let cc = cArr[0].substr(2) cc = cc.substring(0,cc.length-5) let ccs = cc.split("&") for (let i = 0; i < ccs.length; i++) { theRequest[ccs[i].split("=")[0]] = unescape(ccs[i].split("=")[1]); } } let url = decodeURI(location.search); if (url.indexOf("?") != -1) { let str = url.substr(1); let strs = str.split("&"); for (let i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } } return theRequest } function getN(){ let n = { file:"", u:"", fileName:"" } let params = getSearch() if(!params.u && !params.file){ let path = location.pathname path = location.pathname.replace("/npublic","") let arr = path.split("/") if(arr[2]=='h'){ //file类型 n.file = decodeURIComponent(arr[3]) let farr = n.file.split("?")[0].split("/") n.fileName = farr[farr.length-1] }else{ //u类型 n.u = arr[2] n.fileName = arr[3]=='0.pdf'?"":arr[3] } }else if(params.u){ n.u = params.u n.fileName = params.fileName }else if(params.file){ n.file = params.file } return n } //截取地址中domain function getDomain(file){ let domain = file.split("//")[1].split("/")[0] var domainArr = domain.split("."); var preDomain=domainArr[domainArr.length - 2] + "." + domainArr[domainArr.length - 1]; if(/(com|cn|org|net|xin|edu|ac)\..*/.test(preDomain)){ preDomain=domainArr[domainArr.length - 3]+"."+domainArr[domainArr.length - 2] + "." + domainArr[domainArr.length - 1]; } return preDomain } //文件下载方法 function fileDownloadFun(url,name,key){ if(!url){ return } let allowArr = ['site.cn','thefastfile.com','thefastvideo.com','thefastimg.com','yun300.cn','faststatics.com'] allowArr.push(getDomain(location.href)) if((/^http/.test(url) && allowArr.indexOf(getDomain(url))==-1) || /^ftp/.test(url)){ console.log("非法文件") return } var a = document.createElement("a") if(key){ url = decodeURIComponent(decodeURIComponent(url)) let pp="" if(url.indexOf('?')>-1){ let fa = url.split("?") url = fa[0] pp = fa[1] } let urlArr = url.split("/") urlArr[urlArr.length-1]=encodeURIComponent(urlArr[urlArr.length-1]) let turl = urlArr.join("/") if(pp){ turl = turl+"?"+pp } if(/^http:/.test(turl)){ turl = turl.replace(/^http:/,"https:") } let nurl = `/downfilebizce/get?n=${encodeURIComponent(turl)}` a.href = nurl }else{ a.download = name // 设置下载文件名 a.href = url } a.target = "_blank" document.body.appendChild(a) a.click() document.body.removeChild(a) //释放标签 }