Files
im/app/api/controller/BaseController.php
T

123 lines
3.7 KiB
PHP
Raw Normal View History

2025-11-07 09:56:20 +08:00
<?php
namespace app\api\controller;
use support\Request;
use support\Response;
use Shopwwi\WebmanFilesystem\FilesystemFactory;
use Shopwwi\WebmanFilesystem\Facade\Storage;
use hg\apidoc\annotation as Apidoc;
2026-03-06 02:27:52 +08:00
use Tinywan\Validate\Facade\Validate;
2025-11-07 09:56:20 +08:00
/**
* 基础控制器
* @Apidoc\NotParse()
* @Apidoc\NotDebug()
*/
class BaseController
{
/**
* 不需要鉴权的方法
* @var array
*/
public $noNeedAuth = [];
/**
* 无需登录及鉴权的方法
* @var array
*/
public $noNeedLogin = [];
function __construct()
{
$this->_init();
}
protected function _init(){
}
/**
* 返回格式化json数据
*
* @param int $code
* @param string $msg
* @param array $data
* @return Response
*/
protected function json(int $code, string $msg = 'ok', array|object|null $data = []): Response
{
2026-03-06 02:27:52 +08:00
return json(['code' => $code, 'data' => $data, 'msg' => __($msg)]);
2025-11-07 09:56:20 +08:00
}
protected function success(string $msg = '成功', array|object|null $data = []): Response
{
return $this->json(0, $msg, $data);
}
protected function fail(string $msg = '失败', array|object|null $data = []): Response
{
return $this->json(1,$msg, $data);
}
protected function error(string $msg = '失败', array|object|null $data = []): Response
{
return $this->json(1,$msg, $data);
}
2026-02-15 19:41:56 +08:00
protected function _upload($request)
2025-11-07 09:56:20 +08:00
{
2026-02-15 19:41:56 +08:00
2025-12-24 16:59:05 +08:00
try{
$user = \support\Jwt::getUser();
}catch(\Exception $e){
$user = ['id'=>0];
}
$savePath = $request->post('savePath','files');
$validate = Validate::rule('savePath', 'alphaNum');
$data = ['savePath' => $savePath];
if (!$validate->check($data)) {
2026-02-15 19:41:56 +08:00
return '参数错误:'.$validate->getError();
2025-12-24 16:59:05 +08:00
}
$savePath = trim($savePath,'/');
2025-12-25 06:02:38 +08:00
//$savePath = 'upload/'.$savePath.'/'.$user['id'];
$savePath = $savePath.'/'.$user['id'];
2025-12-24 16:59:05 +08:00
\support\Log::alert('savePath:'.$savePath);
$mimetype = explode(',',Config('site.upload_mimetype'));
$maxsize = Config('site.upload_maxsize')*1024*1024;
2025-11-07 09:56:20 +08:00
//多文件上传
$files = $request->file();
2026-02-15 19:41:56 +08:00
$result = Storage::adapter('oss')
->path($savePath)
->size($maxsize)
->extYes($mimetype)
->uploads($files,0,$maxsize * count($files),false);
$save_datas = [];
foreach($result as $k=>$fileinfo){
$save_datas[] = [
'user_id' => $user['id'],
'category' => 'default',
'adapter' => $fileinfo->adapter,
'origin_name' => $fileinfo->origin_name,
'file_name' => $fileinfo->file_name,
'size' => $fileinfo->size,
'mime_type' => $fileinfo->mime_type,
'extension' => $fileinfo->extension,
'file_height' => $fileinfo->file_height,
'file_width' => $fileinfo->file_width,
'file_url' => $fileinfo->file_url,
'sha1' => $fileinfo->storage_key ?:sha1_file(public_path($fileinfo->file_name)),
'use_count' => 0,
];
}
$res = \app\model\Files::saveAll($save_datas);
return $res;
}
/**
* @Apidoc\Title("上传")
* @Apidoc\Method("POST")
2026-03-02 15:59:46 +08:00
* @Apidoc\NotParse()
* @Apidoc\NotDebug()
2026-02-15 19:41:56 +08:00
*/
function upload(Request $request,$return = false)
{
$res = $this->_upload($request);
if(is_string($res)){
return $this->fail( $res);
2025-11-07 09:56:20 +08:00
}
2026-02-15 19:41:56 +08:00
return $this->success(__('successful'),$res);
2025-11-07 09:56:20 +08:00
}
}