You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
3.4 KiB

<?php
namespace backend\logic\file;
use common\models\ars\File;
use common\models\ars\TemFile;
class FileManager
{
//数据表ats_file和ats_tem_file的类型字段type
const TYPE_IMAGE = 1;//图片
const TYPE_VIDEO = 2;//影视
const TYPE_EXCEL = 3;//excel表单
const TYPE_WORD = 4;//word文本
const TYPE_TXT = 5;//txt文本
public static $extension = [
self::TYPE_IMAGE => ['jpg', 'png', 'jpeg'],
self::TYPE_VIDEO => ['mp4'],
self::TYPE_EXCEL => [],
self::TYPE_WORD => ['docx'],
self::TYPE_TXT => ['txt'],
];
/**
* @param $array
* @param $keyword
* @return array
* 根据文件拓展名在$extension中查找对应的文件类型,若不存在则返回false
*/
public function searchType($array, $keyword)
{
foreach($array as $key => $value){
foreach ($value as $k => $v) {
if($v == $keyword){
return ['status' => true, 'info' => '操作成功', 'type' => $key];
}
}
}
return ['status' => false, 'info' => '操作失败'];
}
/**
* @param $temFIleIdArr
* @param $ownId
* @param $ownType
* @return array
* 根据临时文件id将临时文件保存在文件中
*/
public function saveTemFileToFile($temFIleIdArr, $ownId, $ownType)
{
if(!$temFIleIdArr || !$ownId) {
return ['status' => false, 'info' => '参数错误'];
}
$firstFileId = 0;
foreach ($temFIleIdArr as $key => $value) {
$temFile = TemFile::findOne($value);
if(!$temFile) {
return ['status' => false, 'info' => '存在查找不到的文件'];
}
$res = self::saveNewFile($temFile, $ownId, $ownType);
if(!$res['status']) {
return ['status' => false, 'info' => '存在文件保存失败'];
}
if($key == 0) {
$firstFileId = $res['file_id'];
}
}
return ['status' => true, 'info' => '保存成功', 'first_file_id' => $firstFileId];
}
/**
* @param $temFile
* @param $ownId
* @param $ownType
* @return array
* 创建新的文件
*/
private function saveNewFile($temFile, $ownId, $ownType)
{
$newFile = new File();
$newFile->name = $temFile->name;
$newFile->type = $temFile->type;
$newFile->own_id = $ownId;
$newFile->own_type = $ownType;
$newFile->alias = $temFile->alias;
$newFile->path = $temFile->path;
if($newFile->save()) {
return ['status' => true, 'info' => '操作成功', 'file_id' => $newFile->id];
} else {
return ['status' => false, 'info' => '操作失败'];
}
}
/**
* @param $fileIdArr
* @return array
* 删除file表中的文件
*/
public function deleteFile($fileIdArr)
{
if($fileIdArr){
foreach ($fileIdArr as $key => $value) {
$fileModel = File::findOne($value);
if($fileModel){
$fileModel->is_delete = File::IS_DELETE_YES;
if($fileModel->save()){
return ['status' => false, 'info' => '操作失败'];
}
}
}
}
return ['status' => true, 'info' => '操作成功'];
}
}