7c933bad16
- 修复 ApprovalController 路由参数获取方式 - 修复 BaseController 命名空间引用问题 - 添加 use 语句引入 ApprovalOrder - 修复 ApprovalNode 类型常量与数据库 enum 一致 - 修复 ApprovalService 中的 action 常量引用 - 测试验证审批列表、详情、通过接口正常工作 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
113 lines
2.9 KiB
PHP
113 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use app\service\ApprovalService;
|
|
use app\model\ApprovalOrder;
|
|
use Webman\Http\Request;
|
|
use Webman\Http\Response;
|
|
|
|
class ApprovalController extends BaseController
|
|
{
|
|
private ApprovalService $service;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->service = new ApprovalService();
|
|
}
|
|
|
|
/**
|
|
* 获取路由参数
|
|
*/
|
|
private function getRouteParam(Request $request, string $name, $default = null)
|
|
{
|
|
$route = $request->route;
|
|
if ($route && method_exists($route, 'param')) {
|
|
return $route->param($name) ?? $default;
|
|
}
|
|
return $default;
|
|
}
|
|
|
|
/**
|
|
* 获取审批列表
|
|
*/
|
|
public function list(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$type = $request->get('type', 'my');
|
|
$page = (int) $request->get('page', 1);
|
|
$pageSize = (int) $request->get('page_size', 20);
|
|
|
|
$result = $this->service->getList($userId, $type, $page, $pageSize);
|
|
|
|
return $this->success($result);
|
|
}
|
|
|
|
/**
|
|
* 获取审批详情
|
|
*/
|
|
public function detail(Request $request): Response
|
|
{
|
|
$id = (int) $this->getRouteParam($request, 'id');
|
|
|
|
$approval = ApprovalOrder::with(['flow', 'applicant', 'records'])
|
|
->findOrFail($id);
|
|
|
|
return $this->success($approval);
|
|
}
|
|
|
|
/**
|
|
* 审批通过
|
|
*/
|
|
public function approve(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int) $this->getRouteParam($request, 'id');
|
|
$data = $request->post();
|
|
$data = $data ?? [];
|
|
$comment = $data['comment'] ?? null;
|
|
|
|
try {
|
|
$approval = $this->service->approve($id, $userId, $comment);
|
|
return $this->success($approval, '已通过');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 审批拒绝
|
|
*/
|
|
public function reject(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int) $this->getRouteParam($request, 'id');
|
|
$data = $request->post();
|
|
$data = $data ?? [];
|
|
$comment = $data['comment'] ?? null;
|
|
|
|
try {
|
|
$approval = $this->service->reject($id, $userId, $comment);
|
|
return $this->success($approval, '已拒绝');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 撤销申请
|
|
*/
|
|
public function cancel(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int) $this->getRouteParam($request, 'id');
|
|
|
|
try {
|
|
$approval = $this->service->cancel($id, $userId);
|
|
return $this->success($approval, '已撤销');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|