<?php
namespace  App\Http\Controllers;
/* use App\Events\DataOperation;
use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\Route; */
trait Tool
{
    public function success()
    {
        return response()->json(
            [
                'status' => 'success',
                'status_code' => 200
            ]
            ,200);
    }

    public function successWithInfo($info, $code = 200)
    {
        return response()->json(
            [
                'info' => $info,
                'status' => 'success',
                'status_code' => $code
            ]
            ,$code);
    }

    public function successWithData($data)
    {
        return response()->json([
            'data' => $data,
            'status' => 'success',
            'status_code' => 200
        ], 200);
    }

    public function error()
    {
        return response()->json(
            [
                'status' => 'error',
                'status_code' => 404
            ]
            ,404);
    }

    public function errorWithInfo($info, $http_code = 404,$msg_code='')
    {
        return response()->json(
            [
                'info' => $info,
                'status' => 'error',
                'status_code' => $http_code,
                'msg_code' => $msg_code?$msg_code:$http_code
            ] ,$http_code);
    }

    /* public function log($type, $desc)
    {
        $route_name =  Route::currentRouteName();
        $data = [
            'type' => $type,
            'route_name' => $route_name,
            'desc' => $desc
        ];
        event(new DataOperation($data));
    } */

    /* public function isAdmin()
    {
        $roles = explode(',', Auth::user()->roles);
        return  in_array('admin', $roles);
    } */

    /**
    * 模拟post进行url请求
    * @param string $url
    * @param string $param
    */
    public function requestPost($url = '', $post_data = array(),$header=array()) {
        if (empty($url) || empty($post_data)) {
            return false;
        }
        
        $o = "";
        if(is_array($post_data) && count($post_data)){
            foreach ( $post_data as $k => $v )
            {
                $o.= "$k=" . urlencode( $v ). "&" ;
            }
            $post_data = substr($o,0,-1);
        }
        $postUrl = $url;
        $curlPost = $post_data;
        $ch = curl_init();//初始化curl
        if(!empty($header)){
            curl_setopt( $ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, '0');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '0');
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }
}