czt
2026-02-05 f8a1a873410f00beb3e2f3f888cabfd9264b207f
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
var layer;
var laypage;
 
// 监听收储公司 Select2 变化
$(document).ready(function() {
    // 监听收储公司下拉框变化
    $('select[name="key"]').on('change', function() {
        var selectedParentId = $(this).val();
        if(selectedParentId) {
            getSelectData(selectedParentId);
        } else {
            // 如果清空了收储公司选择,也清空库区选项
            var $deptSelect = $('select[name="deptId"]');
            $deptSelect.empty()
                .append('<option value="">所有</option>')
                .select2({
                    placeholder: "请选择库区",
                    allowClear: true,
                    width: 'resolve'
                });
        }
    });
 
    // 在点击库区下拉框时,仅验证而不发起新请求
    $('select[name="deptId"]').on('select2:opening', function(e) {
        var selectedParentId = $('select[name="key"]').val();
        if(!selectedParentId) {
            e.preventDefault();
            layer.msg('请先选择收储公司');
            return false;
        }
 
        // 不在此处发起请求,依赖 change 事件时的预加载
        return true;
    });
});
 
 
$(function () {
    // 初始化分页
    layui.use(['laypage', 'layer'], function () {
        layer = layui.layer;
        laypage = layui.laypage;
 
        // 初始化分页组件
        initPagination();
    });
 
    // 初始化图片预览功能
    initImagePreview();
});
 
 
 
 
// 修改 getSelectData 函数,支持 Select2 的数据格式
function getSelectData(parentId) {
    if(!parentId) {
        return layer.msg('请选择收储公司');
    }
 
    $.ajax({
        url: "../../system/dept-new/getDeptByUserType",
        type: 'POST',
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: parentId,
        success: function (response) {
            if (response) {
                deptList = response;
                // 重新初始化 Select2 或更新选项
                updateDeptSelect2Options(deptList);
            } else {
                layer.msg(response.msg || '数据加载失败');
            }
        },
        error: function (xhr, status, error) {
            layer.msg('数据加载失败');
        }
    });
}
 
// 更新 Select2 下拉框选项
function updateDeptSelect2Options(deptList) {
    var $deptSelect = $('select[name="deptId"]');
 
    // 保存当前选中的值
    var currentVal = $deptSelect.val();
 
    // 清空现有选项
    $deptSelect.empty();
 
    // 添加"所有"选项
    $deptSelect.append('<option value="">所有</option>');
 
    // 添加动态数据选项
    if(deptList && deptList.length > 0) {
        deptList.forEach(function(dept) {
            $deptSelect.append('<option value="' + dept.id + '">' + dept.kqmc + '</option>');
        });
    }
 
    // 重新初始化 Select2
    $deptSelect.select2({
        placeholder: "请选择库区",
        allowClear: true,
        width: 'resolve'
    });
 
    // 恢复之前的选择
    if(currentVal) {
        $deptSelect.val(currentVal).trigger('change');
    }
}
 
 
 
 
/**
 * 初始化分页组件
 */
function initPagination() {
    laypage.render({
        elem: 'pagination',
        count: typeof totalItems !== 'undefined' ? totalItems : 0,
        limit: typeof pageSize !== 'undefined' ? pageSize : 6,
        curr: typeof currentPage !== 'undefined' ? currentPage : 1,
        layout: ['prev','page', 'next'],
        // prev: '<i class="layui-icon layui-icon-left"></i>',
        // next: '<i class="layui-icon layui-icon-right"></i>',
        jump: function (obj, first) {
            if (!first) {
                searchRecord(obj.curr, obj.limit)
            }
        }
    });
}
 
/**
 * 重新初始化分页组件
 * @param {number} totalCount - 总记录数
 * @param {number} pageSize - 每页大小
 * @param {number} currentPage - 当前页码
 */
