FkExceptionObjects.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Request;
  6. class FkExceptionObjects extends Controller
  7. {
  8. /**
  9. * 异常对象列表页面
  10. */
  11. public function index()
  12. {
  13. if ($this->request->isAjax()) {
  14. $page = input('get.page', 1);
  15. $limit = input('get.limit', 10);
  16. $where = [];
  17. // 搜索条件
  18. $exceptionType = input('get.exception_type', '');
  19. if ($exceptionType) {
  20. $where['exception_type'] = $exceptionType;
  21. }
  22. $exceptionValue = input('get.exception_value', '');
  23. if ($exceptionValue) {
  24. $where['exception_value'] = ['like', "%{$exceptionValue}%"];
  25. }
  26. $status = input('get.status', '');
  27. if ($status !== '') {
  28. $where['status'] = $status;
  29. }
  30. $count = Db::name('fk_exception_objects')->where($where)->count();
  31. $data = Db::name('fk_exception_objects')
  32. ->where($where)
  33. ->page($page, $limit)
  34. ->order('object_id desc')
  35. ->select();
  36. foreach($data as $k=>&$v){
  37. if($v['exception_log']){
  38. $v['exception_log'] = json_decode($v['exception_log'], true);
  39. $v['exception_log_by_name'] = implode(';', array_column($v['exception_log'], 'rule_name'));
  40. }
  41. }
  42. return json([
  43. 'code' => 200,
  44. 'msg' => '',
  45. 'count' => $count,
  46. 'data' => $data
  47. ]);
  48. }
  49. return $this->fetch();
  50. }
  51. /**
  52. * 更新状态
  53. */
  54. public function updateStatus()
  55. {
  56. $id = input('post.id');
  57. $status = input('post.status');
  58. if (!$id || !in_array($status, [1, 2, 3, 4])) {
  59. return json(['code' => -100, 'msg' => '参数错误']);
  60. }
  61. try {
  62. Db::name('fk_exception_objects')->where('object_id', $id)->update(['status' => $status]);
  63. return json(['code' => 200, 'msg' => '状态更新成功']);
  64. } catch (\Exception $e) {
  65. return json(['code' => -100, 'msg' => '更新失败:' . $e->getMessage()]);
  66. }
  67. }
  68. /**
  69. * 添加异常对象
  70. */
  71. public function add()
  72. {
  73. if ($this->request->isPost()) {
  74. $data = [
  75. 'exception_type' => input('post.exception_type'),
  76. 'exception_value' => input('post.exception_value'),
  77. 'exception_degree_total' => 0,
  78. 'status' => input('post.status', 1),
  79. 'remark' => ''
  80. ];
  81. // 验证
  82. if (empty($data['exception_type']) || empty($data['exception_value'])) {
  83. return json(['code' => -100, 'msg' => '对象类型和对象值不能为空']);
  84. }
  85. // 检查是否已存在
  86. $exists = Db::name('fk_exception_objects')
  87. ->where('exception_type', $data['exception_type'])
  88. ->where('exception_value', $data['exception_value'])
  89. ->find();
  90. if ($exists) {
  91. return json(['code' => -100, 'msg' => '该异常对象已存在']);
  92. }
  93. try {
  94. Db::name('fk_exception_objects')->insert($data);
  95. return json(['code' => 200, 'msg' => '添加成功']);
  96. } catch (\Exception $e) {
  97. return json(['code' => -100, 'msg' => '添加失败:' . $e->getMessage()]);
  98. }
  99. }
  100. return $this->fetch();
  101. }
  102. /**
  103. * 编辑异常对象
  104. */
  105. public function edit($id = null)
  106. {
  107. if ($this->request->isGet() && $id) {
  108. $info = Db::name('fk_exception_objects')->where('object_id', $id)->find();
  109. if (!$info) {
  110. return json(['code' => -100, 'msg' => '异常对象不存在']);
  111. }
  112. return json(['code' => 200, 'data' => $info]);
  113. }
  114. if ($this->request->isPost()) {
  115. $id = input('post.object_id');
  116. $data = [
  117. 'exception_type' => input('post.exception_type'),
  118. 'exception_value' => input('post.exception_value'),
  119. 'status' => input('post.status', 1)
  120. ];
  121. // 验证
  122. if (empty($data['exception_type']) || empty($data['exception_value'])) {
  123. return json(['code' => -100, 'msg' => '对象类型和对象值不能为空']);
  124. }
  125. // 检查是否已存在(排除自己)
  126. $exists = Db::name('fk_exception_objects')
  127. ->where('exception_type', $data['exception_type'])
  128. ->where('exception_value', $data['exception_value'])
  129. ->where('object_id', '<>', $id)
  130. ->find();
  131. if ($exists) {
  132. return json(['code' => -100, 'msg' => '该异常对象已存在']);
  133. }
  134. try {
  135. Db::name('fk_exception_objects')->where('object_id', $id)->update($data);
  136. return json(['code' => 200, 'msg' => '修改成功']);
  137. } catch (\Exception $e) {
  138. return json(['code' => -100, 'msg' => '修改失败:' . $e->getMessage()]);
  139. }
  140. }
  141. }
  142. /**
  143. * 直接封禁异常对象
  144. */
  145. public function blockObject()
  146. {
  147. $objectId = input('post.object_id');
  148. $exceptionType = input('post.exception_type');
  149. $exceptionValue = input('post.exception_value');
  150. if (!$objectId || !$exceptionType || !$exceptionValue) {
  151. return json(['code' => -100, 'msg' => '参数错误']);
  152. }
  153. Db::startTrans();
  154. try {
  155. // 1. 更新异常对象状态为手动封禁(status=4)
  156. Db::name('fk_exception_objects')
  157. ->where('object_id', $objectId)
  158. ->update([
  159. 'status' => 4,
  160. 'updated_at' => date('Y-m-d H:i:s')
  161. ]);
  162. // 2. 检查是否已存在黑名单记录
  163. $existsBlack = Db::name('fk_roster_lists')
  164. ->where([
  165. 'object_type' => $exceptionType,
  166. 'object_value' => $exceptionValue,
  167. 'list_type' => 'black'
  168. ])
  169. ->find();
  170. if (!$existsBlack) {
  171. // 3. 添加到黑名单
  172. Db::name('fk_roster_lists')->insert([
  173. 'list_type' => 'black',
  174. 'object_type' => $exceptionType,
  175. 'object_value' => $exceptionValue,
  176. 'remark' => '来自异常对象直接封禁操作',
  177. 'created_at' => date('Y-m-d H:i:s'),
  178. ]);
  179. }
  180. // 4. 如果存在白名单记录,删除它
  181. Db::name('fk_roster_lists')
  182. ->where([
  183. 'object_type' => $exceptionType,
  184. 'object_value' => $exceptionValue,
  185. 'list_type' => 'white'
  186. ])
  187. ->delete();
  188. // 批量更新异常记录表,status=2
  189. $res = Db::name('fk_exception_records')
  190. ->where([
  191. 'exception_type' => $exceptionType,
  192. 'exception_value' => $exceptionValue
  193. ])
  194. ->update([
  195. 'status' => 2,
  196. 'updated_at' => date('Y-m-d H:i:s')
  197. ]);
  198. Db::commit();
  199. return json(['code' => 200, 'msg' => '封禁成功,已添加到黑名单']);
  200. } catch (\Exception $e) {
  201. Db::rollback();
  202. return json(['code' => -100, 'msg' => '封禁失败:' . $e->getMessage().' - '.$e->getFile().':'.$e->getLine()]);
  203. }
  204. }
  205. /**
  206. * 移除异常(加入白名单)
  207. * 1. 直接删除此条记录,并增加白名单。
  208. */
  209. public function whitelistObject()
  210. {
  211. $objectId = input('post.object_id');
  212. $exceptionType = input('post.exception_type');
  213. $exceptionValue = input('post.exception_value');
  214. if (!$objectId || !$exceptionType || !$exceptionValue) {
  215. return json(['code' => -100, 'msg' => '参数错误']);
  216. }
  217. Db::startTrans();
  218. try {
  219. // 1. 更新异常对象状态为正常(status=1)
  220. Db::name('fk_exception_objects')
  221. ->where('object_id', $objectId)
  222. ->delete();
  223. // 2. 检查是否已存在白名单记录
  224. $existsWhite = Db::name('fk_roster_lists')
  225. ->where([
  226. 'object_type' => $exceptionType,
  227. 'object_value' => $exceptionValue,
  228. 'list_type' => 'white'
  229. ])
  230. ->find();
  231. if (!$existsWhite) {
  232. // 3. 添加到白名单
  233. Db::name('fk_roster_lists')->insert([
  234. 'list_type' => 'white',
  235. 'object_type' => $exceptionType,
  236. 'object_value' => $exceptionValue,
  237. 'remark' => '来自异常对象移除异常操作',
  238. 'created_at' => date('Y-m-d H:i:s'),
  239. ]);
  240. }
  241. // 4. 如果存在黑名单记录,删除它
  242. Db::name('fk_roster_lists')
  243. ->where([
  244. 'object_type' => $exceptionType,
  245. 'object_value' => $exceptionValue,
  246. 'list_type' => 'black'
  247. ])
  248. ->delete();
  249. // 批量更新异常记录表,status=2
  250. Db::name('fk_exception_records')
  251. ->where([
  252. 'exception_type' => $exceptionType,
  253. 'exception_value' => $exceptionValue
  254. ])
  255. ->update([
  256. 'status' => 2,
  257. 'updated_at' => date('Y-m-d H:i:s')
  258. ]);
  259. Db::commit();
  260. return json(['code' => 200, 'msg' => '移除异常成功,已添加到白名单']);
  261. } catch (\Exception $e) {
  262. Db::rollback();
  263. return json(['code' => -100, 'msg' => '操作失败:' . $e->getMessage()]);
  264. }
  265. }
  266. /**
  267. * 删除异常对象
  268. */
  269. public function delete()
  270. {
  271. $id = input('post.id');
  272. if (!$id) {
  273. return json(['code' => -100, 'msg' => '参数错误']);
  274. }
  275. try {
  276. Db::name('fk_exception_objects')->where('object_id', $id)->delete();
  277. return json(['code' => 200, 'msg' => '删除成功']);
  278. } catch (\Exception $e) {
  279. return json(['code' => -100, 'msg' => '删除失败:' . $e->getMessage()]);
  280. }
  281. }
  282. /**
  283. * 查看详情
  284. */
  285. public function detail($id)
  286. {
  287. $info = Db::name('fk_exception_objects')->where('object_id', $id)->find();
  288. if (!$info) {
  289. $this->error('异常对象不存在');
  290. }
  291. // 获取相关异常记录
  292. $records = Db::name('fk_exception_records')
  293. ->where('exception_type', $info['exception_type'])
  294. ->where('exception_value', $info['exception_value'])
  295. ->order('record_id desc')
  296. ->limit(20)
  297. ->select();
  298. $this->assign('info', $info);
  299. $this->assign('records', $records);
  300. return $this->fetch();
  301. }
  302. }