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\KnowledgeSave;
|
|
|
|
use App\Http\Requests\Admin\KnowledgeSort;
|
|
|
|
use App\Models\Knowledge;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class KnowledgeController extends Controller
|
|
|
|
{
|
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->input('id')) {
|
|
|
|
$knowledge = Knowledge::find($request->input('id'))->toArray();
|
2025-01-06 12:20:11 -05:00
|
|
|
if (!$knowledge)
|
|
|
|
return $this->fail([400202, '知识不存在']);
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success($knowledge);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2023-12-06 15:01:32 -05:00
|
|
|
$data = Knowledge::select(['title', 'id', 'updated_at', 'category', 'show'])
|
2025-01-06 12:20:11 -05:00
|
|
|
->orderBy('sort', 'ASC')
|
|
|
|
->get();
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success($data);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCategory(Request $request)
|
|
|
|
{
|
2023-12-06 15:01:32 -05:00
|
|
|
return $this->success(array_keys(Knowledge::get()->groupBy('category')->toArray()));
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function save(KnowledgeSave $request)
|
|
|
|
{
|
|
|
|
$params = $request->validated();
|
|
|
|
|
|
|
|
if (!$request->input('id')) {
|
|
|
|
if (!Knowledge::create($params)) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([500, '创建失败']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
Knowledge::find($request->input('id'))->update($params);
|
|
|
|
} catch (\Exception $e) {
|
2023-12-06 15:01:32 -05:00
|
|
|
\Log::error($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)
|
|
|
|
{
|
2023-12-06 15:01:32 -05:00
|
|
|
$request->validate([
|
|
|
|
'id' => 'required|numeric'
|
2025-01-06 12:20:11 -05:00
|
|
|
], [
|
|
|
|
'id.required' => '知识库ID不能为空'
|
2023-12-06 15:01:32 -05:00
|
|
|
]);
|
2023-11-17 01:44:01 -05:00
|
|
|
$knowledge = Knowledge::find($request->input('id'));
|
|
|
|
if (!$knowledge) {
|
2023-12-06 15:01:32 -05:00
|
|
|
throw new ApiException('知识不存在');
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2023-12-06 15:01:32 -05:00
|
|
|
$knowledge->show = !$knowledge->show;
|
2023-11-17 01:44:01 -05:00
|
|
|
if (!$knowledge->save()) {
|
2023-12-06 15:01:32 -05:00
|
|
|
throw new ApiException('保存失败');
|
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-06 12:20:11 -05:00
|
|
|
public function sort(Request $request)
|
2023-11-17 01:44:01 -05:00
|
|
|
{
|
2025-01-06 12:20:11 -05:00
|
|
|
$request->validate([
|
|
|
|
'ids' => 'required|array'
|
|
|
|
], [
|
|
|
|
'ids.required' => '参数有误',
|
|
|
|
'ids.array' => '参数有误'
|
|
|
|
]);
|
2023-11-17 01:44:01 -05:00
|
|
|
try {
|
2023-12-06 06:00:26 -05:00
|
|
|
DB::beginTransaction();
|
2025-01-06 12:20:11 -05:00
|
|
|
foreach ($request->input('ids') as $k => $v) {
|
2023-11-17 01:44:01 -05:00
|
|
|
$knowledge = Knowledge::find($v);
|
|
|
|
$knowledge->timestamps = false;
|
|
|
|
$knowledge->update(['sort' => $k + 1]);
|
|
|
|
}
|
2023-12-06 06:00:26 -05:00
|
|
|
DB::commit();
|
2023-11-17 01:44:01 -05:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
2023-12-06 15:01:32 -05:00
|
|
|
throw new ApiException('保存失败');
|
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)
|
|
|
|
{
|
2023-12-06 15:01:32 -05:00
|
|
|
$request->validate([
|
|
|
|
'id' => 'required|numeric'
|
2025-01-06 12:20:11 -05:00
|
|
|
], [
|
|
|
|
'id.required' => '知识库ID不能为空'
|
2023-12-06 15:01:32 -05:00
|
|
|
]);
|
2023-11-17 01:44:01 -05:00
|
|
|
$knowledge = Knowledge::find($request->input('id'));
|
|
|
|
if (!$knowledge) {
|
2025-01-06 12:20:11 -05:00
|
|
|
return $this->fail([400202, '知识不存在']);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
if (!$knowledge->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
|
|
|
}
|
|
|
|
}
|