|
@@ -0,0 +1,96 @@
|
|
|
+<?php
|
|
|
+declare (strict_types = 1);
|
|
|
+
|
|
|
+namespace app\bug\listener;
|
|
|
+
|
|
|
+use app\admin\model\Role;
|
|
|
+use app\bug\model\Work;
|
|
|
+use app\bug\model\WorkAction;
|
|
|
+use app\bug\model\WorkRole;
|
|
|
+use think\facade\Log;
|
|
|
+class RoleEvent
|
|
|
+{
|
|
|
+ private $model;
|
|
|
+ private $type=["on"=>"启用","off"=>"禁用","delete"=>"删除"];
|
|
|
+ /**
|
|
|
+ * 事件监听处理
|
|
|
+ * @param $event
|
|
|
+ * @param $type
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function handle($event)
|
|
|
+ {
|
|
|
+ // 验证输入是否存在且类型正确
|
|
|
+ if (!isset($event['type'], $event['info_id'],$this->type[$event['type']]) || !is_string($event['type']) || !is_int
|
|
|
+ ($event['info_id'])) {
|
|
|
+ throw new \InvalidArgumentException('Invalid event data.');
|
|
|
+ }
|
|
|
+
|
|
|
+ $type=$event["type"];
|
|
|
+ $id=$event["info_id"];
|
|
|
+ try {
|
|
|
+ $info = WorkRole::where('id', $id)->findOrEmpty();
|
|
|
+
|
|
|
+ if ($info->isEmpty()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用switch-case优化多分支逻辑
|
|
|
+ switch ($info->belong) {
|
|
|
+ case 1:
|
|
|
+ $this->model = new Role();
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ $this->model = new \app\cxinv\model\Role();
|
|
|
+ break;
|
|
|
+ case 3: //修改为报表
|
|
|
+ $this->model = new \app\bug\model\Role();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ // 处理未知的belong值
|
|
|
+ throw new \Exception('Unsupported belong value.');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用方法常量代替硬编码字符串
|
|
|
+ $result = $this->{"role{$type}"}($type === 'off' || $type === 'delete' ? $info->belong_role_id : $info);
|
|
|
+
|
|
|
+ if ($result === false) {
|
|
|
+ $date = date('Y-m-d H:i:s');
|
|
|
+ $sys = Work::$blongCn[$info->belong];
|
|
|
+ $typeCn =$this->type[$type];
|
|
|
+ Log::error("[$date]: {$sys} {$info->role_name}角色信息{$typeCn}失败");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }catch (\Exception $e) {
|
|
|
+ // 日志记录异常信息
|
|
|
+ Log::error('Error handling event: ' . $e->getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function roleOn($event){
|
|
|
+ $action = (new WorkAction)->GetBelongActionByIdArrs($event->action);
|
|
|
+ $info=$this->model->where('id',$event->belong_role_id)->findOrEmpty();
|
|
|
+ $info->role_name = $event->role_name;
|
|
|
+ $info->role_level = $event->level;
|
|
|
+ $info->companyNo = $event->companyNo;
|
|
|
+ $info->status = 1;
|
|
|
+ $isSave = $info->together(['RoleInfo'=>['action_conllect'=>$action,
|
|
|
+ 'action_data'=>[],
|
|
|
+ 'private_data'=>[],
|
|
|
+ 'status'=>1]])->save();
|
|
|
+ echo $info->getLastSql();
|
|
|
+ return $isSave;
|
|
|
+ }
|
|
|
+ private function roleOff($eventId){
|
|
|
+ $info=$this->model->where('id',$eventId)->findOrEmpty();
|
|
|
+ $info->status = 0;
|
|
|
+ $isSave = $info->save();
|
|
|
+ return $isSave;
|
|
|
+ }
|
|
|
+ private function roleDelete($eventId){
|
|
|
+ $info=$this->model->where('id',$eventId)->findOrEmpty();
|
|
|
+ $isSave = $info->together(['RoleInfo'])->delete();
|
|
|
+ return $isSave;
|
|
|
+ }
|
|
|
+}
|