0
0

Api.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace app\watch\controller;
  3. use think\App;
  4. use think\facade\Log;
  5. class Api extends Base {
  6. protected $testUrl = 'http://120.24.56.48:8889';
  7. protected $produceUrl = 'http://api.aiqiangua.com:8888';
  8. protected $companyName = '京宜安科技';
  9. protected $userName = '13838379499';
  10. protected $pwd = 'hzd2021';
  11. protected $imeI = '863204050000136';
  12. protected $communityId = '609b36c615f7164e6ff56246';
  13. public function __construct(App $app) {
  14. parent::__construct($app);
  15. }
  16. /**
  17. * 查看人员信息
  18. *
  19. * @author wst
  20. * @date 2021/5/31 17:10
  21. */
  22. public function getPerson($personId){
  23. $url = $this->produceUrl . '/api/person/'.$personId;
  24. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  25. return $data;
  26. }
  27. /**
  28. * 查看指定设备
  29. *
  30. * @author wst
  31. * @date 2021/5/31 14:06
  32. */
  33. public function getDevice($deviceId){
  34. $url = $this->produceUrl . '/api/device/'.$deviceId;
  35. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  36. return $data;
  37. }
  38. /**
  39. * 批量获取设备在离线状态//列表
  40. *
  41. * @author wst
  42. * @date 2021/5/31 17:43
  43. */
  44. public function community($page=1,$pageSize=20){
  45. $url = $this->produceUrl . '/api/community/'.$this->communityId.'/devicesloc_new?rows_per_page='.$pageSize.'&page='.$page;
  46. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  47. return $data;
  48. }
  49. public function t(){
  50. echo "<pre/>";
  51. echo nDate(1620814184843);
  52. die;
  53. print_r($this->community());
  54. die;
  55. }
  56. /**
  57. * 修改设备
  58. *
  59. * @author wst
  60. * @date 2021/5/31 14:44
  61. */
  62. public function editDevice($deviceId,$params){
  63. $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/edit?'.http_build_query($params);
  64. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  65. return $data;
  66. }
  67. /**
  68. * 设置提醒
  69. *
  70. * @author wst
  71. * @date 2021/5/31 15:09
  72. * @alertId 提醒编号 设备详情->alert->seqid 字段值
  73. */
  74. public function deviceAlert($deviceId,$alertId,$params){
  75. $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/alerts/'.$alertId.'?'.http_build_query($params);
  76. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  77. return $data;
  78. }
  79. /**
  80. * 设置亲情号码
  81. *
  82. * @author wst
  83. * @date 2021/5/31 15:31
  84. * @sos_numbers 亲情编号 设备详情->sos_numbers->seqid 字段值
  85. */
  86. public function deviceSosNumbers($deviceId,$sos_numbers,$params){
  87. $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/sos_numbers/'.$sos_numbers.'?'.http_build_query($params);
  88. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  89. return $data;
  90. }
  91. /**
  92. * 设置安全区域
  93. *
  94. * @author wst
  95. * @date 2021/5/31 15:34
  96. */
  97. public function deviceFences($deviceId,$FencesId,$params){
  98. $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/fences/'.$FencesId.'?'.http_build_query($params);
  99. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  100. return $data;
  101. }
  102. /**
  103. * 在线设备操作,获取在线设备实时数据
  104. *
  105. * @author wst
  106. * @date 2021/5/31 15:56
  107. */
  108. public function deviceAction($deviceId,$action){
  109. $url = $this->produceUrl . '/api/device/'.$deviceId.'/4g/action/'.$action;
  110. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  111. return $data;
  112. }
  113. /**
  114. * 查看设备开关机数据
  115. *
  116. * @author wst
  117. * @date 2021/5/31 16:04
  118. */
  119. public function devicePowerData($deviceId,$params){
  120. $params['device'] = $deviceId;
  121. $url = $this->produceUrl . '/api/powerdata/?'.http_build_query($params);
  122. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  123. return $data;
  124. }
  125. /**
  126. * 查看设备定位数据
  127. *
  128. * @author wst
  129. * @date 2021/5/31 16:04
  130. */
  131. public function deviceLocationData($deviceId,$params){
  132. $params['device'] = $deviceId;
  133. $url = $this->produceUrl . '/api/locationdata/?'.http_build_query($params);
  134. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  135. return $data;
  136. }
  137. /**
  138. * 查看设备运动数据
  139. *
  140. * @author wst
  141. * @date 2021/5/31 16:28
  142. */
  143. public function devicePedometerData($deviceId,$params){
  144. $params['device'] = $deviceId;
  145. $url = $this->produceUrl . '/api/pedometerdata/?'.http_build_query($params);
  146. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  147. return $data;
  148. }
  149. /**
  150. * 查看设备心率数据
  151. *
  152. * @author wst
  153. * @date 2021/5/31 16:28
  154. */
  155. public function deviceHeartRatData($deviceId,$params){
  156. $params['device'] = $deviceId;
  157. $url = $this->produceUrl . '/api/heartratedata/?'.http_build_query($params);
  158. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  159. return $data;
  160. }
  161. /**
  162. * 查看设备睡眠数据
  163. *
  164. * @author wst
  165. * @date 2021/5/31 16:28
  166. */
  167. public function deviceSleepData($deviceId,$params){
  168. $params['device'] = $deviceId;
  169. $url = $this->produceUrl . '/api/sleepdata/?'.http_build_query($params);
  170. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  171. return $data;
  172. }
  173. /**
  174. * 查看设备答复数据
  175. *
  176. * @author wst
  177. * @date 2021/5/31 16:28
  178. */
  179. public function deviceReplyData($deviceId,$params){
  180. $params['device'] = $deviceId;
  181. $url = $this->produceUrl . '/api/replydata/?'.http_build_query($params);
  182. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  183. return $data;
  184. }
  185. /**
  186. * 查看设备血压数据
  187. *
  188. * @author wst
  189. * @date 2021/5/31 16:28
  190. */
  191. public function deviceBloodPressureData($deviceId,$params){
  192. $params['device'] = $deviceId;
  193. $url = $this->produceUrl . '/api/bloodpressuredata/?'.http_build_query($params);
  194. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  195. return $data;
  196. }
  197. /**
  198. * 查看设备血氧数据
  199. *
  200. * @author wst
  201. * @date 2021/5/31 16:28
  202. */
  203. public function deviceDoXGenData($deviceId,$params){
  204. $params['device'] = $deviceId;
  205. $url = $this->produceUrl . '/api/bloodoxygendata/?'.http_build_query($params);
  206. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  207. return $data;
  208. }
  209. /**
  210. * 查看设备紧急呼叫
  211. *
  212. * @author wst
  213. * @date 2021/5/31 16:28
  214. */
  215. public function deviceSosData($deviceId,$params){
  216. $params['device'] = $deviceId;
  217. $url = $this->produceUrl . '/api/sosdata/?'.http_build_query($params);
  218. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  219. return $data;
  220. }
  221. /**
  222. * 查看跌倒数据
  223. *
  224. * @author wst
  225. * @date 2021/5/31 16:28
  226. */
  227. public function deviceFallData($deviceId,$params){
  228. $params['device'] = $deviceId;
  229. $url = $this->produceUrl . '/api/falldata/?'.http_build_query($params);
  230. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  231. return $data;
  232. }
  233. /**
  234. * 获取设备最新位置数据
  235. *
  236. * @author wst
  237. * @date 2021/5/31 16:28
  238. */
  239. public function deviceNewLocationData($deviceId,$params){
  240. $url = $this->produceUrl . '/api/device/'.$deviceId.'/data/locationdata?'.http_build_query($params);
  241. $data = $this->getApiContents($url,'GET',[],$this->getCookie());
  242. return $data;
  243. }
  244. /**
  245. * 登录
  246. *
  247. * @author wst
  248. * @date 2021/5/31 14:06
  249. */
  250. public function getCookie() {
  251. $cookie = cache('aqg_cookie');
  252. //\think\facade\Cache::delete('aqg_cookie');
  253. if(empty($cookie)|| $cookie=='false'){
  254. $url = $this->produceUrl . '/api/auth/login';
  255. $cookie = $this->getApiContents($url, 'POST',
  256. ['username' => $this->userName, 'password' => $this->pwd], '', true);
  257. cache('aqg_cookie',$cookie,86400);
  258. }
  259. return $cookie;
  260. }
  261. /**
  262. * 获取接口内容及相应headers详情
  263. * @param string $url 请求的API地址
  264. * @param array $post POST所需提交的数据
  265. * @param string $token 验证的TOKEN,放header里
  266. * @param bool $returnHeader 是否需要查看response header内容
  267. * @return array
  268. */
  269. public function getApiContents($url, $method, $post = array(), $token = '', $returnHeader = false) {
  270. $curl = curl_init();
  271. curl_setopt($curl, CURLOPT_URL, $url); // 需要获取的 URL 地址,也可以在curl_init() 初始化会话的时候。
  272. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  273. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  274. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
  275. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  276. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  277. if (!empty($token)) {
  278. $header = ['Cookie: ' . $token]; //设置一个你的浏览器的header
  279. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  280. }
  281. /* 如果不设置这个则无法获取response header内容 */
  282. curl_setopt($curl, CURLOPT_HEADER, $returnHeader);
  283. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  284. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  285. // 执行 cURL 会话
  286. $data = curl_exec($curl);
  287. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  288. if (curl_errno($curl)) {
  289. return curl_error($curl);
  290. }
  291. // 关闭 cURL 会话
  292. curl_close($curl);
  293. $info['code'] = $httpCode;
  294. $info['cookie'] = '';
  295. if ($returnHeader) {
  296. list($header, $body) = explode("\r\n\r\n", $data, 2);
  297. $h = explode("\r", $header);
  298. foreach ($h as $k => $value) {
  299. if (strpos($value,'Set-Cookie')!==false) {
  300. $c = trim(str_replace('Set-Cookie:','',$value));
  301. $cc = explode(';',$c);
  302. $info['cookie'] = $cc[0];
  303. }
  304. }
  305. $info['data'] = json_decode($body,true);
  306. if (empty($info['cookie'])) {
  307. log::info('登录失败:' . $body);
  308. }
  309. }
  310. else {
  311. $info['data'] = json_decode($data,true);
  312. }
  313. if(!$returnHeader){
  314. if(!isset($info['data']['success'])) $this->error('系统错误!');
  315. return $info['data'];
  316. }
  317. return $info['cookie'];
  318. }
  319. }