改进 计费规则,改为模块需要创建计费项目时才要求余额。

This commit is contained in:
iVampireSP.com 2022-10-28 16:52:29 +08:00
parent 5b0a892241
commit a77e37bb31
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
4 changed files with 22 additions and 13 deletions

View File

@ -3,8 +3,8 @@
namespace App\Http\Controllers;
use Closure;
use Exception;
use Illuminate\Http\Request;
// use Exception;
// use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;

View File

@ -3,10 +3,12 @@
namespace App\Http\Controllers\Remote\Host;
use App\Models\Host;
use App\Models\User;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
// use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use App\Exceptions\User\BalanceNotEnoughException;
class HostController extends Controller
{
@ -36,6 +38,13 @@ public function store(Request $request)
'user_id' => 'required|integer|exists:users,id',
]);
//
$user = User::findOrFail($request->user_id);
if ($user->balance <= 1) {
throw new BalanceNotEnoughException("余额不足,无法开设新的计费项目。");
}
// 如果没有 name则随机
$name = $request->input('name', Str::random(10));
@ -43,7 +52,7 @@ public function store(Request $request)
'name' => $name,
'status' => $request->status,
'price' => $request->price,
'user_id' => $request->user_id,
'user_id' => $user->id,
'module_id' => auth('remote')->id()
];

View File

@ -53,13 +53,13 @@ public function call(Request $request, Module $module)
$method = Str::lower($request->method());
// 如果 method 为 post, 检查用户余额
if ($method == 'post') {
$user = auth('api')->user();
// if ($method == 'post') {
// $user = auth('api')->user();
if ($user->balance < 1) {
return $this->error('账户余额不足,请保证账户余额至少有 1 元。');
}
}
// if ($user->balance < 1) {
// return $this->error('账户余额不足,请保证账户余额至少有 1 元。');
// }
// }
$response = $module->remoteRequest($method, $path, $request->all());

View File

@ -23,9 +23,9 @@ public function update(Request $request, Host $host)
$user = $request->user();
if ($host->user_id == $user->id) {
if ($user->balance < 1) {
return $this->error('余额不足');
}
// if ($user->balance < 1) {
// return $this->error('余额不足');
// }
$host->update([
'status' => 'running'