var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function (e) { var t = ""; var n, r, i, s, o, u, a; var f = 0; e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++); r = e.charCodeAt(f++); i = e.charCodeAt(f++); s = n >> 2; o = (n & 3) << 4 | r >> 4; u = (r & 15) << 2 | i >> 6; a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function (e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0; e = e.replace(/[^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++)); o = this._keyStr.indexOf(e.charAt(f++)); u = this._keyStr.indexOf(e.charAt(f++)); a = this._keyStr.indexOf(e.charAt(f++)); n = s << 2 | o >> 4; r = (o & 15) << 4 | u >> 2; i = (u & 3) << 6 | a; t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function (e) { e = e.replace(/rn/g, "n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192); t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224); t += String.fromCharCode(r >> 6 & 63 | 128); t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function (e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r); n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1); t += String.fromCharCode((r & 31) << 6 | c2 & 63); n += 2 } else { c2 = e.charCodeAt(n + 1); c3 = e.charCodeAt(n + 2); t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63); n += 3 } } return t } } //文件预览 var currentHostname = window.location.hostname; let filePreviewUrl = "http://192.168.0.14:8012/onlinePreview?url="; //公司测试地址 文件预览的地址 // let filePreviewUrl = "http://116.63.172.211:8012/onlinePreview?url="; //线上正式环境--外网 // let filePreviewUrl = "http://172.16.0.235:8012/onlinePreview?url="; //线上正式环境--内网 // let filePreviewUrl = "http://140.210.209.102:8012/onlinePreview?url="; //部署上线地址 文件预览的地址 var fileUrl ="http://192.168.0.14:19118/hnBmw"; //公司测试地址 // var fileUrl ="http://116.63.172.211:1918/hnBmw"; //线上正式环境--外网 // var fileUrl ="http://172.16.0.235:1918/hnBmw"; //线上正式环境--内网 // var fileUrl ="http://140.210.209.102:1918/hnBmw"; //部署上线地址 // let filePreviewUrl = "http://" + currentHostname + ":8012/onlinePreview?url="; function filePreview(url) { let path; if (~url.indexOf("http")) { path = url; } else { path = fileUrl + url; } console.log("url:", path); let fileToken = generateToken(); var filePath = filePreviewUrl + encodeURIComponent(Base64.encode(path))+"&token=" + fileToken; console.log("url+token:", filePath); window.open(filePath); } function updateFileViewTime() { var fileId = localStorage.getItem("fileId"); $.ajax({ type: 'POST', async: true, // 默认异步true,false表示同步 url: smz_ht_url + "/salaryFile/updateFileViewTime", // 请求地址 // contentType: "application/json; charset=utf-8", contentType: "application/x-www-form-urlencoded", dataType: 'json', // 服务器返回数据类型 data: { fileId: fileId, } , //获取提交的表单字段 success: function (data) { }, error: function (XMLHttpRequest, textStatus, e) { top.layer.close(loadingMsg); layer.msg('数据请求发生异常,请稍后重试', {icon: 16, scrollbar: false}); } }); } /** * 获取 blob * @param {String} url 目标文件地址 * @return {Promise} */ function getBlob(url) { return new Promise(resolve => { const xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'blob'; xhr.onload = () => { if (xhr.status === 200) { resolve(xhr.response); } }; xhr.send(); }); } /** * 保存 * @param {Blob} blob * @param {String} filename 想要保存的文件名称 */ function saveAs(blob, filename) { if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, filename); } else { const link = document.createElement('a'); const body = document.querySelector('body'); link.href = window.URL.createObjectURL(blob); link.download = filename; // fix Firefox link.style.display = 'none'; body.appendChild(link); link.click(); body.removeChild(link); window.URL.revokeObjectURL(link.href); } } /** * 下载 * @param {String} url 目标文件地址 * @param {String} filename 想要保存的文件名称 */ function download(url, filename) { getBlob(url).then(blob => { saveAs(blob, filename); }); }