From 7e36657e360a35fa201a3f4c92f88ec200965638 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Thu, 19 Jan 2023 17:25:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E5=B7=A5=E5=8D=95?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=20=E6=94=AF=E6=8C=81=20=E5=B7=A5=E5=8D=95?= =?UTF-8?q?=E6=8A=95=E9=80=92=E7=BB=99=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/WorkOrderController.php | 12 +-- app/Jobs/WorkOrder/PushWorkOrderJob.php | 11 ++- app/Jobs/WorkOrder/Reply.php | 21 ++++- app/Jobs/WorkOrder/WorkOrder.php | 7 +- app/Models/WorkOrder/Reply.php | 6 +- app/Models/WorkOrder/WorkOrder.php | 7 +- app/Notifications/WorkOrder/WorkOrder.php | 18 ++-- ...odule_id_nullable_to_work_orders_table.php | 32 +++++++ .../views/admin/work-orders/index.blade.php | 14 +-- .../views/notifications/work_order.blade.php | 88 +++++++++++-------- 10 files changed, 144 insertions(+), 72 deletions(-) create mode 100644 database/migrations/2023_01_19_162625_set_module_id_nullable_to_work_orders_table.php diff --git a/app/Http/Controllers/Api/WorkOrderController.php b/app/Http/Controllers/Api/WorkOrderController.php index 98f1694..5678384 100644 --- a/app/Http/Controllers/Api/WorkOrderController.php +++ b/app/Http/Controllers/Api/WorkOrderController.php @@ -29,16 +29,6 @@ public function store(Request $request): JsonResponse 'host_id' => 'nullable|sometimes|exists:hosts,id', ]); - // module_id 和 host_id 必须有个要填写 - if ($request->input('module_id') === null && $request->input('host_id') === null) { - $message = 'module_id 和 host_id 必须有个要填写'; - - throw ValidationException::withMessages([ - 'module_id' => $message, - 'host_id' => $message, - ]); - } - $workOrder = (new WorkOrder)->create([ 'title' => $request->input('title'), 'content' => $request->input('content'), @@ -68,7 +58,7 @@ public function update(Request $request, WorkOrder $workOrder): JsonResponse // 访客不能关闭 if ($request->input('status') === 'closed' && !auth('sanctum')->check()) { - return $this->error('访客不能修改工单状态。'); + return $this->forbidden('访客不能修改工单状态。'); } $workOrder->update($request->only('status')); diff --git a/app/Jobs/WorkOrder/PushWorkOrderJob.php b/app/Jobs/WorkOrder/PushWorkOrderJob.php index 8e381a2..6a56224 100644 --- a/app/Jobs/WorkOrder/PushWorkOrderJob.php +++ b/app/Jobs/WorkOrder/PushWorkOrderJob.php @@ -10,8 +10,6 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; -// use Illuminate\Contracts\Queue\ShouldBeUnique; - class PushWorkOrderJob implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; @@ -33,9 +31,15 @@ public function __construct() */ public function handle(): void { - // (new WorkOrder)->whereIn('status', ['pending'])->with(['module', 'user', 'host', 'replies'])->chunk(100, function ($workOrders) { foreach ($workOrders as $workOrder) { + if ($workOrder->isPlatform()) { + if ($workOrder->status == 'pending') { + $workOrder->update(['status' => 'open']); + } + + continue; + } if ($workOrder->host) { if ($workOrder->host->status === 'pending') { @@ -43,7 +47,6 @@ public function handle(): void } } - if ($workOrder->status === 'error') { continue; } diff --git a/app/Jobs/WorkOrder/Reply.php b/app/Jobs/WorkOrder/Reply.php index 51f1fe6..5b5f6a0 100644 --- a/app/Jobs/WorkOrder/Reply.php +++ b/app/Jobs/WorkOrder/Reply.php @@ -22,7 +22,7 @@ class Reply implements ShouldQueue */ public function __construct(WorkOrderReply $reply, $type = 'post') { - $this->reply = $reply; + $this->reply = $reply->load(['workOrder', 'user']); $this->type = $type; } @@ -33,7 +33,14 @@ public function __construct(WorkOrderReply $reply, $type = 'post') */ public function handle(): void { - $this->reply->load(['workOrder', 'user']); + if ($this->reply->workOrder->isPlatform()) { + if ($this->reply->is_pending) { + $this->reply->update(['is_pending' => false]); + } + + return; + } + $this->reply->workOrder->load(['module']); $reply = $this->reply->toArray(); @@ -52,6 +59,16 @@ public function handle(): void } } else if ($this->type == 'patch') { + + // 不更新 is_pending + if ($this->reply->workOrder->isPlatform()) { + $this->reply->update([ + 'is_pending' => false + ]); + + return; + } + $this->reply->workOrder->module->http()->patch('work-orders/' . $this->reply->workOrder->id . '/replies/' . $this->reply->id, $reply); } else if ($this->type == 'delete') { $response = $this->reply->workOrder->module->http()->delete('work-orders/' . $this->reply->workOrder->id . '/replies/' . $this->reply->id); diff --git a/app/Jobs/WorkOrder/WorkOrder.php b/app/Jobs/WorkOrder/WorkOrder.php index 550769c..78b9939 100644 --- a/app/Jobs/WorkOrder/WorkOrder.php +++ b/app/Jobs/WorkOrder/WorkOrder.php @@ -24,7 +24,7 @@ class WorkOrder implements ShouldQueue */ public function __construct(WorkOrderModel $workOrder, $type = 'post') { - $this->workOrder = $workOrder; + $this->workOrder = $workOrder->load(['module']); $this->type = $type; } @@ -35,7 +35,10 @@ public function __construct(WorkOrderModel $workOrder, $type = 'post') */ public function handle(): void { - $this->workOrder->load(['module']); + if ($this->workOrder->isPlatform()) { + return; + } + if ($this->type == 'post') { $response = $this->workOrder->module->http()->post('work-orders', $this->workOrder->toArray()); diff --git a/app/Models/WorkOrder/Reply.php b/app/Models/WorkOrder/Reply.php index 602bf8c..e51d13f 100644 --- a/app/Models/WorkOrder/Reply.php +++ b/app/Models/WorkOrder/Reply.php @@ -34,7 +34,7 @@ protected static function boot() { parent::boot(); static::creating(function (self $model) { - $model->is_pending = 1; + $model->is_pending = false; // load work order $model->load(['workOrder']); @@ -70,6 +70,10 @@ protected static function boot() $model->workOrder->save(); + if ($model->workOrder->isPlatform()) { + $model->is_pending = false; + } + $model->ip = request()->ip(); }); diff --git a/app/Models/WorkOrder/WorkOrder.php b/app/Models/WorkOrder/WorkOrder.php index d20ef09..c1cd0ce 100644 --- a/app/Models/WorkOrder/WorkOrder.php +++ b/app/Models/WorkOrder/WorkOrder.php @@ -85,7 +85,7 @@ protected static function boot() static::updated(function (self $model) { dispatch(new WorkOrderJob($model, 'put')); - if ($model->notify) { + if ($model->notify && $model->isDirty('status')) { $model->notify(new WorkOrderNotification($model)); } }); @@ -136,6 +136,11 @@ public function isClosed(): bool return $this->status === 'closed'; } + public function isPlatform(): bool + { + return $this->module_id === null && $this->host_id === null; + } + /** * @throws CommonException */ diff --git a/app/Notifications/WorkOrder/WorkOrder.php b/app/Notifications/WorkOrder/WorkOrder.php index b721617..1552036 100644 --- a/app/Notifications/WorkOrder/WorkOrder.php +++ b/app/Notifications/WorkOrder/WorkOrder.php @@ -73,11 +73,7 @@ public function toArray(WorkOrderModel $workOrder): array { $array = $workOrder->toArray(); - $array['event'] = 'WorkOrder.updated'; - - if ($workOrder->status === 'replied') { - $array['event'] = 'WorkOrder.replied'; - } + $array['event'] = 'work-order.' . $workOrder->status; return $array; } @@ -86,12 +82,16 @@ public function toWeCom(WorkOrderModel $workOrder): false|array { $workOrder->load(['module', 'user']); - $module = $workOrder->module; + $module = null; + if ($workOrder->module) { + $module = $workOrder->module; - // 取消隐藏字段 - $module->makeVisible(['wecom_key']); + // 取消隐藏字段 + $module->makeVisible(['wecom_key']); - if ($module->wecom_key == null) { + } + + if ($module?->wecom_key == null) { $wecom_key = config('settings.wecom.robot_hook.default'); } else { $wecom_key = $module->wecom_key; diff --git a/database/migrations/2023_01_19_162625_set_module_id_nullable_to_work_orders_table.php b/database/migrations/2023_01_19_162625_set_module_id_nullable_to_work_orders_table.php new file mode 100644 index 0000000..56f2186 --- /dev/null +++ b/database/migrations/2023_01_19_162625_set_module_id_nullable_to_work_orders_table.php @@ -0,0 +1,32 @@ +string('module_id')->nullable()->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('work_orders', function (Blueprint $table) { + $table->string('module_id')->nullable(false)->change(); + }); + } +}; diff --git a/resources/views/admin/work-orders/index.blade.php b/resources/views/admin/work-orders/index.blade.php index 5de01f6..a05dac3 100644 --- a/resources/views/admin/work-orders/index.blade.php +++ b/resources/views/admin/work-orders/index.blade.php @@ -30,11 +30,15 @@ - {{ $workOrder->module_id }} - + @if ($workOrder->module_id) + {{ $workOrder->module_id }} + + @else + {{ config('app.display_name') }} + @endif @if ($workOrder->host_id) diff --git a/resources/views/notifications/work_order.blade.php b/resources/views/notifications/work_order.blade.php index e159c75..45ae65a 100644 --- a/resources/views/notifications/work_order.blade.php +++ b/resources/views/notifications/work_order.blade.php @@ -4,63 +4,77 @@ @switch ($data->status) @case('pending') - @php - $title = '工单挂起'; - @endphp - @break + @php + $title = '工单挂起'; + @endphp + @break @case('open') - @php - $title = '工单投递成功,并且已开启'; - @endphp - @break + @php + $title = '工单投递成功,并且已开启'; + @endphp + @break @case('user_replied') - @php - $title = '用户已回复'; - @endphp - @break + @php + $title = '用户已回复'; + @endphp + @break @case('closed') - @php - $title = '已结单'; - @endphp - @break + @php + $title = '已结单'; + @endphp + @break @case('replied') - @php - $title = '工作人员已回复'; - @endphp - @break + @php + $title = '工作人员已回复'; + @endphp + @break @case('on_hold') - @php - $title = '挂起'; - @endphp - @break + @php + $title = '挂起'; + @endphp + @break @case('in_progress') - @php - $title = '正在处理中'; - @endphp - @break + @php + $title = '正在处理中'; + @endphp + @break @case('error') - @php - $title = '投递失败'; - @endphp - @break + @php + $title = '投递失败'; + @endphp + @break + + @case('read') + @php + $title = '工作人员已读'; + @endphp + @break + + @case('user_read') + @php + $title = '用户已读'; + @endphp + @break @default - @php - $title = '状态更新'; - @endphp - @break + @php + $title = '状态更新'; + @endphp + @break @endswitch # 标题: {{ $title }} -# 模块: {{ $data->module->name }} +@if ($data->module) + # 模块: {{ $data->module->name }} +@endif ## 客户 {{ $data->user->name }} ##### 邮箱 {{ $data->user->email }}