OPCDataTurboController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Modules\OpcData\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\BaseController;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Routing\Controller;
  7. class OPCDataTurboController extends BaseController
  8. {
  9. public function getData(Request $request) {
  10. $sys_key = $request->sys_key;
  11. $sys_name = $request->sys_name;
  12. if (!isset($sys_key) || !isset($sys_name)) {
  13. return $this->error(-1, '参数错误');
  14. }
  15. $db_conf = config('database');
  16. $api_url = 'http://'.$db_conf['python_api'][$sys_key]['url'].'/get/?sys_key='.$sys_key.'&sys_name='.$sys_name;
  17. $response = $this->request_post($api_url);
  18. if (gettype($response) == 'integer') {
  19. switch ($response) {
  20. case 3:
  21. $error_info = 'CURLE_URL_MALFORMAT';
  22. break;
  23. case 7:
  24. $error_info = 'CURLE_COULDNT_CONNECT';
  25. break;
  26. default:
  27. $error_info = '未知错误:'.$response;
  28. }
  29. return $this->error($response, $error_info);
  30. } else {
  31. $data = json_decode($response);
  32. // $data = $this->data_process($data);
  33. // dd($data);
  34. // return $data;
  35. if ($data == null) {
  36. return $this->error(500, preg_replace('/<[^>]*>/','',$response));
  37. } else {
  38. return $this->success($data);
  39. }
  40. }
  41. }
  42. private function request_post($url = '', $param = '') {
  43. if (empty($url)) {
  44. return false;
  45. }
  46. $postUrl = $url;
  47. $curlPost = $param;
  48. $curl = curl_init();//初始化curl
  49. curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
  50. curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
  51. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  52. curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
  53. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);//提交的参数
  54. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  55. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  56. // curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  57. $data = curl_exec($curl);//运行curl
  58. $errno = curl_errno($curl);
  59. curl_close($curl);
  60. if ($errno > 0) return $errno;
  61. return $data;
  62. }
  63. private function data_process($data) {
  64. $data = is_object($data) ? get_object_vars($data) : $data;
  65. $data_arr = [];
  66. foreach ($data as $key => $val) {
  67. $arr = is_object($val) ? get_object_vars($val) : $val;
  68. if (is_array($arr)) {
  69. $arr = $this->data_process(($arr));
  70. // dd($arr);
  71. }
  72. // $arr[$key] = round($val, 2);
  73. $data_arr[$key] = $arr;
  74. if (isset($data_arr['val']) && $data_arr['val'] != '') {
  75. $data_arr[$key] = round($data_arr['val'], 2);
  76. }
  77. }
  78. return $data_arr;
  79. // dd($arr);
  80. // $data = (array)$data;
  81. // $process_arr = [];
  82. // foreach ($data as $key=>$val ) {
  83. // if ($val)
  84. // if ($val == 'val') {
  85. // $data[$key] = round($val);
  86. // return;
  87. // }
  88. //// foreach ($val as $k=>$v) {
  89. //// $this->data_process($v);
  90. //// $vv = (array)$v;
  91. //// dd($vv['val']);
  92. //// }
  93. //// $dd = (array)$val;
  94. //// $process_arr[$key] = $this->data_process($val);
  95. //// print(gettype($this->data_process($val)));
  96. //
  97. //// if ($child_data['val']) {
  98. //// $child_data['val'] = round($child_data, 2);
  99. //// }
  100. //// dd($data[$key]);
  101. //// dd(round($data[$key]['val'], 2));
  102. // }
  103. // return $process_arr;
  104. }
  105. /**
  106. * Display a listing of the resource.
  107. * @return Renderable
  108. */
  109. public function index()
  110. {
  111. return view('opcdata::index');
  112. }
  113. /**
  114. * Show the form for creating a new resource.
  115. * @return Renderable
  116. */
  117. public function create()
  118. {
  119. return view('opcdata::create');
  120. }
  121. /**
  122. * Store a newly created resource in storage.
  123. * @param Request $request
  124. * @return Renderable
  125. */
  126. public function store(Request $request)
  127. {
  128. //
  129. }
  130. /**
  131. * Show the specified resource.
  132. * @param int $id
  133. * @return Renderable
  134. */
  135. public function show($id)
  136. {
  137. return view('opcdata::show');
  138. }
  139. /**
  140. * Show the form for editing the specified resource.
  141. * @param int $id
  142. * @return Renderable
  143. */
  144. public function edit($id)
  145. {
  146. return view('opcdata::edit');
  147. }
  148. /**
  149. * Update the specified resource in storage.
  150. * @param Request $request
  151. * @param int $id
  152. * @return Renderable
  153. */
  154. public function update(Request $request, $id)
  155. {
  156. //
  157. }
  158. /**
  159. * Remove the specified resource from storage.
  160. * @param int $id
  161. * @return Renderable
  162. */
  163. public function destroy($id)
  164. {
  165. //
  166. }
  167. }