function reinitPagination(totalCount, pageSize, currentPage) {
    laypage.render({
        elem: 'pagination',
        count: totalCount,
        limit: pageSize,
        curr: currentPage,
        layout: ['prev', 'page','next'],
        // prev: '<i class="layui-icon layui-icon-left"></i>',
        // next: '<i class="layui-icon layui-icon-right"></i>',
        jump: function (obj, first) {
            if (!first) {
                searchRecord(obj.curr, obj.limit)
            }
        }
    });
}
 
/**
 * 获取事件记录数据
 * @param {Object} params - 查询参数对象
 * @param {Function} callback - 回调函数
 */
function fetchEventInfoData(params, callback) {
    $.ajax({
        url: '../../security/eventInfo/pageData',
        type: 'POST',
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(params),
        success: function (response) {
            if (response.code === '0000') {
                callback(null, response.data);
            } else {
                callback(new Error(response.msg || '数据加载失败'), null);
            }
        },
        error: function (xhr, status, error) {
            callback(new Error('请求失败,请稍后重试'), null);
        }
    });
}
 
/**
 * 构建查询参数
 * @param {number} page - 页码
 * @param {number} size - 每页大小
 * @returns {Object} 查询参数对象
 */
function buildQueryParams(page, size) {
    var params = {
        page: page,
        limit: size
    };
 
    // 添加表单查询条件
    var form = document.getElementById('eventInfo-form');
    if (form) {
        // 处理普通输入框和选择框,排除隐藏域
        var inputs = form.querySelectorAll('input[name]:not([type="hidden"]), select[name]');
        inputs.forEach(function(input) {
            if (input.value) { // 只添加非空值
                params[input.name] = input.value;
            }
        });
    }
 
    return params;
}
 
/**
 * 更新事件画廊内容
 * @param {Array} records - 事件记录数据
 */
function updateGallery(records) {
    var container = document.getElementById('gallery-container');
    if (!container) return;
 
    // 清空现有内容
    container.innerHTML = '';
 
    if (!records || records.length === 0) {
        // 显示空状态
        container.innerHTML = `
            <div class="empty-state">
                <i class="fa-solid fa-bell-slash"></i>
                <h3>暂无事件记录</h3>
                <p>当前没有可展示的AI事件数据</p>
            </div>
        `;
        // 隐藏分页
        $('.pagination-container').hide();
        return;
    }
 
    // 显示分页
    $('.pagination-container').show();
 
    // 生成事件卡片
    var html = '';
    records.forEach(function(record) {
        // 标签展示
        var tagsHtml = '';
        if (record.tags) {
            var tags = record.tags.split(',');
            tags.forEach(function(tag) {
                tagsHtml += `
            <span class="tag-person">
                <i class="layui-icon layui-icon-note"></i>
                <span>${tag.trim()}</span>
            </span>
        `;
            });
        }
 
        html += `
            <div class="gallery-item">
                <img src="${record.imgName || '/logo-sm.png'}" alt="${record.id}"
                    data-url="${record.imgName || '/logo-sm.png'}" data-id="${record.id}"
                     class="gallery-img" onclick="showEventInfoPreview(this.getAttribute('data-url'))">
                <div class="gallery-info">
                    <div class="gallery-header">
                        <h3 class="gallery-title">${record.name || record.id}</h3>
                        <div class="gallery-tags">
                            ${tagsHtml}
                        </div>
                    </div>
                    <div class="gallery-meta">
                        <div style="display: flex; align-items: center; gap: 15px;width: 100%">
                            <div class="meta-item" style="width: 50%">
                                <i class="layui-icon layui-icon-video"></i>
                                <span>${getCameraName(record.serId) || record.serId || ''}</span>
                            </div>
                            <div class="meta-item" style="width: 50%">
                                <i class="layui-icon layui-icon-date"></i>
                                <span>${formatDate(record.time)}</span>
                            </div>
                        </div>
                        <div class="meta-item">
                            <i class="layui-icon layui-icon-component"></i>
                            <span>${getTypeName(record.bizType) || record.bizType || ''}</span>
                        </div>
                    </div>
                </div>
            </div>
        `;
    });
 
    container.innerHTML = html;
}
 
