diff --git a/app/Helpers.php b/app/Helpers.php index 0c3cf6b..4c30749 100644 --- a/app/Helpers.php +++ b/app/Helpers.php @@ -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; } - diff --git a/app/Http/Controllers/User/BalanceController.php b/app/Http/Controllers/User/BalanceController.php index d00a9ce..e3504e2 100644 --- a/app/Http/Controllers/User/BalanceController.php +++ b/app/Http/Controllers/User/BalanceController.php @@ -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); + } } diff --git a/app/Http/Controllers/User/DropController.php b/app/Http/Controllers/User/DropController.php deleted file mode 100644 index 1a0d243..0000000 --- a/app/Http/Controllers/User/DropController.php +++ /dev/null @@ -1,77 +0,0 @@ -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) - { - // - } -} diff --git a/app/Http/Controllers/User/HostController.php b/app/Http/Controllers/User/HostController.php index 8538874..b0256d4 100644 --- a/app/Http/Controllers/User/HostController.php +++ b/app/Http/Controllers/User/HostController.php @@ -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. // * diff --git a/app/Models/Host.php b/app/Models/Host.php index 806282f..281e15f 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -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; diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index e376aef..9cfa1e6 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -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(); diff --git a/routes/api.php b/routes/api.php index f66c219..facdb27 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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', [