init
This commit is contained in:
165
app/admin/controller/Ajax.php
Normal file
165
app/admin/controller/Ajax.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\admin\model\SystemUploadfile;
|
||||
use app\common\controller\AdminController;
|
||||
use app\common\service\MenuService;
|
||||
use app\admin\service\upload\Uploadfile;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use think\db\Query;
|
||||
use think\facade\Cache;
|
||||
use think\response\Json;
|
||||
|
||||
class Ajax extends AdminController
|
||||
{
|
||||
|
||||
/**
|
||||
* 初始化后台接口地址
|
||||
* @return Json
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function initAdmin(): Json
|
||||
{
|
||||
$cacheData = Cache::get('initAdmin_' . session('admin.id'));
|
||||
if (!empty($cacheData)) {
|
||||
return json($cacheData);
|
||||
}
|
||||
$menuService = new MenuService(session('admin.id'));
|
||||
$data = [
|
||||
'logoInfo' => [
|
||||
'title' => sysconfig('site', 'logo_title'),
|
||||
'image' => sysconfig('site', 'logo_image'),
|
||||
'href' => __url('index/index'),
|
||||
],
|
||||
'homeInfo' => $menuService->getHomeInfo(),
|
||||
'menuInfo' => $menuService->getMenuTree(),
|
||||
];
|
||||
Cache::tag('initAdmin')->set('initAdmin_' . session('admin.id'), $data);
|
||||
return json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理缓存接口
|
||||
*/
|
||||
public function clearCache()
|
||||
{
|
||||
Cache::clear();
|
||||
$this->success('清理缓存成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*/
|
||||
public function upload()
|
||||
{
|
||||
$this->checkPostRequest();
|
||||
$data = [
|
||||
'upload_type' => $this->request->post('upload_type'),
|
||||
'file' => $this->request->file('file'),
|
||||
];
|
||||
$uploadConfig = sysconfig('upload');
|
||||
empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
|
||||
$rule = [
|
||||
'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
|
||||
'file|文件' => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
|
||||
];
|
||||
$this->validate($data, $rule);
|
||||
try {
|
||||
$upload = Uploadfile::instance()
|
||||
->setUploadType($data['upload_type'])
|
||||
->setUploadConfig($uploadConfig)
|
||||
->setFile($data['file'])
|
||||
->save();
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($upload['save'] == true) {
|
||||
$this->success($upload['msg'], ['url' => $upload['url']]);
|
||||
} else {
|
||||
$this->error($upload['msg']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片至编辑器
|
||||
* @return Json
|
||||
*/
|
||||
public function uploadEditor(): Json
|
||||
{
|
||||
$this->checkPostRequest();
|
||||
$data = [
|
||||
'upload_type' => $this->request->post('upload_type'),
|
||||
'file' => $this->request->file('upload'),
|
||||
];
|
||||
$uploadConfig = sysconfig('upload');
|
||||
empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
|
||||
$rule = [
|
||||
'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
|
||||
'file|文件' => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
|
||||
];
|
||||
$this->validate($data, $rule);
|
||||
try {
|
||||
$upload = Uploadfile::instance()
|
||||
->setUploadType($data['upload_type'])
|
||||
->setUploadConfig($uploadConfig)
|
||||
->setFile($data['file'])
|
||||
->save();
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($upload['save'] == true) {
|
||||
return json([
|
||||
'error' => [
|
||||
'message' => '上传成功',
|
||||
'number' => 201,
|
||||
],
|
||||
'fileName' => '',
|
||||
'uploaded' => 1,
|
||||
'url' => $upload['url'],
|
||||
]);
|
||||
} else {
|
||||
$this->error($upload['msg']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传文件列表
|
||||
* @return Json
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function getUploadFiles(): Json
|
||||
{
|
||||
$get = $this->request->get();
|
||||
$page = isset($get['page']) && !empty($get['page']) ? $get['page'] : 1;
|
||||
$limit = isset($get['limit']) && !empty($get['limit']) ? $get['limit'] : 10;
|
||||
$title = isset($get['title']) && !empty($get['title']) ? $get['title'] : null;
|
||||
$this->model = new SystemUploadfile();
|
||||
$count = $this->model
|
||||
->where(function (Query $query) use ($title) {
|
||||
!empty($title) && $query->where('original_name', 'like', "%{$title}%");
|
||||
})
|
||||
->count();
|
||||
$list = $this->model
|
||||
->where(function (Query $query) use ($title) {
|
||||
!empty($title) && $query->where('original_name', 'like', "%{$title}%");
|
||||
})
|
||||
->page($page, $limit)
|
||||
->order($this->sort)
|
||||
->select();
|
||||
$data = [
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'count' => $count,
|
||||
'data' => $list,
|
||||
];
|
||||
return json($data);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user