utils->errorThrow('请选择图片'); } $ext = $file->getClientOriginalExtension(); // 单独走 wx 命名空间避免与后台素材库扫描混目录 $key = 'wx/voucher/' . date('Ymd') . '/' . 'wx_' . Str::random() . uniqid() . '.' . $ext; $storage = $this->resolveStorage(); $result = $storage->uploadVideo($file, $key); if (!$result) { $this->utils->errorThrow('图片上传失败'); } // 落一份上传流水到 nl_file,type=image,方便后台审计小程序上传的凭证 FileModel::insert([ 'user_id' => $this->userId, 'url' => $result['url'], 'type' => FileModel::TYPE_IMAGE, 'source' => FileModel::SOURCE_UPLOAD, 'created_at' => time(), 'updated_at' => time(), ]); return $result; } /** * 解析当前启用的存储实现;配置异常时回退本地,避免整站上传不可用 */ private function resolveStorage() { try { $config = OssRuntimeConfigService::getInstance()->getActiveConfig(); return OssStorageFactory::getInstance()->make($config); } catch (\Throwable $e) { return OssStorageFactory::getInstance()->make([ 'driver' => 'local', 'path_prefix' => 'uploads', 'domain' => '', ]); } } }