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

use Illuminate\Support\Facades\Route; */
trait Tool
{
    public function getAllowLoginUser($filedValue='',$filed='user_sn'){
        //return ['user_name'=>'yxl001','user_sn'=>'SYSUSER|9da548a2e392a679eb274a0b2abeb627','parent_sn'=>'ljs'];
        if (empty($filedValue)){
            return false;
        }
        //$allowLoginUser = ['yxl001','yxl002'];        
        $loginUserInfo = [
            ['user_name'=>'yxl001','user_sn'=>'SYSUSER|9da548a2e392a679eb274a0b2abeb627','parent_sn'=>'ljs'],
            ['user_name'=>'yxl002','user_sn'=>'SYSUSER|a15f5d51a8b0156d14f2d8ad2d39e111','parent_sn'=>'ljs'],
            ['user_name'=>'yxl003','user_sn'=>'SYSUSER|bb25ff86b38bdac649494b690a655ed6','parent_sn'=>'ljs'],
            ['user_name'=>'yxl004','user_sn'=>'SYSUSER|da1452dd4bfa9371372b1f665f267540','parent_sn'=>'ljs'],
        ];
        
        foreach ($loginUserInfo as $k=>$v){
            if ($v[$filed] == $filedValue){
                return $v;
            }
        }        
        return false;
    }
    public function getAllowProjects($style=0){        
        $projectInfo = [['sn'=>'projects|a3a4259352c8e964ad6ea6c5ee8a7208','name'=>'liuy-转人工测试'],['sn'=>'projects|17a71649f886bce050d637e86a3c85e8','name'=>'小贷-测试林俊生']];
        if ($style == 1){
            $projectArr = [];
            foreach ($projectInfo as $k=>$v){
                $projectArr[$v['sn']] = $v['name'];
            }   
            return $projectArr;
        }else{
            return $projectInfo;
        }
    }
    public function getAllowCallNumber(){
        return [900002];
    }
    
    
    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;
    }
   
    function uidd($prefix=''){        
        if (function_exists('com_create_guid')){
            return com_create_guid();
        }else{
            $chars = md5(uniqid(mt_rand(), true));
            $uuid  = substr($chars,0,8);
            $uuid .= substr($chars,8,4);
            $uuid .= substr($chars,12,4);
            $uuid .= substr($chars,16,4);
            $uuid .= substr($chars,20,12);
            return $prefix . $uuid;
        }
    } 
    
}