2026-02-25 15:22:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
2026-02-25 18:09:04 +08:00
|
|
|
namespace app\controller;
|
2026-02-25 15:22:12 +08:00
|
|
|
|
2026-02-25 18:09:04 +08:00
|
|
|
use app\service\BusinessTripService;
|
2026-02-25 15:22:12 +08:00
|
|
|
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 ?? [];
|
|
|
|
|
|
2026-02-26 18:01:40 +08:00
|
|
|
error_log("📝 提交申请 - 用户ID: {$userId}, 申请ID: {$id}");
|
|
|
|
|
error_log("📦 接收到的数据: " . json_encode($data, JSON_UNESCAPED_UNICODE));
|
|
|
|
|
|
2026-02-25 15:22:12 +08:00
|
|
|
$approvers = $data['approvers'] ?? [];
|
|
|
|
|
$ccUsers = $data['cc_users'] ?? [];
|
|
|
|
|
|
2026-02-26 18:01:40 +08:00
|
|
|
error_log("👥 审批人列表: " . json_encode($approvers, JSON_UNESCAPED_UNICODE));
|
|
|
|
|
error_log("📨 抄送人列表: " . json_encode($ccUsers, JSON_UNESCAPED_UNICODE));
|
|
|
|
|
|
2026-02-25 15:22:12 +08:00
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 18:09:04 +08:00
|
|
|
/**
|
|
|
|
|
* 删除草稿
|
|
|
|
|
*/
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 15:22:12 +08:00
|
|
|
/**
|
|
|
|
|
* 取消出差申请
|
|
|
|
|
*/
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-25 18:54:38 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取默认审批人和抄送人
|
|
|
|
|
*/
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-25 15:22:12 +08:00
|
|
|
}
|