Lae/app/Http/Controllers/Remote/ModuleController.php

113 lines
2.9 KiB
PHP
Raw Normal View History

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()
{
2022-09-16 11:38:15 +00:00
$module = auth('remote')->user();
2022-09-16 11:56:58 +00:00
$calc = $this->calcModule($module);
2022-09-16 11:38:15 +00:00
$data = [
2022-09-16 12:03:54 +00:00
'module' => $module,
'rate' => (int) config('drops.module_rate'),
2022-09-16 11:38:15 +00:00
];
2022-09-16 11:56:58 +00:00
// merge
$data = array_merge($data, $calc);
2022-09-16 11:38:15 +00:00
return $this->success($data);
2022-08-14 09:40:25 +00:00
}
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 17:12:18 +00:00
// $this->validate($request, [
// 'func' => 'required|string'
// ]);
2022-08-28 17:04:44 +00:00
2022-09-08 17:12:18 +00:00
// $func = $request->func;
2022-08-28 17:17:57 +00:00
2022-09-08 17:12:18 +00:00
// // 不能让 func 的首个字符为 /
// if (Str::startsWith($func, '/')) {
// $func = substr($func, 1);
// }
2022-08-28 17:04:44 +00:00
2022-09-08 17:12:18 +00:00
$path = request()->path();
2022-09-03 17:32:50 +00:00
2022-09-08 17:12:18 +00:00
// 删除 modules/{module} 的部分
$path = substr($path, strlen('/api/modules/' . $module->id));
2022-08-28 17:04:44 +00:00
2022-09-08 17:12:18 +00:00
// 过滤除了 "/" 以外的特殊字符
$path = preg_replace('/[^a-zA-Z0-9\/]/', '', $path);
2022-08-29 09:31:08 +00:00
$method = Str::lower($request->method());
2022-09-03 17:32:50 +00:00
// 如果 method 为 post, 检查用户余额
// if ($method == 'post') {
// $user = auth('api')->user();
2022-09-03 17:32:50 +00:00
// if ($user->balance < 1) {
// return $this->error('账户余额不足,请保证账户余额至少有 1 元。');
// }
// }
2022-09-03 17:32:50 +00:00
2022-08-29 09:31:08 +00:00
2022-09-08 17:12:18 +00:00
$response = $module->remoteRequest($method, $path, $request->all());
2022-08-29 09:31:08 +00:00
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-09-16 11:56:58 +00:00
public function exportCall(Request $request, Module $module)
{
$path = request()->path();
$path = substr($path, strlen('/remote/modules/' . $module->id));
$path = preg_replace('/[^a-zA-Z0-9\/]/', '', $path);
$method = Str::lower($request->method());
$response = $module->moduleRequest($method, $path, $request->all());
if ($response['json'] === null && $response['body'] !== null) {
return response($response['body'], $response['status']);
}
return $this->remoteResponse($response['json'], $response['status']);
}
2022-09-16 11:56:58 +00:00
public function calcModule(Module $module)
{
2022-10-29 03:44:52 +00:00
$default = [
'balance' => 0,
'drops' => 0,
];
2022-09-16 11:56:58 +00:00
$data = [
'transactions' => [
2022-10-29 03:44:52 +00:00
'this_month' => Cache::get('this_month_balance_and_drops_' . $module->id, $default),
'last_month' => Cache::get('last_month_balance_and_drops_' . $module->id, $default),
2022-09-16 11:56:58 +00:00
]
];
return $data;
}
2022-08-14 09:40:25 +00:00
}