<?php

namespace app\api\controller\inspection;
//允许所有的跨域请求
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
date_default_timezone_set('PRC');

use app\api\controller\getui\JgPush;
use app\common\controller\Api;
use fast\Tree;
use think\Db;
use think\Request;
use Qiniu\Auth as QAuth;
use Qiniu\Storage\UploadManager;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Task extends Api
{

    /**
     * Depart模型对象
     * @var \app\admin\model\inspection\Depart
     */
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {

        parent::_initialize();
        \think\Lang::load(APP_PATH . '/admin/lang/zh-cn/inspection/task.php');
        $this->model = new \app\admin\model\inspection\Task;

    }


    /**
     * 文件上传
     *
     */
    public function fileUpload()
    {
//        file_put_contents("test_upload.log","00:".date("Ymd H:i:s").PHP_EOL,FILE_APPEND);

        if ($this->request->isPost()) {
            $file = request()->file('file');
            $data['oldname'] = request()->post('fileName');
//            file_put_contents("test_upload.log","一:".date("Ymd H:i:s").json_encode($file).PHP_EOL,FILE_APPEND);
//            file_put_contents("test_upload.log","二:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND);

            if (empty($file)) {
                $this->error('请选择上传文件');
            }

            //移动到框架应用根目录/public/uploads/目录下
            $info = $file->validate(['ext' => 'jpeg,jpg,png,bmp,doc,docx,pdf,ppt,pptx,xls,xlsx,zip,rar,7z,mp3,mp4,mov'])->move(ROOT_PATH . 'public' . DS . 'uploads');
//            file_put_contents("test_upload.log","三:".date("Ymd H:i:s").json_encode($data).PHP_EOL,FILE_APPEND);


            $domain = 'https://qiniu.ynzhsk.cn/';
            $bucket = 'shuiku';
            $auth = new QAuth('Eb_QX9JpdaILVbUx0ChfMfo0AFK-12J-eD0uVy25', 'VeqduMD6UzUvxkAiXc6e2N6w51N_KwppZXT3Kaix');
            // 生成上传Token
            $token = $auth->uploadToken($bucket);

            // 构建 UploadManager 对象
            $uploadMgr = new UploadManager();

            if ($info) {
                $filepath = ROOT_PATH . 'public/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                $fname = 'uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                list($ret, $err) = $uploadMgr->putFile($token, $fname, $filepath);
                $data['filepath'] = $domain . $ret['key'];
                $data['fileurl'] = '/' . $ret['key'];
                $path = '../public' . $data['fileurl'];
                is_file($path) && unlink($path);//删除服务器上文件

                $this->success('文件上传成功', $data);
            } else {
                //上传失败获取错误信息
//                file_put_contents("test_upload.log","四:".$file->getError().PHP_EOL,FILE_APPEND);

                $this->error($file->getError());
            }
        }
    }

}