| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace Modules\OpcData\Http\Controllers\Api;
- use App\Http\Controllers\Api\BaseController;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Controller;
- class OPCDataTurboController extends BaseController
- {
- public function getData(Request $request) {
- $sys_key = $request->sys_key;
- $sys_name = $request->sys_name;
- if (!isset($sys_key) || !isset($sys_name)) {
- return $this->error(-1, '参数错误');
- }
- $db_conf = config('database');
- $api_url = 'http://'.$db_conf['python_api'][$sys_key]['url'].'/get/?sys_key='.$sys_key.'&sys_name='.$sys_name;
- $response = $this->request_post($api_url);
- if (gettype($response) == 'integer') {
- switch ($response) {
- case 3:
- $error_info = 'CURLE_URL_MALFORMAT';
- break;
- case 7:
- $error_info = 'CURLE_COULDNT_CONNECT';
- break;
- default:
- $error_info = '未知错误:'.$response;
- }
- return $this->error($response, $error_info);
- } else {
- $data = json_decode($response);
- // $data = $this->data_process($data);
- // dd($data);
- // return $data;
- if ($data == null) {
- return $this->error(500, preg_replace('/<[^>]*>/','',$response));
- } else {
- return $this->success($data);
- }
- }
- }
- private function request_post($url = '', $param = '') {
- if (empty($url)) {
- return false;
- }
- $postUrl = $url;
- $curlPost = $param;
- $curl = curl_init();//初始化curl
- curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
- curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
- curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);//提交的参数
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- // curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
- $data = curl_exec($curl);//运行curl
- $errno = curl_errno($curl);
- curl_close($curl);
- if ($errno > 0) return $errno;
- return $data;
- }
- private function data_process($data) {
- $data = is_object($data) ? get_object_vars($data) : $data;
- $data_arr = [];
- foreach ($data as $key => $val) {
- $arr = is_object($val) ? get_object_vars($val) : $val;
- if (is_array($arr)) {
- $arr = $this->data_process(($arr));
- // dd($arr);
- }
- // $arr[$key] = round($val, 2);
- $data_arr[$key] = $arr;
- if (isset($data_arr['val']) && $data_arr['val'] != '') {
- $data_arr[$key] = round($data_arr['val'], 2);
- }
- }
- return $data_arr;
- // dd($arr);
- // $data = (array)$data;
- // $process_arr = [];
- // foreach ($data as $key=>$val ) {
- // if ($val)
- // if ($val == 'val') {
- // $data[$key] = round($val);
- // return;
- // }
- //// foreach ($val as $k=>$v) {
- //// $this->data_process($v);
- //// $vv = (array)$v;
- //// dd($vv['val']);
- //// }
- //// $dd = (array)$val;
- //// $process_arr[$key] = $this->data_process($val);
- //// print(gettype($this->data_process($val)));
- //
- //// if ($child_data['val']) {
- //// $child_data['val'] = round($child_data, 2);
- //// }
- //// dd($data[$key]);
- //// dd(round($data[$key]['val'], 2));
- // }
- // return $process_arr;
- }
- /**
- * Display a listing of the resource.
- * @return Renderable
- */
- public function index()
- {
- return view('opcdata::index');
- }
- /**
- * Show the form for creating a new resource.
- * @return Renderable
- */
- public function create()
- {
- return view('opcdata::create');
- }
- /**
- * Store a newly created resource in storage.
- * @param Request $request
- * @return Renderable
- */
- public function store(Request $request)
- {
- //
- }
- /**
- * Show the specified resource.
- * @param int $id
- * @return Renderable
- */
- public function show($id)
- {
- return view('opcdata::show');
- }
- /**
- * Show the form for editing the specified resource.
- * @param int $id
- * @return Renderable
- */
- public function edit($id)
- {
- return view('opcdata::edit');
- }
- /**
- * Update the specified resource in storage.
- * @param Request $request
- * @param int $id
- * @return Renderable
- */
- public function update(Request $request, $id)
- {
- //
- }
- /**
- * Remove the specified resource from storage.
- * @param int $id
- * @return Renderable
- */
- public function destroy($id)
- {
- //
- }
- }
|