diff --git a/app/Http/Controllers/Admin/MailController.php b/app/Http/Controllers/Admin/MailController.php index f72b0433..3383d6fe 100644 --- a/app/Http/Controllers/Admin/MailController.php +++ b/app/Http/Controllers/Admin/MailController.php @@ -3,24 +3,27 @@ namespace App\Http\Controllers\Admin; use App\Http\Requests\Admin\MailSend; +use App\Services\UserService; use Illuminate\Http\Request; use App\Http\Controllers\Controller; -use App\Models\User; use App\Jobs\SendEmail; class MailController extends Controller { public function send(MailSend $request) { - + $userService = new UserService(); + $users = []; switch ($request->input('type')) { - case 1: $users = $this->getAllUser(); + case 1: $users = $userService->getAllUsers(); break; - case 2: $users = $this->getReceiver($request->input('receiver')); + case 2: $users = $userService->getUsersByIds($request->input('receiver')); break; - case 3: $users = $this->getSubscribeUser(); + // available users + case 3: $users = $userService->getAvailableUsers(); break; - case 4: $users = $this->getExpireUser(); + // un available users + case 4: $users = $userService->getUnAvailbaleUsers(); break; } @@ -41,27 +44,4 @@ class MailController extends Controller 'data' => true ]); } - - private function getAllUser() - { - return User::all(); - } - - private function getReceiver($receiver) - { - if (empty($receiver)) { - abort(500, '收件人不能为空'); - } - return User::whereIn('id', $receiver)->get(); - } - - private function getSubscribeUser() - { - return User::where('expired_at', '=>', time())->get(); - } - - private function getExpireUser() - { - return User::where('expired_at', '<', time())->get(); - } } diff --git a/app/Http/Controllers/Client/ClientController.php b/app/Http/Controllers/Client/ClientController.php index 685876d8..093f6ee1 100755 --- a/app/Http/Controllers/Client/ClientController.php +++ b/app/Http/Controllers/Client/ClientController.php @@ -17,8 +17,8 @@ class ClientController extends Controller $user = $request->user; $server = []; // account not expired and is not banned. - $userService = new UserService($user); - if ($userService->isAvailable()) { + $userService = new UserService(); + if ($userService->isAvailable($user)) { $servers = Server::where('show', 1) ->orderBy('name') ->get(); diff --git a/app/Http/Controllers/User/ServerController.php b/app/Http/Controllers/User/ServerController.php index 2641f1dd..60c1ccb6 100644 --- a/app/Http/Controllers/User/ServerController.php +++ b/app/Http/Controllers/User/ServerController.php @@ -18,8 +18,8 @@ class ServerController extends Controller { $user = User::find($request->session()->get('id')); $server = []; - $userService = new UserService($user); - if ($userService->isAvailable()) { + $userService = new UserService(); + if ($userService->isAvailable($user)) { $servers = Server::where('show', 1) ->orderBy('name') ->get(); diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 2182ccc4..a110d342 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -6,18 +6,57 @@ use App\Models\User; class UserService { - public $user; - - public function __construct(User $user) + public function isAvailable(User $user) { - $this->user = $user; - } - - public function isAvailable() - { - if ($this->user->enable && $this->user->transfer_enable && ($this->user->expired_at > time() || $this->user->expired_at == 0)) { + if ($user->enable && $user->transfer_enable && ($user->expired_at > time() || $user->expired_at == 0)) { return true; } return false; } + + public function getAvailableUsers() + { + return User::whereRaw('u + d < transfer_enable') + ->where(function ($query) { + $query->where('expired_at', '>=', time()) + ->orWhere('expired_at', 0); + }) + ->where('enable', 1) + ->select([ + 'id', + 'email', + 't', + 'u', + 'd', + 'transfer_enable', + 'enable', + 'v2ray_uuid', + 'v2ray_alter_id', + 'v2ray_level' + ]) + ->get(); + } + + public function getUnAvailbaleUsers() + { + return User::where(function ($query) { + $query->where('expired_at', '<', time()) + ->orWhere('expired_at', 0); + }) + ->where(function ($query) { + $query->where('plan_id', NULL) + ->orWhere('transfer_enable', 0); + }) + ->get(); + } + + public function getUsersByIds($ids) + { + return User::whereIn('id', $ids)->get(); + } + + public function getAllUsers() + { + return User::all(); + } }