mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-24 12:07:28 +08:00
refactor: 规范状态码、抛出异常的使用
This commit is contained in:
@@ -12,9 +12,8 @@ class NoticeController extends Controller
|
||||
{
|
||||
public function fetch(Request $request)
|
||||
{
|
||||
return response([
|
||||
'data' => Notice::orderBy('id', 'DESC')->get()
|
||||
]);
|
||||
$data = Notice::orderBy('id', 'DESC')->get();
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function save(NoticeSave $request)
|
||||
@@ -26,34 +25,34 @@ class NoticeController extends Controller
|
||||
]);
|
||||
if (!$request->input('id')) {
|
||||
if (!Notice::create($data)) {
|
||||
throw new ApiException(500, '保存失败');
|
||||
return $this->fail([500, '创建失败']);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Notice::find($request->input('id'))->update($data);
|
||||
} catch (\Exception $e) {
|
||||
throw new ApiException(500, '保存失败');
|
||||
\Log::error($e);
|
||||
return $this->fail([500, '保存失败']);
|
||||
}
|
||||
}
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
public function drop(Request $request)
|
||||
{
|
||||
if (empty($request->input('id'))) {
|
||||
throw new ApiException(422, '参数错误');
|
||||
}
|
||||
$request->validate([
|
||||
'id' => 'required'
|
||||
],[
|
||||
'id.required' => '公告ID不能为空'
|
||||
]);
|
||||
|
||||
$notice = Notice::find($request->input('id'));
|
||||
if (!$notice) {
|
||||
throw new ApiException(500, '公告不存在');
|
||||
return $this->fail([400202,'公告不存在']);
|
||||
}
|
||||
if (!$notice->delete()) {
|
||||
throw new ApiException(500, '删除失败');
|
||||
return $this->fail([500,'公告删除失败']);
|
||||
}
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,6 @@ class PlanController extends Controller
|
||||
if ($plans[$k]->id === $counts[$kk]->plan_id) $plans[$k]->count = $counts[$kk]->count;
|
||||
}
|
||||
}
|
||||
return response([
|
||||
'data' => $plans
|
||||
]);
|
||||
return $this->success($plans);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class TicketController extends Controller
|
||||
$ticket = Ticket::where('id', $request->input('id'))
|
||||
->first();
|
||||
if (!$ticket) {
|
||||
throw new ApiException(500, '工单不存在');
|
||||
return $this->fail([400,'工单不存在']);
|
||||
}
|
||||
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
|
||||
for ($i = 0; $i < count($ticket['message']); $i++) {
|
||||
@@ -27,9 +27,7 @@ class TicketController extends Controller
|
||||
$ticket['message'][$i]['is_me'] = false;
|
||||
}
|
||||
}
|
||||
return response([
|
||||
'data' => $ticket
|
||||
]);
|
||||
return $this->success($ticket);
|
||||
}
|
||||
$current = $request->input('current') ? $request->input('current') : 1;
|
||||
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
|
||||
@@ -48,39 +46,37 @@ class TicketController extends Controller
|
||||
|
||||
public function reply(Request $request)
|
||||
{
|
||||
if (empty($request->input('id'))) {
|
||||
throw new ApiException(422, '参数错误');
|
||||
}
|
||||
if (empty($request->input('message'))) {
|
||||
throw new ApiException(500, '消息不能为空');
|
||||
}
|
||||
$request->validate([
|
||||
'id' => 'required',
|
||||
'message' => 'required|string'
|
||||
],[
|
||||
'id.required' => '工单ID不能为空',
|
||||
'message.required' => '消息不能为空'
|
||||
]);
|
||||
$ticketService = new TicketService();
|
||||
$ticketService->replyByAdmin(
|
||||
$request->input('id'),
|
||||
$request->input('message'),
|
||||
$request->user['id']
|
||||
);
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
public function close(Request $request)
|
||||
{
|
||||
|
||||
if (empty($request->input('id'))) {
|
||||
throw new ApiException(422, '参数错误');
|
||||
return $this->fail([422,'工单ID不能为空']);
|
||||
}
|
||||
$ticket = Ticket::where('id', $request->input('id'))
|
||||
->first();
|
||||
if (!$ticket) {
|
||||
throw new ApiException(500, '工单不存在');
|
||||
return $this->fail([400202,'工单不存在']);
|
||||
}
|
||||
$ticket->status = 1;
|
||||
if (!$ticket->save()) {
|
||||
throw new ApiException(500, '关闭失败');
|
||||
return $this->fail([500, '工单关闭失败']);
|
||||
}
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,16 +16,14 @@ class UserController extends Controller
|
||||
public function getUserInfoById(Request $request)
|
||||
{
|
||||
if (empty($request->input('id'))) {
|
||||
throw new ApiException(422, '参数错误');
|
||||
return $this->fail([422,'用户ID不能为空']);
|
||||
}
|
||||
$user = User::where('is_admin', 0)
|
||||
->where('id', $request->input('id'))
|
||||
->where('is_staff', 0)
|
||||
->first();
|
||||
if (!$user) throw new ApiException(500, '用户不存在');
|
||||
return response([
|
||||
'data' => $user
|
||||
]);
|
||||
if (!$user) return $this->fail([400202,'用户不存在']);
|
||||
return $this->success($user);
|
||||
}
|
||||
|
||||
public function update(UserUpdate $request)
|
||||
@@ -33,10 +31,10 @@ class UserController extends Controller
|
||||
$params = $request->validated();
|
||||
$user = User::find($request->input('id'));
|
||||
if (!$user) {
|
||||
throw new ApiException(500, '用户不存在');
|
||||
return $this->fail([400202,'用户不存在']);
|
||||
}
|
||||
if (User::where('email', $params['email'])->first() && $user->email !== $params['email']) {
|
||||
throw new ApiException(500, '邮箱已被使用');
|
||||
return $this->fail([400201,'邮箱已被使用']);
|
||||
}
|
||||
if (isset($params['password'])) {
|
||||
$params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
|
||||
@@ -47,7 +45,7 @@ class UserController extends Controller
|
||||
if (isset($params['plan_id'])) {
|
||||
$plan = Plan::find($params['plan_id']);
|
||||
if (!$plan) {
|
||||
throw new ApiException(500, '订阅计划不存在');
|
||||
return $this->fail([400202,'订阅不存在']);
|
||||
}
|
||||
$params['group_id'] = $plan->group_id;
|
||||
}
|
||||
@@ -55,11 +53,10 @@ class UserController extends Controller
|
||||
try {
|
||||
$user->update($params);
|
||||
} catch (\Exception $e) {
|
||||
throw new ApiException(500, '保存失败');
|
||||
\Log::error($e);
|
||||
return $this->fail([500,'更新失败']);
|
||||
}
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
public function sendMail(UserSendMail $request)
|
||||
@@ -82,9 +79,7 @@ class UserController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
public function ban(Request $request)
|
||||
@@ -98,11 +93,10 @@ class UserController extends Controller
|
||||
'banned' => 1
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
throw new ApiException(500, '处理失败');
|
||||
\Log::error($e);
|
||||
return $this->fail([500,'处理上失败']);
|
||||
}
|
||||
|
||||
return response([
|
||||
'data' => true
|
||||
]);
|
||||
return $this->success(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user