checkfacility.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace app\common\middleware;
  3. use app\common\model\FacilityModel;
  4. use app\common\model\Sysusermodel;
  5. class checkfacility
  6. {
  7. public function handle($request, \Closure $next)
  8. {
  9. if (!request()->isPost()) {
  10. return backjson3(500, "请求错误");
  11. }
  12. $param = $request->post();
  13. $token = request()->header('token');
  14. $suinfo = $this->checktoken($token);
  15. if (empty($suinfo)) {
  16. return backjson3(401, "认证失败,无法访问系统资源");
  17. }
  18. $finfo = $this->checkfilleData($param);
  19. if (empty($finfo)) {
  20. return backjson3(500, "设备信息错误");
  21. }
  22. $request->finfo = $finfo;
  23. $request->suinfo = $suinfo;
  24. return $next($request);
  25. }
  26. /**
  27. * 校验必填字段
  28. *
  29. * @return void
  30. * @author wj
  31. * @date 2023-09-02
  32. */
  33. private function checkfilleData($param)
  34. {
  35. $fillfields = ['facility_id', 'device_id_code', 'command'];
  36. foreach ($fillfields as $key => $value) {
  37. if (!isset($param[$value]) || empty($param[$value])) {
  38. return false;
  39. }
  40. }
  41. $facility_id = $param['facility_id'];
  42. $device_id_code = $param['device_id_code'];
  43. $m_f = new FacilityModel();
  44. $where = ['id' => $facility_id, 'code' => $device_id_code];
  45. $finfo = $m_f->getInfo($where, ['id']);
  46. if (empty($finfo)) {
  47. return false;
  48. }
  49. return $finfo;
  50. }
  51. /**
  52. * 验证用户token
  53. *
  54. * @return void
  55. * @author wj
  56. * @date 2023-09-04
  57. */
  58. private function checktoken($token)
  59. {
  60. if (empty($token)) {
  61. return false;
  62. }
  63. $m_su = new Sysusermodel();
  64. $suinfo = $m_su->getinfobytoken($token);
  65. if (empty($suinfo)) {
  66. return false;
  67. }
  68. return $suinfo;
  69. }
  70. }