TdwyController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qiuzijian
  5. * Date: 3/17/22
  6. * Time: 7:01 PM
  7. */
  8. namespace Modules\Camera\Http\Controllers\Api;
  9. use App\Http\Controllers\Api\BaseController;
  10. use Illuminate\Filesystem\Cache;
  11. use Illuminate\Support\Facades\Input;
  12. use Modules\Camera\Enum\CameraEnum;
  13. class TdwyController extends BaseController
  14. {
  15. protected $pre_url; // 请求地址
  16. protected $username; // 请求username
  17. protected $password; // 请求password
  18. protected $sysId; // 请求sysId
  19. protected $token; // 请求sysId
  20. public function __construct()
  21. {
  22. $this->pre_url = Input::get('url');
  23. $this->username = Input::get('username');
  24. $this->password = Input::get('password');
  25. $this->sysId = Input::get('sysId');
  26. //先获取token
  27. $this->token = $this->loginUser();
  28. }
  29. //登录获取token
  30. protected function loginUser()
  31. {
  32. $url = '/pangu/sdkServer/user/loginUser';
  33. $params = [
  34. 'username' => $this->username,
  35. 'password' => $this->password,
  36. 'sysId' => $this->sysId,
  37. ];
  38. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  39. $result = json_decode($result, true);
  40. $token = '';
  41. if (Cache::has(CameraEnum::TDWY_TOKEN_CACHE)) {
  42. $token = Cache::get(CameraEnum::TDWY_TOKEN_CACHE);
  43. return $token;
  44. }
  45. if ($result['statusCode'] == CameraEnum::TDWY_API_STATUS_CODE_SUCCSS && $result['content']) {
  46. $token = $result['content']['token'];
  47. Cache::put(CameraEnum::TDWY_TOKEN_CACHE, $token, 1440);
  48. }
  49. return $token;
  50. }
  51. //查询设备列表
  52. public function queryDeviceList()
  53. {
  54. $url = '/pangu/sdkServer/device/queryDeviceList';
  55. $sOrgId = Input::get('sOrgId', '');
  56. $iDeviceTypes = Input::get('iDeviceTypes', [5]);
  57. $rootCodes = Input::get('rootCodes', ["BH-0001"]);
  58. $isOrgTree = Input::get('isOrgTree', true);
  59. $orgTypeIds = Input::get('orgTypeIds', []);
  60. $needPage = Input::get('needPage', true);
  61. $pageSize = Input::get('pageSize', 50);
  62. $currentPage = Input::get('currentPage', 1);
  63. $params = [
  64. 'sOrgId' => $sOrgId,
  65. 'iDeviceTypes' => $iDeviceTypes,
  66. 'rootCodes' => $rootCodes,
  67. 'isOrgTree' => $isOrgTree,
  68. 'orgTypeIds' => $orgTypeIds,
  69. 'needPage' => $needPage,
  70. 'pageSize' => $pageSize,
  71. 'currentPage' => $currentPage,
  72. ];
  73. $result = $this->curlPost($this->pre_url . $url, json_encode($params));
  74. $result = json_decode($result, true);
  75. return $result;
  76. }
  77. /**
  78. * 发送请求
  79. * @param string $url
  80. * @param string $postData
  81. * @return bool|string
  82. */
  83. public function curlPost($url = '', $postData = '')
  84. {
  85. if (is_array($postData)) {
  86. $postData = http_build_query($postData);
  87. }
  88. $ch = curl_init();
  89. curl_setopt($ch, CURLOPT_URL, $url);
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  91. curl_setopt($ch, CURLOPT_POST, 1);
  92. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  93. curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  94. curl_setopt_array($ch, array(
  95. CURLOPT_HTTPHEADER => array(
  96. "Accept:" . '*/*',
  97. "Content-Type:" . 'application/json',
  98. "token:" . $this->token,
  99. )
  100. ));
  101. //https请求 不验证证书和host
  102. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  103. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  104. $data = curl_exec($ch);
  105. curl_close($ch);
  106. return $data;
  107. }
  108. }