behavior = $behavior; //初始化virus域名 $this->client = new Client([ 'base_uri' => config('constants.VIRUS_URL'), 'timeout' => 3 ]); } public function index($request) { try { $signKey = [ 'app' => config('constants.VIRUS_APP_ID') ]; ksort($signKey); $signKey = urldecode(http_build_query($signKey)); $sign = md5(config('constants.VIRUS_APP_SECRET') . $signKey); $response = $this->client->request('GET', 'behaviorList', [ 'headers' => [ 'Content-Type' => 'application/json', 'app' => config('constants.VIRUS_APP_ID'), 'sign' => $sign ] ]); $res = json_decode($response->getBody()->getContents(), true); $res = $res ? $res['behaviors'] : []; }catch (RequestException $exception){ $res = [ 'returnCode' => '-1', 'errMsg' => '网络超时'.$exception->getMessage() ]; } //已登记行为 $registered_bahavior = $this->behavior->get(); $registered_bahaviors = $registered_bahavior->toArray(); foreach ($registered_bahaviors as $key=>$val){ $registered_bahaviors[$key]['behavior_status'] = 1;//已登记 } $virus_behavior_id = array_column($registered_bahaviors,'virus_behavior_id'); $new_res = []; //行为列表去重 foreach ($res as $k=>$v) { if (isset($v['_id']) && !empty($virus_behavior_id)){ $res[$k]['behavior_status'] = 0;//未登记 if (in_array($v['_id'], $virus_behavior_id)) { unset($res[$k]); } $new_res = array_merge($res, $registered_bahaviors); }elseif (!isset($v['_id']) && $virus_behavior_id) { $new_res = $registered_bahaviors; }elseif (isset($v['_id']) && empty($virus_behavior_id)){ $res[$k]['behavior_status'] = 0;//未登记 $new_res = $res; } } $new_re = []; //根据行为ID筛选 if (isset($request['virus_behavior_id'])){ foreach ($new_res as $k=>$v){ if (isset($v['virus_behavior_id']) && $v['virus_behavior_id'] != $request['virus_behavior_id']){ unset($new_res[$k]); $new_re = array_merge($new_res); }elseif (isset($v['_id']) && $v['_id'] != $request['virus_behavior_id']){ unset($new_res[$k]); $new_re = array_merge($new_res); } } return $new_re; } //根据行为状态(类型)筛选 if (isset($request['behavior_status'])){ foreach ($new_res as $kk=>$vv){ if (isset($vv['behavior_status']) && $vv['behavior_status'] != $request['behavior_status']){ unset($new_res[$kk]); $new_re = array_merge($new_res); } } return $new_re; } //根据绑定的动作ID筛选 if (isset($request['behavior_action_id'])){ foreach ($new_res as $key=>$val) { if (isset($val['behavior_action_id']) && ($val['behavior_action_id'] != $request['behavior_action_id'])) { unset($new_res[$key]); $new_re = array_merge($new_res); } elseif (!isset($val['behavior_action_id'])) { unset($new_res[$key]); $new_re = array_merge($new_res); } } return $new_re; } //根据行为名称筛选 if (isset($request['name'])){ foreach ($new_res as $key => $val) { if (isset($val['name']) && strpos($val['name'],$request['name']) !== false) { $new_re[] = $new_res[$key]; } if (isset($val['behavior_name']) && strpos($val['behavior_name'],$request['name']) !== false ) { $new_re[] = $new_res[$key]; } } return $new_re; } return $new_res; } public function create($request) { $behavior_name = $this->behavior->where(['name'=>$request['name']])->first(); if($behavior_name){ return Response::create([ 'message' => '该行为已存在', 'status_code' => 500 ]); } $data = [ 'virus_behavior_id' => $request['virus_behavior_id'], 'name' => $request['name'], 'behavior_level' => $request['behavior_level'], 'behavior_cycle_type' => $request['behavior_cycle_type'], 'is_open' => 0, 'behavior_action_id' => isset($request['behavior_action_id']) ? $request['behavior_action_id'] : 0, 'behavior_cycle' => isset($request['behavior_cycle']) ? $request['behavior_cycle'] : '', 'behavior_binding_users' => isset($request['behavior_binding_users']) ? $request['behavior_binding_users'] : 0, 'physical_strength' => isset($request['physical_strength']) ? $request['physical_strength'] : '', 'rainbow_beans' => isset($request['rainbow_beans']) ? $request['rainbow_beans'] : '', 'remarks' => isset($request['remarks']) ? $request['remarks'] : '', 'behavioral_cycle_start_time' => isset($request['behavioral_cycle_start_time']) ? $request['behavioral_cycle_start_time'] : null, 'behavioral_cycle_end_time' => isset($request['behavioral_cycle_end_time']) ? $request['behavioral_cycle_end_time'] : null, 'allotted_quantity_rule' => isset($request['allotted_quantity_rule']) ? json_encode($request['allotted_quantity_rule']) : '', 'behavior_identification' => isset($request['behavior_identification']) ? $request['behavior_identification'] : '', 'trigger_times' => isset($request['trigger_times']) ? $request['trigger_times'] : 0, 'effective_trigger' => isset($request['effective_trigger']) ? $request['effective_trigger'] : 0, 'relative_series' => isset($request['relative_series']) ? $request['relative_series'] : 0, 'absolute_progression' => isset($request['absolute_progression']) ? $request['absolute_progression'] : 0, ]; if (!$this->behavior->create($data)) { throw new HttpException(500, '添加失败,请重试'); } } }