2022-08-15 14:29:57 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-16 05:22:41 +00:00
|
|
|
namespace App\Http\Controllers\Api;
|
2022-08-15 14:29:57 +00:00
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2022-09-13 05:07:36 +00:00
|
|
|
use App\Models\WorkOrder\Reply;
|
|
|
|
use App\Models\WorkOrder\WorkOrder;
|
2022-12-11 11:47:30 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use function auth;
|
2022-08-15 14:29:57 +00:00
|
|
|
|
|
|
|
class ReplyController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
2022-12-27 16:25:22 +00:00
|
|
|
* @return JsonResponse
|
2022-08-15 14:29:57 +00:00
|
|
|
*/
|
2022-09-13 05:07:36 +00:00
|
|
|
public function index(WorkOrder $workOrder)
|
2022-08-15 14:29:57 +00:00
|
|
|
{
|
2023-01-02 12:12:32 +00:00
|
|
|
$replies = Reply::workOrderId($workOrder->id)->with('module')->withUser()->simplePaginate(20);
|
2022-08-15 14:29:57 +00:00
|
|
|
|
|
|
|
return $this->success($replies);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
2022-12-11 11:47:30 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @param WorkOrder $workOrder
|
2022-11-06 11:28:22 +00:00
|
|
|
*
|
2023-01-01 13:00:21 +00:00
|
|
|
* @return JsonResponse
|
2022-08-15 14:29:57 +00:00
|
|
|
*/
|
2022-09-13 05:07:36 +00:00
|
|
|
public function store(Request $request, WorkOrder $workOrder)
|
2022-08-15 14:29:57 +00:00
|
|
|
{
|
2022-09-08 16:12:02 +00:00
|
|
|
$this->validate($request, [
|
2022-08-15 14:29:57 +00:00
|
|
|
'content' => 'string|required|min:1|max:1000',
|
|
|
|
]);
|
|
|
|
|
2023-01-01 13:00:21 +00:00
|
|
|
if ($workOrder->isFailure()) {
|
|
|
|
return $this->error('工单状态异常,无法进行回复。请尝试重新建立工单。');
|
|
|
|
}
|
2022-08-19 15:27:57 +00:00
|
|
|
|
2023-01-02 10:52:38 +00:00
|
|
|
// 如果工单已经关闭,那么访客不能回复
|
|
|
|
if ($workOrder->isClosed() && !auth('sanctum')->check()) {
|
|
|
|
return $this->error('工单已关闭,无法进行回复。');
|
|
|
|
}
|
|
|
|
|
2023-01-01 13:00:21 +00:00
|
|
|
$create = [
|
2022-11-21 04:43:43 +00:00
|
|
|
'content' => $request->input('content'),
|
2022-09-13 05:07:36 +00:00
|
|
|
'work_order_id' => $workOrder->id,
|
2023-01-01 13:00:21 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (auth('sanctum')->check()) {
|
|
|
|
$create['user_id'] = auth('sanctum')->id();
|
|
|
|
} else {
|
|
|
|
$this->validate($request, [
|
|
|
|
'name' => 'string|required|min:1|max:255',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$create['name'] = $request->input('name');
|
|
|
|
}
|
2022-08-15 14:29:57 +00:00
|
|
|
|
2023-01-01 13:00:21 +00:00
|
|
|
$reply = Reply::create($create);
|
2022-08-15 14:29:57 +00:00
|
|
|
|
|
|
|
return $this->success($reply);
|
|
|
|
}
|
|
|
|
}
|