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

133 lines
3.9 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();
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)
{
// begin of this month
$beginOfMonth = now()->startOfMonth();
// end of this month
$endOfMonth = now()->endOfMonth();
2022-10-03 02:38:29 +00:00
$this_month_balance_and_drops = Cache::remember($module->id . '_this_month_balance_and_drops', 3600, function () use ($module, $beginOfMonth, $endOfMonth) {
2022-10-09 07:01:21 +00:00
$this_month = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
2022-09-16 11:56:58 +00:00
// this month transactions
return [
'balance' => $this_month->sum('outcome'),
'drops' => $this_month->sum('outcome_drops')
];
});
2022-10-03 02:38:29 +00:00
$last_month_balance_and_drops = Cache::remember($module->id . '_last_month_balance_and_drops', 3600, function () use ($module, $beginOfMonth, $endOfMonth) {
2022-09-16 11:56:58 +00:00
// last month transactions
2022-10-09 07:01:21 +00:00
$last_moth = Transaction::where('module_id', $module->id)->where('type', 'payout')->whereBetween('created_at', [$beginOfMonth, $endOfMonth]);
2022-09-16 11:56:58 +00:00
return [
'balance' => $last_moth->sum('outcome'),
'drops' => $last_moth->sum('outcome_drops')
];
});
$data = [
'transactions' => [
'this_month' => $this_month_balance_and_drops,
'last_month' => $last_month_balance_and_drops,
]
];
return $data;
}
2022-08-14 09:40:25 +00:00
}