Files
Smart-Farm/app/admin/controller/File.php
2025-12-22 14:32:54 +08:00

355 lines
12 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\admin\controller;
use app\AdminRest;
use think\App;
use think\Request;
use app\admin\model\AttachmentGroup;
use app\admin\model\CoreAttachment;
use app\Common\Upload;
class File extends AdminRest
{
public $uid = 0;
public function __construct(App $app) {
parent::__construct($app);
//测试数据
// $this->_uniacid = 2;
$this->uid = 0;
}
//创建分组
public function createGroup()
{
//获取参数
$input = $this->_input;
//兼容写法-- lichuanming 2020/5/13
if(!isset($input['group']['name']) && !isset($input['name'])) return $this->error('not group name ,please check .');
$group['name'] = isset($input['group']['name'])?$input['group']['name']:$input['name'];
//获取uid
// if(!empty($this->uid)) $group['uid'] = $this->uid;
$group['uniacid'] = $this->_uniacid;
//生成分组模型
$group_model = new AttachmentGroup();
$group_count = $group_model->where('uniacid','=',$this->_uniacid)->count('id');
if($group_count >= 20){ //分组不超过20个
return $this->error('分组最多限制为20个');
}
$repeat = $group_model->where(['uniacid'=>$this->_uniacid,'name'=>$group['name']])->count();
if($repeat){
return $this->error('已存在同名分组');
}
$result = $group_model->createGroup($group);
return $this->success($result);
}
//获取分组列表
public function listGroup()
{
//获取参数
$param = $this->_param;
if(isset($param['name'])) $filter[] = $param['name'];
$filter['uniacid'] = $this->_uniacid;
//获取uid
// if(!empty($this->uid)) $filter['uid'] = $this->uid;
//生成分组模型
$group_model = new AttachmentGroup();
//获取数据
$result = $group_model->listGroup($filter);
return $this->success(['groups' => $result]);
}
//更新分组列表
public function updateGroup()
{
//获取参数
$group_id = $this->_param['group_id'];
//获取更新数据
$data = $this->_input['group'];
//生成分组模型
$group_model = new AttachmentGroup();
$repeat = $group_model->where(['uniacid'=>$this->_uniacid,'name'=>$data['name']])->where('id','<>',$group_id)->count();
if($repeat){
return $this->error('已存在同名分组');
}
//更新数据
$result = $group_model->updateGroup(['id' => $group_id,'uniacid'=>$this->_uniacid] ,$data);
//返回数据
return $this->success($result);
}
//删除分组信息
public function delGroup()
{
//获取参数
if(!isset($this->_param['group_id'])) return $this->error('not group id');
$group_id = $this->_param['group_id'];
$where = array(
['group_id','=',$group_id],
['uniacid','=',$this->_uniacid]
);
//生成分组模型
$attachment_model = new CoreAttachment();
$file_count = $attachment_model->where($where)->count('id');
if($file_count > 0){
return $this->error('目前分组中有文件数据,不可删除,如要删除,请先清除文件数据');
}
$group_model = new AttachmentGroup();
//删除数据
$result = $group_model->delGroup(['id' => $group_id]);
//返回数据
return $this->success($result);
}
/**
**@author lichuanming
* @DataTime: 2020/5/15 14:35
* @功能说明:批量删除分组
*/
public function delAllGroup(){
if(!isset($this->_param['group_id'])) return $this->error('not group id');
$group_id = $this->_param['group_id'];
$attachment_model = new CoreAttachment();
if(is_array($group_id)){
foreach ($group_id as $id){
$where = array(
['group_id','=',$id],
['uniacid','=',$this->_uniacid]
);
$file_count = $attachment_model->where($where)->count('id');
if($file_count > 0){
return $this->error('目前分组中有文件数据,不可删除,如要删除,请先清除文件数据');
}
}
$group_model = new AttachmentGroup();
//删除数据
$result = $group_model->delGroup(['id' => $group_id]);
//返回数据
return $this->success($result);
}else{
return $this->delGroup();
}
}
//上传文件
public function uploadFile()
{
$input = $this->_param;
$file = $this->request->file('file');
if(empty($file)) return $this->error('not file ,please check file.');
$uploda_model = new Upload($this->_uniacid);
$type = 'picture';
if(isset($input['type'])) $type = $input['type'];
$info = $uploda_model->upload($type ,$file);
$result = false;
if(!empty($info))
{
$info['uid'] = $this->uid;
$info['longbing_attachment_path'] = longbingGetFilePath($info['attachment'] , $this->_host,$this->_uniacid);
$info['longbing_from'] = 'web';
$attachment_model = new CoreAttachment();
$result = $attachment_model->createAttach($info);
if(!empty($result)) $result = $info;
}
//数据处理
return $this->success($result);
}
public function uploadFiles()
{
//获取参数
$input = $this->_param;
//获取文件列表
$files = $this->request->file('file');
//检查文件是否存在
if(empty($files)) return $this->error('not file ,please check file.');
//设置类型
$type = 'picture';
if(isset($input['type'])) $type = $input['type'];
//上传文件分组 --lichuanming 2020/5/13
$group_id = -1;
if(isset($input['group_id'])) $group_id = $input['group_id'];
$result = [];
//生成上传模型
$uploda_model = new Upload($this->_uniacid);
foreach($files as $file)
{
//上传文件
$info = $uploda_model->upload($type ,$file);
if(!empty($info))
{
//获取上传者id
$info['uid'] = $this->uid;
$info['attachment_path'] = longbingGetFilePath($info['attachment'] , $this->_host,$this->_uniacid ,$info['longbing_driver']);
//文件分组界定 --lichuanming 2020/5/13
$info['group_id'] = $group_id;
$info['uniacid'] = $this->_uniacid;
//写入数据库
$attachment_model = new CoreAttachment();
$data = $attachment_model->createAttach($info);
//判断写入数据库是否成功
if(!empty($data)) $result[] = $info;
}
}
//数据处理
return $this->success($result);
}
//获取文件列表
public function listFile()
{
$param = $this->_param;
$filter['uniacid'] = $this->_uniacid;
$filter['type'] = 1;
$filter['uid'] = 0;
//判断分组是否存在
if(isset($param['group_id'])) $filter['group_id'] = $param['group_id']?$param['group_id']:['0','-1'];
//判断文件类型是否存在
if(isset($param['type']) && in_array($param['type'], [1,2,3,'1','2','3'])) $filter['type'] = $param['type'];
//判断用户id是否存在
// if(isset($this->uid)) return $this->error('not login,please login');
// $filter['uid'] = $this->uid;
//获取和生成分页信息
$page_config = array(
'page' => 1,
'page_count' => 10
);
if(isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
if(isset($param['page_count']) && $param['page_count'] > 0) $page_config['page_count'] = $param['page_count'];
//生成模型类
$file_model = new CoreAttachment();
//查询总数
$count = $file_model->listAttachCount($filter);
$files = $file_model->listAttach($filter ,$page_config);
$files = transImagesOne($files ,['attachment'] ,$this->_uniacid);
// foreach($files as $key => $val)
// {
// $files[$key]['attachment_path'] = longbingGetFilePath($val['attachment'] ,$this->_host ,$this->_uniacid);
// }
//生成返回数据
$page_config['total'] = $count;
$page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
if(($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
$result = $page_config;
$result['files'] = $files;
//返回数据
return $this->success($result);
}
//获取文件
public function getFile()
{
$param = $this->_param;
$filter['uniacid'] = $this->_uniacid;
$filter['id'] = $param['id'];
// $filter['uid'] = $this->uid;
//生成模型
$file_model = new CoreAttachment();
//查询数据
$file = $file_model->getFile($filter);
// if(isset($file['attachment']))
// {
// $file['attachment_path'] = longbingGetFilePath($file['attachment'] ,$this->_host ,$this->_uniacid);
// }
//返回数据
return $this->success($file);
}
//删除文件
public function delFile()
{
//获取参数
$input = $this->_input;
//获取参数
if(!isset($input['ids'])) return $this->error('not file id,please check.');
$filter['ids'] = $input['ids'];
// $filter['uid'] = $this->uid;
$filter['uniacid'] = $this->_uniacid;
//生成模型
$file_model = new CoreAttachment();
//删除
$result = $file_model->delAttach($filter);
return $this->success($result);
}
/**
* @author chenniang
* @DataTime: 2021-04-08 14:16
* @功能说明:获取上传配置
*/
public function uploadConfig(){
$data = longbingGetOssConfig($this->_uniacid);
$data['uniacid'] = $this->_uniacid;
return $this->success($data);
}
/**
* @author chenniang
* @DataTime: 2021-04-08 14:17
* @功能说明:上传文件到数据库
*/
public function addFile(){
$info = $this->_input;
$group_id = -1;
if(isset($info['group_id'])) {
$group_id = $info['group_id'];
}
//获取上传者id
$info['uid'] = $this->uid;
//$info['attachment_path'] = longbingGetFilePath($info['attachment'] , $this->_host,$this->_uniacid ,$info['longbing_driver']);
//文件分组界定 --lichuanming 2020/5/13
$info['group_id'] = $group_id;
$info['uniacid'] = $this->_uniacid;
$info['createtime'] = time();
$attachment_model = new CoreAttachment();
$data = $attachment_model->addAttach($info);
return $this->success($data);
}
/**
* @author chenniang
* @DataTime: 2021-08-13 15:26
* @功能说明:图片移动分组
*/
public function moveGroup(){
$input = $this->_input;
$attachment_model = new CoreAttachment();
$res = $attachment_model->where('id','in',$input['file_id'])->update(['group_id'=>$input['group_id']]);
return $this->success($res);
}
}