Compare commits

..

3 Commits

Author SHA1 Message Date
weiz 1f0cdf586f Merge pull request 'dev' (#4) from dev into master
Reviewed-on: #4
2023-12-04 16:41:25 +08:00
weiz 1205579a17 新增上传文件接口 2023-12-04 13:43:53 +08:00
weiz 0e1663a166 新增获取最新版本接口 2023-12-04 10:44:39 +08:00
5 changed files with 87 additions and 1 deletions

View File

@ -60,4 +60,15 @@ class UploadController extends BaseAdminController
} }
} }
public function file()
{
try {
$cid = $this->request->post('cid', 0);
$result = UploadService::file($cid);
return $this->success('上传成功', $result);
} catch (Exception $e) {
return $this->fail($e->getMessage());
}
}
} }

View File

@ -0,0 +1,13 @@
<?php
namespace app\api\controller;
use app\common\model\AppUpdate;
class AppUpdateController extends BaseApiController
{
public function index() {
$data = AppUpdate::field('id,title,content,type,version,dow_url,force,quiet')->where('')->order('id desc')->findOrEmpty();
return $this->success('请求成功',$data->toArray());
}
}

View File

@ -161,4 +161,61 @@ class UploadService
} }
} }
public static function file($cid, int $sourceId = 0, int $source = FileEnum::SOURCE_ADMIN, string $saveDir = 'uploads/file')
{
try {
$config = [
'default' => ConfigService::get('storage', 'default', 'local'),
'engine' => ConfigService::get('storage') ?? ['local'=>[]],
];
// 2、执行文件上传
$StorageDriver = new StorageDriver($config);
$StorageDriver->setUploadFile('file');
$fileName = $StorageDriver->getFileName();
$fileInfo = $StorageDriver->getFileInfo();
// 校验上传文件后缀
if (!in_array(strtolower($fileInfo['ext']), config('project.file_file'))) {
throw new Exception("上传文件不允许上传". $fileInfo['ext'] . "文件");
}
// 上传文件
$saveDir = $saveDir . '/' . date('Ymd');
if (!$StorageDriver->upload($saveDir)) {
throw new Exception($StorageDriver->getError());
}
// 3、处理文件名称
if (strlen($fileInfo['name']) > 128) {
$name = substr($fileInfo['name'], 0, 123);
$nameEnd = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
$fileInfo['name'] = $name . $nameEnd;
}
// 4、写入数据库中
$file = File::create([
'cid' => $cid,
'type' => FileEnum::VIDEO_TYPE,
'name' => $fileInfo['name'],
'uri' => $saveDir . '/' . str_replace("\\","/", $fileName),
'source' => $source,
'source_id' => $sourceId,
'create_time' => time(),
]);
// 5、返回结果
return [
'id' => $file['id'],
'cid' => $file['cid'],
'type' => $file['type'],
'name' => $file['name'],
'uri' => FileService::getFileUrl($file['uri']),
'url' => $file['uri']
];
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
} }

View File

@ -42,7 +42,7 @@ abstract class Server
} }
// 校验上传文件后缀 // 校验上传文件后缀
$limit = array_merge(config('project.file_image'), config('project.file_video')); $limit = array_merge(config('project.file_image'), config('project.file_video'),config('project.file_file'));
if (!in_array(strtolower($this->file->extension()), $limit)) { if (!in_array(strtolower($this->file->extension()), $limit)) {
throw new Exception('不允许上传' . $this->file->extension() . '后缀文件'); throw new Exception('不允许上传' . $this->file->extension() . '后缀文件');
} }

View File

@ -74,6 +74,11 @@ return [
'wmv', 'avi', 'mpg', 'mpeg', '3gp', 'mov', 'mp4', 'flv', 'f4v', 'rmvb', 'mkv' 'wmv', 'avi', 'mpg', 'mpeg', '3gp', 'mov', 'mp4', 'flv', 'f4v', 'rmvb', 'mkv'
], ],
// 文件上传限制 (文件)
'file_file' => [
'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'txt','apk','ipa','wgt'
],
// 登录设置 // 登录设置
'login' => [ 'login' => [
// 登录方式1-账号密码登录2-手机短信验证码登录 // 登录方式1-账号密码登录2-手机短信验证码登录