/**
 * 根据摄像头ID获取摄像头名称
 * @param {string} serId - 摄像头ID
 * @returns {string|null} 摄像头名称或null
 */
function getCameraName(serId) {
    // 确保cameraList存在且为数组
    if (typeof cameraList !== 'undefined' && Array.isArray(cameraList) && serId) {
        // 查找匹配的摄像头对象
        var camera = cameraList.find(function(item) {
            return item.id === serId;
        });
        // 返回摄像头名称,如果找不到则返回null
        return camera ? camera.name : null;
    }
    return null;
}
 
/**
 * 根据业务类型编码获取业务类型名称
 * @param {string} bizType - 业务类型编码
 * @returns {string|null} 业务类型名称或null
 */
function getTypeName(bizType) {
    if (typeof bizTypeList !== 'undefined' && Array.isArray(bizTypeList) && bizType) {
        // 查找匹配的业务类型对象
        var type = bizTypeList.find(function(item) {
            return item.code === bizType;
        });
        // 返回业务类型名称,如果找不到则返回null
        return type ? type.msg : null;
    }
    return null;
}
 
 
/**
 * 格式化日期
 * @param {string|number} date - 日期字符串或时间戳
 */
function formatDate(date) {
    if (!date) return '';
    var d = new Date(date);
    return d.getFullYear() + '-' +
        String(d.getMonth() + 1).padStart(2, '0') + '-' +
        String(d.getDate()).padStart(2, '0') + ' ' +
        String(d.getHours()).padStart(2, '0') + ':' +
        String(d.getMinutes()).padStart(2, '0');
}
 
/**
 * 初始化图片预览功能
 */
function initImagePreview() {
    var preview = document.getElementById('imgPreview');
    var previewImg = document.getElementById('previewImg');
    var closeBtn = document.getElementById('closePreview');
 
    // 如果预览元素不存在,则不初始化
    if (!preview || !previewImg) {
        return;
    }
 
    // 关闭按钮点击事件
    if (closeBtn) {
        closeBtn.addEventListener('click', closePreview);
    }
 
    // 点击预览区域外关闭
    preview.addEventListener('click', function (e) {
        if (e.target === preview) {
            closePreview();
        }
    });
 
    // 键盘事件监听
    document.addEventListener('keydown', function (e) {
        if (e.key === 'Escape' && preview.style.display === 'flex') {
            closePreview();
        }
    });
 
    // 关闭预览函数
    function closePreview() {
        preview.style.display = 'none';
        previewImg.src = '';
    }
}
 
/**
 * 显示图片预览
 * @param {string} imgUrl 图片URL
 */
function showEventInfoPreview(imgUrl) {
    var preview = document.getElementById('imgPreview');
    var previewImg = document.getElementById('previewImg');
 
    if (preview && previewImg) {
        previewImg.src = imgUrl;
        preview.style.display = 'flex';
    }
}
 
/**
 * 读取事件记录
 */
function searchRecord(page, size) {
    var pageNumber = 1;
    var sizeNumber = 6;
    if (pageSize && pageSize > 0){
        size = pageSize;
    }
 
    if (size && size > 0){
        sizeNumber = size;
    }
    if (page && page > 0){
        pageNumber = page;
    }
    // 构造查询参数,从第一页开始
    var queryParams = buildQueryParams(pageNumber, sizeNumber);
    // 显示loading
    var loadingIndex = layer.load(1, {shade: [0.1, '#fff']});
    // 调用数据请求方法
    fetchEventInfoData(queryParams, function(error, data) {
        // 关闭loading
        layer.close(loadingIndex);
        if (error) {
            layer.msg(error.message);
            return;
        }
 
        // 更新页面数据
        updateGallery(data.records);
        // 重新初始化分页组件
        reinitPagination(data.total, data.size, data.current);
    });
}