<?php

namespace app\admin\controller;

use app\model\Banner as ModelBanner;
use app\BaseController;
use app\Request;
use app\exception\ErrorMsg;
use think\facade\Validate;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use think\facade\Db;
use think\facade\Env;

class Common extends BaseController
{

    /**
     * 上传图片
     */
    public function uploadImg(Request $request)
    {
        $dirName = $request->param('dirName');

        $upload = new UploadFile('uploads', 'file');
        $path = $upload->putFile($dirName . 'Img');
        $url = "/uploads/" . $path;

        //图片大小>500k，压缩图片质量
        $absolute_path = public_path() . $url;
        if (ceil(filesize($absolute_path) / 1000) > 500) {
            $image = \think\Image::open($absolute_path);
            $image->save($absolute_path, null, 80);
        }

        return [
            'code' => 0,
            'data' => [
                "name" => $path,
                "url" => $url,
            ],
            'msg' => '上传成功！'
        ];
    }

    /**
     * 上传文件
     */
    public function uploadFile(Request $request)
    {
        $dirName = $request->param('dirName');

        $upload = new UploadFile('uploads', 'file');
        $path = $upload->putFile($dirName . 'File');
        return [
            'code' => 0,
            'data' => [
                "name" => $path,
                "url" => "/uploads/" . $path,
            ],
            'msg' => '上传成功！'
        ];
    }
}
