laravel-admin集成ueditor编辑器的图片列表显示问题解决方法

laravel-admin集成ueditor推荐使用laravel-u-editor,它是ueditor的laravel composer版本。基于UEditor 1.4.3.3开发,支持en、zh_CN、zh_TW,并且支持本地和七牛云存储,默认为本地上传 public/uploads

安装

composer require stevenyangecho/laravel-u-editor
然后在config/app.php的providers下增加一行
Stevenyangecho\UEditor\UEditorServiceProvider::class
执行
php artisan vendor:publish

基本配置

1.增加组件文件:app/Admin/Extensions/Form/uEditor.php:

<?php
namespace App\Admin\Extensions\Form;
use Encore\Admin\Form\Field;

/**
 * 百度编辑器
 * Class uEditor
 * @package App\Admin\Extensions\Form
 */
class uEditor extends Field
{
    // 定义视图
    protected $view = 'admin.uEditor';

    // css资源
    protected static $css = [];

    // js资源
    protected static $js = [
        'laravel-u-editor/ueditor.config.js',
        'laravel-u-editor/ueditor.all.min.js',
        'laravel-u-editor/lang/zh-cn/zh-cn.js'
    ];

    public function render()
    {
        $cs=csrf_token();
        $this->script = <<<EOT
        //解决第二次进入加载不出来的问题
        UE.delEditor("ueditor");
        // 默认id是ueditor
        var ue = UE.getEditor('ueditor'); 
        ue.ready(function () {
            ue.execCommand('serverparam', '_token', '$cs');
        });

EOT;
        return parent::render();
    }
}



2. 增加视图文件: resources/views/admin/uEditor.blade.php

<div class="form-group {!! !$errors->has($errorKey) ?: 'has-error' !!}">
    <label for="{{$id}}" class="col-sm-2 control-label">{{$label}}</label>
    <div class="col-sm-8">
        @include('admin::form.error')
        {{-- 这个style可以限制他的高度,不会随着内容变长 --}}
        <textarea type='text/plain' style="height:400px;" id='ueditor' id="{{$id}}" name="{{$name}}" placeholder="{{ $placeholder }}" {!! $attributes !!}  class='ueditor'>
            {!! old($column, $value) !!}
        </textarea>
        @include('admin::form.help-block')
    </div>
</div>

3. 然后注册进laravel-admin,在app/Admin/bootstrap.php中添加以下代码:

use App\Admin\Extensions\Form\uEditor;
Encore\Admin\Form::extend('ueditor', uEditor::class);

4. 调用:
$form->ueditor('content', '内容')->rules('required');

配置七牛服务

打开文件config\UEditorUpload.php配置七牛配置参数

// 将下一行注释
//        'mode'=>'local',//上传方式,local 为本地   qiniu 为七牛
        'mode'=>'qiniu',//上传方式,local 为本地   qiniu 为七牛

        //七牛配置,若mode='qiniu',以下为必填.
        'qiniu'=>[
            'accessKey'=>'。。。',
            'secretKey'=>'。。。',
            'bucket'=>'。。。',
            'url'=>'。。。',//七牛分配的CDN域名,注意带上http://

        ]
    ],

修复上传界面中无法管理文件列表的问题

vendor\stevenyangecho\laravel-u-editor\src\ListsQiniu.php
public function getList()
    {
        $size = $this->request->get('size', $this->listSize);
        $start = $this->request->get('start', '');
        $auth = new Auth(config('UEditorUpload.core.qiniu.accessKey'), config('UEditorUpload.core.qiniu.secretKey'));

        $bucketManager = new BucketManager($auth);
        // 注释掉下面的行
//        list($items, $marker, $error) = $bucketManager->listFiles(config('UEditorUpload.core.qiniu.bucket'), $this->path, $start, $size);
// 下面三行是修改的
        $result1 = $bucketManager->listFiles(config('UEditorUpload.core.qiniu.bucket'), $this->path, $start, $size);
        $size = count($result1);
        // 如果不取到items,报找不到key的错误
        $items = $result1[0]['items'];
// 判断是否错误
        if ($size > 2) {
            return [
                "state" => $error->message(),
                "list" => array(),
                "start" => $start,
                "total" => 0
            ];
        }
        if(empty($items)){
            return [
                "state" => "no match file",
                "list" => array(),
                "start" => $start,
                "total" => 0
            ];
        }

        $files=[];
//        dd($items['items']);
        foreach ($items as  $v) {
            if (preg_match("/\.(" . $this->allowFiles . ")$/i", $v['key'])) {
                $files[] = array(
                    'url' =>rtrim(config('UEditorUpload.core.qiniu.url'),'/').'/'.$v['key'],
                    'mtime' => $v['mimeType'],
                );
            }
        }
        if(empty($files)){
            return [
                "state" => "no match file",
                "list" => array(),
                "start" => $start,
                "total" => 0
            ];
        }
        /* 返回数据 */
        $result = [
            "state" => "SUCCESS",
            "list" => $files,
            "start" => $start,
            "total" => count($files)
        ];

        return $result;
    }
    原文作者:Tianby
    原文地址: https://segmentfault.com/a/1190000015255605
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