tp/app/admin/controller/Cms/Demand/DemandController.php
2026-03-09 09:40:36 +08:00

181 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
namespace app\admin\controller\Cms\Demand;
use app\index\BaseController;
use Symfony\Component\VarDumper\VarDumper;
use think\exception\ValidateException;
use think\facade\Request;
use think\facade\Session;
use think\response\Json;
use think\db\exception\DbException;
use app\model\Cms\Demand;
use app\model\Cms\DemandCategory;
class DemandController extends BaseController
{
/**
* 获取需求列表
* @return Json
*/
public function getDemandList(): Json
{
// 查询分类
$demandList = Demand::where('delete_time', null)
->order('id', 'desc')
->select();
if (!$demandList) {
return json([
'code' => 200,
'msg' => 'success',
'list' => [],
]);
}
return json([
'code' => 200,
'msg' => 'success',
'list' => $demandList,
]);
}
/**
* 增加需求
* @return Json
*/
public function addDemand(): Json
{
try {
$data = Request::only(['title', 'desc', 'applicant', 'status']);
// 验证数据
if (empty($data['title']) || empty($data['desc'])) {
return json([
'code' => 400,
'msg' => '标题和描述不能为空',
]);
}
// 创建需求
$demand = Demand::create([
'title' => $data['title'],
'desc' => $data['desc'],
'applicant' => $data['applicant'] ?? '',
'status' => $data['status'] ?? 'pending',
]);
return json([
'code' => 200,
'msg' => '添加成功',
'data' => $demand,
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '添加失败:' . $e->getMessage(),
]);
}
}
/**
* 编辑需求
* @return Json
*/
public function editDemand(): Json
{
try {
$id = Request::param('id');
$data = Request::only(['title', 'desc', 'applicant', 'status']);
// 验证数据
if (empty($id)) {
return json([
'code' => 400,
'msg' => '需求ID不能为空',
]);
}
if (empty($data['title']) || empty($data['desc'])) {
return json([
'code' => 400,
'msg' => '标题和描述不能为空',
]);
}
// 查找需求
$demand = Demand::find($id);
if (!$demand) {
return json([
'code' => 404,
'msg' => '需求不存在',
]);
}
// 更新需求
$demand->save([
'title' => $data['title'],
'desc' => $data['desc'],
'applicant' => $data['applicant'] ?? '',
'status' => $data['status'] ?? 'pending',
]);
return json([
'code' => 200,
'msg' => '编辑成功',
'data' => $demand,
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '编辑失败:' . $e->getMessage(),
]);
}
}
/**
* 删除需求
* @return Json
*/
public function deleteDemand(): Json
{
try {
$id = Request::param('id');
// 验证数据
if (empty($id)) {
return json([
'code' => 400,
'msg' => '需求ID不能为空',
]);
}
// 查找需求
$demand = Demand::find($id);
if (!$demand) {
return json([
'code' => 404,
'msg' => '需求不存在',
]);
}
// 软删除
$demand->delete();
return json([
'code' => 200,
'msg' => '删除成功',
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '删除失败:' . $e->getMessage(),
]);
}
}
}