const uploadUrl = '/api-common/v1/upload?folder=' + 'gk/news' + '&cmptId=' + getCId() // 定义 typeDesc 映射 const typeDesc = { 0: { label: '文本', placeholder: '请输入文本内容', }, 1: { label: '网页', placeholder: '请输入网页地址', }, 2: { label: '小程序', placeholder: '请输入小程序地址', }, 3: { label: '短连接', placeholder: '请输入短连接地址', }, 4: { label: '页面', placeholder: '请输入页面地址', }, 5: { label: '文档', placeholder: '请输入文档地址或点击上方上传', }, 6: { label: '图片', placeholder: '请输入图片地址或点击上方上传', }, 7: { label: '地图', placeholder: '请输入地址和经纬度', } }; $(document).ready(function () { editor = UE.getEditor('container'); $('#save').click(function () { var title = $('#title').val(); if (!title) { layer.alert("标题不能为空!"); return; } let linkUrl = $('#linkUrl').val(); var content = editor.getContent(); // 新增字段 const openType = $('#openType').val(); const path = $('#path').val(); const lat = $('#lat').val(); const lng = $('#lng').val(); const appId = $('#appId').val(); if (!content) { layer.alert("内容不能为空!"); return; } $.ajax({ url: "/gk/news/save", type: 'post', contentType: 'application/json', data: JSON.stringify({ linkUrl, id: $('.edit-container').attr('data-id'), title: title, appId: appId, content: editor.getContent(), userId: getUId(), cmptId: getCId(), cover: $('#file-cover').attr('data-src'), type: $('input[name="type"]:checked').val(), // 新增的字段 openType: openType, path: path, lat: lat ?? 0, lng: lng ?? 0 }), success: function (data) { layer.msg('保存成功') window.location.reload(); // $('.edit-container').attr('data-id', data.content.id); // var length = $('.container .item').length; // var copy = null; // if (length == 0) { // copy = $('.copy').clone().clone(); // } else { // copy = $('.item').last().clone(); // $('.item[data-id="' + data.content.id + '"]').remove(); // } // $(copy).find('.title').text(data.content.title); // $(copy).find('.type').text(data.content.type == 1 ? '通知' : "赛训"); // $(copy).attr('data-id', data.content.id); // $(copy).show() // $('.container').append($(copy)); } }) }); $('.cover').click(function () { $('#file').trigger('click'); }); $('.file-cover').click(function () { $('#file-doc').trigger('click'); }); $('.back').click(function () { $('.container').show(); $('.edit-context').hide(); $('.edit-container').removeAttr('data-id'); $('.edit-container').removeAttr('add-news') }); $('.add').click(function () { clearInput() $('.container').hide(); $('.edit-context').show(); $('.edit-container').removeAttr('data-id'); $('.edit-container').attr('add-news', 'true') }); $('body').on('click', '.edit', function () { var id = $(this).parents('.item').attr('data-id'); $('.edit-container').attr('data-id', id); getNews(id); $('.container').hide(); $('.edit-context').show(); }) $('body').on('click', '.del', function () { var obj = $(this).parents('.item'); var id = obj.attr('data-id'); qikoo.dialog.confirm('确认要删除?', function () { delNews(id, function () { obj.remove(); window.location.reload(); }); }, function () { }); }) $('.cancel').click(function () { $('.edit-mask,.edit-dialog').fadeOut(); }); $('.sure').click(function () { var val = $('#newsAlias').val(); if (val.length > 10) { layer.msg("不能超过10个字"); return; } $.ajax({ url: '/setting/update', type: "post", data: { cmptId: getCmptId(), newsAlias: val }, success: function (data) { if (data.code == 200) { layer.msg('保存成功'); window.location.reload(); $('.edit-mask,.edit-dialog').fadeOut(); } } }) }); $('.edit-btn').click(function () { var text = $.trim($('.news-title span').text()) == '未命名' ? $.trim($('.news-title').text()) : ''; $('#newsAlias').val(); $('.edit-mask,.edit-dialog').fadeIn(); }) // 监听select选项 // 监听 openType 变化 $('#openType').change(function () { const openType = $(this).val(); updateViews(openType); }); }); // 显示/隐藏必要的参数 function updateViews(openType = 0) { const desc = typeDesc[openType]; if (desc) { // 修改 path 的 label 和 placeholder $('#path').prev('label').text(desc.label); $('#path').attr('placeholder', desc.placeholder); if (openType == '0') { // $('#path-input').addClass('hidden'); } else { $('#path-input').removeClass('hidden'); } if (openType == '2') { // $('#appId-input').removeClass('hidden'); } else { $('#appId-input').addClass('hidden'); } // 根据类型显示/隐藏经纬度输入框 if (openType == '7') { // $('#lat-input').removeClass('hidden'); $('#lng-input').removeClass('hidden'); } else { $('#lat-input').addClass('hidden'); $('#lng-input').addClass('hidden'); } } // 显示上传的弹框 if (["5", "6"].includes(openType)) { $('#file-upload').removeClass('hidden'); } else { $('#file-upload').addClass('hidden'); } } //文件上传处理 function uploadFile(obj) { var reads = new FileReader(); file = obj.files[0]; if (file.type && file.type.indexOf("image") == -1) { layer.msg('请上传图片', { icon: 5 }) return; } reads.readAsDataURL(file); reads.onload = function (e) { //将处理完的图片文件上传到阿里云服务器 //上传LOGO图片 var formData = new FormData(); formData.append("file", file); $.ajax({ url: uploadUrl, /*接口域名地址*/ type: 'post', data: formData, contentType: false, processData: false, success: function (res) { if (res.code == 200) { layer.msg('上传成功', { icon: 1, time: 1500 }); $('#file-cover').attr('data-src', res.content); $('#file-cover').attr('data-src', res.content); $('.cover').css('background-image', 'url(' + res.content + ')') $('.icon-cover').remove(); } } }) }; } //文件上传处理 function uploadImageOrDocFile(obj) { var reads = new FileReader(); const openType = $('#openType').val(); file = obj.files[0]; if (file.type && file.type.indexOf("image") == -1 && openType == 6) { layer.msg('请上传图片', { icon: 5 }) return; } // 文档 if (file.type && !['.docx', '.xlsx', '.pptx'].includes(file.name.slice(file.name.lastIndexOf('.'))) && openType == 5) { layer.msg('请上传文档', { icon: 5 }) return; } reads.readAsDataURL(file); reads.onload = function (e) { //将处理完的图片文件上传到阿里云服务器 //上传LOGO图片 var formData = new FormData(); formData.append("file", file); $.ajax({ url: uploadUrl, /*接口域名地址*/ type: 'post', data: formData, contentType: false, processData: false, success: function (res) { if (res.code == 200) { layer.msg('上传成功', { icon: 1, time: 1500 }); // 正确的方式 - 使用val()方法 $("#path").val(res.content); if (openType == 6) { $('.file-cover').css('background-image', 'url(' + res.content + ')') $('.file-icon-cover').remove(); } } } }) }; } function getNews(id) { layer.load(2) $.ajax({ url: '/gk/news/get/' + id, type: 'get', success: function (data) { if (data.code == 200) { $('#title').val(data.content.title) $('#linkUrl').val(data.content.linkUrl) $('input[name="type"][value=' + data.content.type + ']').attr('checked', true) $('#openType').val(data.content.openType); $('#path').val(data.content.path); $('#lat').val(data.content.lat); $('#lng').val(data.content.lng); $('#appId').val(data.content.appId); updateViews(data.content.openType) if (data.content.cover) { $('.cover').css('background-image', "url(" + data.content.cover + ")") $('.icon-cover').remove(); $('#file-cover').attr('data-src', data.content.cover) } editor.setContent(data.content.content) } layer.closeAll(); }, compelete: function () { layer.closeAll(); } }) } function clearInput() { $('#title').val('') $($('input[name="type"]')[0]).attr('checked', "checked") $('.cover').css('background-image', '') editor.setContent(''); $('#openType').val(0); $('#path').val(""); $('#lat').val(""); $('#lng').val(""); $('#appId').val(""); } function delNews(id, callback) { $.ajax({ url: '/gk/news/delete/' + id, type: 'post', success: function (data) { if (data.code == 200) { callback(); } layer.closeAll(); layer.msg('删除成功!'); }, compelete: function () { layer.closeAll(); } }) }