Files
swiftadmin/app/admin/controller/Ajax.php

86 lines
2.2 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
2023-04-25 20:11:49 +08:00
2022-08-19 19:48:37 +08:00
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于WebMan开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.net High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\AdminController;
use app\common\library\ResultCode;
use app\common\library\Upload;
2023-04-25 20:11:49 +08:00
use Psr\SimpleCache\InvalidArgumentException;
2022-11-28 19:11:12 +08:00
use support\Response;
2023-04-25 20:11:49 +08:00
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
2022-08-19 19:48:37 +08:00
/**
* Ajax类
* Class Ajax
* @package app\admin\controller
*/
class Ajax extends AdminController
{
2023-04-25 20:11:49 +08:00
/**
* 初始化方法
* @return void
* @throws \Exception
*/
public function __construct()
{
parent::__construct();
}
2022-08-19 19:48:37 +08:00
/**
* 测试接口
2022-11-28 19:11:12 +08:00
* @return Response
2022-08-19 19:48:37 +08:00
*/
2022-11-28 19:11:12 +08:00
public function index(): Response
2022-08-19 19:48:37 +08:00
{
return json(ResultCode::SUCCESS);
}
/**
* 文件上传
2022-11-28 19:11:12 +08:00
* @return Response|void
2022-08-19 19:48:37 +08:00
* @throws \Exception
*/
public function upload()
{
if (request()->isPost()) {
$file = Upload::instance()->upload();
if (!$file) {
return $this->error(Upload::instance()->getError());
}
return json($file);
}
}
/**
* 远程下载图片
2022-11-28 19:11:12 +08:00
* @return Response
2023-04-25 20:11:49 +08:00
* @throws InvalidArgumentException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
2022-08-19 19:48:37 +08:00
*/
2022-11-28 19:11:12 +08:00
public function getImage(): Response
2022-08-19 19:48:37 +08:00
{
if (request()->isPost()) {
$file = Upload::instance()->download(input('url'));
if (!$file) {
return $this->error(Upload::instance()->getError());
}
return json($file);
}
2022-11-28 19:11:12 +08:00
return json(ResultCode::EXCEPTION);
2022-08-19 19:48:37 +08:00
}
}