belongsTo('UploadGroup', 'group_id'); } /** * 获取图片完整路径 * * @param $value * @param $data * @return string */ public function getFilePathAttr($value, $data) { if ($data['storage'] === 'local') { return self::$base_url . 'uploads/' . $data['file_name']; } if ($data['storage'] === 'third_party') { return $data['file_name']; } $domain = str_replace('http://dy-dxf.oss-cn-hangzhou.aliyuncs.com', 'https://dy-dxf.oss-cn-hangzhou.aliyuncs.com', $data['file_url']); return $domain . '/' . $data['file_name']; } /** * [getCoverFilePathAttr] * 获取封面图片 * * @Author: Yeshihao * @DateTime: 2024/6/14 15:23 * @param $value * @param $data * @return array|bool|float|int|mixed|object|\stdClass|string|null */ public function getCoverFilePathAttr($value, $data) { if (empty($data['file_type'])) { return $this->file_path ?? ''; } if ($data['file_type'] == 'video') { return static_file('store/img/video.png'); } if ($data['file_type'] == 'file') { return static_file('store/img/file.png'); } if ($data['file_type'] == 'pdf') { return static_file('store/img/pdf.png'); } return $this->file_path; } /** * 通过mime获取文件类型 * * @param string $mime * @return string * @Author Mark * @DateTime 2024-08-07 */ public static function mime2fileType(string $mime): string { $map = [ //图片 'image/jpeg' => 'image', 'image/jpg' => 'image', 'image/png' => 'image', 'image/gif' => 'image', 'image/bmp' => 'image', 'image/webp' => 'image', //视频 'video/mp4' => 'video', //pdf 'application/pdf' => 'pdf', ]; return $map[$mime] ?? ''; } /** * 文件详情 * * @param $file_id * @return null|static * @throws \think\exception\DbException */ public static function detail($file_id) { return self::get($file_id); } /** * 根据文件名查询文件id * * @param $fileName * @return mixed */ public static function getFildIdByName($fileName) { return (new static)->where(['file_name' => $fileName])->value('file_id'); } /** * 查询文件id * * @param $fileId * @return mixed */ public static function getFileName($fileId) { return (new static)->where(['file_id' => $fileId])->value('file_name'); } /** * 添加新记录 * * @param $data * @return false|int */ public function add($data) { $data['wxapp_id'] = $data['wxapp_id'] ?? (self::$wxapp_id ?: 0); return $this->save($data); } /** * [imageArr] * * @Author: Yeshihao * @DateTime: 2023/6/16 20:47 */ public static function id2url($id) { $file = self::cache(3600)->find($id); return $file->file_path ?? ''; } /** * 文件上传 * * @param integer $wxappId * @param integer $group_id * @param string $fileField * @return static * @Author Mark * @DateTime 2024-08-22 */ public function uploadFile($wxappId, $group_id = -1, $fileField = 'iFile', $comapnyId = 0) { $config = Setting::getItem('storage', $wxappId); // 实例化存储驱动 $StorageDriver = new StorageDriver($config); // 设置上传文件的信息 $StorageDriver->setUploadFile($fileField); //判断上传类型 $fileInfo = $StorageDriver->getFileInfo(); if (!UploadFile::mime2fileType($fileInfo['type'])) { $this->error = '图片上传失败:mime类型错误'; return false; } // 上传图片 if (!$StorageDriver->upload()) { $this->error = '图片上传失败:' . $StorageDriver->getError(); return false; } // 图片上传路径 $fileName = $StorageDriver->getFileName(); // 图片信息 $fileInfo = $StorageDriver->getFileInfo(); $uploadFile = self::addUploadFile($group_id, $fileName, $fileInfo, $config, $comapnyId); // 图片上传成功 return $uploadFile; } /** * 写入数据库 * * @param int $group_id * @param string $fileName * @param array $fileInfo * @param array $config * @return static * @Author Mark * @DateTime 2024-08-22 */ private function addUploadFile($group_id, $fileName, $fileInfo, $config, $companyId) { // 存储引擎 $storage = $config['default']; // 存储域名 $fileUrl = isset($config['engine'][$storage]['domain']) ? $config['engine'][$storage]['domain'] : ''; // 添加文件库记录 $model = new self; $model->add([ 'group_id' => $group_id > 0 ? (int)$group_id : 0, 'storage' => $storage, 'file_url' => $fileUrl, 'file_name' => $fileName, 'file_size' => $fileInfo['size'], 'file_type' => self::mime2fileType($fileInfo['type']), 'extension' => pathinfo($fileInfo['name'], PATHINFO_EXTENSION), 'origin_name' => $fileInfo['name'], 'mime' => $fileInfo['type'], 'company_id' => $companyId, ]); return $model; } }