2024-09-12 22:11:31 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\controller;
|
|
|
|
|
|
|
|
|
|
use think\Request;
|
2024-09-13 10:37:11 +08:00
|
|
|
|
use think\Env;
|
2024-09-12 22:11:31 +08:00
|
|
|
|
|
|
|
|
|
class Index
|
|
|
|
|
{
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
|
|
|
|
return json([
|
|
|
|
|
'code' => 200,
|
|
|
|
|
'msg' => 'success',
|
|
|
|
|
'data' => [
|
2024-09-13 11:05:08 +08:00
|
|
|
|
'name' => 'thinkphp'
|
2024-09-12 22:11:31 +08:00
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function upload()
|
|
|
|
|
{
|
|
|
|
|
$file = request()->file('file');
|
|
|
|
|
$trueName = $file->getOriginalName();
|
|
|
|
|
|
2024-09-13 10:37:11 +08:00
|
|
|
|
$env = new Env();
|
2024-09-13 10:56:48 +08:00
|
|
|
|
// thinkphp里面无法获取SERVER_ADDR变量,会被nacos拦截替换
|
2024-09-14 10:25:39 +08:00
|
|
|
|
// 服务暴露的端口
|
|
|
|
|
$servicePort = $env->get('SERVICE_PORT', 8000);
|
|
|
|
|
// 服务可以访问到的IP
|
|
|
|
|
$serverIP = $env->get('SERVER_ADDR');
|
|
|
|
|
// 允许跨域的域名
|
|
|
|
|
$originUrl = $env->get('ORIGIN_URL', $serverIP);
|
2024-09-12 22:11:31 +08:00
|
|
|
|
|
|
|
|
|
// 组装保存路径
|
2024-09-13 10:45:01 +08:00
|
|
|
|
$savename = \think\facade\Filesystem::disk('upload')->putFile('uploads', $file);
|
2024-09-12 22:11:31 +08:00
|
|
|
|
$savename = str_replace('\\', '/', $savename);
|
|
|
|
|
|
|
|
|
|
return json([
|
|
|
|
|
'code' => 200,
|
|
|
|
|
'msg' => null,
|
|
|
|
|
'data' => [
|
|
|
|
|
"name" => $trueName,
|
2024-09-14 10:25:39 +08:00
|
|
|
|
"url" => '//' . $originUrl . '/' . $savename,
|
2024-09-12 22:11:31 +08:00
|
|
|
|
// "url" => 'http://192.168.1.105:8080/file/' . $savename,
|
2024-09-13 10:48:51 +08:00
|
|
|
|
],
|
2024-09-13 11:05:08 +08:00
|
|
|
|
// 'env' => $env->get(),
|
2024-09-12 22:11:31 +08:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|