diff --git a/app/Http/Controllers/Admin/ModuleController.php b/app/Http/Controllers/Admin/ModuleController.php index d79a240..388c82e 100644 --- a/app/Http/Controllers/Admin/ModuleController.php +++ b/app/Http/Controllers/Admin/ModuleController.php @@ -66,16 +66,6 @@ public function store(Request $request): RedirectResponse } - private function rules(): array - { - return [ - 'id' => 'required|string|max:255', - 'name' => 'required|string|max:255', - 'url' => 'required|url', - 'status' => 'required|string|in:up,down,maintenance', - ]; - } - /** * Display the specified resource. * @@ -116,25 +106,22 @@ public function edit(Module $module): View */ public function update(Request $request, Module $module): RedirectResponse { - // - $request->validate($this->rules()); - - if ($request->reset_api_token) { + if ($request->input('reset_api_token')) { $module->api_token = Str::random(60); } - $module->id = $request->id; - $module->name = $request->name; - $module->url = $request->url; - $module->status = $request->status; + $module->id = $request->input('id'); + $module->name = $request->input('name'); + $module->url = $request->input('url'); + $module->status = $request->input('status'); $module->save(); $text = '模块更新成功'; - if ($request->reset_api_token) { + if ($request->input('reset_api_token')) { $text .= ', API Token 为 ' . $module->api_token . '。'; } else { $text .= '。'; @@ -152,7 +139,6 @@ public function update(Request $request, Module $module): RedirectResponse */ public function destroy(Module $module): RedirectResponse { - // $module->delete(); return redirect()->route('admin.modules.index')->with('success', '模块已删除。'); @@ -200,4 +186,13 @@ public function fast_login(Module $module): View|RedirectResponse } } + private function rules(): array + { + return [ + 'id' => 'required|string|max:255', + 'name' => 'required|string|max:255', + 'url' => 'required|url', + 'status' => 'required|string|in:up,down,maintenance', + ]; + } } diff --git a/app/Http/Controllers/Admin/WorkOrderController.php b/app/Http/Controllers/Admin/WorkOrderController.php index 7136865..950dc64 100644 --- a/app/Http/Controllers/Admin/WorkOrderController.php +++ b/app/Http/Controllers/Admin/WorkOrderController.php @@ -54,8 +54,6 @@ public function show(WorkOrder $workOrder): View */ public function edit(WorkOrder $workOrder): View { - // - return view('admin.work-orders.edit', compact('workOrder')); } @@ -69,17 +67,17 @@ public function edit(WorkOrder $workOrder): View */ public function update(Request $request, WorkOrder $workOrder): RedirectResponse { - // - $request->validate([ 'status' => 'required|string|in:open,closed,read,user_read,replied,user_replied,on_hold,in_progress', + 'notify' => 'nullable|boolean', ]); $workOrder->update([ - 'status' => $request->status + 'status' => $request->input('status'), + 'notify' => $request->input('notify'), ]); - return back()->with('success', '工单更新成功'); + return back()->with('success', '工单更新成功。'); } /** diff --git a/app/Models/WorkOrder/WorkOrder.php b/app/Models/WorkOrder/WorkOrder.php index 2b36590..bcd1f68 100644 --- a/app/Models/WorkOrder/WorkOrder.php +++ b/app/Models/WorkOrder/WorkOrder.php @@ -82,52 +82,12 @@ class WorkOrder extends Model 'user_id', 'module_id', 'status', + 'notify' ]; - protected static function boot() - { - parent::boot(); - - static::creating(function (self $model) { - // 生成 uuid - $model->uuid = Str::uuid()->toString(); - - if ($model->host_id) { - $model->load(['host']); - $model->module_id = $model->host->module_id; - } - - if (auth('sanctum')->check()) { - $model->user_id = auth()->id(); - - if ($model->host_id) { - if (!$model->user_id == $model->host->user_id) { - throw new CommonException('user_id not match host user_id'); - } - } - } else { - if (!$model->user_id) { - throw new CommonException('user_id is required'); - } - } - - if ($model->host_id) { - $model->host->load('module'); - $module = $model->host->module; - - if ($module === null) { - $model->status = 'open'; - } else { - $model->status = 'pending'; - } - } - }); - - // updated - static::updated(function ($model) { - dispatch(new WorkOrderJob($model, 'put')); - }); - } + protected $casts = [ + 'notify' => 'boolean' + ]; public function scopeThisModule($query) { @@ -187,4 +147,49 @@ public function safeDelete(): bool return true; } + + protected static function boot() + { + parent::boot(); + + static::creating(function (self $model) { + // 生成 uuid + $model->uuid = Str::uuid()->toString(); + + if ($model->host_id) { + $model->load(['host']); + $model->module_id = $model->host->module_id; + } + + if (auth('sanctum')->check()) { + $model->user_id = auth()->id(); + + if ($model->host_id) { + if (!$model->user_id == $model->host->user_id) { + throw new CommonException('user_id not match host user_id'); + } + } + } else { + if (!$model->user_id) { + throw new CommonException('user_id is required'); + } + } + + if ($model->host_id) { + $model->host->load('module'); + $module = $model->host->module; + + if ($module === null) { + $model->status = 'open'; + } else { + $model->status = 'pending'; + } + } + }); + + // updated + static::updated(function ($model) { + dispatch(new WorkOrderJob($model, 'put')); + }); + } } diff --git a/app/Notifications/WorkOrderNotification.php b/app/Notifications/WorkOrderNotification.php index 854e7ab..f749a79 100644 --- a/app/Notifications/WorkOrderNotification.php +++ b/app/Notifications/WorkOrderNotification.php @@ -50,6 +50,11 @@ public function toGroup($notifiable) $workOrder->load(['module', 'user']); $module = $workOrder->module; + + if (!$workOrder->notify) { + return; + } + } else if ($notifiable instanceof Reply) { $view = 'notifications.work_order.reply'; @@ -61,6 +66,10 @@ public function toGroup($notifiable) $reply = $workOrder; $workOrder = $workOrder->workOrder; + if (!$workOrder->notify) { + return; + } + $module = $workOrder->module; } else { return; diff --git a/database/migrations/2023_01_02_201811_add_notify_to_work_orders_table.php b/database/migrations/2023_01_02_201811_add_notify_to_work_orders_table.php new file mode 100644 index 0000000..a88bc5c --- /dev/null +++ b/database/migrations/2023_01_02_201811_add_notify_to_work_orders_table.php @@ -0,0 +1,32 @@ +boolean('notify')->default(true)->comment('是否通知')->after('status'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('work_orders', function (Blueprint $table) { + $table->dropColumn('notify'); + }); + } +}; diff --git a/resources/views/admin/work-orders/edit.blade.php b/resources/views/admin/work-orders/edit.blade.php index 3d4a742..97d035b 100644 --- a/resources/views/admin/work-orders/edit.blade.php +++ b/resources/views/admin/work-orders/edit.blade.php @@ -20,14 +20,25 @@ + + + {{-- 启用通知 --}} +
+ +