2023-11-17 01:44:01 -05:00
|
|
|
<?php
|
|
|
|
|
2025-01-06 12:20:11 -05:00
|
|
|
namespace App\Http\Controllers\V2\Admin;
|
2023-11-17 01:44:01 -05:00
|
|
|
|
2023-12-04 07:40:49 -05:00
|
|
|
use App\Exceptions\ApiException;
|
2023-11-17 01:44:01 -05:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\Admin\NoticeSave;
|
|
|
|
use App\Models\Notice;
|
|
|
|
use Illuminate\Http\Request;
|
2025-01-12 08:10:52 -05:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2023-11-17 01:44:01 -05:00
|
|
|
|
|
|
|
class NoticeController extends Controller
|
|
|
|
{
|
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
2025-01-12 08:10:52 -05:00
|
|
|
return $this->success(
|
|
|
|
Notice::orderBy('sort', 'ASC')
|
|
|
|
->orderBy('id', 'DESC')
|
|
|
|
->get()
|
|
|
|
);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function save(NoticeSave $request)
|
|
|
|
{
|
|
|
|
$data = $request->only([
|
|
|
|
'title',
|
|
|
|
'content',
|
|
|
|
'img_url',
|
2025-01-06 12:20:11 -05:00
|
|
|
'tags',
|
|
|
|
'show',
|
|
|
|
'popup'
|
2023-11-17 01:44:01 -05:00
|
|
|
]);
|
|
|
|
if (!$request->input('id')) {
|
|
|
|
if (!Notice::create($data)) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([500, '保存失败']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
Notice::find($request->input('id'))->update($data);
|
|
|
|
} catch (\Exception $e) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([500, '保存失败']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
}
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success(true);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function show(Request $request)
|
|
|
|
{
|
|
|
|
if (empty($request->input('id'))) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([500, '公告ID不能为空']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
$notice = Notice::find($request->input('id'));
|
|
|
|
if (!$notice) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([400202, '公告不存在']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
$notice->show = $notice->show ? 0 : 1;
|
|
|
|
if (!$notice->save()) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([500, '保存失败']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success(true);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function drop(Request $request)
|
|
|
|
{
|
|
|
|
if (empty($request->input('id'))) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([422, '公告ID不能为空']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
$notice = Notice::find($request->input('id'));
|
|
|
|
if (!$notice) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([400202, '公告不存在']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
if (!$notice->delete()) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([500, '删除失败']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success(true);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2025-01-12 08:10:52 -05:00
|
|
|
|
|
|
|
public function sort(Request $request)
|
|
|
|
{
|
|
|
|
$params = $request->validate([
|
|
|
|
'ids' => 'required|array'
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
DB::beginTransaction();
|
|
|
|
foreach ($params['ids'] as $k => $v) {
|
|
|
|
$notice = Notice::findOrFail($v);
|
|
|
|
$notice->update(['sort' => $k + 1]);
|
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return $this->success(true);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
\Log::error($e);
|
|
|
|
return $this->fail([500, '排序保存失败']);
|
|
|
|
}
|
|
|
|
}
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|