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.
 
 
 

122 lines
3.5 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 bool|int|string
* 根据文件拓展名在$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 $imgIdStr
* @param $ownId
* @param $ownType
* @return array
* 根据临时文件id(字符串)将临时文件保存在文件中
*/
public function saveTemFileToFile($temFIleIdArr, $ownId, $ownType)
{
if(!$temFIleIdArr || !$ownId) {
return ['status' => false, 'info' => '参数错误'];
}
$i = 1;
$first_file_id = 0;
foreach ($temFIleIdArr as $key => $value) {
$tem_file = TemFile::findOne($value);
if(!$tem_file) {
return ['status' => false, 'info' => '存在查找不到的文件'];
}
$res = self::saveNewFile($tem_file, $ownId, $ownType);
if(!$res['status']) {
return ['status' => false, 'info' => '存在文件保存失败'];
}
if($i == 1) {
$first_file_id = $res['file_id'];
}
$i++;
}
return ['status' => true, 'info' => '保存成功', 'first_file_id' => $first_file_id];
}
/**
* @param $temFile
* @param $ownId
* @param $ownType
* @return bool
* 创建新的文件
*/
private function saveNewFile($temFile, $ownId, $ownType)
{
$new_file = new File();
$new_file->name = $temFile->name;
$new_file->type = $temFile->type;
$new_file->own_id = $ownId;
$new_file->own_type = $ownType;
$new_file->alias = $temFile->alias;
$new_file->path = $temFile->path;
if($new_file->save()) {
return ['status' => true, 'info' => '操作成功', 'file_id' => $new_file->id];
} else {
return ['status' => false, 'info' => '操作失败'];
}
}
/**
* @param $fileIdArr
* 删除file表中的文件
*/
public function deleteFile($fileIdArr)
{
if($fileIdArr){
foreach ($fileIdArr as $key => $value) {
$file_model = File::findOne($value);
if($file_model){
$file_model->is_delete = File::IS_DELETE_YES;
if($file_model->save()){
return ['status' => false, 'info' => '操作失败'];
}
}
}
}
return ['status' => true, 'info' => '操作成功'];
}
}