This commit is contained in:
mkm 2023-11-12 16:37:36 +08:00
parent a1f00675f3
commit d2bb96446f
3 changed files with 105 additions and 25 deletions

View File

@ -60,4 +60,18 @@ class UploadController extends BaseAdminController
}
}
/**
* @notes 上传文件
* @return Json
*/
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

@ -39,7 +39,7 @@ class UploadService
try {
$config = [
'default' => ConfigService::get('storage', 'default', 'local'),
'engine' => ConfigService::get('storage') ?? ['local'=>[]],
'engine' => ConfigService::get('storage') ?? ['local' => []],
];
// 2、执行文件上传
@ -50,7 +50,7 @@ class UploadService
// 校验上传文件后缀
if (!in_array(strtolower($fileInfo['ext']), config('project.file_image'))) {
throw new Exception("上传图片不允许上传". $fileInfo['ext'] . "文件");
throw new Exception("上传图片不允许上传" . $fileInfo['ext'] . "文件");
}
// 上传文件
@ -62,7 +62,7 @@ class UploadService
// 3、处理文件名称
if (strlen($fileInfo['name']) > 128) {
$name = substr($fileInfo['name'], 0, 123);
$nameEnd = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
$nameEnd = substr($fileInfo['name'], strlen($fileInfo['name']) - 5, strlen($fileInfo['name']));
$fileInfo['name'] = $name . $nameEnd;
}
@ -71,7 +71,7 @@ class UploadService
'cid' => $cid,
'type' => FileEnum::IMAGE_TYPE,
'name' => $fileInfo['name'],
'uri' => $saveDir . '/' . str_replace("\\","/", $fileName),
'uri' => $saveDir . '/' . str_replace("\\", "/", $fileName),
'source' => $source,
'source_id' => $sourceId,
'create_time' => time(),
@ -86,7 +86,6 @@ class UploadService
'uri' => FileService::getFileUrl($file['uri']),
'url' => $file['uri']
];
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
@ -108,7 +107,7 @@ class UploadService
try {
$config = [
'default' => ConfigService::get('storage', 'default', 'local'),
'engine' => ConfigService::get('storage') ?? ['local'=>[]],
'engine' => ConfigService::get('storage') ?? ['local' => []],
];
// 2、执行文件上传
@ -119,7 +118,7 @@ class UploadService
// 校验上传文件后缀
if (!in_array(strtolower($fileInfo['ext']), config('project.file_video'))) {
throw new Exception("上传视频不允许上传". $fileInfo['ext'] . "文件");
throw new Exception("上传视频不允许上传" . $fileInfo['ext'] . "文件");
}
// 上传文件
@ -131,7 +130,7 @@ class UploadService
// 3、处理文件名称
if (strlen($fileInfo['name']) > 128) {
$name = substr($fileInfo['name'], 0, 123);
$nameEnd = substr($fileInfo['name'], strlen($fileInfo['name'])-5, strlen($fileInfo['name']));
$nameEnd = substr($fileInfo['name'], strlen($fileInfo['name']) - 5, strlen($fileInfo['name']));
$fileInfo['name'] = $name . $nameEnd;
}
@ -140,7 +139,7 @@ class UploadService
'cid' => $cid,
'type' => FileEnum::VIDEO_TYPE,
'name' => $fileInfo['name'],
'uri' => $saveDir . '/' . str_replace("\\","/", $fileName),
'uri' => $saveDir . '/' . str_replace("\\", "/", $fileName),
'source' => $source,
'source_id' => $sourceId,
'create_time' => time(),
@ -155,10 +154,74 @@ class UploadService
'uri' => FileService::getFileUrl($file['uri']),
'url' => $file['uri']
];
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* @notes 上传文件
* @param $cid
* @param int $user_id
* @param string $saveDir
* @return array
* @throws Exception
* @author 段誉
* @date 2021/12/29 16:30
*/
public static function file($cid, int $sourceId = 0, int $source = FileEnum::SOURCE_ADMIN, string $saveDir = 'uploads/files')
{
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::FILE_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

@ -32,19 +32,19 @@ return [
// 后台管理员token登录令牌配置
'admin_token' => [
'expire_duration' => 3600 * 8,//管理后台token过期时长(单位秒)
'be_expire_duration' => 3600,//管理后台token临时过期前时长自动续期
'expire_duration' => 3600 * 8, //管理后台token过期时长(单位秒)
'be_expire_duration' => 3600, //管理后台token临时过期前时长自动续期
],
// 商城用户token登录令牌配置
'user_token' => [
'expire_duration' => 3600 * 8,//用户token过期时长(单位秒)
'be_expire_duration' => 3600,//用户token临时过期前时长自动续期
'expire_duration' => 3600 * 8, //用户token过期时长(单位秒)
'be_expire_duration' => 3600, //用户token临时过期前时长自动续期
],
// 列表页
'lists' => [
'page_size_max' => 25000,//列表页查询数量限制(列表页每页数量、导出每页数量)
'page_size_max' => 25000, //列表页查询数量限制(列表页每页数量、导出每页数量)
'page_size' => 25, //默认每页数量
],
@ -54,14 +54,14 @@ return [
'user_avatar' => 'resource/image/adminapi/default/default_avatar.png',
'qq_group' => 'resource/image/adminapi/default/qq_group.png', // qq群
'customer_service' => 'resource/image/adminapi/default/customer_service.jpg', // 客服
'menu_admin' => 'resource/image/adminapi/default/menu_admin.png',// 首页快捷菜单-管理员
'menu_admin' => 'resource/image/adminapi/default/menu_admin.png', // 首页快捷菜单-管理员
'menu_role' => 'resource/image/adminapi/default/menu_role.png', // 首页快捷菜单-角色
'menu_dept' => 'resource/image/adminapi/default/menu_dept.png',// 首页快捷菜单-部门
'menu_dict' => 'resource/image/adminapi/default/menu_dict.png',// 首页快捷菜单-字典
'menu_generator' => 'resource/image/adminapi/default/menu_generator.png',// 首页快捷菜单-代码生成器
'menu_auth' => 'resource/image/adminapi/default/menu_auth.png',// 首页快捷菜单-菜单权限
'menu_web' => 'resource/image/adminapi/default/menu_web.png',// 首页快捷菜单-网站信息
'menu_file' => 'resource/image/adminapi/default/menu_file.png',// 首页快捷菜单-素材中心
'menu_dept' => 'resource/image/adminapi/default/menu_dept.png', // 首页快捷菜单-部门
'menu_dict' => 'resource/image/adminapi/default/menu_dict.png', // 首页快捷菜单-字典
'menu_generator' => 'resource/image/adminapi/default/menu_generator.png', // 首页快捷菜单-代码生成器
'menu_auth' => 'resource/image/adminapi/default/menu_auth.png', // 首页快捷菜单-菜单权限
'menu_web' => 'resource/image/adminapi/default/menu_web.png', // 首页快捷菜单-网站信息
'menu_file' => 'resource/image/adminapi/default/menu_file.png', // 首页快捷菜单-素材中心
],
// 文件上传限制 (图片)
@ -73,7 +73,10 @@ return [
'file_video' => [
'wmv', 'avi', 'mpg', 'mpeg', '3gp', 'mov', 'mp4', 'flv', 'f4v', 'rmvb', 'mkv'
],
// 文件上传限制 (文件)
'file_file' => [
'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'txt', 'zip','rar','tar'
],
// 登录设置
'login' => [
// 登录方式1-账号密码登录2-手机短信验证码登录