135 lines
3.2 KiB
PHP
135 lines
3.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Controller;
|
||
|
|
|
||
|
|
use App\Service\ApprovalService;
|
||
|
|
use Webman\Http\Request;
|
||
|
|
use Webman\Http\Response;
|
||
|
|
|
||
|
|
class ApprovalController extends BaseController
|
||
|
|
{
|
||
|
|
private ApprovalService $approvalService;
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->approvalService = new ApprovalService();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取审批列表
|
||
|
|
*/
|
||
|
|
public function list(Request $request): Response
|
||
|
|
{
|
||
|
|
$type = $request->input('type', 'my');
|
||
|
|
$page = (int) $request->input('page', 1);
|
||
|
|
$pageSize = (int) $request->input('page_size', 20);
|
||
|
|
|
||
|
|
$userId = $this->getUserId($request);
|
||
|
|
|
||
|
|
if (!$userId) {
|
||
|
|
return $this->error('用户未登录', 401);
|
||
|
|
}
|
||
|
|
|
||
|
|
$result = $this->approvalService->getList($userId, $type, $page, $pageSize);
|
||
|
|
|
||
|
|
return $this->success($result);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取审批详情
|
||
|
|
*/
|
||
|
|
public function detail(Request $request): Response
|
||
|
|
{
|
||
|
|
$id = $request->route('id');
|
||
|
|
|
||
|
|
$order = \App\Model\ApprovalOrder::with(['flow', 'applicant', 'records.approver'])
|
||
|
|
->findOrFail($id);
|
||
|
|
|
||
|
|
return $this->success($order);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 提交审批
|
||
|
|
*/
|
||
|
|
public function submit(Request $request): Response
|
||
|
|
{
|
||
|
|
$data = $request->all();
|
||
|
|
$userId = $this->getUserId($request);
|
||
|
|
|
||
|
|
if (!$userId) {
|
||
|
|
return $this->error('用户未登录', 401);
|
||
|
|
}
|
||
|
|
|
||
|
|
$data['applicant_id'] = $userId;
|
||
|
|
|
||
|
|
try {
|
||
|
|
$order = $this->approvalService->submit($data);
|
||
|
|
return $this->success($order);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
return $this->error($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 审批通过
|
||
|
|
*/
|
||
|
|
public function approve(Request $request): Response
|
||
|
|
{
|
||
|
|
$id = $request->route('id');
|
||
|
|
$comment = $request->post('comment');
|
||
|
|
$userId = $this->getUserId($request);
|
||
|
|
|
||
|
|
if (!$userId) {
|
||
|
|
return $this->error('用户未登录', 401);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$order = $this->approvalService->approve((int) $id, $userId, $comment);
|
||
|
|
return $this->success($order);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
return $this->error($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 审批拒绝
|
||
|
|
*/
|
||
|
|
public function reject(Request $request): Response
|
||
|
|
{
|
||
|
|
$id = $request->route('id');
|
||
|
|
$comment = $request->post('comment');
|
||
|
|
$userId = $this->getUserId($request);
|
||
|
|
|
||
|
|
if (!$userId) {
|
||
|
|
return $this->error('用户未登录', 401);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$order = $this->approvalService->reject((int) $id, $userId, $comment);
|
||
|
|
return $this->success($order);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
return $this->error($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 撤销申请
|
||
|
|
*/
|
||
|
|
public function cancel(Request $request): Response
|
||
|
|
{
|
||
|
|
$id = $request->route('id');
|
||
|
|
$userId = $this->getUserId($request);
|
||
|
|
|
||
|
|
if (!$userId) {
|
||
|
|
return $this->error('用户未登录', 401);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$order = $this->approvalService->cancel((int) $id, $userId);
|
||
|
|
return $this->success($order);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
return $this->error($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|