huawei
4 天以前 5b1ced24cc4a7a3f873ff685f3a5e5c9f2d5441a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>看板轮播</title>
    <link rel="stylesheet" href="js/layui/css/layui.css">
    <style>
        body {
            margin: 0;
            padding: 0;
            background: #000;
            overflow: hidden;
            width: 100vw;
            height: 100vh;
        }
        .iframe-container {
            position: relative;
            width: 100%;
            height: 100%;
        }
        .iframe {
            position: absolute;
            width: 100%;
            height: 100%;
            border: none;
            top: 0;
            left: 0;
            opacity: 0;
            visibility: hidden;
            transition: opacity 1s ease-in-out; /* 淡入淡出效果 */
            background: #fff;
        }
        .iframe.active {
            opacity: 1;
            visibility: visible;
            z-index: 1;
        }
    </style>
</head>
<body>
 
<div id="iframeContainer" class="iframe-container">
    <!-- iframe 将通过 JS 动态生成 -->
</div>
 
<script src="js/layui/layui.js"></script>
 
<script>
layui.use(['jquery', 'layer'], function () {
    const $ = layui.jquery;
    const layer = layui.layer;
 
    // 1. 获取 URL 参数
    const urlParams = new URLSearchParams(window.location.search);
    const menuId = urlParams.get('menuId');
    // 注意:后端现在返回每个页面的具体时长,URL上的lbsj仅作为兜底默认值
    const defaultDuration = (urlParams.get('lbsj') || 10) * 1000; 
 
    if (!menuId) {
        layer.alert("参数错误:缺少 menuId", {icon: 2});
        return;
    }
 
    const container = $('#iframeContainer');
    let viewList = [];      // 存储 { url, duration }
    let currentIndex = 0;   // 当前播放索引
 
    // 2. 请求后端获取播放列表
    $.ajax({
        url: 'http://localhost:5204/simple/listByMenuId/' + menuId,
        method: 'GET',
        success: function(res) {
            console.log("后端返回的完整 res:", res);
                console.log("树数据 res.data:", res.data);
            if (res.code === 0 && res.data && res.data.length > 0) {
                // 适配新DTO字段: item.url 和 item.duration
                viewList = res.data.map(item => ({
                    url: item.url,
                    // 如果后端 duration 为空或0,使用默认值,转换为毫秒
                    duration: (item.duration && item.duration > 0) ? item.duration * 1000 : defaultDuration
                }));
 
                initCarousel();
            } else {
                layer.msg("当前菜单下没有可轮播的内容", {icon: 0});
            }
        },
        error: function(xhr, status, error) {
            layer.msg("获取轮播数据失败", {icon: 2});
        }
    });
 
    // 3. 初始化 DOM 和 播放逻辑
    function initCarousel() {
        // 先生成所有 iframe (预加载)
        viewList.forEach((item, index) => {
            const iframeHtml = `<iframe class="iframe" id="frm_${index}" src="${item.url}"></iframe>`;
            container.append(iframeHtml);
        });
 
        // 开始播放第一张
        playView(0);
    }
 
    // 4. 播放指定索引的视图 (递归调用实现不等时长轮播)
    function playView(index) {
        // 移除所有 active 类
        $('.iframe').removeClass('active');
        
        // 激活当前 frame
        const $currentFrame = $(`#frm_${index}`);
        $currentFrame.addClass('active');
 
        // 刷新当前 iframe (可选:如果看板实时性要求高,每次展示时重新加载)
        // $currentFrame.attr('src', $currentFrame.attr('src'));
 
        // 获取当前页面的停留时间
        const stayTime = viewList[index].duration;
        console.log(`正在播放第 ${index + 1} 页,停留 ${stayTime/1000} 秒`);
 
        // 计算下一页索引
        const nextIndex = (index + 1) % viewList.length;
 
        // 设置定时器播放下一页
        setTimeout(() => {
            playView(nextIndex);
        }, stayTime);
    }
});
</script>
 
</body>
</html>