cc3c599c90
主要修改:
1. submit() 方法不再修改 approval_node 表,节点仅作为流程模板
2. getList() 的 pending 查询从 form_data.approvers 获取审批人列表
3. approve() 方法从 form_data.approvers 检查用户是否有权限
4. 兼容两种数据格式:[1,2,3] 或 [{id:1},{id:2},{id:3}]
修复问题:
- 之前每次提交申请都会修改 approval_node 表的 approvers 字段
- 这导致流程模板被污染,不同的申请共享同一个节点配置
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
230 lines
6.4 KiB
PHP
230 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use app\service\BusinessTripService;
|
|
use Webman\Http\Request;
|
|
use Webman\Http\Response;
|
|
|
|
class BusinessTripController extends BaseController
|
|
{
|
|
private BusinessTripService $service;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->service = new BusinessTripService();
|
|
}
|
|
|
|
/**
|
|
* 获取路由参数
|
|
*/
|
|
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');
|
|
if (!$id) {
|
|
return $this->error('参数错误');
|
|
}
|
|
|
|
$detail = $this->service->getDetail($id);
|
|
|
|
return $this->success($detail);
|
|
}
|
|
|
|
/**
|
|
* 创建出差申请(草稿)
|
|
*/
|
|
public function create(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$data = $request->post();
|
|
$data = $data ?? [];
|
|
|
|
// 验证必填字段
|
|
if (empty($data['reason'])) {
|
|
return $this->error('请输入出差原因');
|
|
}
|
|
|
|
if (empty($data['itineraries']) || !is_array($data['itineraries'])) {
|
|
return $this->error('请至少添加一条行程');
|
|
}
|
|
|
|
// 验证行程数据
|
|
foreach ($data['itineraries'] as $index => $itinerary) {
|
|
if (empty($itinerary['start_time'])) {
|
|
return $this->error("行程" . ($index + 1) . ":请选择开始时间");
|
|
}
|
|
if (empty($itinerary['destination'])) {
|
|
return $this->error("行程" . ($index + 1) . ":请输入目的地");
|
|
}
|
|
}
|
|
|
|
try {
|
|
$businessTrip = $this->service->createDraft($userId, $data);
|
|
return $this->success($businessTrip, '创建成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新出差申请
|
|
*/
|
|
public function update(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int) $this->getRouteParam($request, 'id');
|
|
if (!$id) {
|
|
return $this->error('参数错误');
|
|
}
|
|
|
|
$data = $request->post();
|
|
$data = $data ?? [];
|
|
|
|
try {
|
|
$businessTrip = $this->service->update($userId, $id, $data);
|
|
return $this->success($businessTrip, '更新成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 提交出差申请
|
|
*/
|
|
public function submit(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int) $this->getRouteParam($request, 'id');
|
|
if (!$id) {
|
|
return $this->error('参数错误');
|
|
}
|
|
|
|
$data = $request->post();
|
|
$data = $data ?? [];
|
|
|
|
error_log("📝 提交申请 - 用户ID: {$userId}, 申请ID: {$id}");
|
|
error_log("📦 接收到的数据: " . json_encode($data, JSON_UNESCAPED_UNICODE));
|
|
|
|
$approvers = $data['approvers'] ?? [];
|
|
$ccUsers = $data['cc_users'] ?? [];
|
|
|
|
error_log("👥 审批人列表: " . json_encode($approvers, JSON_UNESCAPED_UNICODE));
|
|
error_log("📨 抄送人列表: " . json_encode($ccUsers, JSON_UNESCAPED_UNICODE));
|
|
|
|
if (empty($approvers)) {
|
|
return $this->error('请选择审批人');
|
|
}
|
|
|
|
try {
|
|
$businessTrip = $this->service->submit($userId, $id, $approvers, $ccUsers);
|
|
return $this->success($businessTrip, '提交成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除草稿
|
|
*/
|
|
public function deleteDraft(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int) $this->getRouteParam($request, 'id');
|
|
if (!$id) {
|
|
return $this->error('参数错误');
|
|
}
|
|
|
|
try {
|
|
$this->service->deleteDraft($userId, $id);
|
|
return $this->success(null, '删除成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 审批操作
|
|
*/
|
|
public function approve(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int) $this->getRouteParam($request, 'id');
|
|
if (!$id) {
|
|
return $this->error('参数错误');
|
|
}
|
|
|
|
$data = $request->post();
|
|
$data = $data ?? [];
|
|
$action = $data['action'] ?? 'approve'; // approve 或 reject
|
|
$comment = $data['comment'] ?? '';
|
|
|
|
try {
|
|
$result = $this->service->approve($userId, $id, $action, $comment);
|
|
return $this->success($result, $action === 'approve' ? '已同意' : '已拒绝');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 取消出差申请
|
|
*/
|
|
public function cancel(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
$id = (int) $this->getRouteParam($request, 'id');
|
|
if (!$id) {
|
|
return $this->error('参数错误');
|
|
}
|
|
|
|
try {
|
|
$businessTrip = $this->service->cancel($userId, $id);
|
|
return $this->success($businessTrip, '取消成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取默认审批人和抄送人
|
|
*/
|
|
public function getDefaultApprovers(Request $request): Response
|
|
{
|
|
$userId = $this->getUserId($request);
|
|
|
|
try {
|
|
$result = $this->service->getDefaultApprovers($userId);
|
|
return $this->success($result);
|
|
} catch (\Exception $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|