checkfacility.php 2.0 KB

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