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

57 lines
1.9 KiB
PHP
Raw Normal View History

2019-10-24 19:30:16 +08:00
<?php
namespace App\Http\Controllers\Admin;
2019-10-25 15:10:01 +08:00
use App\Http\Requests\Admin\UserSave;
2019-10-24 19:30:16 +08:00
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\User;
class UserController extends Controller
{
public function index (Request $request) {
$current = $request->input('current') ? $request->input('current') : 1;
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
$userModel = User::orderBy('created_at', 'DESC');
if ($request->input('email')) {
$userModel->where('email', $request->input('email'));
}
$total = $userModel->count();
return response([
'data' => $userModel->forPage($current, $pageSize)
->get(),
'total' => $total
]);
}
2019-10-25 15:10:01 +08:00
public function save (UserSave $request) {
if ($request->input('id')) {
2019-10-25 15:12:28 +08:00
$user = User::find($request->input('id'));
if (!$user) {
abort(500, '用户不存在');
}
2019-10-25 15:10:01 +08:00
} else {
2019-10-25 15:12:28 +08:00
$user = new User();
2019-10-25 15:10:01 +08:00
}
2019-10-25 15:35:47 +08:00
if (User::where('email', $request->input('email'))->first()) {
2019-10-25 15:10:01 +08:00
abort(500, '邮箱已被使用');
}
2019-10-25 15:33:14 +08:00
if ($request->input('id') && !$request->input('password')) {
abort(500, '密码不能为空');
}
2019-10-25 15:12:28 +08:00
$user->email = $request->input('email');
$user->password = password_hash($request->input('password'), PASSWORD_DEFAULT);
$user->transfer_enable = $request->input('transfer_enable') * 1073741824;
$user->expired_at = $request->input('expired_at');
$user->banned = $request->input('banned');
$user->is_admin = $request->input('is_admin');
if (!$user->save()) {
2019-10-25 15:10:01 +08:00
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
2019-10-24 19:30:16 +08:00
}