2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Requests\Admin\UserUpdate;
|
|
|
|
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;
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2020-01-11 13:36:52 +08:00
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
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);
|
2019-10-29 15:33:36 +08:00
|
|
|
if ($request->input('email')) {
|
|
|
|
$userModel->where('email', $request->input('email'));
|
|
|
|
}
|
2020-02-15 23:16:44 +08:00
|
|
|
if ($request->input('invite_user_id')) {
|
|
|
|
$userModel->where('invite_user_id', $request->input('invite_user_id'));
|
|
|
|
}
|
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'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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([
|
|
|
|
'data' => User::select([
|
|
|
|
'email',
|
|
|
|
'u',
|
|
|
|
'd',
|
|
|
|
'transfer_enable',
|
|
|
|
'expired_at'
|
2020-01-29 16:49:32 +08:00
|
|
|
])->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-02-28 16:38:44 +08:00
|
|
|
$params = $request->only(array_keys(UserUpdate::RULES));
|
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
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|