const _token = ''; let insTb; let Tools = { getInsTb: function () { return insTb; }, setInsTb: function (ins) { insTb = ins; } } layui.use(['jquery', 'form', 'layer', 'miniTab', 'laytpl', 'table', 'skuTable'], function () { let $ = layui.jquery, form = layui.form, layer = layui.layer, miniTab = layui.miniTab, skuTable = layui.skuTable, table = layui.table; let modifyUrl = $('#row-modify').data('url'); miniTab.listen(); // tinymce富文本编辑器 版本5.10.3没有使用6.0 因6将部分插件升级为了高级功能(独立出来了) let tConfig = function (selector) { return { selector: selector, //skin:'oxide-dark', language:'zh_CN', plugins: 'importword formatpainter print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link media template code codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists wordcount imagetools textpattern help emoticons autosave bdmap indent2em autoresize formatpainter axupimgs', toolbar: [ 'code undo redo restoredraft |importword formatpainter cut copy paste pastetext | image forecolor backcolor bold italic underline strikethrough link anchor | alignleft aligncenter alignright alignjustify outdent indent styleselect formatselect fontselect fontsizeselect', 'pagebreak insertdatetime print preview | fullscreen | bdmap indent2em lineheight formatpainter axupimgs bullist numlist | blockquote subscript superscript removeformat table media charmap emoticons hr', ], height: 650, //编辑器高度 min_height: 400, convert_urls: false, lineheight_formats : '0.1 0.2 0.5 1 1.5 2', fontsize_formats: '12px 14px 16px 18px 24px 36px 48px 56px 72px 88px', font_formats: '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;', // link_list: [ // { title: '预置链接1', value: 'http://www.tinymce.com' }, // { title: '预置链接2', value: 'http://tinymce.ax-z.cn' } // ], // image_list: [ // { title: '预置图片1', value: 'https://www.tiny.cloud/images/glyph-tinymce@2x.png' }, // { title: '预置图片2', value: 'https://www.baidu.com/img/bd_logo1.png' } // ], // image_class_list: [ // { title: 'None', value: '' }, // { title: 'Some class', value: 'class-name' } // ], images_upload_handler: function (blobInfo, succFun, failFun) { // http://tinymce.ax-z.cn/more-plugins/axupimgs.php var xhr, formData; var file = blobInfo.blob();//转化为易于理解的file对象 xhr = new XMLHttpRequest(); xhr.withCredentials = false; xhr.open('POST', '/manager/upload/tiny-image'); xhr.onload = function() { var json; console.log(xhr,'ss') if (xhr.status !== 200) { failFun('HTTP Error: ' + xhr.status + ' ' + xhr.statusText); return; } json = JSON.parse(xhr.responseText); if (!json || typeof json.location != 'string') { failFun('Invalid JSON: ' + xhr.responseText); return; } succFun(json.location); }; formData = new FormData(); formData.append('file', file, file.name); console.log(file, 'file') xhr.send(formData); }, importcss_append: true, //自定义文件选择器的回调内容 // file_picker_callback: function (callback, value, meta) { // if (meta.filetype === 'file') { // callback('https://www.baidu.com/img/bd_logo1.png', { text: 'My text' }); // } // if (meta.filetype === 'image') { // callback('https://www.baidu.com/img/bd_logo1.png', { alt: 'My alt text' }); // } // if (meta.filetype === 'media') { // callback('movie.mp4', { source2: 'alt.ogg', poster: 'https://www.baidu.com/img/bd_logo1.png' }); // } // }, toolbar_sticky: true, autosave_ask_before_unload: false, setup: function(editor){ editor.on('change',function(){ // editor.getContent(); // console.log(editor.getContent(), '当前内容') // console.log(editor, 'editor') // console.log(editor.id, 'sss') // console.log(selector, 'selector') // console.log('#'+editor.id, 'id') // 内容变更时 更新到textarea document.getElementById(editor.id).innerHTML = editor.getContent(); }); }, }; } tinymce.init(tConfig('.tinymce-editor')); //页面加载时,根据需要加载富文本编辑器 if ($('.editor').length) { editor(); } //监听配置页面提交 form.on('submit(saveConfig)', function (data) { let url = $(data.elem).data('url'); $.post(url, data.field, function (res) { layer.msg(res.msg); if (res.code === 0) { setTimeout(function () { //关闭当前弹出层 miniTab.deleteCurrentByIframe(); }, 1000) } }); return false; }); //监听提交 form.on('submit(saveBtn)', function (data) { let url = $(data.elem).data('url'); let isTab = $(data.elem).data('tab'); let index = layer.load(2); $.post(url, data.field, function (res) { layer.close(index); layer.msg(res.msg); if (res.code === 0) { if (isTab) { //关闭tab setTimeout(function () { miniTab.deleteCurrentByIframe(); }, 1000) } else { //刷新父级列表 parent.layui.$('[data-table-refresh]').trigger("click"); setTimeout(function () { //关闭当前弹出层 let iframeIndex = parent.layer.getFrameIndex(window.name); parent.layer.close(iframeIndex); }, 1000) } } }); return false; }); //监听关闭 form.on('submit(close)', function () { let iframeIndex = parent.layer.getFrameIndex(window.name); parent.layer.close(iframeIndex); return false; }); //监听单元格编辑 table.on('edit(table-container-filter)', function (obj) { let insTb = Tools.getInsTb(); $.post(modifyUrl, {id: obj.data.id, field: obj.field, value: obj.value}, function (res) { layer.msg(res.msg) if (res.code !== 0 || obj.field === 'sort') { refreshTab(insTb); } }) }); //监听状态改变 form.on('switch(changeStatus)', function (obj) { let val = obj.elem.checked ? 1 : 0; let insTb = Tools.getInsTb(); $.post(modifyUrl, {id: this.value, field: this.name, value: val}, function (res) { layer.msg(res.msg) if (res.code !== 0) { setTimeout(function () { refreshTab(insTb); }, 1000) } }) }); // 监听搜索操作 form.on('submit(data-search-btn)', function (data) { //执行搜索重载 table.reload('table-container', { page: {curr: 1} , where: {searchParams: data.field} }, 'data'); return false; }); //监听工具条 注意区别toolbar和tool toolbar是表头上的工具条 tool是行中的工具条 table.on('toolbar(table-container)', function (obj) { let layEvent = obj.event; let insTb = Tools.getInsTb(); let url = $($(this).context).data('href') let title = $($(this).context).data('title') let width = $($(this).context).data('width') ? $($(this).context).data('width') : '100%'; let height = $($(this).context).data('height') ? $($(this).context).data('height') : '100%'; // debugger; switch (layEvent) { // toolbar 删除 case 'del': let checkStatus = table.checkStatus('table-container'); if (checkStatus.data.length <= 0) { layer.msg('请先选择数据'); return false; } let selected = checkStatus.data; let ids = []; $.each(selected, function (index, val) { ids.push(val.id); }) delRow(url, ids, insTb); return false; // toolbar 刷新 case 'refresh': refreshTab(insTb); return false; // toolbar 搜索 case 'search': let search = $('.table-search-fieldset'); if (search.hasClass('div-show')) { search.css('display', 'none').removeClass('div-show'); } else { search.css('display', 'block').addClass('div-show'); } return false; // 其他 默认为打开弹出层 default: if (layEvent !== 'LAYTABLE_COLS' && layEvent !== 'LAYTABLE_EXPORT') { openLayer(url, title, width, height); return false; } } }); //监听行工具条 table.on('tool(table-container)', function (obj) { let data = obj.data; let layEvent = obj.event; let url = $($(this).context).data('href'); let title = $($(this).context).data('title'); let width = $($(this).context).data('width') ? $($(this).context).data('width') : '100%'; let height = $($(this).context).data('height') ? $($(this).context).data('height') : '100%'; let insTb = Tools.getInsTb(); switch (layEvent) { // 行 删除 case 'del': let ids = [data.id]; delRow(url, ids, insTb); return false; //其他 默认为打开弹出层 default: openLayer(url, title, width, height); return false; } }); /** 图片预览 **/ $('body').on('click', '.layui-layer-photos', function () { layer.photos({ photos: '.layui-layer-photos' // 指向图片的父容器 ,anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(3.0之前的版本用shift参数) }); }) }); //富文本编辑器,单页面可以添加多个,需要使用不同的ID TODO 使用的原CMS操作 function editor() { layui.use(['jquery', 'wangEditor'], function () { let $ = layui.jquery, E = layui.wangEditor; if ($('.editor').length) { $.each($('.editor'), function (i) { var wang = new E(this); var $text1 = $(this).parent().find('textarea'); wang.customConfig.onchange = function (html) { // 监控变化,同步更新到 textarea $text1.val(html) } wang.customConfig.pasteTextHandle = function (content) { if (content == '' && !content) return '';//其中content就是你zhantie过来的原始的文本 return removeFormatWrd(content); }; function removeFormatWrd(html) { // console.log(html) // html = html.replace(/[\s\S]*?<\/xml>/ig, ''); // html = html.replace(/