<?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;
        }
        /*      
        $loginUserInfo = [
            ['user_name'=>'ljss','user_sn'=>'SYSUSER|61f8d0f38ce9b0af1d0a405a2586c6cb','parent_sn'=>'ljs'],
            ['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'],
        ];
        */
        $loginUserInfo = [
            ['user_name'=>'hz900718001','user_sn'=>'SYSUSER|b6f3e7dfeccb7c455d28674943d42b90','parent_sn'=>'hz900718']
        ];
        
        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'=>'小贷-测试林俊生']];
        
        $projectInfo = [
            ['sn'=>'project|8a693306a9ef4d44e09231c90b9859b8','name'=>'杭州房建公寓段首次提醒'],
            ['sn'=>'project|5b85293a1ae0623d727745f6a68f4f74','name'=>'杭州房建公寓段再次提醒'],
            ['sn'=>'projects|c40931516b75aaa8b4a14a1b207aec99','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];
        return [900718];
    }
    
    
    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;
        }
    } 
    
}