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.
 
 
 

112 lines
3.0 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 $value
* @param int $key
* @return int
* 查看$extension数组中是否存在文件类型,不存在则返回-1
*/
public function searchType($array,$value, $key=-1){
foreach($array as $k=>$row){
if(!is_array($row)){
if($row == $value){
if($key != -1) {
return $key;
}else{
return -1;
}
}
}else{
$r = self::searchType($row,$value, $k);
if($r != -1){
return $r;
}
}
}
return -1;
}
/**
* @param $imgIdStr
* @param $ownId
* @param $ownType
* @return array
* 根据临时文件id(字符串)将临时文件保存在文件中
*/
public function saveTemFileToFile($temFIleIdStr, $ownId, $ownType)
{
if(!$temFIleIdStr || !$ownId) {
return ['status' => false, 'info' => '参数错误'];
}
$img_id_arr = explode(',', $temFIleIdStr);
$i = 1;
$first_file_id = 0;
foreach ($img_id_arr 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, 'file_id' => $new_file->id];
} else {
return ['status' => false];
}
}
}