Loading src/UploadOss/UploadOssService.php +54 −28 Original line number Original line Diff line number Diff line Loading @@ -38,6 +38,12 @@ class UploadOssService */ */ private $appName; private $appName; /** * 是否自动保存数据库 * @var bool */ private $autoSaveDataBase = true; public static $options = [ public static $options = [ 'maxSize' => 10 * 1024 * 1024, // 文件大小,10M 'maxSize' => 10 * 1024 * 1024, // 文件大小,10M 'mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg', 'txt', 'pdf', 'xlsx', 'xls', 'doc', 'docx', 'rar', 'zip', 'csv'], // 允许上传的文件类型 'mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg', 'txt', 'pdf', 'xlsx', 'xls', 'doc', 'docx', 'rar', 'zip', 'csv'], // 允许上传的文件类型 Loading @@ -53,11 +59,21 @@ class UploadOssService $this->appName = $this->config->get('app_name'); $this->appName = $this->config->get('app_name'); } } /** * 设置是否自动保存记录到数据库 * @param bool $bool * @author chentianyu */ public function setAutoSaveDataBase(bool $bool) { $this->autoSaveDataBase = $bool; } /** /** * 上传图片 * 上传图片 * @param \Hyperf\HttpMessage\Upload\UploadedFile $image * @param \Hyperf\HttpMessage\Upload\UploadedFile $image * @param string $module * @param string $module * @return array * @return array|string * @throws \Meibuyu\Common\Exceptions\HttpResponseException * @throws \Meibuyu\Common\Exceptions\HttpResponseException * @author chentianyu * @author chentianyu */ */ Loading @@ -69,6 +85,7 @@ class UploadOssService $options = ['mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg']]; $options = ['mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg']]; $filePath = 'oss2/' . $this->appDev . "/" . $this->appName . "/$module/images/" . today() . "/$userId/" . $clientFilename; $filePath = 'oss2/' . $this->appDev . "/" . $this->appName . "/$module/images/" . today() . "/$userId/" . $clientFilename; $this->upload($image, $filePath, $options); $this->upload($image, $filePath, $options); if($this->autoSaveDataBase){ $model = new FileModel(); $model = new FileModel(); $model = $model->newInstance([ $model = $model->newInstance([ 'type' => 'image', 'type' => 'image', Loading @@ -82,13 +99,17 @@ class UploadOssService ]); ]); $model->save(); $model->save(); return $model->toArray(); return $model->toArray(); }else{ return $this->urlPrefix . $filePath; } } } /** /** * 上传文件 * 上传文件 * @param \Hyperf\HttpMessage\Upload\UploadedFile $image * @param \Hyperf\HttpMessage\Upload\UploadedFile $image * @param string $module * @param string $module * @return array * @return array|string * @throws \Meibuyu\Common\Exceptions\HttpResponseException * @throws \Meibuyu\Common\Exceptions\HttpResponseException * @author chentianyu * @author chentianyu */ */ Loading @@ -99,6 +120,7 @@ class UploadOssService $fileName = $uniqueFileName ? ($this->genUniqueFileName() . '.' . $ext) : $clientFilename; $fileName = $uniqueFileName ? ($this->genUniqueFileName() . '.' . $ext) : $clientFilename; $filePath = 'oss2/' . $this->appDev . "/" . $this->appName . "/$module/files/" . today() . "/$userId/" . $clientFilename; $filePath = 'oss2/' . $this->appDev . "/" . $this->appName . "/$module/files/" . today() . "/$userId/" . $clientFilename; $this->upload($file, $filePath, $options); $this->upload($file, $filePath, $options); if($this->autoSaveDataBase){ $model = new FileModel(); $model = new FileModel(); $model = $model->newInstance([ $model = $model->newInstance([ 'type' => 'file', 'type' => 'file', Loading @@ -112,6 +134,10 @@ class UploadOssService ]); ]); $model->save(); $model->save(); return $model->toArray(); return $model->toArray(); }else{ return $this->urlPrefix . $filePath; } } } /** /** Loading Loading
src/UploadOss/UploadOssService.php +54 −28 Original line number Original line Diff line number Diff line Loading @@ -38,6 +38,12 @@ class UploadOssService */ */ private $appName; private $appName; /** * 是否自动保存数据库 * @var bool */ private $autoSaveDataBase = true; public static $options = [ public static $options = [ 'maxSize' => 10 * 1024 * 1024, // 文件大小,10M 'maxSize' => 10 * 1024 * 1024, // 文件大小,10M 'mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg', 'txt', 'pdf', 'xlsx', 'xls', 'doc', 'docx', 'rar', 'zip', 'csv'], // 允许上传的文件类型 'mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg', 'txt', 'pdf', 'xlsx', 'xls', 'doc', 'docx', 'rar', 'zip', 'csv'], // 允许上传的文件类型 Loading @@ -53,11 +59,21 @@ class UploadOssService $this->appName = $this->config->get('app_name'); $this->appName = $this->config->get('app_name'); } } /** * 设置是否自动保存记录到数据库 * @param bool $bool * @author chentianyu */ public function setAutoSaveDataBase(bool $bool) { $this->autoSaveDataBase = $bool; } /** /** * 上传图片 * 上传图片 * @param \Hyperf\HttpMessage\Upload\UploadedFile $image * @param \Hyperf\HttpMessage\Upload\UploadedFile $image * @param string $module * @param string $module * @return array * @return array|string * @throws \Meibuyu\Common\Exceptions\HttpResponseException * @throws \Meibuyu\Common\Exceptions\HttpResponseException * @author chentianyu * @author chentianyu */ */ Loading @@ -69,6 +85,7 @@ class UploadOssService $options = ['mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg']]; $options = ['mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg']]; $filePath = 'oss2/' . $this->appDev . "/" . $this->appName . "/$module/images/" . today() . "/$userId/" . $clientFilename; $filePath = 'oss2/' . $this->appDev . "/" . $this->appName . "/$module/images/" . today() . "/$userId/" . $clientFilename; $this->upload($image, $filePath, $options); $this->upload($image, $filePath, $options); if($this->autoSaveDataBase){ $model = new FileModel(); $model = new FileModel(); $model = $model->newInstance([ $model = $model->newInstance([ 'type' => 'image', 'type' => 'image', Loading @@ -82,13 +99,17 @@ class UploadOssService ]); ]); $model->save(); $model->save(); return $model->toArray(); return $model->toArray(); }else{ return $this->urlPrefix . $filePath; } } } /** /** * 上传文件 * 上传文件 * @param \Hyperf\HttpMessage\Upload\UploadedFile $image * @param \Hyperf\HttpMessage\Upload\UploadedFile $image * @param string $module * @param string $module * @return array * @return array|string * @throws \Meibuyu\Common\Exceptions\HttpResponseException * @throws \Meibuyu\Common\Exceptions\HttpResponseException * @author chentianyu * @author chentianyu */ */ Loading @@ -99,6 +120,7 @@ class UploadOssService $fileName = $uniqueFileName ? ($this->genUniqueFileName() . '.' . $ext) : $clientFilename; $fileName = $uniqueFileName ? ($this->genUniqueFileName() . '.' . $ext) : $clientFilename; $filePath = 'oss2/' . $this->appDev . "/" . $this->appName . "/$module/files/" . today() . "/$userId/" . $clientFilename; $filePath = 'oss2/' . $this->appDev . "/" . $this->appName . "/$module/files/" . today() . "/$userId/" . $clientFilename; $this->upload($file, $filePath, $options); $this->upload($file, $filePath, $options); if($this->autoSaveDataBase){ $model = new FileModel(); $model = new FileModel(); $model = $model->newInstance([ $model = $model->newInstance([ 'type' => 'file', 'type' => 'file', Loading @@ -112,6 +134,10 @@ class UploadOssService ]); ]); $model->save(); $model->save(); return $model->toArray(); return $model->toArray(); }else{ return $this->urlPrefix . $filePath; } } } /** /** Loading