Files
ulthon_admin/extend/base/admin/controller/AjaxBase.php
2026-03-26 20:22:34 +08:00

149 lines
4.1 KiB
PHP

<?php
namespace base\admin\controller;
use app\admin\model\SystemUploadfile;
use app\common\controller\AdminController;
use app\common\service\MenuService;
use app\common\service\UploadService;
use think\db\Query;
use think\facade\App;
use think\facade\Cache;
class AjaxBase extends AdminController
{
/**
* 初始化后台接口地址
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function initAdmin()
{
$adminId = $this->getAdminId();
$cacheData = Cache::get('initAdmin_' . $adminId);
if (!empty($cacheData)) {
return json($cacheData);
}
$menuService = new MenuService($adminId);
$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_' . $adminId, $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'),
];
try {
$upload_service = new UploadService($data['upload_type']);
$upload_service->validateException($data['file']);
$result = $upload_service->save($data['file']);
} catch (\Exception $e) {
if(App::isDebug()){
throw $e;
}
$this->error($e->getMessage());
}
$this->success('上传成功', $result);
}
/**
* 上传图片至编辑器.
* @return \think\response\Json
*/
public function uploadEditor()
{
$this->checkPostRequest();
$data = [
'upload_type' => $this->request->post('upload_type'),
'file' => $this->request->file('upload'),
];
try {
$upload_service = new UploadService($data['upload_type']);
$upload_service->validateException($data['file']);
$result = $upload_service->save($data['file']);
} catch (\Exception $e) {
if(App::isDebug()){
throw $e;
}
$this->error($e->getMessage());
}
return json([
'errno' => 0,
'data' => [
'url' => $result['url'],
],
]);
}
/**
* 获取上传文件列表.
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUploadFiles()
{
$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);
}
}