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

106 lines
3.2 KiB
PHP
Raw Normal View History

2022-08-14 09:40:25 +00:00
<?php
namespace App\Http\Controllers\Remote;
2022-09-16 11:38:15 +00:00
use App\Models\Transaction;
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();
$transactions = new Transaction();
// begin of this month
$beginOfMonth = now()->startOfMonth();
// end of this month
$endOfMonth = now()->endOfMonth();
$this_month_balance_and_drops = Cache::remember('this_month_balance_and_drops_' . $module->id, 3600, function () use ($transactions, $module, $beginOfMonth, $endOfMonth) {
$this_month = $transactions->where('module_id', $module->id)->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
// this month transactions
return [
'balance' => $this_month->sum('outcome'),
'drops' => $this_month->sum('outcome_drops')
];
});
$last_month_balance_and_drops = Cache::remember('last_month_balance_and_drops_' . $module->id, 3600, function () use ($transactions, $module, $beginOfMonth, $endOfMonth) {
// last month transactions
$last_moth = $transactions->where('module_id', $module->id)->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
return [
'balance' => $last_moth->sum('outcome'),
'drops' => $last_moth->sum('outcome_drops')
];
});
$rate = (int)config('drops.rate') - 10;
$data = [
'module' => $module,
'transactions' => [
'this_month' => $this_month_balance_and_drops,
'last_month' => $last_month_balance_and_drops,
],
'balance' => [
'rate' => $rate,
]
];
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') {
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
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-08-14 09:40:25 +00:00
}