改进 Drops

This commit is contained in:
iVampireSP.com 2022-09-09 21:42:12 +08:00
parent ae972decd6
commit c4ef847681
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
7 changed files with 70 additions and 85 deletions

View File

@ -40,6 +40,10 @@ function reduceDrops($user_id, $amount = 0)
$multiple *= 10;
}
$month = now()->month;
Cache::increment('user_' . $user_id . '_month_' . $month . '_drops', $amount);
$amount = $amount * $multiple;
$drops = Cache::decrement($cache_key, $amount);
@ -66,4 +70,3 @@ function addDrops($user_id, $amount = 0)
return $drops;
}

View File

@ -6,10 +6,11 @@
use App\Models\User\Balance;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Alipay\EasySDK\Kernel\Util\ResponseChecker;
use Alipay\EasySDK\Kernel\Factory as AlipayFactory;
use Illuminate\Support\Facades\Log;
class BalanceController extends Controller
{
@ -170,4 +171,20 @@ public function checkAndCharge($out_trade_no, Balance $balance)
// }
public function drops()
{
$month = now()->month;
$user_id = auth()->id();
$cache_key = 'user_' . $user_id . '_month_' . $month . '_drops';
$resp = [
'drops' => (float) Cache::get($cache_key),
'month_usage' => getDrops($user_id),
];
return $this->success($resp);
}
}

View File

@ -1,77 +0,0 @@
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Models\User\Drop;
use Illuminate\Http\Request;
class DropController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Drop $drop)
{
$drop = $drop->simplePaginate(10);
return $this->success($drop);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$this->validate($request, [
'payment' => 'required',
'amount' => 'integer|required|min:1|max:1000',
]);
$drop = Drop::create($request->all());
return $this->success($drop);
}
/**
* Display the specified resource.
*
* @param \App\Models\User\Drop $drop
* @return \Illuminate\Http\Response
*/
public function show(Drop $drop)
{
//
$this->authorize('show', $drop);
return $drop;
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\User\Drop $drop
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Drop $drop)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\User\Drop $drop
* @return \Illuminate\Http\Response
*/
public function destroy(Drop $drop)
{
//
}
}

View File

@ -7,6 +7,7 @@
use App\Models\Module\Module;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
class HostController extends Controller
{
@ -63,6 +64,16 @@ public function destroy(Host $host)
return $this->deleted($host);
}
public function usages()
{
$month = now()->month;
$month_cache_key = 'user_' . auth()->id() . '_month_' . $month . 'hosts_drops';
$hosts_drops = Cache::get($month_cache_key, []);
return $this->success($hosts_drops);
}
// /**
// * Store a newly created resource in storage.
// *

View File

@ -109,6 +109,20 @@ public function cost($price = null)
]);
}
$month = now()->month;
$month_cache_key = 'user_' . $this->user_id . '_month_' . $month . 'hosts_drops';
$hosts_drops = Cache::get($month_cache_key, []);
// 统计 Host 消耗的 Drops
if (isset($hosts_drops[$this->id])) {
$hosts_drops[$this->id] += $this->price;
} else {
$hosts_drops[$this->id] = $this->price;
}
Cache::put($month_cache_key, $hosts_drops, 604800);
reduceDrops($this->user_id, $this->price);
return true;

View File

@ -43,9 +43,13 @@ public function boot()
// bearerToken
$bearerToken = $request->bearerToken();
return Cache::remember('api_token_' . $bearerToken, 60, function () use ($bearerToken) {
return AccessToken::where('token', $bearerToken)->with('user')->first()->user ?? null;
});
if ($bearerToken) {
return Cache::remember('api_token_' . $bearerToken, 60, function () use ($bearerToken) {
return AccessToken::where('token', $bearerToken)->with('user')->first()->user ?? null;
});
} else {
return null;
}
// if ($request->input('api_token')) {
// return User::where('api_token', $request->input('api_token'))->first();
@ -60,9 +64,15 @@ public function boot()
// bearerToken
$bearerToken = $request->bearerToken();
return Cache::remember('api_token_' . $bearerToken, 60, function () use ($bearerToken) {
return Module::where('token', $bearerToken)->first() ?? null;
});
if ($bearerToken) {
return Cache::remember('api_token_' . $bearerToken, 60, function () use ($bearerToken) {
return Module::where('token', $bearerToken)->first() ?? null;
});
} else {
return null;
}
// if ($request->input('api_token')) {
// return User::where('api_token', $request->input('api_token'))->first();

View File

@ -14,6 +14,9 @@
$router->get('/', [
'uses' => 'User\HostController@index'
]);
$router->get('/usages', [
'uses' => 'User\HostController@usages'
]);
$router->patch('/{host}', [
'uses' => 'User\HostController@update'
]);
@ -30,6 +33,10 @@
$router->post('/', [
'uses' => 'User\BalanceController@store'
]);
$router->get('/drops', [
'uses' => 'User\BalanceController@drops'
]);
});
$router->get('/tasks', [