分离系统服务接口

This commit is contained in:
Anyon 2019-11-04 18:54:50 +08:00
parent 12b83b44f7
commit d97c1ad62d
4 changed files with 122 additions and 108 deletions

View File

@ -15,12 +15,10 @@
namespace app\admin\controller\api;
use app\admin\service\AuthService;
use think\admin\Controller;
use think\admin\Storage;
/**
* 后台插件管理
* 通用插件管理
* Class Plugs
* @package app\admin\controller\api
*/
@ -37,106 +35,4 @@ class Plugs extends Controller
$this->fetch();
}
/**
* 获取文件上传参数
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function check()
{
$diff1 = explode(',', strtolower(input('exts', '')));
$diff2 = explode(',', strtolower(sysconf('storage.allow_exts')));
$exts = array_intersect($diff1, $diff2);
$this->success('获取文件上传参数', [
'exts' => join('|', $exts),
'mine' => Storage::mime($exts),
'type' => $this->getUploadType(),
'data' => $this->getUploadData(),
]);
}
/**
* 后台通用文件上传
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function upload()
{
if (!AuthService::isLogin()) {
$this->error('访问授权失败,请重新登录授权再试!');
}
if (!($file = $this->getUploadFile()) || empty($file)) {
return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件可能过大或未上传']]);
}
$this->extension = $file->getOriginalExtension();
if (!in_array($this->extension, explode(',', sysconf('storage.allow_exts')))) {
return json(['uploaded' => false, 'error' => ['message' => '文件上传类型受限,请在后台配置']]);
}
if (in_array($this->extension, ['php', 'sh'])) {
return json(['uploaded' => false, 'error' => ['message' => '可执行文件禁止上传到本地服务器']]);
}
$this->safe = boolval(input('safe'));
$this->uptype = $this->getUploadType();
$name = Storage::name($file->getPathname(), $this->extension, '', 'md5_file');
$info = Storage::instance($this->uptype)->set($name, file_get_contents($file->getRealPath()), $this->safe);
if (is_array($info) && isset($info['url'])) {
return json(['uploaded' => true, 'filename' => $name, 'url' => $this->safe ? $name : $info['url']]);
} else {
return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
}
}
/**
* 生成文件上传参数
* @return array
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function getUploadData()
{
if ($this->getUploadType() === 'qiniu') {
$file = Storage::instance('qiniu');
return ['url' => $file->upload(), 'token' => $file->buildUploadToken(), 'uptype' => $this->getUploadType()];
} else {
$file = Storage::instance('local');
return ['url' => $file->upload(), 'token' => uniqid('local_upload_'), 'uptype' => $this->getUploadType()];
}
}
/**
* 获取文件上传方式
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function getUploadType()
{
$this->uptype = input('uptype');
if (!in_array($this->uptype, ['local', 'qiniu'])) {
$this->uptype = sysconf('storage.type');
}
return $this->uptype;
}
/**
* 获取本地文件对象
* @return \think\file\UploadedFile
*/
private function getUploadFile()
{
try {
return $this->request->file('file');
} catch (\Exception $e) {
$this->error(lang($e->getMessage()));
}
}
}

View File

@ -16,11 +16,10 @@
namespace app\admin\controller\api;
use think\admin\Controller;
use think\admin\extend\PlugsExtend;
use think\admin\install\ExtendInstall;
/**
* 系统更新接口
* 安装服务端支持
* Class Update
* @package app\admin\controller\api
*/

View File

@ -0,0 +1,119 @@
<?php
namespace app\admin\controller\api;
use app\admin\service\AuthService;
use think\admin\Controller;
use think\admin\Storage;
/**
* 文件上传接口
* Class Upload
* @package app\admin\controller\api
*/
class Upload extends Controller
{
/**
* 上传安全检查
* @login true
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function check()
{
$diff1 = explode(',', strtolower(input('exts', '')));
$diff2 = explode(',', strtolower(sysconf('storage.allow_exts')));
$exts = array_intersect($diff1, $diff2);
$this->success('获取文件上传参数', [
'type' => $this->getType(), 'data' => $this->getData(),
'exts' => join('|', $exts), 'mine' => Storage::mime($exts),
]);
}
/**
* 文件上传入口
* @login true
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function file()
{
if (!AuthService::isLogin()) {
$this->error('访问授权失败,请重新登录授权再试!');
}
if (!($file = $this->getFile()) || empty($file)) {
return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件可能过大或未上传']]);
}
$this->extension = $file->getOriginalExtension();
if (!in_array($this->extension, explode(',', sysconf('storage.allow_exts')))) {
return json(['uploaded' => false, 'error' => ['message' => '文件上传类型受限,请在后台配置']]);
}
if (in_array($this->extension, ['php', 'sh'])) {
return json(['uploaded' => false, 'error' => ['message' => '可执行文件禁止上传到本地服务器']]);
}
$this->safe = boolval(input('safe'));
$this->uptype = $this->getType();
$name = Storage::name($file->getPathname(), $this->extension, '', 'md5_file');
$info = Storage::instance($this->uptype)->set($name, file_get_contents($file->getRealPath()), $this->safe);
if (is_array($info) && isset($info['url'])) {
return json(['uploaded' => true, 'filename' => $name, 'url' => $this->safe ? $name : $info['url']]);
} else {
return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
}
}
/**
* 生成文件上传参数
* @return array
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function getData()
{
if ($this->getType() === 'qiniu') {
$file = Storage::instance('qiniu');
return ['url' => $file->upload(), 'token' => $file->buildUploadToken(), 'uptype' => $this->getType()];
} else {
$file = Storage::instance('local');
return ['url' => $file->upload(), 'token' => uniqid('local_upload_'), 'uptype' => $this->getType()];
}
}
/**
* 获取文件上传方式
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function getType()
{
$this->uptype = input('uptype');
if (!in_array($this->uptype, ['local', 'qiniu'])) {
$this->uptype = sysconf('storage.type');
}
return $this->uptype;
}
/**
* 获取本地文件对象
* @return \think\file\UploadedFile
*/
private function getFile()
{
try {
return $this->request->file('file');
} catch (\Exception $e) {
$this->error(lang($e->getMessage()));
}
}
}

View File

@ -4,7 +4,7 @@ define(function () {
var uptype = $(element).attr('data-uptype') || '';
// 检查可以上传的文件后缀
$.form.load('?s=admin/api.plugs/check', {exts: exts, uptype: uptype}, 'post', function (ret, options) {
$.form.load('?s=admin/api.upload/check', {exts: exts, uptype: uptype}, 'post', function (ret, options) {
options = {url: ret.data.data.url, exts: ret.data.exts, acceptMime: ret.data.mine, data: ret.data.data};
if (exts.indexOf('*') > -1) delete options.exts, delete options.acceptMime;
return renderUploader(options), false;