v2board/app/Http/Controllers/V1/User/UserController.php

240 lines
6.9 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
2023-06-12 02:32:49 +08:00
namespace App\Http\Controllers\V1\User;
2019-10-29 15:33:36 +08:00
2020-01-29 16:08:50 +08:00
use App\Http\Controllers\Controller;
2023-06-12 02:32:49 +08:00
use App\Http\Requests\User\UserChangePassword;
2021-02-17 16:11:17 +08:00
use App\Http\Requests\User\UserTransfer;
2020-01-29 16:22:39 +08:00
use App\Http\Requests\User\UserUpdate;
2023-06-12 02:32:49 +08:00
use App\Models\Order;
use App\Models\Plan;
use App\Models\Ticket;
use App\Models\User;
2022-12-18 15:03:23 +08:00
use App\Services\AuthService;
2022-05-30 15:42:25 +08:00
use App\Services\UserService;
2021-08-05 13:48:24 +08:00
use App\Utils\CacheKey;
2019-10-29 15:33:36 +08:00
use App\Utils\Helper;
2023-06-12 02:32:49 +08:00
use Illuminate\Http\Request;
2021-08-05 13:48:24 +08:00
use Illuminate\Support\Facades\Cache;
2019-10-29 15:33:36 +08:00
class UserController extends Controller
{
2022-12-18 15:03:23 +08:00
public function getActiveSession(Request $request)
{
$user = User::find($request->user['id']);
if (!$user) {
abort(500, __('The user does not exist'));
}
$authService = new AuthService($user);
return response([
'data' => $authService->getSessions()
]);
}
2022-12-18 16:06:51 +08:00
public function removeActiveSession(Request $request)
{
$user = User::find($request->user['id']);
if (!$user) {
abort(500, __('The user does not exist'));
}
$authService = new AuthService($user);
return response([
2023-02-03 23:39:31 +08:00
'data' => $authService->removeSession($request->input('session_id'))
2022-12-18 16:06:51 +08:00
]);
}
2022-07-11 14:48:35 +08:00
public function checkLogin(Request $request)
2020-01-11 13:36:52 +08:00
{
2022-07-11 14:48:35 +08:00
$data = [
2022-07-19 03:11:36 +08:00
'is_login' => $request->user['id'] ? true : false
2022-07-11 14:48:35 +08:00
];
2022-07-19 03:11:36 +08:00
if ($request->user['is_admin']) {
2022-07-11 14:48:35 +08:00
$data['is_admin'] = true;
}
2019-10-29 15:33:36 +08:00
return response([
2022-07-11 14:48:35 +08:00
'data' => $data
2019-10-29 15:33:36 +08:00
]);
}
2020-03-31 10:32:08 +08:00
public function changePassword(UserChangePassword $request)
2020-01-11 13:36:52 +08:00
{
2022-07-19 03:11:36 +08:00
$user = User::find($request->user['id']);
if (!$user) {
2021-06-12 00:56:39 +08:00
abort(500, __('The user does not exist'));
}
2020-02-02 20:44:52 +08:00
if (!Helper::multiPasswordVerify(
$user->password_algo,
2021-09-14 12:10:29 +08:00
$user->password_salt,
2020-02-02 20:44:52 +08:00
$request->input('old_password'),
$user->password)
) {
2021-06-12 00:56:39 +08:00
abort(500, __('The old password is wrong'));
2019-10-29 15:33:36 +08:00
}
$user->password = password_hash($request->input('new_password'), PASSWORD_DEFAULT);
2020-01-31 21:54:17 +08:00
$user->password_algo = NULL;
2021-09-14 12:12:44 +08:00
$user->password_salt = NULL;
2019-10-29 15:33:36 +08:00
if (!$user->save()) {
2021-06-12 00:56:39 +08:00
abort(500, __('Save failed'));
2019-10-29 15:33:36 +08:00
}
return response([
'data' => true
]);
}
2020-01-11 13:36:52 +08:00
public function info(Request $request)
{
2022-07-19 03:11:36 +08:00
$user = User::where('id', $request->user['id'])
2019-10-29 15:33:36 +08:00
->select([
'email',
2020-01-05 23:47:23 +08:00
'transfer_enable',
2019-10-29 15:33:36 +08:00
'last_login_at',
'created_at',
2020-03-02 20:32:15 +08:00
'banned',
2019-10-29 15:33:36 +08:00
'remind_expire',
2019-11-25 00:08:14 +08:00
'remind_traffic',
2019-11-29 01:16:15 +08:00
'expired_at',
'balance',
2020-01-03 21:29:25 +08:00
'commission_balance',
2020-02-14 22:11:13 +08:00
'plan_id',
'discount',
2020-05-19 16:32:35 +08:00
'commission_rate',
2021-10-11 21:39:55 +08:00
'telegram_id',
'uuid'
2019-10-29 15:33:36 +08:00
])
->first();
if (!$user) {
2021-06-12 00:56:39 +08:00
abort(500, __('The user does not exist'));
}
2019-10-29 15:33:36 +08:00
$user['avatar_url'] = 'https://cdn.v2ex.com/gravatar/' . md5($user->email) . '?s=64&d=identicon';
return response([
'data' => $user
]);
}
2019-12-23 15:26:08 +08:00
2020-01-11 13:36:52 +08:00
public function getStat(Request $request)
{
2019-12-23 15:33:34 +08:00
$stat = [
Order::where('status', 0)
2022-07-19 03:11:36 +08:00
->where('user_id', $request->user['id'])
2019-12-23 15:33:34 +08:00
->count(),
Ticket::where('status', 0)
2022-07-19 03:11:36 +08:00
->where('user_id', $request->user['id'])
2019-12-23 15:33:34 +08:00
->count(),
2022-07-19 03:11:36 +08:00
User::where('invite_user_id', $request->user['id'])
2019-12-23 15:33:34 +08:00
->count()
];
return response([
'data' => $stat
]);
}
2020-01-11 13:36:52 +08:00
public function getSubscribe(Request $request)
{
2022-07-19 03:11:36 +08:00
$user = User::where('id', $request->user['id'])
->select([
'plan_id',
'token',
'expired_at',
'u',
'd',
2021-01-08 13:21:13 +08:00
'transfer_enable',
2022-11-25 03:53:18 +08:00
'email',
'uuid'
])
->first();
if (!$user) {
2021-06-12 00:56:39 +08:00
abort(500, __('The user does not exist'));
}
2019-12-23 15:26:08 +08:00
if ($user->plan_id) {
$user['plan'] = Plan::find($user->plan_id);
if (!$user['plan']) {
2021-06-12 00:56:39 +08:00
abort(500, __('Subscription plan does not exist'));
2019-12-23 15:26:08 +08:00
}
}
2022-05-09 23:26:59 +08:00
$user['subscribe_url'] = Helper::getSubscribeUrl("/api/v1/client/subscribe?token={$user['token']}");
2022-05-30 15:42:25 +08:00
$userService = new UserService();
$user['reset_day'] = $userService->getResetDay($user);
2019-12-23 15:26:08 +08:00
return response([
'data' => $user
]);
}
2020-01-11 13:36:52 +08:00
public function resetSecurity(Request $request)
{
2022-07-19 03:11:36 +08:00
$user = User::find($request->user['id']);
if (!$user) {
2021-06-12 00:56:39 +08:00
abort(500, __('The user does not exist'));
}
2020-06-08 01:08:07 +08:00
$user->uuid = Helper::guid(true);
2019-12-01 21:34:56 +08:00
$user->token = Helper::guid();
2019-10-29 15:33:36 +08:00
if (!$user->save()) {
2021-06-12 00:56:39 +08:00
abort(500, __('Reset failed'));
2019-10-29 15:33:36 +08:00
}
return response([
2022-05-09 23:26:59 +08:00
'data' => Helper::getSubscribeUrl('/api/v1/client/subscribe?token=' . $user->token)
2019-10-29 15:33:36 +08:00
]);
}
2020-01-11 13:36:52 +08:00
public function update(UserUpdate $request)
{
2019-10-29 15:33:36 +08:00
$updateData = $request->only([
'remind_expire',
'remind_traffic'
]);
2020-01-11 13:36:52 +08:00
2022-07-19 03:11:36 +08:00
$user = User::find($request->user['id']);
2019-10-29 15:33:36 +08:00
if (!$user) {
2021-06-12 00:56:39 +08:00
abort(500, __('The user does not exist'));
2019-10-29 15:33:36 +08:00
}
2020-03-17 14:28:47 +08:00
try {
$user->update($updateData);
} catch (\Exception $e) {
2021-06-12 00:56:39 +08:00
abort(500, __('Save failed'));
2019-10-29 15:33:36 +08:00
}
return response([
'data' => true
]);
}
2020-05-10 18:38:02 +08:00
2021-02-17 16:11:17 +08:00
public function transfer(UserTransfer $request)
2020-05-10 18:38:02 +08:00
{
2022-07-19 03:11:36 +08:00
$user = User::find($request->user['id']);
2020-05-10 18:38:02 +08:00
if (!$user) {
2021-06-12 00:56:39 +08:00
abort(500, __('The user does not exist'));
2020-05-10 18:38:02 +08:00
}
if ($request->input('transfer_amount') > $user->commission_balance) {
2021-06-12 00:56:39 +08:00
abort(500, __('Insufficient commission balance'));
2020-05-10 18:38:02 +08:00
}
$user->commission_balance = $user->commission_balance - $request->input('transfer_amount');
$user->balance = $user->balance + $request->input('transfer_amount');
if (!$user->save()) {
2021-06-12 00:56:39 +08:00
abort(500, __('Transfer failed'));
2020-05-10 18:38:02 +08:00
}
return response([
'data' => true
]);
}
2020-08-10 21:17:01 +08:00
2021-08-05 13:48:24 +08:00
public function getQuickLoginUrl(Request $request)
{
2022-07-19 03:11:36 +08:00
$user = User::find($request->user['id']);
2021-08-05 13:48:24 +08:00
if (!$user) {
abort(500, __('The user does not exist'));
}
$code = Helper::guid();
$key = CacheKey::get('TEMP_TOKEN', $code);
Cache::put($key, $user->id, 60);
$redirect = '/#/login?verify=' . $code . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
if (config('v2board.app_url')) {
$url = config('v2board.app_url') . $redirect;
} else {
$url = url($redirect);
}
return response([
'data' => $url
]);
}
2019-10-29 15:33:36 +08:00
}