From a99f3cce8544034e900d2f0c72d43cf2943ed14a Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Tue, 20 Feb 2024 16:24:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=96=87=E6=9C=AC=E9=A2=84?= =?UTF-8?q?=E8=A7=88=E5=92=8C=E7=BC=96=E8=BE=91=E4=BF=9D=E5=AD=98=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/HomeController.php | 32 ++++++---- views/home/index.php | 17 +++++ web/js/home_script.js | 109 +++++++++++++++++++++++---------- 3 files changed, 113 insertions(+), 45 deletions(-) diff --git a/controllers/HomeController.php b/controllers/HomeController.php index db2e70e..bde93f3 100644 --- a/controllers/HomeController.php +++ b/controllers/HomeController.php @@ -330,15 +330,16 @@ class HomeController extends Controller * 注意,已存在的同名文件会被覆盖 * https://devs.chenx221.cyou:8081/index.php?r=home%2Fupload * - * @return int + * @return int|Response */ - public function actionUpload(): int + public function actionUpload() { $model = new UploadForm(); $model->targetDir = Yii::$app->request->post('targetDir', '.'); $uploadedFiles = UploadedFile::getInstancesByName('files'); $successCount = 0; $totalCount = count($uploadedFiles); + $sp = Yii::$app->request->post('sp', null); foreach ($uploadedFiles as $uploadedFile) { $model->uploadFile = $uploadedFile; @@ -349,16 +350,25 @@ class HomeController extends Controller $successCount++; } } - - if ($successCount === $totalCount) { - Yii::$app->session->setFlash('success', 'All files uploaded successfully.'); - } elseif ($successCount > 0) { - Yii::$app->session->setFlash('warning', 'Some files uploaded successfully.'); - } else { - Yii::$app->session->setFlash('error', 'Failed to upload files.'); + if ($sp === 'editSaving') { + if ($successCount === $totalCount) { + Yii::$app->response->statusCode = 200; + return $this->asJson(['status' => 200, 'message' => 'All files uploaded successfully.']); + } else{ + Yii::$app->response->statusCode = 500; + return $this->asJson(['status' => 500, 'message' => 'Failed to upload files.']); + } + }else { + if ($successCount === $totalCount) { + Yii::$app->session->setFlash('success', 'All files uploaded successfully.'); + } elseif ($successCount > 0) { + Yii::$app->session->setFlash('warning', 'Some files uploaded successfully.'); + } else { + Yii::$app->session->setFlash('error', 'Failed to upload files.'); + } + //返回状态码200 + return Yii::$app->response->statusCode = 200; // 如果出错请删掉return} } - //返回状态码200 - return Yii::$app->response->statusCode = 200; // 如果出错请删掉return } /** diff --git a/views/home/index.php b/views/home/index.php index 77d34d3..88dc73b 100644 --- a/views/home/index.php +++ b/views/home/index.php @@ -6,6 +6,7 @@ /* @var $directory string 当前路径 */ +use app\assets\AceAsset; use app\assets\PlyrAsset; use app\assets\ViewerJsAsset; use app\models\NewFolderForm; @@ -28,6 +29,7 @@ FontAwesomeAsset::register($this); JqueryAsset::register($this); ViewerJsAsset::register($this); PlyrAsset::register($this); +AceAsset::register($this); $this->registerCssFile('@web/css/home_style.css'); ?>
@@ -150,6 +152,8 @@ $this->registerCssFile('@web/css/home_style.css'); $relativePath], ['class' => 'file_name', 'onclick' => 'previewImage(this, event)']) ?> $relativePath, 'type' => $item['rawType']], ['class' => 'file_name', 'onclick' => 'previewVideo(this, event)']) ?> + + $relativePath, 'type' => $item['rawType']], ['class' => 'file_name', 'onclick' => 'textEdit(this, event)']) ?> $relativePath, 'type' => $item['rawType']], ['class' => 'file_name', 'onclick' => 'previewAudio(this, event)']) ?> @@ -298,6 +302,19 @@ echo ''; +Modal::end(); + +Modal::begin([ + 'title' => '

文本编辑

', + 'id' => 'textEditModal', + 'size' => 'modal-lg', +]); + +echo''; +echo ''; +echo ''; +echo '
'; + Modal::end(); $this->registerJsFile('@web/js/home_script.js', ['depends' => [JqueryAsset::class], 'position' => View::POS_END]); ?> diff --git a/web/js/home_script.js b/web/js/home_script.js index e496b30..3eb430b 100644 --- a/web/js/home_script.js +++ b/web/js/home_script.js @@ -100,9 +100,9 @@ $(document).on('click', '.unzip-btn', function () { $.ajax({ type: "POST", url: "index.php?r=home%2Funzip", - data: { relativePath: relativePath }, + data: {relativePath: relativePath}, dataType: "json", // 期望从服务器接收json格式的响应 - success: function(response) { + success: function (response) { // 如果服务器返回的状态码是200,说明解压成功 if (response.status === 200) { // 刷新页面,加载到解压后的目录 @@ -112,7 +112,7 @@ $(document).on('click', '.unzip-btn', function () { window.location.href = 'index.php?r=home%2Findex&directory=' + encodeURIComponent(response.parentDirectory); } }, - error: function() { + error: function () { // 处理错误 console.error('AJAX request failed.'); } @@ -151,12 +151,12 @@ $(document).on('click', '.batch-paste-btn', function () { $.ajax({ type: "POST", url: "index.php?r=home%2Fpaste", - data: { operation: operation, relativePaths: relativePaths, targetDirectory: targetDirectory }, - success: function() { + data: {operation: operation, relativePaths: relativePaths, targetDirectory: targetDirectory}, + success: function () { // 处理响应 location.reload(); }, - error: function() { + error: function () { // 处理错误 console.error('AJAX request failed.'); } @@ -172,16 +172,16 @@ $(document).on('click', '.calc-sum-btn', function () { $.ajax({ type: "POST", url: "index.php?r=home%2Fchecksum", - data: { relativePath: relativePath }, + data: {relativePath: relativePath}, dataType: "json", - success: function(response) { + success: function (response) { // 更新模态框中的内容 $('#crc32b').text('CRC32B: ' + response.crc32b); $('#sha256').text('SHA256: ' + response.sha256); // 显示模态框 $('#checksumModal').modal('show'); }, - error: function() { + error: function () { console.error('AJAX request failed.'); } }); @@ -197,26 +197,6 @@ $(document).on('click', '#generate_access_code', function () { $('#shareModal #share-access_code').val(accessCode); }); -// $(document).on('submit', '#share-form', function (event) { -// event.preventDefault(); -// -// var relativePath = $('#shareModal #share-file_relative_path').val(); -// var accessCode = $('#shareModal #share-access_code').val(); -// -// $.ajax({ -// type: "POST", -// url: "index.php?r=share%2Fcreate", -// data: { file_relative_path: relativePath, access_code: accessCode }, -// success: function() { -// // 处理响应 -// location.reload(); -// }, -// error: function() { -// // 处理错误 -// console.error('AJAX request failed.'); -// } -// }); -// }); $(document).on('click', '.shares-btn', function () { var relativePath = $(this).closest('tr').find('.select-item').data('relativePath'); $('#shareModal #share-file_relative_path').val(relativePath); @@ -230,12 +210,12 @@ $(document).on('click', '.batch-delete-btn', function () { $.ajax({ type: "POST", url: "index.php?r=home%2Fdelete", - data: { relativePath: relativePaths }, - success: function() { + data: {relativePath: relativePaths}, + success: function () { // 处理响应 location.reload(); }, - error: function() { + error: function () { // 处理错误 console.error('AJAX request failed.'); location.reload(); @@ -392,7 +372,7 @@ function updateButtons() { // 当checkbox的状态改变时,调用updateButtons函数 $(document).on('change', '.select-item', updateButtons); -$(document).ready(function() { +$(document).ready(function () { updateButtons(); }); @@ -414,7 +394,7 @@ function previewImage(element, event) { flipHorizontal: 1, flipVertical: 1, }, - hidden: function() { + hidden: function () { viewer.destroy(); } }); @@ -448,6 +428,7 @@ videoModal.on('hidden.bs.modal', function () { //music preview var aPlayer; var audioModal = $('#audioModal'); + function previewAudio(element, event) { event.preventDefault(); var audioElement = document.getElementById('aPlayer'); @@ -460,8 +441,68 @@ function previewAudio(element, event) { // 显示模态框 audioModal.modal('show'); } + audioModal.on('hidden.bs.modal', function () { if (aPlayer) { aPlayer.destroy(); } +}); + +//text edit +var editor; +var editorModal = $('#textEditModal'); + +function textEdit(element, event) { + event.preventDefault(); + editorModal.modal('show'); + var fileUrl = element.href; + $('#edFilename').val(element.innerText); + $('#ed-alert-success').hide(); + $('#ed-alert-fail').hide(); + $.get(fileUrl, function (data) { + editor = ace.edit("editor"); + editor.session.setOption("useWorker", false); + editor.setTheme("ace/theme/github"); + editor.renderer.setOption("fontSize", 15); + editor.renderer.setOption("animatedScroll", true) + editor.session.setMode("ace/mode/text"); + editor.setValue(data); + }); +} + +editorModal.on('hidden.bs.modal', function () { + if (editor) { + editor.destroy(); + // editor.container.remove(); + } +}); +$('#saveButton').on('click', function () { + var content = editor.getValue(); + var blob = new Blob([content], {type: "text/plain"}); + var file = new File([blob], $('#edFilename').val(), {type: "text/plain"}); + var formData = new FormData(); + formData.append('files', file); + formData.append('sp', 'editSaving'); + formData.append('targetDir', $('#target-dir').val()); + + $.ajax({ + type: "POST", + url: "index.php?r=home%2Fupload", // 替换为你的上传URL + data: formData, + processData: false, // 告诉jQuery不要处理发送的数据 + contentType: false, // 告诉jQuery不要设置Content-Type请求头 + success: function (data) { + if (data.status === 200) { + $('#ed-alert-success').show(); + $('#ed-alert-fail').hide(); + } else { + $('#ed-alert-success').hide(); + $('#ed-alert-fail').show(); + } + }, + error: function () { + $('#ed-alert-success').hide(); + $('#ed-alert-fail').show(); + } + }); }); \ No newline at end of file