63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
||
|
||
namespace app\controller;
|
||
|
||
use think\Request;
|
||
use think\Env;
|
||
|
||
class Index
|
||
{
|
||
public function index(Request $request)
|
||
{
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => 'success',
|
||
'data' => [
|
||
'name' => 'thinkphp'
|
||
]
|
||
]);
|
||
}
|
||
|
||
public function upload()
|
||
{
|
||
$file = request()->file('file');
|
||
$trueName = $file->getOriginalName();
|
||
|
||
$env = new Env();
|
||
// thinkphp里面无法获取SERVER_ADDR变量,会被nacos拦截替换
|
||
// 服务暴露的端口
|
||
$servicePort = $env->get('SERVICE_PORT', 8000);
|
||
// 服务可以访问到的IP
|
||
$serverIP = $env->get('SERVER_ADDR');
|
||
// 允许跨域的域名
|
||
$originUrl = $env->get('ORIGIN_URL', $serverIP . ':' . $servicePort);
|
||
|
||
// 获取文件原文件名
|
||
$trueName = $file->getOriginalName();
|
||
// 获取文件后缀名
|
||
$ext = $file->extension();
|
||
|
||
// 生成新文件名
|
||
$trueName = rtrim($trueName, ".$ext");
|
||
$fileName = $trueName . '_' . date('His') . '.' . $ext;
|
||
|
||
// 保存文件
|
||
// $file = $file->move(public_path() . 'uploads', $fileName);
|
||
|
||
// 组装保存路径
|
||
$savename = \think\facade\Filesystem::disk('upload')->putFileAs('uploads' . '/' . date('Ymd'), $file, $fileName);
|
||
$savename = str_replace('\\', '/', $savename);
|
||
|
||
return json([
|
||
'code' => 200,
|
||
'msg' => null,
|
||
'data' => [
|
||
"name" => $trueName . '.' . $ext,
|
||
"url" => '//' . $originUrl . '/' . $savename,
|
||
// "url" => 'http://192.168.1.105:8080/file/' . $savename,
|
||
],
|
||
// 'env' => $env->get(),
|
||
]);
|
||
}
|
||
}
|