65 lines
2.0 KiB
PHP
65 lines
2.0 KiB
PHP
<?php
|
||
|
||
namespace app\adminapi\controller;
|
||
|
||
use app\admin\model\AdminLog;
|
||
use app\common\controller\Upload;
|
||
use Overtrue\Pinyin\Pinyin;
|
||
use think\Request;
|
||
|
||
/**
|
||
* 后台首页
|
||
* @internal
|
||
*/
|
||
class UploadFile extends Upload
|
||
{
|
||
protected $request;
|
||
public function __construct(Request $request)
|
||
{
|
||
$this->request = $request;
|
||
parent::__construct();
|
||
$this->config = get_system_config();
|
||
}
|
||
/*
|
||
* 上传文件
|
||
* @return array
|
||
*/
|
||
public function file_upload(){
|
||
// 获取上传的文件对象
|
||
$file = $this->request->file('files');
|
||
if (!$file) {
|
||
return V(0, '未上传文件', null);
|
||
}
|
||
try {
|
||
// 获取临时路径和原始文件名
|
||
$filePath = $file->getRealPath();
|
||
$originalName = $file->getInfo('name');
|
||
|
||
// 提取文件扩展名
|
||
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
|
||
|
||
// 处理文件名(中文转拼音) 移除扩展名(如果有的话)
|
||
$fileName = pathinfo($originalName, PATHINFO_FILENAME);
|
||
if (preg_match('/[\x{4e00}-\x{9fa5}]+/u', $fileName)) {
|
||
$pinyin = new Pinyin();
|
||
$fileName = $pinyin->permalink($fileName, ''); // 转换为无分隔符的拼音
|
||
}
|
||
|
||
// 添加唯一标识符(时间戳+随机数)
|
||
$uniqueId = time() . mt_rand(1000, 9999);
|
||
$objectName = $fileName . '_' . $uniqueId . '.' . $extension;
|
||
|
||
// 调用父类方法上传到 OSS
|
||
$result = $this->uploadFile($objectName, $filePath);
|
||
if (!$result) {
|
||
return V(0, '上传失败,请检查OSS配置或网络', null);
|
||
}
|
||
// 返回访问地址
|
||
$url = $this->config['oss_cdn_url'] . $objectName;
|
||
return V(1, '上传成功', ['url' => $url]);
|
||
} catch (\Exception $e) {
|
||
return V(0, '上传异常: ' . $e->getMessage(), null);
|
||
}
|
||
}
|
||
|
||
} |