|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\Manager;
|
|
|
|
|
|
use App\Models\Uploads;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
class PublicController extends CommonController
|
|
|
{
|
|
|
/**
|
|
|
* @OA\POST(
|
|
|
* path="/manager/upload-image",
|
|
|
* tags={"管理端共用接口"},
|
|
|
* summary="V2-上传图片",
|
|
|
* description="上传图片",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Parameter(name="folder", in="query", @OA\Schema(type="string"), required=true, description="文件存放目录:[public/manager],public表示可以公开查看的,一般用于头像等公开图片,manager是需要鉴权后查看的,一般用于身份证、资格证书等非公开图片"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="上传图片",
|
|
|
* content={
|
|
|
* @OA\MediaType(
|
|
|
* mediaType="application/json",
|
|
|
* @OA\Schema(
|
|
|
* @OA\Property(
|
|
|
* property="id",
|
|
|
* type="integer",
|
|
|
* description="文件存储id"
|
|
|
* ),
|
|
|
* @OA\Property(
|
|
|
* property="folder",
|
|
|
* type="string",
|
|
|
* description="文件存储目录"
|
|
|
* ),
|
|
|
* @OA\Property(
|
|
|
* property="name",
|
|
|
* type="string",
|
|
|
* description="文件名"
|
|
|
* ),
|
|
|
* @OA\Property(
|
|
|
* property="public_path",
|
|
|
* type="string",
|
|
|
* description="公开访问路径,如果为null,表示不可公开访问"
|
|
|
* ),
|
|
|
* example={
|
|
|
* "id": "1",
|
|
|
* "folder": "public",
|
|
|
* "name": "eufgw83ascasc.jpg",
|
|
|
* "public_path": "/storage/eufgw83ascasc.jpg"
|
|
|
* }
|
|
|
* )
|
|
|
* )
|
|
|
* }
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
|
|
|
public function uploadImage(Request $request)
|
|
|
{
|
|
|
try {
|
|
|
$file = $request->file("file");
|
|
|
$original_name = $file->getClientOriginalName();
|
|
|
$extension = last(explode(".", $original_name));
|
|
|
if (!in_array($extension, ["jpg", "jpeg", "gif", "png", "bmp"])) {
|
|
|
return response()->json([
|
|
|
"errorcode" => "403",
|
|
|
"errormsg" => "不正确的文件格式"
|
|
|
]);
|
|
|
}
|
|
|
$save_folder = $request->folder ? $request->folder : "manager";
|
|
|
|
|
|
if (!is_dir(Storage::path($save_folder))) {
|
|
|
Storage::makeDirectory($save_folder);
|
|
|
}
|
|
|
|
|
|
$name = uniqid() . "." . $extension;
|
|
|
$store_result = $file->storeAs($save_folder, $name);
|
|
|
|
|
|
$upload = new Uploads();
|
|
|
$upload->original_name = $original_name;
|
|
|
$upload->folder = $save_folder;
|
|
|
$upload->name = $name;
|
|
|
$upload->extension = $extension;
|
|
|
$upload->creator_type = get_class($this->manager);
|
|
|
$upload->creator_id = $this->manager->id;
|
|
|
$upload->size = Storage::size($save_folder . "/" . $name);
|
|
|
$upload->save();
|
|
|
if ($save_folder == "public") {
|
|
|
$upload->public_path = "/storage/" . $name;
|
|
|
} else {
|
|
|
$upload->public_path = null;
|
|
|
}
|
|
|
|
|
|
return response()->json($upload);
|
|
|
} catch (\Exception $exception) {
|
|
|
return response()->json([
|
|
|
"errorcode" => $exception->getCode(),
|
|
|
"errormsg" => $exception->getMessage()
|
|
|
]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/manager/get-attachment/{id}",
|
|
|
* tags={"管理端共用接口"},
|
|
|
* summary="V2-获取附件,进行展示或下载",
|
|
|
* description="获取附件,进行展示或下载",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Parameter(name="id", in="path", @OA\Schema(type="string"), required=true, description="id"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="获取附件,进行展示或下载"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
|
|
|
public function getAttachment($id)
|
|
|
{
|
|
|
$attachment = Uploads::find($id);
|
|
|
if (!$attachment) {
|
|
|
abort("404");
|
|
|
}
|
|
|
if (!Storage::exists($attachment->folder . "/" . $attachment->name)) {
|
|
|
abort("404");
|
|
|
}
|
|
|
|
|
|
return Storage::download($attachment->folder . "/" . $attachment->name, null, [
|
|
|
'Cache-Control' => 'no-cache, no-store, must-revalidate',
|
|
|
'Pragma' => 'no-cache',
|
|
|
'Expires' => '0',
|
|
|
], null);
|
|
|
}
|
|
|
}
|