2022-08-14 09:40:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Remote;
|
|
|
|
|
2022-08-28 17:04:44 +00:00
|
|
|
use Illuminate\Support\Str;
|
2022-08-14 09:40:25 +00:00
|
|
|
use Illuminate\Http\Request;
|
2022-08-28 17:04:44 +00:00
|
|
|
use App\Models\Module\Module;
|
|
|
|
use App\Http\Controllers\Controller;
|
2022-09-03 17:32:50 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-08-14 09:40:25 +00:00
|
|
|
|
2022-08-14 13:57:56 +00:00
|
|
|
class ModuleController extends Controller
|
2022-08-14 09:40:25 +00:00
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return $this->success(auth('remote')->user());
|
|
|
|
}
|
2022-08-28 17:04:44 +00:00
|
|
|
|
2022-08-28 17:17:57 +00:00
|
|
|
public function call(Request $request, Module $module)
|
2022-08-28 17:04:44 +00:00
|
|
|
{
|
2022-09-08 16:12:02 +00:00
|
|
|
$this->validate($request, [
|
2022-08-28 17:04:44 +00:00
|
|
|
'func' => 'required|string'
|
|
|
|
]);
|
|
|
|
|
2022-08-28 17:17:57 +00:00
|
|
|
$func = $request->func;
|
|
|
|
|
2022-08-28 17:04:44 +00:00
|
|
|
// 不能让 func 的首个字符为 /
|
|
|
|
if (Str::startsWith($func, '/')) {
|
|
|
|
$func = substr($func, 1);
|
|
|
|
}
|
|
|
|
|
2022-08-29 09:31:08 +00:00
|
|
|
// 过滤除了 "/" 以外的特殊字符
|
|
|
|
$func = preg_replace('/[^a-zA-Z0-9\/]/', '', $func);
|
2022-09-03 17:32:50 +00:00
|
|
|
|
2022-08-28 17:04:44 +00:00
|
|
|
|
2022-08-29 09:31:08 +00:00
|
|
|
|
|
|
|
// dd($func);
|
|
|
|
|
|
|
|
$method = Str::lower($request->method());
|
|
|
|
|
2022-09-03 17:32:50 +00:00
|
|
|
// 如果 method 为 post, 检查用户余额
|
|
|
|
if ($method == 'post') {
|
2022-09-08 16:12:02 +00:00
|
|
|
$user = auth('api')->user();
|
2022-09-03 17:32:50 +00:00
|
|
|
|
|
|
|
if ($user->balance < 1) {
|
|
|
|
return $this->error('余额小于 1, 无法使用 POST 请求。');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 09:31:08 +00:00
|
|
|
|
|
|
|
$response = $module->remoteRequest($method, $func, $request->all());
|
|
|
|
|
|
|
|
if ($response['json'] === null && $response['body'] !== null) {
|
|
|
|
return response($response['body'], $response['status']);
|
|
|
|
}
|
|
|
|
|
2022-08-29 16:04:50 +00:00
|
|
|
return $this->remoteResponse($response['json'], $response['status']);
|
2022-08-28 17:04:44 +00:00
|
|
|
}
|
2022-08-14 09:40:25 +00:00
|
|
|
}
|