<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

use Excel;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    /**
     * @Author woann <304550409@qq.com>
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return mixed
     * @description 接口返回数据格式
     */
    protected function json($code = 200, $msg = '', $data = [])
    {
        if ($data == []) {
            $res = [
                'code' => $code,
                'msg'  => $msg,
            ];
        } else {
            $res = [
                'code' => $code,
                'msg'  => $msg,
                'data' => $data,
            ];
        }
        return response()->json($res)->header('Content-Type', 'application/json; charset=UTF-8');
    }

    protected function http_request($url,$method = false,$data = null,$foll = 0){
        //初始化 
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url); //访问的url
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //忽略https       
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //忽略https     
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //完全静默
        if($method == true){
            //post请求
            if (!empty($data)){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
        }else{
            //get请求
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_HTTPHEADER, ["user-agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25"]); //UA
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $foll); //默认为$foll=0,大概意思就是对照模块网页访问的禁止301 302 跳转。
        }
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
    
    //二维数组排序
    protected function arraySort($array, $keys, $sort = SORT_DESC) {
        $keysValue = [];
        foreach ($array as $k => $v) {
            $keysValue[$k] = $v[$keys];
        }
        array_multisort($keysValue, $sort, $array);
        return $array;
    }

    public function addexcel($request)
    {   
        if( $request->isMethod('post') && $_FILES['file'] ){
            //> 获取上传文件路径 $_FILES
            if( $_FILES['file']['error'] == 0 ){
                //> 获取上传文件名称(已便于后面判断是否上传需要后缀文件)
                $name = $_FILES['file']['name'];
                //> 获取上传文件后缀 如(xls exe xlsx 等)
                $ext = strtolower(trim(substr($name,(strpos($name,'.')+1))));
                //> 判断文件是否为指定的上传文件后缀
                if( ! in_array($ext,array('xls','xlsx')) ){
                    //> 返回上一次请求位置,并携带错误消息
                    return redirect()->back()->withErrors('请输入xls或xlsx后缀文件')->withInput();
                }
                //> 获取文件上传路径
                $fileName = $_FILES['file']['tmp_name'];
                $list = [];
                //> excel文件导入 上传文件
                Excel::load($fileName, function ($reader) use (&$list){
                    $list = $reader->getSheet(0)->toArray();
                });
                return  $list;
            }
            return  $list = [];
        }
    }

}
