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

184 lines
5.4 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
2020-01-29 16:08:50 +08:00
namespace App\Http\Controllers\User;
2019-10-29 15:33:36 +08:00
2020-01-29 16:08:50 +08:00
use App\Http\Controllers\Controller;
2020-01-29 16:22:39 +08:00
use App\Http\Requests\User\UserUpdate;
2020-03-31 10:32:08 +08:00
use App\Http\Requests\User\UserChangePassword;
2019-10-29 15:33:36 +08:00
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Plan;
use App\Models\Server;
2019-12-15 22:24:49 +08:00
use App\Models\Ticket;
2019-10-29 15:33:36 +08:00
use App\Utils\Helper;
use App\Models\Order;
2019-12-22 23:54:33 +08:00
use App\Models\ServerLog;
2019-10-29 15:33:36 +08:00
class UserController extends Controller
{
2020-01-11 13:36:52 +08:00
public function logout(Request $request)
{
2020-01-31 00:03:18 +08:00
$request->session()->flush();
2019-10-29 15:33:36 +08:00
return response([
2020-01-31 00:03:18 +08:00
'data' => true
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
{
2019-10-29 15:33:36 +08:00
$user = User::find($request->session()->get('id'));
2020-02-02 20:44:52 +08:00
if (!Helper::multiPasswordVerify(
$user->password_algo,
$request->input('old_password'),
$user->password)
) {
2019-10-29 15:33:36 +08:00
abort(500, '旧密码有误');
}
$user->password = password_hash($request->input('new_password'), PASSWORD_DEFAULT);
2020-01-31 21:54:17 +08:00
$user->password_algo = NULL;
2019-10-29 15:33:36 +08:00
if (!$user->save()) {
abort(500, '保存失败');
}
$request->session()->flush();
return response([
'data' => true
]);
}
2020-01-11 13:36:52 +08:00
public function info(Request $request)
{
2019-10-29 15:33:36 +08:00
$user = User::where('id', $request->session()->get('id'))
->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',
'telegram_id'
2019-10-29 15:33:36 +08:00
])
->first();
$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)
->where('user_id', $request->session()->get('id'))
->count(),
Ticket::where('status', 0)
->where('user_id', $request->session()->get('id'))
->count(),
User::where('invite_user_id', $request->session()->get('id'))
->count()
];
return response([
'data' => $stat
]);
}
2020-01-11 13:36:52 +08:00
public function getSubscribe(Request $request)
{
2019-12-23 15:26:08 +08:00
$user = User::find($request->session()->get('id'));
if ($user->plan_id) {
$user['plan'] = Plan::find($user->plan_id);
if (!$user['plan']) {
abort(500, '订阅计划不存在');
}
}
2019-12-24 16:27:11 +08:00
$user['subscribe_url'] = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL'))) . '/api/v1/client/subscribe?token=' . $user['token'];
2020-08-10 21:17:01 +08:00
$user['reset_day'] = $this->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)
{
2019-10-29 15:33:36 +08:00
$user = User::find($request->session()->get('id'));
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()) {
abort(500, '重置失败');
}
return response([
2020-01-31 00:16:00 +08:00
'data' => config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL'))) . '/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
2019-10-29 15:33:36 +08:00
$user = User::find($request->session()->get('id'));
if (!$user) {
abort(500, '该用户不存在');
}
2020-03-17 14:28:47 +08:00
try {
$user->update($updateData);
} catch (\Exception $e) {
2019-10-29 15:33:36 +08:00
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
2020-05-10 18:38:02 +08:00
public function transfer(Request $request)
{
$user = User::find($request->session()->get('id'));
if (!$user) {
abort(500, '该用户不存在');
}
if ($request->input('transfer_amount') <= 0) {
abort(500, '参数错误');
}
if ($request->input('transfer_amount') > $user->commission_balance) {
abort(500, '推广佣金余额不足');
}
$user->commission_balance = $user->commission_balance - $request->input('transfer_amount');
$user->balance = $user->balance + $request->input('transfer_amount');
if (!$user->save()) {
abort(500, '划转失败');
}
return response([
'data' => true
]);
}
2020-08-10 21:17:01 +08:00
private function getResetDay(User $user)
{
if ($user->expired_at <= time() || $user->expired_at === NULL) return null;
$day = date('d', $user->expired_at);
2020-12-04 21:17:25 +08:00
$today = date('d');
2020-08-10 21:17:01 +08:00
$lastDay = date('d', strtotime('last day of +0 months'));
if ((int)config('v2board.reset_traffic_method') === 0) {
return $lastDay - $today;
}
if ((int)config('v2board.reset_traffic_method') === 1) {
if ((int)$day >= (int)$today) {
return $day - $today;
} else {
return $lastDay - $today + $day;
}
}
return null;
}
2019-10-29 15:33:36 +08:00
}