You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.8 KiB

2 months ago
(function() {
'use strict';
var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
var register = function (editor) {
var pluginName = '附件上传';
// 从window对象获取baseURL或者从editor配置中获取
var baseURL = '';
try {
// 尝试从全局变量获取
if (window.VUE_APP_BASE_API) {
baseURL = window.VUE_APP_BASE_API;
} else if (typeof process !== 'undefined' && process.env && process.env.VUE_APP_BASE_API) {
baseURL = process.env.VUE_APP_BASE_API;
}
} catch(e) {
console.warn('无法获取baseURL', e);
}
// 注册自定义图标,使用 📎 emoji
var icons = editor.ui.registry.getAll().icons;
if (!icons || !icons.attachment) {
editor.ui.registry.addIcon('attachment', '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg"><foreignObject width="24" height="24"><div xmlns="http://www.w3.org/1999/xhtml" style="font-size: 18px; line-height: 24px; text-align: center;">📎</div></foreignObject></svg>');
}
editor.ui.registry.addButton('attachment', {
icon: 'attachment',
tooltip: '上传附件',
onAction: function () {
openAttachmentDialog(editor);
}
});
editor.ui.registry.addMenuItem('attachment', {
icon: 'attachment',
text: '上传附件',
onAction: function () {
openAttachmentDialog(editor);
}
});
function openAttachmentDialog(editor) {
// 创建文件输入元素
var input = document.createElement('input');
input.type = 'file';
input.multiple = true;
input.accept = '*/*'; // 接受所有文件类型
input.onchange = function(e) {
var files = e.target.files;
if (files.length === 0) return;
// 上传每个文件
Array.from(files).forEach(function(file) {
uploadAttachment(editor, file);
});
};
// 触发文件选择
input.click();
}
function uploadAttachment(editor, file) {
// 检查文件大小50MB限制
var maxSize = 50 * 1024 * 1024; // 50MB
if (file.size > maxSize) {
editor.notificationManager.open({
text: '文件大小不能超过50MB',
type: 'error'
});
return;
}
// 显示上传进度
var notificationId = editor.notificationManager.open({
text: '正在上传 ' + file.name + '...',
type: 'info',
timeout: 0
});
// 创建FormData
var formData = new FormData();
formData.append('file', file);
// 上传文件
var xhr = new XMLHttpRequest();
var uploadUrl = baseURL + '/api/admin/upload-file';
if (!baseURL) {
// 如果没有baseURL尝试从当前页面获取
var currentOrigin = window.location.origin;
uploadUrl = currentOrigin + '/api/admin/upload-file';
}
xhr.open('POST', uploadUrl, true);
// 设置请求头获取token
var token = '';
try {
// 尝试从localStorage获取token与request.js保持一致
var storageData = localStorage.getItem('stbc1_lifeData');
if (storageData) {
var data = JSON.parse(storageData);
if (data && data.token) {
token = data.token;
}
}
// 如果还没有尝试直接获取token
if (!token) {
token = localStorage.getItem('token') || sessionStorage.getItem('token') || '';
}
} catch(e) {
console.warn('获取token失败', e);
}
if (token) {
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
}
// 设置其他必要的请求头
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onload = function() {
editor.notificationManager.close(notificationId);
if (xhr.status === 200) {
try {
var response = JSON.parse(xhr.responseText);
// 处理不同的响应格式
var fileData = null;
if (response.code === 200 && response.data) {
fileData = response.data;
} else if (response.data) {
fileData = response.data;
} else if (response.url) {
fileData = response;
}
if (fileData && fileData.url) {
// 创建附件链接
var fileName = fileData.original_name || file.name;
var fileUrl = fileData.url;
var fileSize = formatFileSize(fileData.size || file.size);
// 插入附件链接到编辑器
var attachmentHtml = '<p><a href="' + fileUrl + '" download="' + fileName + '" style="display: inline-flex; align-items: center; padding: 8px 12px; background-color: #f5f7fa; border: 1px solid #dcdfe6; border-radius: 4px; text-decoration: none; color: #606266;">' +
'<i style="margin-right: 8px; color: #409EFF;">📎</i>' +
'<span style="margin-right: 8px;">' + fileName + '</span>' +
'<span style="font-size: 12px; color: #909399;">(' + fileSize + ')</span>' +
'</a></p>';
editor.insertContent(attachmentHtml);
editor.notificationManager.open({
text: fileName + ' 上传成功',
type: 'success',
timeout: 3000
});
} else {
throw new Error('响应格式错误');
}
} catch (e) {
editor.notificationManager.open({
text: '上传失败:' + (e.message || '未知错误'),
type: 'error'
});
}
} else {
editor.notificationManager.open({
text: '上传失败:服务器错误',
type: 'error'
});
}
};
xhr.onerror = function() {
editor.notificationManager.close(notificationId);
editor.notificationManager.open({
text: '上传失败:网络错误',
type: 'error'
});
};
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
var percentComplete = (e.loaded / e.total) * 100;
editor.notificationManager.open({
text: '正在上传 ' + file.name + '... ' + Math.round(percentComplete) + '%',
type: 'info',
timeout: 0
});
}
};
xhr.send(formData);
}
function formatFileSize(bytes) {
if (bytes === 0) return '0 B';
var k = 1024;
var sizes = ['B', 'KB', 'MB', 'GB'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}
};
function Plugin() {
global.add('attachment', register);
}
Plugin();
})();