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

295 lines
11 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Http\Controllers\Admin;
2020-10-08 12:13:47 +08:00
use App\Http\Requests\Admin\UserFetch;
use App\Http\Requests\Admin\UserGenerate;
2020-10-19 00:37:27 +08:00
use App\Http\Requests\Admin\UserSendMail;
2019-10-29 15:33:36 +08:00
use App\Http\Requests\Admin\UserUpdate;
2020-10-19 00:37:27 +08:00
use App\Jobs\SendEmailJob;
2020-10-08 12:13:47 +08:00
use App\Utils\Helper;
2019-10-29 15:33:36 +08:00
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\User;
2019-11-04 02:02:55 +08:00
use App\Models\Plan;
2020-10-08 12:13:47 +08:00
use Illuminate\Support\Facades\DB;
2019-10-29 15:33:36 +08:00
class UserController extends Controller
{
2021-01-02 21:35:41 +08:00
public function resetSecret(Request $request)
{
$user = User::find($request->input('id'));
if (!$user) abort(500, '用户不存在');
$user->token = Helper::guid();
$user->uuid = Helper::guid(true);
return response([
'data' => $user->save()
]);
}
2020-10-08 12:13:47 +08:00
private function filter(Request $request, $builder)
{
if ($request->input('filter')) {
foreach ($request->input('filter') as $filter) {
2020-10-18 19:21:21 +08:00
if ($filter['key'] === 'invite_by_email') {
$user = User::where('email', $filter['value'])->first();
if (!$user) continue;
$builder->where('invite_user_id', $user->id);
continue;
}
2020-10-08 12:13:47 +08:00
if ($filter['key'] === 'd' || $filter['key'] === 'transfer_enable') {
$filter['value'] = $filter['value'] * 1073741824;
}
2020-10-24 23:08:14 +08:00
if ($filter['condition'] === '模糊') {
$filter['condition'] = 'like';
$filter['value'] = "%{$filter['value']}%";
}
2020-10-08 12:13:47 +08:00
$builder->where($filter['key'], $filter['condition'], $filter['value']);
}
}
}
public function fetch(UserFetch $request)
2020-01-11 13:36:52 +08:00
{
2019-10-29 15:33:36 +08:00
$current = $request->input('current') ? $request->input('current') : 1;
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
2020-02-09 18:53:45 +08:00
$sortType = in_array($request->input('sort_type'), ['ASC', 'DESC']) ? $request->input('sort_type') : 'DESC';
$sort = $request->input('sort') ? $request->input('sort') : 'created_at';
$userModel = User::orderBy($sort, $sortType);
2020-10-08 12:13:47 +08:00
$this->filter($request, $userModel);
2019-10-29 15:33:36 +08:00
$total = $userModel->count();
2019-11-04 02:02:55 +08:00
$res = $userModel->forPage($current, $pageSize)
->get();
$plan = Plan::get();
for ($i = 0; $i < count($res); $i++) {
for ($k = 0; $k < count($plan); $k++) {
if ($plan[$k]['id'] == $res[$i]['plan_id']) {
$res[$i]['plan_name'] = $plan[$k]['name'];
}
}
2020-12-13 23:09:32 +08:00
$res[$i]['subscribe_url'] = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL'))) . '/api/v1/client/subscribe?token=' . $res[$i]['token'];
2019-11-04 02:02:55 +08:00
}
2019-10-29 15:33:36 +08:00
return response([
2019-11-04 02:02:55 +08:00
'data' => $res,
2019-10-29 15:33:36 +08:00
'total' => $total
]);
}
2020-01-29 16:49:32 +08:00
public function getUserInfoById(Request $request)
2020-01-11 13:36:52 +08:00
{
2020-01-29 16:49:32 +08:00
if (empty($request->input('id'))) {
2019-12-16 13:59:15 +08:00
abort(500, '参数错误');
}
return response([
2020-05-25 17:20:48 +08:00
'data' => User::find($request->input('id'))
2019-12-16 13:59:15 +08:00
]);
}
2020-01-11 13:36:52 +08:00
public function update(UserUpdate $request)
{
2020-07-15 15:57:12 +08:00
$params = $request->validated();
2019-10-29 15:33:36 +08:00
$user = User::find($request->input('id'));
if (!$user) {
abort(500, '用户不存在');
}
2020-02-28 16:38:44 +08:00
if (User::where('email', $params['email'])->first() && $user->email !== $params['email']) {
2019-10-29 15:33:36 +08:00
abort(500, '邮箱已被使用');
}
2020-02-28 16:38:44 +08:00
if (isset($params['password'])) {
$params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
2020-05-23 23:21:49 +08:00
$params['password_algo'] = NULL;
2019-10-29 15:33:36 +08:00
} else {
2020-02-28 16:38:44 +08:00
unset($params['password']);
2019-10-29 15:33:36 +08:00
}
2020-02-28 16:38:44 +08:00
if (isset($params['plan_id'])) {
$plan = Plan::find($params['plan_id']);
2019-11-27 19:55:53 +08:00
if (!$plan) {
abort(500, '订阅计划不存在');
}
2020-02-28 16:38:44 +08:00
$params['group_id'] = $plan->group_id;
2019-11-27 19:55:53 +08:00
}
2020-03-17 14:28:47 +08:00
try {
$user->update($params);
} catch (\Exception $e) {
2019-10-29 15:33:36 +08:00
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
2020-10-08 12:13:47 +08:00
2020-10-08 22:35:07 +08:00
public function dumpCSV(Request $request)
2020-10-08 12:13:47 +08:00
{
2020-10-08 22:42:26 +08:00
$userModel = User::orderBy('id', 'asc');
2020-10-08 12:13:47 +08:00
$this->filter($request, $userModel);
$res = $userModel->get();
$plan = Plan::get();
for ($i = 0; $i < count($res); $i++) {
for ($k = 0; $k < count($plan); $k++) {
if ($plan[$k]['id'] == $res[$i]['plan_id']) {
$res[$i]['plan_name'] = $plan[$k]['name'];
}
}
}
2020-10-08 22:35:07 +08:00
$data = "邮箱,余额,推广佣金,总流量,剩余流量,套餐到期时间,订阅计划,订阅地址\r\n";
$baseUrl = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL')));
foreach($res as $user) {
2020-10-14 19:42:08 +08:00
$expireDate = $user['expired_at'] === NULL ? '长期有效' : date('Y-m-d H:i:s', $user['expired_at']);
2020-10-08 22:35:07 +08:00
$balance = $user['balance'] / 100;
$commissionBalance = $user['commission_balance'] / 100;
$transferEnable = $user['transfer_enable'] ? $user['transfer_enable'] / 1073741824 : 0;
$notUseFlow = (($user['transfer_enable'] - ($user['u'] + $user['d'])) / 1073741824) ?? 0;
$planName = $user['plan_name'] ?? '无订阅';
$subscribeUrl = $baseUrl . '/api/v1/client/subscribe?token=' . $user['token'];
$data .= "{$user['email']},{$balance},{$commissionBalance},{$transferEnable},{$notUseFlow},{$expireDate},{$planName},{$subscribeUrl}\r\n";
}
echo "\xEF\xBB\xBF" . $data;
2020-10-08 12:13:47 +08:00
}
public function generate(UserGenerate $request)
{
if ($request->input('email_prefix')) {
if ($request->input('plan_id')) {
$plan = Plan::find($request->input('plan_id'));
if (!$plan) {
abort(500, '订阅计划不存在');
}
}
$user = [
'email' => $request->input('email_prefix') . '@' . $request->input('email_suffix'),
'plan_id' => isset($plan->id) ? $plan->id : NULL,
'group_id' => isset($plan->group_id) ? $plan->group_id : NULL,
'transfer_enable' => isset($plan->transfer_enable) ? $plan->transfer_enable * 1073741824 : 0,
'expired_at' => $request->input('expired_at') ?? NULL,
'uuid' => Helper::guid(true),
'token' => Helper::guid()
];
$user['password'] = password_hash($request->input('password') ?? $user['email'], PASSWORD_DEFAULT);
if (!User::create($user)) {
abort(500, '生成失败');
}
return response([
'data' => true
]);
}
2020-10-24 23:08:14 +08:00
if ($request->input('generate_count')) {
2020-10-08 12:13:47 +08:00
$this->multiGenerate($request);
}
}
private function multiGenerate(Request $request)
{
if ($request->input('plan_id')) {
$plan = Plan::find($request->input('plan_id'));
if (!$plan) {
abort(500, '订阅计划不存在');
}
}
$users = [];
2020-10-24 23:08:14 +08:00
for ($i = 0;$i < $request->input('generate_count');$i++) {
2020-10-08 12:13:47 +08:00
$user = [
'email' => Helper::randomChar(6) . '@' . $request->input('email_suffix'),
'plan_id' => isset($plan->id) ? $plan->id : NULL,
'group_id' => isset($plan->group_id) ? $plan->group_id : NULL,
'transfer_enable' => isset($plan->transfer_enable) ? $plan->transfer_enable * 1073741824 : 0,
'expired_at' => $request->input('expired_at') ?? NULL,
'uuid' => Helper::guid(true),
'token' => Helper::guid(),
'created_at' => time(),
'updated_at' => time()
];
$user['password'] = password_hash($request->input('password') ?? $user['email'], PASSWORD_DEFAULT);
array_push($users, $user);
}
DB::beginTransaction();
if (!User::insert($users)) {
DB::rollBack();
abort(500, '生成失败');
}
DB::commit();
2020-10-14 19:42:08 +08:00
$data = "账号,密码,过期时间,UUID,创建时间,订阅地址\r\n";
$baseUrl = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL')));
2020-10-08 12:13:47 +08:00
foreach($users as $user) {
2020-10-14 19:42:08 +08:00
$expireDate = $user['expired_at'] === NULL ? '长期有效' : date('Y-m-d H:i:s', $user['expired_at']);
2020-10-08 12:13:47 +08:00
$createDate = date('Y-m-d H:i:s', $user['created_at']);
$password = $request->input('password') ?? $user['email'];
2020-10-14 19:42:08 +08:00
$subscribeUrl = $baseUrl . '/api/v1/client/subscribe?token=' . $user['token'];
$data .= "{$user['email']},{$password},{$expireDate},{$user['uuid']},{$createDate},{$subscribeUrl}\r\n";
2020-10-08 12:13:47 +08:00
}
echo $data;
}
2020-10-19 00:37:27 +08:00
public function sendMail(UserSendMail $request)
{
$sortType = in_array($request->input('sort_type'), ['ASC', 'DESC']) ? $request->input('sort_type') : 'DESC';
$sort = $request->input('sort') ? $request->input('sort') : 'created_at';
2020-10-20 02:09:32 +08:00
$builder = User::orderBy($sort, $sortType);
$this->filter($request, $builder);
$users = $builder->get();
2020-10-19 00:37:27 +08:00
foreach ($users as $user) {
SendEmailJob::dispatch([
'email' => $user->email,
'subject' => $request->input('subject'),
'template_name' => 'notify',
'template_value' => [
'name' => config('v2board.app_name', 'V2Board'),
'url' => config('v2board.app_url'),
'content' => $request->input('content')
]
]);
}
return response([
'data' => true
]);
}
2020-10-20 02:09:32 +08:00
public function ban(Request $request)
{
$sortType = in_array($request->input('sort_type'), ['ASC', 'DESC']) ? $request->input('sort_type') : 'DESC';
$sort = $request->input('sort') ? $request->input('sort') : 'created_at';
$builder = User::orderBy($sort, $sortType);
$this->filter($request, $builder);
try {
$builder->update([
'banned' => 1
]);
} catch (\Exception $e) {
abort(500, '处理失败');
}
return response([
'data' => true
]);
}
2021-03-26 01:20:53 +08:00
public function setInviteUser(Request $request)
{
$request->validate([
'user_id' => 'required|integer',
'invite_user' => 'required',
], [
'user_id.required' => '用户ID不能为空',
'user_id.integer' => '用户ID参数有误',
'invite_user.required' => '邀请人不能为空'
]);
$user = User::find($request->input('user_id'));
if (!$user) abort(500, '用户不存在');
if (strpos($request->input('invite_user'), '@') !== -1) {
$inviteUser = User::where('email', $request->input('invite_user'))->first();
} else {
$inviteUser = User::find($request->input('invite_user'));
}
if (!$inviteUser) abort(500, '邀请人不存在');
$user->invite_user_id = $inviteUser->id;
return response([
'data' => $user->save()
]);
}
2019-10-29 15:33:36 +08:00
}