Files
XueXiTongSrtDownloads/学生学习页面_files/yun.file.util.js.下载
2025-10-11 18:15:20 +08:00

482 lines
16 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
var $ctx = "";
try {
var src = document.scripts[document.scripts.length - 1].src;
if(src == ''){
src = document.scripts[document.scripts.length - 2].src;
}
$ctx = src.substr(1).match(new RegExp("(\\?|&)ctx=([^&]*)(&|$)"))[2] || "";
} catch (e) {
}
var isSafari = /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent);// 是否是safari浏览器
YunFileUtil = {
downloadYunFile : function () {},
url: '//noteyd.chaoxing.com/screen/note_note/files/status/',
fileSuffix: '.txt,.pdz,.pdf,.epub,.ppt,.pptx,.doc,.docx,.xls,.xlsx,.xlt,.xlsm,.xlsm,.xltm,.xlsb',
}
//获取js当前路径
YunFileUtil.url = function () {
var jsPath = document.currentScript && document.currentScript.src ? document.currentScript.src : function () {
var js = document.scripts
, last = js.length - 1
, src;
for (var i = last; i > 0; i--) {
if (js[i].readyState === 'interactive') {
src = js[i].src;
break;
}
if (js[i].src.indexOf('yun.file.util.js') > -1) {
src = js[i].src;
break;
}
}
return src || js[last].src;
}();
return jsPath.substring(0, jsPath.indexOf('/res/')) + '/screen/note_note/files/status/';
}();
/**
* 下载云盘附件。默认直接下载传了callback则会回传地址不进行下载
* objectId和attachment不能同时为空
* @param attachment 完整附件数据
* @param objectId 云盘文件objectId
* @param callback 获取到下载地址后的回调
*/
YunFileUtil.downloadYunFile = function (attachment, objectId, callback) {
if (!attachment && !objectId) {
return;
}
var fileName = '',suffix = '';
var resid = '';
if (attachment) {
switch (attachment.attachmentType) {
case 18:
// 云盘附件
fileName = attachment.att_clouddisk.name;
objectId = attachment.att_clouddisk.fileId || '';
if(window.location.host.indexOf('chaoxing.com') > -1 || window.location.host.indexOf('xuexi365.com') > -1){
resid = attachment.att_clouddisk.residstr || attachment.att_clouddisk.resid || '';
}
try {
if(attachment.att_clouddisk && attachment.att_clouddisk.resid && typeof attachment.att_clouddisk.resid == 'number' && attachment.att_clouddisk.infoJsonStr && attachment.att_clouddisk.infoJsonStr.residstr){
resid = attachment.att_clouddisk.infoJsonStr.residstr;
}
}catch (e) {
}
suffix = attachment.att_clouddisk.suffix;
break;
case 29:
// 视频附件
fileName = attachment.att_video.fileTitle || '';
objectId = attachment.att_video.objectId2 || '';
suffix = attachment.att_video.type;
break;
case 26:
// 音频附件
fileName = attachment.att_voice.fileTitle || '';
objectId = attachment.att_voice.objectId2 || '';
suffix = attachment.att_voice.type;
break;
}
}
if (fileName) {
// 2024-2-2 不再去文件后缀,音视频文件名里没有后缀的加上后缀
fileName = fileName.replace(/;/g,''); //带分号的文件名下载会从分号处截断,没有后缀,需要把分号去掉
if(fileName.indexOf('.') == -1 && attachment.attachmentType == 29){
//视频没有后缀补上,音频之前添加的后缀不准确,就不处理音频了
fileName = fileName + '.' + suffix
}
if (fileName.lastIndexOf('.') == fileName.length - 1) {
// 以.结尾的,去掉最后面的.,不用 endsWith 老版ie不支持
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
} else if (fileName.indexOf('.') > -1) {
// 文件名里面包含文件后缀,去掉文件后缀
//中心存储在2023年修改成了是按照文件名里有没有.来区分带不带扩展名的文件名中如果带点就不再加后缀需要改成去掉后缀后如果有点就加上后缀具体笔记https://noteyd.chaoxing.com/pc/065dfd40-a332-4bda-81f3-a5740ef0e714
var tempFileName = fileName.substring(0, fileName.lastIndexOf('.'));
if(tempFileName.indexOf('.') == -1){
fileName = tempFileName
}
}
}
if (!objectId) {
return;
}
if (window.location.host.indexOf('istudy.szpt.edu.cn') != -1 && typeof RichTextUitl != 'undefined') {
// 这两个域名做了云盘镜像,要请求定制的笔记域名,才能请求到镜像云盘
YunFileUtil.url = RichTextUitl.convertUrl('https://noteyd.chaoxing.com/screen/note_note/files/status/');
}
// 分享页面和一些门户页面没登录也可以下载,请求投屏下的接口,不需要登录
var url = YunFileUtil.url + objectId + '?resid=' + resid + '&puid=' + (getCookie('UID') || '');
// 如果是镜像模式
if(window.obj && window.obj.mirrorDomain && window.obj.mirrorDomain.hasOwnProperty('isMirrorDeploy')){
if(window.obj.mirrorDomain.isMirrorDeploy || window.obj.mirrorDomain.isMirrorDeploy == false && window.obj.mirrorDomain.NoteDomain.indexOf('chaoxing.com') == -1){
url = window.obj.mirrorDomain.NoteDomain + '/screen/note_note/files/status/'+objectId+'?resid='+resid+'&puid='+(getCookie('UID') || '');
}
}
url = window.location.protocol + url.replace('https:','').replace('http:','');
if(typeof RichTextUitl != 'undefined'){
if(typeof RichTextUitl.cloudUrl != 'undefined'){
url += '&cloudUrl=' + RichTextUitl.cloudUrl;
}
if(typeof RichTextUitl.csUrl != 'undefined'){
url += '&csUrl=' + RichTextUitl.csUrl;
}
}
//先从镜像获取 如果失败 并且是镜像域名,从公网获取一遍
if (window.location.host.indexOf("course.ustc.edu.cn") != -1 || window.location.host.indexOf("istudy.szpt.edu.cn") != -1
|| window.location.host.indexOf("mooc.ucas.edu.cn") != -1) {
let result = getCommonFileDownLoadUrl(url);
if ( !result ){
let urlDomain = url.replace('http:','').replace('https:','').replace('//','');
if(urlDomain.indexOf('/') > -1){
url = urlDomain.substring(urlDomain.indexOf('/'))
}
url = window.location.protocol + "//noteyd.chaoxing.com"+url ;
}
}
//镜像附件地址
if(typeof RichTextUitl != "undefined" && RichTextUitl.annexMirrorPrefix && attachment && attachment.attachmentType == 18 && attachment.att_clouddisk.isMirror){
url = RichTextUitl.annexMirrorPrefix + '/screen/note_note/files/status/' + objectId + '?resid=' + resid + '&puid=' + (getCookie('UID') || '');
}
$.ajax({
url: url,
type: "get",
xhrFields: {
withCredentials: true
},
success: function (data) {
if(data && data.status == false && data.msg){
//如果云盘文件被删除,取不到下载地址,给错误提示
if(typeof RichTextUitl != 'undefined'){
RichTextUitl.showTips(data.msg,0)
}
}
if (!data || (data.status && !data.download))
return;
var downloadUrl = data.download;
if (!downloadUrl) {
return;
}
if(fileName){
if(downloadUrl.indexOf('fn=') > -1 ){
//下载地址里已经有fn的,替换fileName
downloadUrl = changeURLArg(downloadUrl,'fn',encodeURIComponent(fileName))
}else if (downloadUrl.indexOf('fn=') == -1 ) {
// 下载地址带上云盘文件名,避免修改了附件文件名,下载下来的还是旧的文件名
if (downloadUrl.indexOf('?') > -1) {
downloadUrl += '&fn=' + encodeURIComponent(fileName);
} else {
downloadUrl += '?fn=' + encodeURIComponent(fileName)
}
}
}
if(isSafari){ //safari浏览器下载时加参数&mac=1
downloadUrl += '&mac=1'
}
if (self != top) {
// 嵌入在iframe里面
if (location.protocol.indexOf('https') != -1) {
// https服务下下载地址换成https的
downloadUrl = downloadUrl.replace("http://", 'https://');
}
} else {
if(window.location.protocol == 'https:'){
downloadUrl = downloadUrl.replace("http:", 'https:');
}
}
if (typeof callback == 'function') {
callback(downloadUrl);
return;
}
downloadFileFn(downloadUrl)
},
error: function () {
}
});
}
/**
* 同步判断是否有下载地址
* @param url
* @returns {boolean}
*/
function getCommonFileDownLoadUrl (url){
var result = false;
$.ajax({
url: url,
type: "get",
async: false,
xhrFields: {
withCredentials: true
},
success: function (data) {
if(data && data.status){
result = data.status;
}
},
error: function () {
}
});
return result;
}
/**
* 同步获取下载地址
* @param objectId
* @returns {string}
*/
YunFileUtil.getDownloadUrl = function (objectId) {
if (!objectId) {
return '';
}
$.ajaxSettings.async = false;//设置同步执行
var downloadUrl = '';
if(typeof RichTextUitl != 'undefined'){
if (window.location.host.indexOf('course.ustc.edu.cn') != -1 || window.location.host.indexOf('istudy.szpt.edu.cn') != -1) {
// 这两个域名做了云盘镜像,要请求定制的笔记域名,才能请求到镜像云盘
YunFileUtil.url = RichTextUitl.convertUrl('https://noteyd.chaoxing.com/screen/note_note/files/status/');
}
if(typeof RichTextUitl.cloudUrl != 'undefined'){
url += '&cloudUrl=' + RichTextUitl.cloudUrl;
}
if(typeof RichTextUitl.csUrl != 'undefined'){
url += '&csUrl=' + RichTextUitl.csUrl;
}
}
$.ajax({
url: YunFileUtil.url + objectId,
type: "get",
xhrFields: {
withCredentials: true
},
success: function (data) {
if (!data || data.status && !data.download)
return;
downloadUrl = data.download;
if (location.protocol.indexOf('https') != -1) {
// https服务下下载地址换成https的
downloadUrl = downloadUrl.replace("http://", 'https://');
}
},
error: function () {
}
});
$.ajaxSettings.async = true;//取消设置同步执行
return downloadUrl;
};
/**
* 下载本地文件夹
* @param residArray resid数组
* @param folderPuid 文件夹所属人puid
* @param folderName 文件夹名称
*/
YunFileUtil.downloadLocalFolder = function (residArray, folderPuid, folderName) {
if (!residArray || residArray.length == 0) {
return;
}
var batchDownloadCallback = function () {
var cloudArray = new Array();
$.each(residArray, function (idx, val) {
cloudArray.push({'puid': folderPuid, 'resid': val});
})
var open_url = 'https://ypdownload.chaoxing.com/download/downloadFilePackage?_token='
+ YunFileUtil.yunToken + '&puid=' + YunFileUtil.puid
+ '&fileArray=' + encodeURIComponent(JSON.stringify(cloudArray))
+ '&downloadFileName=' + encodeURIComponent(folderName);
window.location.href = open_url;
}
if (typeof RichTextUitl != 'undefined' && RichTextUitl.yunToken && RichTextUitl.puid) {
// 页面上有引用 rich_text_util.js 或者 rich.text.util.js里面会调云盘接口获取token
YunFileUtil.yunToken = RichTextUitl.yunToken;
YunFileUtil.puid = RichTextUitl.puid;
batchDownloadCallback();
} else {
YunFileUtil.getUploadConfig(batchDownloadCallback)
}
}
/**
* 获取云盘token
* @param callback
*/
YunFileUtil.getUploadConfig = function (callback) {
if (typeof jQuery != 'undefined') {
// 之前http页面可以调用https的接口现在新版的chrome浏览器上面不行了这里做下域名适配
// var noteDomain = 'https://noteyd.chaoxing.com';
var noteDomain = window.obj.mirrorDomain.NoteDomainHttps;
if (window.location.protocol == 'http:') {
// noteDomain = 'http://note.yd.chaoxing.com';
noteDomain = window.obj.mirrorDomain.NoteDomain;
}
var json = {
url: YunFileUtil.convertUrl(noteDomain + "/pc/files/getUploadConfig"),
type: "get",
xhrFields: {
withCredentials: true
},
headers: {"Content-Type": "application/x-www-form-urlencoded"},
data: {},
dataType: "json",
success: function (resultData) {
if (resultData && resultData.result == 1) {
YunFileUtil.puid = resultData.msg.puid;
YunFileUtil.yunToken = resultData.msg.token || '';
}
if (typeof callback == 'function') {
callback();
}
},
error: function () {
}
}
$.ajax(json);
}
}
//获取js当前路径
YunFileUtil.currentDomain = function () {
var jsPath = document.currentScript && document.currentScript.src ? document.currentScript.src : function () {
var js = document.scripts
, last = js.length - 1
, src;
for (var i = last; i > 0; i--) {
if (js[i].readyState === 'interactive') {
src = js[i].src;
break;
}
if (js[i].src.indexOf('yun.file.util.js') > -1) {
src = js[i].src;
break;
}
}
return src || js[last].src;
}();
jsPath = jsPath.substring(jsPath.indexOf('//') + 2, jsPath.lastIndexOf('/') + 1);
// jsPath = jsPath.substring(0, jsPath.indexOf('/'));
return jsPath;
}();
/**
* 地址转换
* @param url
* @returns {*}
*/
YunFileUtil.convertUrl = function (url) {
try {
if(window.location.origin.indexOf('file:') > -1 || window.location.origin.indexOf('//localhost') > -1){
return url;
}
var protocol = window.location.protocol;
// 获取当前域名,有一些单位定制了域名,需求请求对应的定制笔记域名
// 例如: http://groupyd2.ecourse.ucas.ac.cn/pc/activity/activityList
var domain = window.location.host;
//处理统一域名+路径访问模式域名转换为xxxx.com/noteyd/xxx
if (YunFileUtil.currentDomain && YunFileUtil.currentDomain.indexOf('/noteyd/') > -1 && url.indexOf('yd.') > -1) {
url = url.substring(url.indexOf('//') + 2, url.length);
urlDomain = url.substring(0, url.indexOf('/'));
var service = '';
if (urlDomain.substring(0, urlDomain.indexOf('.')).indexOf('yd') > -1) {
service = '/' + urlDomain.substring(0, urlDomain.indexOf('.'));
}
url = url.replace(urlDomain, domain + service);
return '//' + url;
}else if(YunFileUtil.currentDomain && YunFileUtil.currentDomain.indexOf('bistatic-') > -1 && url.indexOf('chaoxing.com') > -1){
//超星域名换成ipv6域名bistatic-xx.chaoxing.com,例noteyd.chaoxing.com换成bistatic-noteyd.chaoxing.com
url = url.substring(url.indexOf('//')+2,url.length);
return '//bistatic-' + url;
}
if (domain.indexOf('chaoxing.com') > -1) {
// 超星域名
if (protocol == 'http:') {
// 当前协议是http的之前http页面可以调用https的接口现在新版的chrome浏览器上面不行了这里做下域名适配
url = url.replace('https://', 'http://');
}
return url;
}
// 截取域名
domain = domain.substring(domain.indexOf('.'))
// 笔记服务和小组有http页面是两级的note.yd.chaoxing.com , group.yd.chaoxing.com去掉yd
domain = domain.replace('.yd.', '.');
if (protocol == 'http:' && url.indexOf('https://noteyd.chaoxing.com') > -1) {
// 当前页面是http协议的使用的接口地址是笔记的https域名需要改成笔记http域名大多定制单位都是配置http的定制域名没有配https的
var prefix = 'http://note.yd';
// 丽水学院 特殊判断
if (url.indexOf('lsu.edu.cn') > -1) {
prefix = 'http://noteyd';
}
if(YunFileUtil.currentDomain.indexOf('noteyd') > -1 && YunFileUtil.currentDomain.indexOf('chaoxing.com') == -1){
url = url.replace('https://noteyd.chaoxing.com', 'http://noteyd' + domain);
}else{
url = url.replace('https://noteyd.chaoxing.com', prefix + domain);
}
} else {
if (url.indexOf('mooc1-2.chaoxing.com') > -1) {
// 目前看一个定制域名的单位国科大慕课域名是用的mooc1mooc1.ecourse.ucas.ac.cn
url = url.replace('mooc1-2.chaoxing.com', 'mooc1.chaoxing.com')
}
url = url.replace('.chaoxing.com', domain);
}
if (protocol == 'http:' && url.indexOf('https://') > -1) {
// 当前页面是http的请求的地址是https的换换成http
url = url.replace('https://', 'http://')
}
} catch (e) {
}
return url;
}
function getCookie(name) {
var strcookie = document.cookie;//获取cookie字符串
var arrcookie = strcookie.split("; ");//分割
//遍历匹配
for (var i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].split("=");
if (arr[0].trim() == name) {
return arr[1];
}
}
return '';
}
//获取地址里传过来的参数
function getUrlParamNew(name,str){
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = str.match(reg); //匹配目标参数
if (r != null) return r[2]; return null; //返回参数值
}
//修改url参数
function changeURLArg(url,arg,arg_val){
var pattern=arg+'=([^&]*)';
var replaceText=arg+'='+arg_val;
if(url.match(pattern)){
var tmp='/('+ arg+'=)([^&]*)/gi';
tmp=url.replace(eval(tmp),replaceText);
return tmp;
}else{
if(url.match('[\?]')){
return url+'&'+replaceText;
}else{
return url+'?'+replaceText;
}
}
}
// 文件下载
function downloadFileFn(url) {
if(typeof MultiEditor !== 'undefined') {
MultiEditor.isFileDownloadPageJump = true
}
window.location.href = url
if(typeof MultiEditor !== 'undefined') {
MultiEditor.isFileDownloadPageJump = false
}
}