diff --git a/app/AdminAgent/Controllers/WorkorderController.php b/app/AdminAgent/Controllers/WorkorderController.php index ffa9f72..6645bb7 100755 --- a/app/AdminAgent/Controllers/WorkorderController.php +++ b/app/AdminAgent/Controllers/WorkorderController.php @@ -10,6 +10,7 @@ use App\Models\Guide; use App\Models\OrderProductItem; use App\Models\Supplier; use App\Models\WorkorderItem; +use App\Service\Admin\GlobalNoticeService; use App\Traits\DemandTraits; use App\Traits\WorkorderTraits; use Dcat\Admin\Admin; @@ -85,7 +86,13 @@ class WorkorderController extends AdminController } // 更新已读未读,此处没有考虑性能代码解藕等问题 - WorkorderItem::query()->where('workorder_id',$workorderId)->update(['is_read' => 1]); + WorkorderItem::query() + ->where('workorder_id',$workorderId) + ->where([ + 'point_type' => DemandTraits::$col[0], + 'point_id' => Admin::user()->id, + ]) + ->update(['is_read' => 1]); $data = [ 'user_list' => $userList, @@ -94,12 +101,13 @@ class WorkorderController extends AdminController 'workorder' => [ 'workorder_id' => $workorderId ], + 'route' => 'admin-agent', ]; return $content ->header('工单') ->description('列表') - ->body(admin_view('admin.pages.index',$data)); + ->body(admin_view('admin.pages.workorder',$data)); } // ///** @@ -236,7 +244,12 @@ class WorkorderController extends AdminController $item->type = 1; $item->publisher_type = DemandTraits::$col[0]; $item->publisher_id = Admin::user()->id; + $item->point_type = $form->point_type; + $item->point_id = $form->point_id; $item->save(); + + // 缓存提示信息 + (new GlobalNoticeService())->cacheWOrderNotice($item->id); } }); }); @@ -266,16 +279,37 @@ class WorkorderController extends AdminController $path = $request->image->store('public/images/workorder'); $path = (Storage::url($path)); + + // 指定发送对象 + $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0)); + if (empty($workorder)) { + return $this->error('工单异常'); + } + + if ($workorder->publisher_type == DemandTraits::$col[0] && $workorder->publisher_id == Admin::user()->id) { + $pointType = $workorder->point_type; + $pointId = $workorder->point_id; + } else { + $pointType = $workorder->publisher_type; + $pointId = $workorder->publisher_id; + } + $item = new WorkorderItem(); $item->workorder_id = request('workorder_id',0); $item->content = $path; $item->type = 2; $item->publisher_type = DemandTraits::$col[0]; $item->publisher_id = Admin::user()->id; + $item->point_type = $pointType; + $item->point_id = $pointId; $item->save(); // 更新一下主表的时间做排序用 - \App\Models\Workorder::query()->where('id', $item->workorder_id)->update(['updated_at' => $item->updated_at]); + $workorder->updated_at = $item->updated_at; + $workorder->save(); + + // 缓存提示信息 + (new GlobalNoticeService())->cacheWOrderNotice($item->id); return $this->jsonSuccess($item); } @@ -293,22 +327,42 @@ class WorkorderController extends AdminController return $this->jsonFailValidated('数据不全:'.$validator->errors()->first()); } + // 指定发送对象 + $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0)); + if (empty($workorder)) { + return $this->error('工单异常'); + } + + if ($workorder->publisher_type == DemandTraits::$col[0] && $workorder->publisher_id == Admin::user()->id) { + $pointType = $workorder->point_type; + $pointId = $workorder->point_id; + } else { + $pointType = $workorder->publisher_type; + $pointId = $workorder->publisher_id; + } + $item = new WorkorderItem(); $item->workorder_id = request('workorder_id',0); $item->content = request('content',''); $item->type = 1; $item->publisher_type = DemandTraits::$col[0]; $item->publisher_id = Admin::user()->id; + $item->point_type = $pointType; + $item->point_id = $pointId; $item->save(); // 更新一下主表的时间做排序用 - \App\Models\Workorder::query()->where('id', $item->workorder_id)->update(['updated_at' => $item->updated_at]); + $workorder->updated_at = $item->updated_at; + $workorder->save(); + + // 缓存提示信息 + (new GlobalNoticeService())->cacheWOrderNotice($item->id); return $this->jsonSuccess($item); } public function checkUnread() { - + return (new GlobalNoticeService())->unreadWOrder(DemandTraits::$col[0]); } } diff --git a/app/AdminAgent/routes.php b/app/AdminAgent/routes.php index 986b917..97a63cb 100644 --- a/app/AdminAgent/routes.php +++ b/app/AdminAgent/routes.php @@ -49,11 +49,6 @@ Route::group([ $router->any('send_image', 'WorkorderController@sendImage'); $router->any('workorder_item', 'WorkorderController@index'); - -}); -Route::group([ - 'namespace' => config('admin.route.namespace'), - 'middleware' => config('admin.route.middleware'), -], function (Router $router) { $router->any('workorder_notice', 'WorkorderController@checkUnread'); + }); diff --git a/app/AdminGuide/Controllers/WorkorderController.php b/app/AdminGuide/Controllers/WorkorderController.php index d31b82b..f13a196 100755 --- a/app/AdminGuide/Controllers/WorkorderController.php +++ b/app/AdminGuide/Controllers/WorkorderController.php @@ -11,6 +11,7 @@ use App\Models\Guide; use App\Models\OrderProductItem; use App\Models\Supplier; use App\Models\WorkorderItem; +use App\Service\Admin\GlobalNoticeService; use App\Traits\DemandTraits; use App\Traits\WorkorderTraits; use Dcat\Admin\Admin; @@ -94,12 +95,13 @@ class WorkorderController extends AdminController 'workorder' => [ 'workorder_id' => $workorderId ], + 'route' => 'admin-guide', ]; return $content ->header('工单') ->description('列表') - ->body(admin_view('admin.pages.guide-index',$data)); + ->body(admin_view('admin.pages.workorder',$data)); } ///** @@ -207,7 +209,12 @@ class WorkorderController extends AdminController $item->type = 1; $item->publisher_type = DemandTraits::$col[2]; $item->publisher_id = Admin::user()->id; + $item->point_type = $form->point_type; + $item->point_id = $form->point_id; $item->save(); + + // 缓存提示信息 + (new GlobalNoticeService())->cacheWOrderNotice($item->id); } }); }); @@ -237,16 +244,36 @@ class WorkorderController extends AdminController $path = $request->image->store('public/images/workorder'); $path = (Storage::url($path)); + + $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0)); + if (empty($workorder)) { + return $this->error('工单异常'); + } + + if ($workorder->publisher_type == DemandTraits::$col[2] && $workorder->publisher_id == Admin::user()->id) { + $pointType = $workorder->point_type; + $pointId = $workorder->point_id; + } else { + $pointType = $workorder->publisher_type; + $pointId = $workorder->publisher_id; + } + $item = new WorkorderItem(); $item->workorder_id = request('workorder_id',0); $item->content = $path; $item->type = 2; $item->publisher_type = DemandTraits::$col[2]; $item->publisher_id = Admin::user()->id; + $item->point_type = $pointType; + $item->point_id = $pointId; $item->save(); // 更新一下主表的时间做排序用 - \App\Models\Workorder::query()->where('id', $item->workorder_id)->update(['updated_at' => $item->updated_at]); + $workorder->updated_at = $item->updated_at; + $workorder->save(); + + // 缓存提示信息 + (new GlobalNoticeService())->cacheWOrderNotice($item->id); return $this->jsonSuccess($item); } @@ -264,17 +291,41 @@ class WorkorderController extends AdminController return $this->jsonFailValidated('数据不全:'.$validator->errors()->first()); } + $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0)); + if (empty($workorder)) { + return $this->error('工单异常'); + } + + if ($workorder->publisher_type == DemandTraits::$col[2] && $workorder->publisher_id == Admin::user()->id) { + $pointType = $workorder->point_type; + $pointId = $workorder->point_id; + } else { + $pointType = $workorder->publisher_type; + $pointId = $workorder->publisher_id; + } + $item = new WorkorderItem(); $item->workorder_id = request('workorder_id',0); $item->content = request('content',''); $item->type = 1; $item->publisher_type = DemandTraits::$col[2]; $item->publisher_id = Admin::user()->id; + $item->point_type = $pointType; + $item->point_id = $pointId; $item->save(); // 更新一下主表的时间做排序用 - \App\Models\Workorder::query()->where('id', $item->workorder_id)->update(['updated_at' => $item->updated_at]); + $workorder->updated_at = $item->updated_at; + $workorder->save(); + + // 缓存提示信息 + (new GlobalNoticeService())->cacheWOrderNotice($item->id); return $this->jsonSuccess($item); } + + public function checkUnread() + { + return (new GlobalNoticeService())->unreadWOrder(DemandTraits::$col[2]); + } } diff --git a/app/AdminGuide/routes.php b/app/AdminGuide/routes.php index 2038891..76d5b1e 100644 --- a/app/AdminGuide/routes.php +++ b/app/AdminGuide/routes.php @@ -27,4 +27,5 @@ Route::group([ $router->any('send_image', 'WorkorderController@sendImage'); $router->any('workorder_item', 'WorkorderController@index'); + $router->any('workorder_notice', 'WorkorderController@checkUnread'); }); diff --git a/app/AdminSupplier/Controllers/WorkorderController.php b/app/AdminSupplier/Controllers/WorkorderController.php index 23740a1..90e6d37 100755 --- a/app/AdminSupplier/Controllers/WorkorderController.php +++ b/app/AdminSupplier/Controllers/WorkorderController.php @@ -10,6 +10,7 @@ use App\Models\Guide; use App\Models\OrderProductItem; use App\Models\Supplier; use App\Models\WorkorderItem; +use App\Service\Admin\GlobalNoticeService; use App\Traits\DemandTraits; use App\Traits\WorkorderTraits; use Dcat\Admin\Admin; @@ -90,12 +91,13 @@ class WorkorderController extends AdminController 'workorder' => [ 'workorder_id' => $workorderId ], + 'route' => 'admin-supplier', ]; return $content ->header('工单') ->description('列表') - ->body(admin_view('admin.pages.supplier-index',$data)); + ->body(admin_view('admin.pages.workorder',$data)); } ///** @@ -205,7 +207,12 @@ class WorkorderController extends AdminController $item->type = 1; $item->publisher_type = DemandTraits::$col[1]; $item->publisher_id = Admin::user()->id; + $item->point_type = $form->point_type; + $item->point_id = $form->point_id; $item->save(); + + // 缓存提示信息 + (new GlobalNoticeService())->cacheWOrderNotice($item->id); } }); }); @@ -233,6 +240,20 @@ class WorkorderController extends AdminController return $this->error('上传图片格式错误'); } + // 指定发送对象 + $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0)); + if (empty($workorder)) { + return $this->error('工单异常'); + } + + if ($workorder->publisher_type == DemandTraits::$col[1] && $workorder->publisher_id == Admin::user()->id) { + $pointType = $workorder->point_type; + $pointId = $workorder->point_id; + } else { + $pointType = $workorder->publisher_type; + $pointId = $workorder->publisher_id; + } + $path = $request->image->store('public/images/workorder'); $path = (Storage::url($path)); $item = new WorkorderItem(); @@ -241,10 +262,16 @@ class WorkorderController extends AdminController $item->type = 2; $item->publisher_type = DemandTraits::$col[1]; $item->publisher_id = Admin::user()->id; + $item->point_type = $pointType; + $item->point_id = $pointId; $item->save(); // 更新一下主表的时间做排序用 - \App\Models\Workorder::query()->where('id', $item->workorder_id)->update(['updated_at' => $item->updated_at]); + $workorder->updated_at = $item->updated_at; + $workorder->save(); + + // 缓存提示信息 + (new GlobalNoticeService())->cacheWOrderNotice($item->id); return $this->jsonSuccess($item); } @@ -262,17 +289,42 @@ class WorkorderController extends AdminController return $this->jsonFailValidated('数据不全:'.$validator->errors()->first()); } + // 指定发送对象 + $workorder = \App\Models\Workorder::query()->find(request('workorder_id',0)); + if (empty($workorder)) { + return $this->error('工单异常'); + } + + if ($workorder->publisher_type == DemandTraits::$col[1] && $workorder->publisher_id == Admin::user()->id) { + $pointType = $workorder->point_type; + $pointId = $workorder->point_id; + } else { + $pointType = $workorder->publisher_type; + $pointId = $workorder->publisher_id; + } + $item = new WorkorderItem(); $item->workorder_id = request('workorder_id',0); $item->content = request('content',''); $item->type = 1; $item->publisher_type = DemandTraits::$col[1]; $item->publisher_id = Admin::user()->id; + $item->point_type = $pointType; + $item->point_id = $pointId; $item->save(); // 更新一下主表的时间做排序用 - \App\Models\Workorder::query()->where('id', $item->workorder_id)->update(['updated_at' => $item->updated_at]); + $workorder->updated_at = $item->updated_at; + $workorder->save(); + + // 缓存提示信息 + (new GlobalNoticeService())->cacheWOrderNotice($item->id); return $this->jsonSuccess($item); } + + public function checkUnread() + { + return (new GlobalNoticeService())->unreadWOrder(DemandTraits::$col[1]); + } } diff --git a/app/AdminSupplier/routes.php b/app/AdminSupplier/routes.php index d6f5e40..313ba6b 100644 --- a/app/AdminSupplier/routes.php +++ b/app/AdminSupplier/routes.php @@ -37,4 +37,5 @@ Route::group([ $router->any('send_image', 'WorkorderController@sendImage'); $router->any('workorder_item', 'WorkorderController@index'); + $router->any('workorder_notice', 'WorkorderController@checkUnread'); }); diff --git a/app/Service/Admin/GlobalNoticeService.php b/app/Service/Admin/GlobalNoticeService.php new file mode 100644 index 0000000..e673d32 --- /dev/null +++ b/app/Service/Admin/GlobalNoticeService.php @@ -0,0 +1,47 @@ +has('workorder') + ->where(function ($query) use ($pointType) { + $query->where('point_id',Admin::user()->id) + ->where('point_type',$pointType); + }) + ->where('is_read', 0) + ->whereIn('id', $cacheNoticeIds) + ->pluck('id') + ->toArray(); + + $cacheNoticeIds = array_diff($cacheNoticeIds, $res); + Cache::forever('w_order_notice', json_encode($cacheNoticeIds)); + return empty($res) ? 0 : 1; + } + + public function cacheWOrderNotice($id) + { + $cacheNotice = Cache::get('w_order_notice', '[]'); + $cacheNoticeIds = json_decode($cacheNotice, true); + $cacheNoticeIds[] = $id; + Cache::forever('w_order_notice', json_encode($cacheNoticeIds)); + } +} diff --git a/database/migrations/2021_09_10_144727_update_workorder_table.php b/database/migrations/2021_09_10_144727_update_workorder_table.php index 28a3800..2108d31 100644 --- a/database/migrations/2021_09_10_144727_update_workorder_table.php +++ b/database/migrations/2021_09_10_144727_update_workorder_table.php @@ -20,6 +20,8 @@ class UpdateWorkorderTable extends Migration Schema::table('workorder_item', function (Blueprint $table) { $table->text('content')->nullable()->change(); $table->tinyInteger('is_read')->default(0)->comment('是否未读,1已读 0未读'); + $table->string('point_type')->default('')->comment('接收人类型'); + $table->integer('point_id')->comment('接收人id'); }); } @@ -37,6 +39,8 @@ class UpdateWorkorderTable extends Migration Schema::table('workorder_item', function (Blueprint $table) { $table->string('content')->change(); $table->dropColumn('is_read'); + $table->dropColumn('point_type'); + $table->dropColumn('point_id'); }); } } diff --git a/public/js/workorder-notice.js b/public/js/workorder-notice.js index 91c2ac4..378911b 100644 --- a/public/js/workorder-notice.js +++ b/public/js/workorder-notice.js @@ -1,22 +1,28 @@ -Dcat.ready(function () { +const workordertimer = setInterval(function () { - // const workorderNoticeTimer = setInterval(function () { - // - // $.ajax({ - // url: '/workorder_notice', - // type: 'POST', - // data: {}, - // dataType: 'json', - // success: function (res) { - // Dcat.info('您有新的工单信息', null, { - // timeOut: 5000, // 5秒后自动消失 - // }); - // }, - // error: function (error) { - // - // } - // }); - // - // }, 2000); + $.ajax({ + url: '/'+window.location.pathname.split('/')[1]+'/workorder_notice', + type: 'POST', + data: {}, + dataType: 'text', + success: function (res) { + if (res == true) { + Dcat.info("你有新的工单信息 [前往查看]", '新消息提醒', { + closeButton: true, + escapeHtml: false, + timeOut: 10000, + extendedTimeOut: 5000, + onHidden: function () { + if (window.location.pathname == '/'+window.location.pathname.split('/')[1]+'/workorder_item') { + location.reload() + } + } + }) + } + }, + error: function (error) { -}); + } + }); + +}, 5000); diff --git a/resources/views/admin/pages/index.blade.php b/resources/views/admin/pages/workorder.blade.php similarity index 96% rename from resources/views/admin/pages/index.blade.php rename to resources/views/admin/pages/workorder.blade.php index 597e4e4..7b9c4dc 100644 --- a/resources/views/admin/pages/index.blade.php +++ b/resources/views/admin/pages/workorder.blade.php @@ -135,9 +135,6 @@
{{$item['created_at_text']}} - @if($item['is_read'] == 0) - [ 新消息 ] - @endif
@@ -215,7 +212,7 @@ formData.append("image", $(this)[0].files[0]) formData.append("workorder_id", $('input[name=workorder-id]').val()) $.ajax({ - url: '/admin-agent/send_image', + url: '/{{$route}}/send_image', type: 'POST', data: formData, processData: false, @@ -244,16 +241,27 @@ }); $('.msg-text').on('click', function (e) { - let msgText = $('input[name=msg-input]').val() let workorderId = $('input[name=workorder-id]').val() + if (msgText == '') { + $('input[name=msg-input]').css('border-color', 'red').focus(); + setTimeout(function () { + $('input[name=msg-input]').css('border-color', '#dbe3e6'); + }, 3000) + } + $.ajax({ - url: '/admin-agent/send_text', + url: '/{{$route}}/send_text', type: 'POST', data: {content: msgText, workorder_id: workorderId}, dataType: 'json', success: function(res) { + + if (res.code != 0) { + return false; + } + let msgText = res.data.content let msgTime = res.data.created_at let templateHtml = $('.template-my').html()