norieli/laravel-ueditor

为laravel api使用百度ueditor,配合阿里云OSS。

v1.0 2020-04-16 02:35 UTC

This package is auto-updated.

Last update: 2024-09-16 13:10:59 UTC


README

laravel api & baidu ueditor & aliyun oss

填坑百度,终呈于此。

灵感来源

需求

  • Laravel 5+
  • cURL 扩展
  • php 7.0+

重要

  • 无百度原始前端静态资源!!!

后端安装

  • 步骤 1

为了安装AliOSS-storage,只需在composer.json中添加

"norieli/laravel-ueditor": "^1.0"

然后运行composer installcomposer update
或者你可以直接运行以下命令来安装

"composer require norieli/laravel-ueditor:^1.0"
  • 步骤 2

复制

"yourproject/vendor/norieli/laravel-ueditor/src/config.php"

"yourproject/config/ueditor.php"

并修改为你的配置

  • 步骤 3

添加到路由

// ueditor get
Route::get('/ueditor',function ()
{
    return (new \Norie\Laravel\UE\Controller())->do();
});
// ueditor get
Route::middleware(['auth:api'])->post('/ueditor',function ()
{
    return (new \Norie\Laravel\UE\Controller())->do();
});
  • 步骤 4(如果你需要阿里云OSS)
  1. 在你的config/app.php中将此行添加到providers数组中
Jacobcyl\AliOSS\AliOssServiceProvider::class,
  1. 在app/filesystems.php中添加以下内容
'disks'=>[
    ...
    'oss' => [
            'driver'        => 'oss',
            'access_id'     => '<Your Aliyun OSS AccessKeyId>',
            'access_key'    => '<Your Aliyun OSS AccessKeySecret>',
            'bucket'        => '<OSS bucket name>',
            'endpoint'      => '<the endpoint of OSS, E.g: oss-cn-hangzhou.aliyuncs.com | custom domain, E.g:img.abc.com>', // OSS 外网节点或自定义外部域名
            //'endpoint_internal' => '<internal endpoint [OSS内网节点] 如:oss-cn-shenzhen-internal.aliyuncs.com>', // v2.0.4 新增配置属性,如果为空,则默认使用 endpoint 配置(由于内网上传有点小问题未解决,请大家暂时不要使用内网节点上传,正在与阿里技术沟通中)
            'cdnDomain'     => '<CDN domain, cdn域名>', // 如果isCName为true, getUrl会判断cdnDomain是否设定来决定返回的url,如果cdnDomain未设置,则使用endpoint来生成url,否则使用cdn
            'ssl'           => <true|false> // true to use 'https://' and false to use 'http://'. default is false,
            'isCName'       => <true|false> // 是否使用自定义域名,true: 则Storage.url()会使用自定义的cdn或域名生成文件url, false: 则使用外部节点生成url
            'debug'         => <true|false>
    ],
    ...
]
  1. 然后在app/filesystems.php中设置默认驱动
'default' => 'oss',

好了!后端完成了!

后端安装

  • 步骤 1

查看vue-ueditor了解更多信息。

如果你不使用vue,可以下载此静态资源。下载

  • 步骤 2(如果你需要认证,如果不,请从路由中删除认证中间件)

无论你选择什么。你还需要将以下4个文件改为添加http请求头。

  1. static/UE/dialogs/image/image.js
  2. static/UE/dialogs/image/image.js
  3. static/UE/dialogs/video/video.js

相同的代码区域

 uploader.on('uploadBeforeSend', function (file, data, header) {
        //这里可以通过data对象添加POST参数
        header['X_Requested_With'] = 'XMLHttpRequest';
        // like this
        header['Authorization'] = 'Bearer ' + localStorage.getItem('token');
    });
  1. static/UE/ueditor.all.js
var xhr = new XMLHttpRequest()
    xhr.open('post', action, true)
    // like this
    xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('token'));
    if (me.options.headers && Object.prototype.toString.apply(me.options.headers) === "[object Object]") {
    for (var key in me.options.headers) {
      xhr.setRequestHeader(key, me.options.headers[key])
    }
}
  1. static/UE/ueditor.config.js
    // 服务器统一请求接口路径
    , serverUrl: URL + "/api/ueditor"

好了!都完成了!

其他

欢迎提问

文档

更多开发细节请参阅阿里云OSS文档

许可证

源代码以MIT许可证发布。阅读LICENSE文件获取更多信息。