luofl
2025-04-11 2fc4d1e0a7d1e786c99f3d113ac4449be2659c3f
cloud-server-management/src/main/webapp/WEB-INF/view/system/tHuiminAgreement/tHuiminAgreement_add.html
@@ -14,21 +14,11 @@
                            <div id="tab-1" class="tab-pane active">
                                <div class="panel-body">
                                    <textarea id="editor_1" type="text/plain" style="width:1200px;height:400px;">${item.addUserRemark!}</textarea>
                                    <!--<div class="row btn-group-m-t">
                                        <div class="col-sm-10 col-sm-offset-5">
                                            <#button btnCss="info" name="保存" id="ensure" icon="fa-check" clickFun=""/>
                                        </div>
                                    </div>-->
                                </div>
                            </div>
                            <div id="tab-2" class="tab-pane">
                                <div class="panel-body">
                                    <textarea type="text/plain" id="editor_2" style="width:1200px;height:400px;"${item.selectUserRemark!}</textarea>
                                    <!--<div class="row btn-group-m-t">
                                        <div class="col-sm-10">
                                            <#button btnCss="info" name="保存" id="ensure" icon="fa-check" clickFun=""/>
                                        </div>
                                    </div>-->
                                </div>
                            </div>
                        </div>
@@ -108,6 +98,12 @@
        editor_2 = UE.getEditor('editor_2');
        editor_3 = UE.getEditor('editor_3');
        // 改用全局方法处理删除
        window.deleteTab = function(counter) {
            console.log("全局删除方法被调用,counter =", counter);
            deleteEditor(parseInt(counter));
        };
        // 绑定添加按钮的点击事件
        $("#agreementAdd").click(function() {
            var title = $("#agreement").val().trim();
@@ -123,27 +119,91 @@
            $("#agreement").val("");
        });
        
        //协议配置回显
        let settingList = '${tHuiminAgreementSettingList}';
        if (settingList != "" && typeof settingList != "undefined"){
            let settingArr = JSON.parse(settingList);
            review(settingArr);
        }
        // 确保编辑器初始化完成后再获取数据
        editor_3.ready(function() {
            console.log("编辑器初始化完成,准备加载数据");
            // 页面初始化时通过AJAX请求获取协议配置数据
            var operatorId = $("#operator").val();
            console.log("页面初始化,当前运营商ID:", operatorId);
            // 调用接口获取协议配置数据(只回显协议配置,不回显门店无惠民卡介绍页)
            var ajax = new $ax(Feng.ctxPath + "/tHuiminAgreement/selectAgreementByOperationId", function(data){
                console.log("获取到的惠民卡数据:", data);
                if (data.data != null){
                    // 只更新协议配置
                    if (data.data.settingList && data.data.settingList.length > 0) {
                        console.log("协议配置数据:", data.data.settingList);
                        review(data.data.settingList);
                    }
                }
            },function(data){
                console.error("获取惠民卡数据失败:", data);
                Feng.error("获取惠民卡数据失败!");
            });
            ajax.set("operatorId", operatorId);
            ajax.start();
        });
        
        //经营商切换事件
        $("#operator").change(function() {
            console.log("运营商切换事件触发,选择的运营商ID:", $("#operator").val());
            // 先主动清空编辑器内容
            if(editor_3 && editor_3.body) {
                editor_3.setContent("");
                console.log("主动清空门店无惠民卡介绍页内容");
            }
            //清空协议配置
            removeAll()
            removeAll();
            //调用接口获取协议配置数据
            //提交信息
            var ajax = new $ax(Feng.ctxPath + "/tHuiminAgreement/selectAgreementByOperationId", function(data){
                if (data.data != "" && data.data != null){
                    console.log("协议配置数据:"+data.data);
                    let settingArr = JSON.parse(data.data);
                    review(settingArr);
                console.log("获取到的惠民卡数据:", data);
                if (data.data != null){
                    // 更新门店无惠民卡介绍页内容
                    try {
                        var introContent = "";
                        // 严格判断内容是否存在且非空
                        if(data.data.storeNoHuiminCardIntro !== null &&
                           data.data.storeNoHuiminCardIntro !== undefined &&
                           data.data.storeNoHuiminCardIntro !== "") {
                            introContent = data.data.storeNoHuiminCardIntro;
                        }
                        // 确保编辑器实例存在且已初始化
                        if(editor_3 && editor_3.body) {
                            // 设置编辑器内容(空内容会清空编辑器)
                            editor_3.setContent(introContent);
                            console.log("已更新门店无惠民卡介绍页内容:", introContent ? "有内容" : "内容为空");
                        } else {
                            console.error("编辑器未准备好,无法设置内容");
                            // 延迟设置内容
                            setTimeout(function() {
                                try {
                                    if(editor_3) {
                                        editor_3.setContent(introContent);
                                        console.log("延迟更新门店无惠民卡介绍页内容成功");
                                    }
                                } catch(e) {
                                    console.error("延迟设置编辑器内容出错:", e);
                                }
                            }, 1000);
                        }
                    } catch(e) {
                        console.error("设置编辑器内容出错:", e);
                    }
                    // 更新协议配置
                    if (data.data.settingList && data.data.settingList.length > 0) {
                        console.log("协议配置数据:", data.data.settingList);
                        review(data.data.settingList);
                    }
                }
            },function(data){
                console.error("获取惠民卡数据失败:", data);
                Feng.error("获取惠民卡数据失败!");
            });
            ajax.set("operatorId",$("#operator").val());
            ajax.start();
@@ -180,7 +240,15 @@
    function review(settingArr){
        if (typeof settingArr !== 'undefined' && settingArr.length > 0) {
            settingArr.forEach(function(item) {
                addNewEditor(item.title, item.content || "");
                // 详细记录每个配置项
                console.log("处理协议配置项:", item);
                // 获取标题和内容,兼容不同字段名
                var title = item.agreementName || item.title || "";
                var content = item.agreementContent || item.content || "";
                console.log("使用标题:", title, "内容长度:", content.length, "内容前20字符:", content.substring(0, 20));
                addNewEditor(title, content);
            });
        }
    }
@@ -188,8 +256,11 @@
    // 添加新的编辑器函数 - 修改为接受内容参数
    function addNewEditor(title, content) {
        editorCounter++;
        var editorId = "dynamic-editor-" + editorCounter;
        var tabId = "agreement-tab-" + editorCounter;
        // 立即捕获当前counter值,防止闭包问题
        var currentCounter = editorCounter;
        var editorId = "dynamic-editor-" + currentCounter;
        var tabId = "agreement-tab-" + currentCounter;
        var isFirstTab = (dynamicEditors.length === 0);
        
        // 如果是第一个编辑器,显示tabs容器
@@ -202,10 +273,15 @@
        $("#agreement-content .tab-pane").removeClass("active");
        
        // 创建新的Tab - 始终设置为激活状态
        var tabHtml = '<li class="active" id="tab-li-' + editorCounter + '">' +
                      '<a data-toggle="tab" href="#' + tabId + '" aria-expanded="true">' + title +
                      ' <i class="fa fa-times-circle delete-tab" data-editor-id="' + editorId + '" ' +
                      'data-tab-id="' + tabId + '" data-counter="' + editorCounter + '"></i></a></li>';
        // 修改删除按钮DOM结构,确保按钮在标题右侧
        var tabHtml = '<li class="active" id="tab-li-' + currentCounter + '" style="position:relative;">' +
                      '<a data-toggle="tab" href="#' + tabId + '" aria-expanded="true" style="padding-right:25px;">' + title + '</a>' +
                      '<span onclick="deleteTab(' + currentCounter + ')" ' +
                      'style="position:absolute;right:5px;top:10px;cursor:pointer;color:#f00;z-index:100;">' +
                      '<i class="fa fa-times-circle"></i></span></li>';
        // 记录创建的按钮信息,辅助调试
        console.log("创建删除按钮, counter =", currentCounter, "title =", title);
        
        // 创建Tab内容 - 始终设置为激活状态
        var contentHtml = '<div id="' + tabId + '" class="tab-pane active">' +
@@ -226,90 +302,134 @@
            autoFloatEnabled: false
        });
        
        // 确保编辑器已准备好
        // 确保编辑器已准备好,使用立即执行函数捕获当前counter值
        (function(capturedCounter, editorId, tabId, titleText, contentText) {
        editor.ready(function() {
            try {
                // 检查编辑器是否已准备好接收内容
                if(editor.body) {
                    // 初始化内容
                    editor.setContent(content || "");
                        editor.setContent(contentText || "");
                } else {
                    // 如果编辑器body未准备好,延迟设置内容
                    setTimeout(function() {
                        try {
                            editor.setContent(content || "");
                                editor.setContent(contentText || "");
                        } catch(e) {
                            console.error("延迟设置编辑器内容出错:", e);
                        }
                    }, 500);
                }
                
                // 存储编辑器实例和标题信息
                    // 存储编辑器实例和标题信息,使用捕获的counter
                dynamicEditors.push({
                    id: editorId,
                    tabId: tabId,
                    counter: editorCounter,
                    title: title,
                        counter: capturedCounter,  // 使用捕获的值,而不是外部变量
                        title: titleText,
                    editor: editor,
                    content: content || ""
                        content: contentText || ""
                });
                
                // 绑定删除按钮事件
                $(".delete-tab[data-editor-id='" + editorId + "']").click(function(e) {
                    e.preventDefault();
                    e.stopPropagation();
                    deleteEditor($(this).data("counter"));
                });
                    console.log("编辑器准备完成,添加到dynamicEditors,counter =", capturedCounter);
                    // 不再需要在这里绑定事件,因为已经使用事件委托
            } catch(e) {
                console.error("编辑器初始化出错:", e);
            }
        });
        })(currentCounter, editorId, tabId, title, content);
        
        // 手动激活新添加的标签页
        $('#agreement-tabs a[href="#' + tabId + '"]').tab('show');
    }
    // 删除编辑器函数
    // 删除编辑器函数 - 优化标签页激活逻辑
    function deleteEditor(counter) {
        // 确保counter是整数
        counter = parseInt(counter);
        var index = -1;
        var isActive = $("#tab-li-" + counter).hasClass("active");
        var nextActive = null;
        
        // 输出当前所有编辑器的counter值,辅助调试
        console.log("当前所有编辑器的counter值:", dynamicEditors.map(function(item) { return item.counter; }));
        console.log("完整的dynamicEditors数组:", JSON.stringify(dynamicEditors.map(function(item) {
            return {
                counter: item.counter,
                title: item.title,
                tabId: item.tabId,
                id: item.id
            };
        })));
        // 查找编辑器索引
        for(var i = 0; i < dynamicEditors.length; i++) {
            if(dynamicEditors[i].counter == counter) {
            console.log("检查编辑器", i, "counter =", dynamicEditors[i].counter, "要删除的counter =", counter);
            // 使用严格的整数比较
            if(parseInt(dynamicEditors[i].counter) === counter) {
                index = i;
                break;
            }
        }
        
        if(index === -1) return;
        // 销毁编辑器实例
        if(dynamicEditors[index].editor) {
            dynamicEditors[index].editor.destroy();
        if(index === -1) {
            console.error("找不到要删除的编辑器:", counter);
            // 尝试遍历DOM查找是否存在该元素
            console.log("尝试检查DOM元素是否存在:", "#tab-li-" + counter);
            console.log("DOM元素存在:", $("#tab-li-" + counter).length > 0);
            return;
        }
        
        // 如果删除的是当前活跃tab,需要激活另一个tab
        console.log("删除编辑器", counter, "索引:", index, "是否当前活跃:", isActive);
        // 确定删除后需要激活哪个标签页
        if(isActive && dynamicEditors.length > 1) {
            // 优先选择下一个,如果没有则选择上一个
            if(index < dynamicEditors.length - 1) {
                nextActive = dynamicEditors[index + 1].tabId;
                console.log("将激活下一个标签页:", nextActive);
            } else {
                nextActive = dynamicEditors[index - 1].tabId;
                console.log("将激活上一个标签页:", nextActive);
            }
        }
        
        // 如果是当前活跃标签,先激活另一个标签,再删除当前标签
        if(isActive && nextActive) {
            try {
                console.log("先激活其他标签页:", nextActive);
                $('a[href="#' + nextActive + '"]').tab('show');
            } catch(e) {
                console.error("激活其他标签页失败:", e);
            }
        }
        // 销毁编辑器实例
        try {
            if(dynamicEditors[index].editor) {
                dynamicEditors[index].editor.destroy();
            }
        } catch(e) {
            console.error("销毁编辑器出错:", e);
        }
        // 完全清理DOM元素
        try {
        $("#tab-li-" + counter).remove();
        $("#" + dynamicEditors[index].tabId).remove();
        
        // 确保UEditor容器被完全移除
        $("#" + dynamicEditors[index].id).parents(".edui-default").remove();
        $("#" + dynamicEditors[index].id).remove();
        } catch(e) {
            console.error("清理DOM元素出错:", e);
        }
        
        // 从数组中移除
        dynamicEditors.splice(index, 1);
        console.log("删除后的编辑器列表:", dynamicEditors.map(function(item) { return item.title; }));
        
        // 如果没有编辑器了,清空并隐藏tabs容器
        if(dynamicEditors.length === 0) {
@@ -317,10 +437,8 @@
            // 彻底清空容器内容,确保没有残留
            $("#agreement-tabs").empty();
            $("#agreement-content").empty();
        } else if(nextActive) {
            // 激活下一个tab
            $('a[href="#' + nextActive + '"]').tab('show');
        }
        // 不再需要这个逻辑,因为我们在删除前已经激活了其他标签
    }
    
    //收集动态编辑器内容
@@ -340,11 +458,13 @@
                content = dynamicEditors[i].content || "";
            }
            
            // 使用与后端一致的字段名
            agreements.push({
                title: dynamicEditors[i].title,
                content: content
                agreementName: dynamicEditors[i].title,
                agreementContent: content
            });
        }
        console.log("收集到的协议配置数据:", agreements);
        return agreements;
    };