WIN-2308041133\Administrator 4 months ago
parent
commit
79d6b18bc1
1 changed files with 82 additions and 41 deletions
  1. 82 41
      app/admin/view/article/article/create.php

+ 82 - 41
app/admin/view/article/article/create.php

@@ -266,12 +266,10 @@
         // 上传视频按钮点击事件
         // 上传视频按钮点击事件
         $('#upload_video_btn').on('click', function() {
         $('#upload_video_btn').on('click', function() {
             if ($('#video_link').val()) {
             if ($('#video_link').val()) {
-                // 如果输入框有值,直接设置为视频链接
                 $('#video_preview').show();
                 $('#video_preview').show();
                 $('#video_preview video').attr('src', $('#video_link').val());
                 $('#video_preview video').attr('src', $('#video_link').val());
                 $(this).text('上传视频');
                 $(this).text('上传视频');
             } else {
             } else {
-                // 否则触发文件选择
                 $('#video_file').trigger('click');
                 $('#video_file').trigger('click');
             }
             }
         });
         });
@@ -283,67 +281,110 @@
             $('#upload_video_btn').text('上传视频');
             $('#upload_video_btn').text('上传视频');
         });
         });
 
 
-        // 文件选择变化事件
-// 修改后的上传逻辑
+        // 文件选择变化事件 - 修复后的上传逻辑
         $('#video_file').on('change', function() {
         $('#video_file').on('change', function() {
-            const file = this.files[0];
+            var file = this.files[0];
             if (!file) return;
             if (!file) return;
 
 
-            // 校验文件类型和大小...
+            // 验证文件类型
+            if (!file.type.includes('video/mp4') && !file.name.toLowerCase().endsWith('.mp4')) {
+                $eb.message('error', '请选择MP4格式的视频文件');
+                return;
+            }
+
+            // 验证文件大小(100MB以内)
+            if (file.size > 100 * 1024 * 1024) {
+                $eb.message('error', '视频文件大小不能超过100MB');
+                return;
+            }
 
 
             // 显示进度条
             // 显示进度条
-            $('#video_progress').show().find('.progress-bar').css('width', '0%');
+            $('#video_progress').show();
+            $('#progress_bar').css('width', '0%').text('0%');
 
 
-            // 获取上传凭证
+            // 获取签名信息 - 添加错误处理和数据结构验
             $.ajax({
             $.ajax({
                 url: "https://shop.yzcyzjkc.com/admin/widget.video/get_signature",
                 url: "https://shop.yzcyzjkc.com/admin/widget.video/get_signature",
                 type: 'GET',
                 type: 'GET',
                 dataType: 'json',
                 dataType: 'json',
-                success: function(res) {
-                    // 状态码检查
-                    console.log(res.code);
-                    if (res.code !== 200) {
-                        $eb.message('error', res.msg || '上传凭证获取失败');
-                        $('#video_progress').hide();
-                        return;
-                    }
-                    // 关键字段检查
-                    const data = res.data;
-                    const requiredKeys = ['uploadType'];
-                    if (!data || requiredKeys.some(k => !data[k])) {
-                        $eb.message('error', '服务端返回参数缺失');
-                        return;
-                    }
-                    // 执行上传
-                    AdminUpload.upload(
-                        data.uploadType,  // 关键参数必须存在
-                        {
-                            token: data.uploadToken || '',
+                success: function(response) {
+                    try {
+                        // 验证响应数据结构
+                        if (!response || !response.data) {
+                            throw new Error('无效的响应数据');
+                        }
+
+                        var uploadData = response.data;
+
+                        // 确保必要字段存在
+                        if (!uploadData.uploadType || !uploadData.token) {
+                            throw new Error('缺少必要的上传参数');
+                        }
+
+                        // 初始化上传配置
+                        var uploadConfig = {
+                            token: uploadData.token,
                             file: file,
                             file: file,
-                            accessKeyId: data.accessKey || '',
-                            accessKeySecret: data.secretKey || '',
-                            bucketName: data.storageName || '',
-                            region: data.storageRegion || '',
-                            domain: data.domain || '',
                             uploadIng: function(progress) {
                             uploadIng: function(progress) {
                                 $('#progress_bar').css('width', progress + '%').text(progress + '%');
                                 $('#progress_bar').css('width', progress + '%').text(progress + '%');
                             }
                             }
+                        };
+
+                        // 添加可选配置参数
+                        if (uploadData.accessKey) uploadConfig.accessKeyId = uploadData.accessKey;
+                        if (uploadData.secretKey) uploadConfig.accessKeySecret = uploadData.secretKey;
+                        if (uploadData.storageName) uploadConfig.bucketName = uploadData.storageName;
+                        if (uploadData.storageRegion) uploadConfig.region = uploadData.storageRegion;
+                        if (uploadData.domain) uploadConfig.domain = uploadData.domain;
+
+                        // 确保AdminUpload已加载
+                        if (typeof AdminUpload === 'undefined') {
+                            throw new Error('文件上传组件未正确加载');
                         }
                         }
-                    ).then(function(res) {
-                        // 上传成功处理...
-                    }).catch(function(err) {
-                        $eb.message('error', '上传失败: ' + err.message);
-                    });
+
+                        // 执行上传
+                        AdminUpload.upload(uploadData.uploadType, uploadConfig)
+                            .then(function(res) {
+                                if (!res || !res.url) {
+                                    throw new Error('上传成功但返回数据无效');
+                                }
+
+                                $('#video_link').val(res.url);
+                                $('#video_preview').show();
+
+                                // 确保video元素存在
+                                if ($('#video_preview video').length === 0) {
+                                    $('#video_preview').html('<video controls src="' + res.url + '">您的浏览器不支持 video 标签。</video>' +
+                                        '<i class="layui-icon layui-icon-delete delete-video" id="delete_video"></i>');
+                                } else {
+                                    $('#video_preview video').attr('src', res.url);
+                                }
+
+                                $('#upload_video_btn').text('确认添加');
+                                $eb.message('success', '视频上传成功');
+                                $('#video_progress').hide();
+                            })
+                            .catch(function(err) {
+                                console.error('上传失败:', err);
+                                $eb.message('error', '视频上传失败: ' + (err.message || '未知错误'));
+                                $('#video_progress').hide();
+                            });
+
+                    } catch (err) {
+                        console.error('上传处理错误:', err);
+                        $eb.message('error', '上传处理错误: ' + err.message);
+                        $('#video_progress').hide();
+                    }
                 },
                 },
-                error: function(xhr) {
-                    $eb.message('error', '网络请求失败');
+                error: function(xhr, status, error) {
+                    $eb.message('error', '获取上传凭证失败: ' + (error || '未知错误'));
                     $('#video_progress').hide();
                     $('#video_progress').hide();
                 }
                 }
             });
             });
         });
         });
-
     });
     });
 
 
+
     // 选择图片
     // 选择图片
     function changeIMG(index,pic){
     function changeIMG(index,pic){
         $(".image_img").css('background-image',"url("+pic+")");
         $(".image_img").css('background-image',"url("+pic+")");