heart_cabin_api/app/api/controller/CommonApi/CommonApi.php

81 lines
2.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\api\controller\CommonApi;
use app\BaseController;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;
use think\captcha\facade\Captcha;
use think\helper\Arr;
class CommonApi extends BaseController
{
/**
* 获取验证码接口
*
* @return \think\response\Html
* @date 2023-04-26
* @author xjh
* @since 1.0.0
*/
public function getCaptcha(): \think\response\Html
{
return Captcha::create('verify');
}
/**
* 上传图片
*/
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');
$uploadedFile = $request->file('file');
$originalFilename = $uploadedFile->getOriginalName(); // 获取上传文件的原始文件名
$upload = new UploadFile('uploads', 'file');
$path = $upload->putFileAs($dirName . 'File', $originalFilename);
return [
'code' => 0,
'data' => [
"fileName" => $originalFilename,
"url" => "/uploads/" . $path,
],
'msg' => '上传成功!'
];
}
}