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

82 lines
1.9 KiB
PHP
Raw Normal View History

2019-12-12 13:45:46 +08:00
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\NoticeSave;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Notice;
2020-01-12 01:27:36 +08:00
use Illuminate\Support\Facades\Cache;
2019-12-12 13:45:46 +08:00
class NoticeController extends Controller
{
2020-01-11 13:36:52 +08:00
public function fetch(Request $request)
{
2019-12-12 13:45:46 +08:00
return response([
2019-12-12 14:39:26 +08:00
'data' => Notice::orderBy('id', 'DESC')->get()
2019-12-12 13:45:46 +08:00
]);
}
2020-01-11 13:36:52 +08:00
public function save(NoticeSave $request)
{
2019-12-12 13:45:46 +08:00
$data = $request->only([
'title',
2019-12-12 16:29:35 +08:00
'content',
2022-04-15 02:45:52 +08:00
'img_url',
'tags'
2019-12-12 13:45:46 +08:00
]);
2020-01-02 00:43:25 +08:00
if (!$request->input('id')) {
if (!Notice::create($data)) {
abort(500, '保存失败');
}
} else {
2020-03-17 14:28:47 +08:00
try {
Notice::find($request->input('id'))->update($data);
} catch (\Exception $e) {
2020-01-02 00:43:25 +08:00
abort(500, '保存失败');
}
2019-12-12 14:34:28 +08:00
}
return response([
'data' => true
]);
}
2019-12-12 14:41:27 +08:00
2022-01-22 02:30:05 +08:00
public function show(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数有误');
}
$notice = Notice::find($request->input('id'));
if (!$notice) {
abort(500, '公告不存在');
}
$notice->show = $notice->show ? 0 : 1;
if (!$notice->save()) {
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
2020-01-11 13:36:52 +08:00
public function drop(Request $request)
{
2019-12-12 14:41:27 +08:00
if (empty($request->input('id'))) {
abort(500, '参数错误');
}
$notice = Notice::find($request->input('id'));
if (!$notice) {
abort(500, '公告不存在');
}
2019-12-12 14:43:21 +08:00
if (!$notice->delete()) {
2019-12-12 14:41:27 +08:00
abort(500, '删除失败');
}
return response([
'data' => true
]);
}
2019-12-12 13:45:46 +08:00
}