<?php
declare (strict_types=1);
namespace app\api\controller\v1;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 15:05
// +----------------------------------------------------------------------
use app\BaseController;
use app\Request;
use library\services\UtilService;
use library\utils\Qiniu;
use think\Image;

class Upload extends BaseController {


    public function index(Request $request){
        $file =$request->file('file');
            $isZ = $request->post('isz',1);
        if(empty($file)) {
            return app('json')->fail("未上传文件");
        }
        $rootTmp = config('filesystem.disks.local.root') .'/' . \think\facade\Filesystem::putFile( 'tmp', $file);
        if($isZ == 1) {
            $image_size = @getimagesize($rootTmp);
            if ($image_size[0] > 1000) {
                $imgS = Image::open($rootTmp);
                $imgS->thumb(1000, $image_size[1]);
                $imgS->save($rootTmp);
            } else {
                if ($image_size[1] > 1000) {
                    $imgS = Image::open($rootTmp);
                    $imgS->thumb($image_size[0], 1000);
                    $imgS->save($rootTmp);
                }
            }
        }
        $qiniu = new  Qiniu;
        $img_url = $qiniu->updateFile('img', $rootTmp, $rootTmp);
        if(empty($img_url['url'])){
            return app('json')->fail( $qiniu->getError());
        }
        return app('json')->success(['img'=>$img_url['url']]);
    }


    /**
     * 读取数据
     */
    public function xls(Request $request){
        $post = UtilService::getMore([
            ['platform_id','0','empty','请选择平台在上传']
        ],$request);
        $file = $request->file('file');
        if(empty($file)) {
            $this->ajaxReturn(['code'=>-1,'msg'=>'没有上传数据']);
        }
        $info = $file->getFileInfo();
        $extension = strtolower( pathinfo($file->getOriginalName(), PATHINFO_EXTENSION) );
        if ($extension =='xlsx') {
            $objReader = new \PHPExcel_Reader_Excel2007();
            $objPHPExcel = $objReader ->load($info->getPathname());
        }

        if ($extension =='xls') {
            $objReader = new \PHPExcel_Reader_Excel5();
            $objPHPExcel = $objReader ->load($info->getPathname());
        }

        if ($extension =='csv') {
            $objReader = new \PHPExcel_Reader_CSV();
            //默认输入字符集
            $objReader->setInputEncoding('GBK');
            //默认的分隔符
            $objReader->setDelimiter(',');
            //载入文件
            $objPHPExcel = $objReader->load($info->getPathname());
        }
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        //读取第一行信息
        $headerAr = [];
        for($i = 0;$i <= \PHPExcel_Cell::columnIndexFromString($highestColumn);$i++) {
            $column = \PHPExcel_Cell::stringFromColumnIndex($i);
            $value = $objPHPExcel
                            ->getActiveSheet()
                            ->getCell($column.'1')
                            ->getValue();
            if(!empty($value)) $value = trim($value);
            $headerAr[$value] = $column;
        }
            $tAr = [];
            for ($i = 2; $i <= $highestRow ; $i++) {
                //外部订单号
                $d['out_order_id'] = empty($headerAr['订单编号']) ? '' : $objPHPExcel
                    ->getActiveSheet()
                    ->getCell($headerAr['订单编号'].$i)
                    ->getValue();
                $d['out_order_id'] = preg_replace('/\="(.*?)"/','$1',$d['out_order_id']);

                //收货人
                $d['name'] = empty($headerAr['收货人姓名']) ? '' : $objPHPExcel
                    ->getActiveSheet()
                    ->getCell($headerAr['收货人姓名'].$i)
                    ->getValue();
                //收货地址
                $d['address'] = empty($headerAr['收货地址']) ? '' : $objPHPExcel
                    ->getActiveSheet()
                    ->getCell($headerAr['收货地址'].$i)
                    ->getValue();
                //收货地址
                $d['mobile'] = empty($headerAr['联系手机']) ? '' : $objPHPExcel
                    ->getActiveSheet()
                    ->getCell($headerAr['联系手机'].$i)
                    ->getValue();
                $d['mobile'] = str_replace("'","",$d['mobile']);
                if(!empty($d['mobile']) && !empty($d['name']))  $tAr[] = $d;
            }


            return app('json')->success($tAr);


    }

}