363 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			363 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php /*a:2:{s:50:"E:\Demo\PHP\yunzer\app\admin\view\articles\add.php";i:1747649140;s:51:"E:\Demo\PHP\yunzer\app\admin\view\public\header.php";i:1746890051;}*/ ?>
 | 
						||
<!DOCTYPE html>
 | 
						||
<html>
 | 
						||
	<head>
 | 
						||
		<title><?php echo htmlentities((string) $config['admin_name']); ?></title>
 | 
						||
		<meta name="renderer" content="webkit">
 | 
						||
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 | 
						||
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
 | 
						||
		<link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css" media="all"/>
 | 
						||
		<link rel="stylesheet" type="text/css" href="/static/css/moban.css" media="all"/>
 | 
						||
		<link rel="stylesheet" type="text/css" href="/static/css/wangeditor.css" media="all"/>
 | 
						||
		<style type="text/css">
 | 
						||
			.header span{background:#009688;margin-left:30px;padding:10px;color:#ffffff;}
 | 
						||
			.header div{border-bottom:solid 2px #009688;margin-top: 8px;}
 | 
						||
			.header button{float:right;margin-top:-5px;}
 | 
						||
			.pagination {
 | 
						||
				display: inline-block;
 | 
						||
				padding-left: 0;
 | 
						||
				margin: 20px 0;
 | 
						||
				border-radius: 4px;
 | 
						||
			}
 | 
						||
			.pagination > li {
 | 
						||
				display: inline;
 | 
						||
			}
 | 
						||
			.pagination > li > a,
 | 
						||
			.pagination > li > span {
 | 
						||
				position: relative;
 | 
						||
				float: left;
 | 
						||
				padding: 6px 12px;
 | 
						||
				margin-left: -1px;
 | 
						||
				line-height: 1.42857143;
 | 
						||
				color: #337ab7;
 | 
						||
				text-decoration: none;
 | 
						||
				background-color: #fff;
 | 
						||
				border: 1px solid #ddd;
 | 
						||
			}
 | 
						||
			.pagination > li:first-child > a,
 | 
						||
			.pagination > li:first-child > span {
 | 
						||
				margin-left: 0;
 | 
						||
				border-top-left-radius: 4px;
 | 
						||
				border-bottom-left-radius: 4px;
 | 
						||
			}
 | 
						||
			.pagination > li:last-child > a,
 | 
						||
			.pagination > li:last-child > span {
 | 
						||
				border-top-right-radius: 4px;
 | 
						||
				border-bottom-right-radius: 4px;
 | 
						||
			}
 | 
						||
			.pagination > li > a:hover,
 | 
						||
			.pagination > li > span:hover,
 | 
						||
			.pagination > li > a:focus,
 | 
						||
			.pagination > li > span:focus {
 | 
						||
				z-index: 2;
 | 
						||
				color: #23527c;
 | 
						||
				background-color: #eee;
 | 
						||
				border-color: #ddd;
 | 
						||
			}
 | 
						||
			.pagination > .active > a,
 | 
						||
			.pagination > .active > span,
 | 
						||
			.pagination > .active > a:hover,
 | 
						||
			.pagination > .active > span:hover,
 | 
						||
			.pagination > .active > a:focus,
 | 
						||
			.pagination > .active > span:focus {
 | 
						||
				z-index: 3;
 | 
						||
				color: #fff;
 | 
						||
				cursor: default;
 | 
						||
				background-color: #337ab7;
 | 
						||
				border-color: #337ab7;
 | 
						||
			}
 | 
						||
			.pagination > .disabled > span,
 | 
						||
			.pagination > .disabled > span:hover,
 | 
						||
			.pagination > .disabled > span:focus,
 | 
						||
			.pagination > .disabled > a,
 | 
						||
			.pagination > .disabled > a:hover,
 | 
						||
			.pagination > .disabled > a:focus {
 | 
						||
				color: #777;
 | 
						||
				cursor: not-allowed;
 | 
						||
				background-color: #fff;
 | 
						||
				border-color: #ddd;
 | 
						||
			}
 | 
						||
			.close-img { background: url(/static/images/close_img.png); background-size: 20px 20px; width:20px; height: 20px; position: absolute; right: 5px; top: 5px; z-index: 2;}
 | 
						||
		</style>
 | 
						||
		<script type="text/javascript" src="/static/layui/layui.js"></script>
 | 
						||
		<script type="text/javascript">
 | 
						||
			layui.use(['layer','form','table','laydate','element','upload'],function(){
 | 
						||
				layer = layui.layer;		// layui 弹框
 | 
						||
				form = layui.form;			// layui form表单
 | 
						||
				table = layui.table;		// layui 表格
 | 
						||
				laydate = layui.laydate;	// layui 时间框
 | 
						||
				element = layui.element;	// layui element
 | 
						||
				upload = layui.upload;		// layui 上传
 | 
						||
				$ = layui.jquery;			// layui jquery
 | 
						||
			})
 | 
						||
		</script>
 | 
						||
	</head>
 | 
						||
	<body style="padding:10px; box-sizing: border-box;">
 | 
						||
<div class="config-container">
 | 
						||
    <div class="config-header" style="display:flex;justify-content: space-between;">
 | 
						||
        <div>
 | 
						||
            <span>添加文章</span>
 | 
						||
        </div>
 | 
						||
        <div>
 | 
						||
            <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" onclick="goBack()">
 | 
						||
                <i class="layui-icon layui-icon-return"></i>返回
 | 
						||
            </button>
 | 
						||
        </div>
 | 
						||
    </div>
 | 
						||
    <form class="layui-form" action="" method="post">
 | 
						||
        <div class="layui-form-item">
 | 
						||
            <label class="layui-form-label">标题</label>
 | 
						||
            <div class="layui-input-block">
 | 
						||
                <input type="text" name="title" required lay-verify="required" placeholder="请输入文章标题" autocomplete="off"
 | 
						||
                    class="layui-input">
 | 
						||
            </div>
 | 
						||
        </div>
 | 
						||
 | 
						||
        <div class="layui-form-item">
 | 
						||
            <label class="layui-form-label">分类</label>
 | 
						||
            <div class="layui-input-block">
 | 
						||
                <select name="cate" lay-verify="required">
 | 
						||
                    <option value="">请选择分类</option>
 | 
						||
                </select>
 | 
						||
            </div>
 | 
						||
        </div>
 | 
						||
 | 
						||
        <div class="layui-form-item">
 | 
						||
            <label class="layui-form-label">描述</label>
 | 
						||
            <div class="layui-input-block">
 | 
						||
                <textarea name="desc" placeholder="请输入描述内容" class="layui-textarea"></textarea>
 | 
						||
            </div>
 | 
						||
        </div>
 | 
						||
 | 
						||
        <div class="layui-form-item">
 | 
						||
            <label class="layui-form-label">作者</label>
 | 
						||
            <div class="layui-input-block">
 | 
						||
                <input type="text" name="author" required lay-verify="required" placeholder="请输入作者" autocomplete="off"
 | 
						||
                    class="layui-input">
 | 
						||
            </div>
 | 
						||
        </div>
 | 
						||
 | 
						||
        <div class="layui-form-item">
 | 
						||
            <label class="layui-form-label">封面</label>
 | 
						||
            <div class="layui-input-block">
 | 
						||
                <button type="button" class="layui-btn" id="upload-btn">
 | 
						||
                    <i class="layui-icon layui-icon-upload"></i> 图片上传
 | 
						||
                </button>
 | 
						||
                <div style="width: 120px;">
 | 
						||
                    <div class="layui-upload-list">
 | 
						||
                        <img class="layui-upload-img" id="upload-img"
 | 
						||
                            style="width: 118px; height: 118px;object-fit: cover;">
 | 
						||
                        <div id="upload-text"></div>
 | 
						||
                    </div>
 | 
						||
                    <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="filter-demo">
 | 
						||
                        <div class="layui-progress-bar" lay-percent=""></div>
 | 
						||
                    </div>
 | 
						||
                    <input type="hidden" name="image" id="image" value="">
 | 
						||
                </div>
 | 
						||
            </div>
 | 
						||
        </div>
 | 
						||
 | 
						||
        <div class="layui-form-item layui-form-text">
 | 
						||
            <label class="layui-form-label">内容</label>
 | 
						||
            <div class="layui-input-block">
 | 
						||
                <div id="editor—wrapper" id="content" name="content" style="border: 1px solid #ccc;">
 | 
						||
                    <div id="toolbar-container" style="border-bottom: 1px solid #ccc;"><!-- 工具栏 --></div>
 | 
						||
                    <div id="editor-container" style="height: 800px;"><!-- 编辑器 --></div>
 | 
						||
                </div>
 | 
						||
            </div>
 | 
						||
        </div>
 | 
						||
 | 
						||
        <div class="layui-form-item">
 | 
						||
            <div class="layui-input-block">
 | 
						||
                <button class="layui-btn" lay-submit lay-filter="formSubmit">立即提交</button>
 | 
						||
                <button class="layui-btn layui-btn-primary" lay-submit lay-filter="formDraft">存草稿</button>
 | 
						||
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
 | 
						||
            </div>
 | 
						||
        </div>
 | 
						||
    </form>
 | 
						||
</div>
 | 
						||
 | 
						||
<script src="/static/js/wangeditor.js"></script>
 | 
						||
<script>
 | 
						||
    layui.use(['form', 'layer'], function () {
 | 
						||
        var form = layui.form;
 | 
						||
        var layer = layui.layer;
 | 
						||
        var $ = layui.jquery;
 | 
						||
        var upload = layui.upload;
 | 
						||
        var element = layui.element;
 | 
						||
 | 
						||
        // 图片上传
 | 
						||
        var uploadInst = upload.render({
 | 
						||
            elem: '#upload-btn',
 | 
						||
            url: '<?php echo url("index/upload_img"); ?>', // 上传图片接口
 | 
						||
            before: function (obj) {
 | 
						||
                // 预读本地文件示例,不支持ie8
 | 
						||
                obj.preview(function (index, file, result) {
 | 
						||
                    $('#upload-img').attr('src', result); // 图片链接(base64)
 | 
						||
                });
 | 
						||
                element.progress('filter-demo', '0%'); // 进度条复位
 | 
						||
                layer.msg('上传中', { icon: 16, time: 0 });
 | 
						||
            },
 | 
						||
            done: function (res) {
 | 
						||
                // 若上传失败
 | 
						||
                if (res.code > 0) {
 | 
						||
                    return layer.msg('上传失败');
 | 
						||
                }
 | 
						||
                // 上传成功
 | 
						||
                $('#image').val(res.data); // 设置图片路径到隐藏输入框
 | 
						||
                $('#upload-text').html(''); // 置空上传失败的状态
 | 
						||
                layer.msg('上传成功', { icon: 1 });
 | 
						||
            },
 | 
						||
            uploadError: function () {  // 这里改为 uploadError
 | 
						||
                // 演示失败状态,并实现重传
 | 
						||
                var demoText = $('#upload-text');
 | 
						||
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
 | 
						||
                demoText.find('.demo-reload').on('click', function () {
 | 
						||
                    uploadInst.upload();
 | 
						||
                });
 | 
						||
            },
 | 
						||
            // 进度条
 | 
						||
            progress: function (n, elem, e) {
 | 
						||
                element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
 | 
						||
                if (n == 100) {
 | 
						||
                    layer.msg('上传完毕', { icon: 1 });
 | 
						||
                }
 | 
						||
            }
 | 
						||
        });
 | 
						||
 | 
						||
        // 获取分类列表
 | 
						||
        $.get('<?php echo url("articles/getcate"); ?>', function (res) {
 | 
						||
            if (res.code == 0) {
 | 
						||
                var html = '<option value="">请选择分类</option>';
 | 
						||
                res.data.forEach(function (item) {
 | 
						||
                    // 如果cid为0,则设置为禁用
 | 
						||
                    var disabled = item.cid == 0 ? 'disabled' : '';
 | 
						||
                    html += '<option value="' + item.id + '" ' + disabled + '>' + item.name + '</option>';
 | 
						||
                    // 如果有子分类,添加子分类选项
 | 
						||
                    if (item.children && item.children.length > 0) {
 | 
						||
                        item.children.forEach(function (child) {
 | 
						||
                            html += '<option value="' + child.id + '">├─ ' + child.name + '</option>';
 | 
						||
                        });
 | 
						||
                    }
 | 
						||
                });
 | 
						||
                $('select[name="cate"]').html(html);
 | 
						||
                form.render('select');
 | 
						||
            } else {
 | 
						||
                layer.msg(res.msg, { icon: 2 });
 | 
						||
            }
 | 
						||
        });
 | 
						||
 | 
						||
        // 表单提交
 | 
						||
        form.on('submit(formSubmit)', function (data) {
 | 
						||
            // 获取编辑器内容
 | 
						||
            var content = editor.getHtml();
 | 
						||
            if (!content || content === '<p><br></p>') {
 | 
						||
                layer.msg('请输入文章内容', { icon: 2 });
 | 
						||
                return false;
 | 
						||
            }
 | 
						||
 | 
						||
            var loadIndex = layer.load(2);
 | 
						||
            data.field.content = content;
 | 
						||
 | 
						||
            $.ajax({
 | 
						||
                url: '<?php echo url("articles/add"); ?>',
 | 
						||
                type: 'POST',
 | 
						||
                data: data.field,
 | 
						||
                success: function (res) {
 | 
						||
                    layer.close(loadIndex);
 | 
						||
                    if (res.code == 0) {
 | 
						||
                        layer.msg(res.msg, { icon: 1 });
 | 
						||
                        setTimeout(function () {
 | 
						||
                            window.location.href = '<?php echo url("articles/articlelist"); ?>';
 | 
						||
                        }, 1000);
 | 
						||
                    } else {
 | 
						||
                        layer.msg(res.msg, { icon: 2 });
 | 
						||
                    }
 | 
						||
                }
 | 
						||
            });
 | 
						||
            return false;
 | 
						||
        });
 | 
						||
    });
 | 
						||
 | 
						||
</script>
 | 
						||
 | 
						||
<!-- wangeditor编辑器脚本 -->
 | 
						||
<script>
 | 
						||
    const { createEditor, createToolbar } = window.wangEditor
 | 
						||
 | 
						||
    const editorConfig = {
 | 
						||
        MENU_CONF: {},
 | 
						||
        placeholder: '请输入内容...',
 | 
						||
        onChange(editor) {
 | 
						||
            const html = editor.getHtml()
 | 
						||
        },
 | 
						||
    }
 | 
						||
 | 
						||
    // 配置图片上传
 | 
						||
    editorConfig.MENU_CONF['uploadImage'] = {
 | 
						||
        server: '<?php echo url("index/upload_img"); ?>',
 | 
						||
        fieldName: 'file',
 | 
						||
        maxFileSize: 10 * 1024 * 1024, // 10M
 | 
						||
        maxNumberOfFiles: 10,
 | 
						||
        allowedFileTypes: ['image/*'],
 | 
						||
        meta: {
 | 
						||
            token: 'xxx'
 | 
						||
        },
 | 
						||
        metaWithUrl: true,
 | 
						||
        headers: {
 | 
						||
            Accept: 'text/x-json'
 | 
						||
        },
 | 
						||
        timeout: 5 * 1000, // 5s
 | 
						||
 | 
						||
        onBeforeUpload(file) {
 | 
						||
            console.log('准备上传图片', file)
 | 
						||
            return file
 | 
						||
        },
 | 
						||
        onProgress(progress) {
 | 
						||
            console.log('上传进度', progress)
 | 
						||
        },
 | 
						||
        onSuccess(file, res) {
 | 
						||
            console.log('上传成功', file, res)
 | 
						||
        },
 | 
						||
        onFailed(file, res) {
 | 
						||
            layer.msg('上传失败:' + res.msg, { icon: 2 })
 | 
						||
            console.log('上传失败', file, res)
 | 
						||
        },
 | 
						||
        onError(file, err, res) {
 | 
						||
            layer.msg('上传出错:' + err.message, { icon: 2 })
 | 
						||
            console.error('上传出错', file, err, res)
 | 
						||
        },
 | 
						||
        customInsert(res, insertFn) {
 | 
						||
            // res 即服务端的返回结果
 | 
						||
            if (res.code === 0 && res.data) {
 | 
						||
                // 直接使用res.data作为图片地址
 | 
						||
                insertFn(res.data);
 | 
						||
            } else {
 | 
						||
                layer.msg('图片上传失败', { icon: 2 });
 | 
						||
            }
 | 
						||
        }
 | 
						||
    }
 | 
						||
 | 
						||
    const editor = createEditor({
 | 
						||
        selector: '#editor-container',
 | 
						||
        html: '<p><br></p>',
 | 
						||
        config: editorConfig,
 | 
						||
        mode: 'default', // or 'simple'
 | 
						||
    })
 | 
						||
 | 
						||
    const toolbarConfig = {}
 | 
						||
 | 
						||
    const toolbar = createToolbar({
 | 
						||
        editor,
 | 
						||
        selector: '#toolbar-container',
 | 
						||
        config: toolbarConfig,
 | 
						||
        mode: 'default', // or 'simple'
 | 
						||
    })
 | 
						||
</script>
 | 
						||
 | 
						||
<script>
 | 
						||
    //返回文章列表
 | 
						||
    function goBack() {
 | 
						||
        window.location.href = '<?php echo url("articles/articlelist"); ?>';
 | 
						||
    }
 | 
						||
</script>
 |