Kaynağa Gözat

新增行为管理接口

durong 5 yıl önce
ebeveyn
işleme
9870e5ede8

+ 14 - 0
app/Http/Controllers/Behavior/BehaviorController.php

@@ -69,6 +69,20 @@ class BehaviorController extends Controller
 
     }
 
+    //行为管理
+    public function editStatus(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:behavior',
+            'is_open' => ['required', Rule::in(0,1)],
+        ]);
+
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->behaviorRepository->editStatus($request->all());
+    }
+
 
 
 }

+ 44 - 2
app/Repositories/Behavior/BehaviorRepository.php

@@ -172,7 +172,7 @@ class BehaviorRepository
             if ($res) {
                 $cerate_bahavior_data = [
                     'operator_id' => Auth::user()->id,
-                    'behavior_id' => $res->virus_behavior_id,
+                    'behavior_id' => $res['id'],
                     'username' => Auth::user()->username,
                     'content' => json_encode($data),
                     'type' => 0,
@@ -231,7 +231,7 @@ class BehaviorRepository
                 if ($res) {
                     $cerate_bahavior_data = [
                         'operator_id' => Auth::user()->id,
-                        'behavior_id' => $request['virus_behavior_id'],
+                        'behavior_id' => $request['id'],
                         'username' => Auth::user()->username,
                         'content' => json_encode($update_bahavior),
                         'type' => 1,
@@ -256,5 +256,47 @@ class BehaviorRepository
                 ]);
             }
     }
+
+    public function editStatus($request)
+    {
+        $behavior = $this->behavior->find($request['id']);
+
+        $behavior_array = [
+            'is_open' => $request['is_open'],
+            'updated_at' => date('Y-m-d H:i:s')
+        ];
+
+        DB::beginTransaction();
+        try {
+            $res = $behavior->where('id',$request['id'])->update($behavior_array);
+            if ($res) {
+                $cerate_bahavior_data = [
+                    'operator_id' => Auth::user()->id,
+                    'behavior_id' => $request['id'],
+                    'username' => Auth::user()->username,
+                    'content' => json_encode($behavior_array),
+                    'type' => 1,
+                    'created_at' => date('Y-m-d H:i:s'),
+                    'updated_at' => date('Y-m-d H:i:s')
+                ];
+                $result = $this->behaviorOperationLog->insert($cerate_bahavior_data);
+                if (!$result) {
+                    throw new HttpException(500, '操作日志记录失败');
+                }
+            }
+            DB::commit();
+            return Response::create();
+
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            Log::debug('修改状态行为:' . $exception->getMessage());
+            return Response::create([
+                'message' => '修改状态失败,请重试',
+                'error' => $exception->getMessage(),
+                'status_code' => 500
+            ]);
+        }
+
+    }
 }
 

+ 12 - 9
routes/api.php

@@ -84,14 +84,17 @@ $api->version('v1', [
             $api->put('topic/topicSetStatus', 'TopicController@setStatus');
         });
 
+        //行为
+        $api->group(['namespace' => 'Behavior'], function ($api) {
+            //行为列表
+            $api->get('behavior/list', 'BehaviorController@index');
+            //登记/注册行为
+            $api->post('behavior/create', 'BehaviorController@create');
+            //编辑行为
+            $api->put('behavior/edit', 'BehaviorController@edit');
+            //列表修改行为状态(行为管理)
+            $api->post('behavior/editStatus', 'BehaviorController@editStatus');
+        });
     });
-    //行为
-    $api->group(['namespace' => 'Behavior'], function ($api) {
-        //行为列表
-        $api->get('behavior/list', 'BehaviorController@index');
-        //登记/注册行为
-        $api->post('behavior/create', 'BehaviorController@create');
-        //编辑行为
-        $api->put('behavior/edit', 'BehaviorController@edit');
-    });
+
 });