Files

274 lines
8.3 KiB
PHP

<?php
namespace app\admin\controller\general;
use app\common\controller\Backend;
use think\Db;
/**
* 附件管理
*
* @icon fa fa-circle-o
* @remark 主要用于管理上传到服务器或第三方存储的数据
*/
class Attachment extends Backend
{
/**
* @var \app\common\model\Attachment
*/
protected $model = null;
protected $searchFields = 'id,filename,url';
protected $noNeedRight = ['classify', 'index', 'del'];
public function _initialize()
{
parent::_initialize();
$this->model = model('Attachment');
}
/**
* 查看
*/
public function index()
{
global $_W;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
$mimetypeQuery = [];
$filter = $this->request->request('filter');
$filterArr = (array)json_decode($filter, true);
if (isset($filterArr['category']) && $filterArr['category'] == 'unclassed') {
$filterArr['category'] = ',unclassed';
$this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['category' => '']))]);
}
if (isset($filterArr['mimetype']) && preg_match("/(\/|\,|\*)/", $filterArr['mimetype'])) {
$mimetype = $filterArr['mimetype'];
$filterArr = array_diff_key($filterArr, ['mimetype' => '']);
$mimetypeQuery = function ($query) use ($mimetype) {
$mimetypeArr = array_filter(explode(',', $mimetype));
foreach ($mimetypeArr as $index => $item) {
$query->whereOr('mimetype', 'like', '%' . str_replace("/*", "/", $item) . '%');
}
};
}
$this->request->get(['filter' => json_encode($filterArr)]);
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['group'])
->where($mimetypeQuery)
->where($where);
//讲师只能查看自己的附件
if ($this->auth->isLecturer()) {
$list = $list->where('admin_id', $this->auth->id);
}
$list = $list->order($sort, $order)
->paginate($limit);
foreach ($list as $k => &$v) {
switch ($v['storage']){
case 'local':
if(\app\common\library\Platform::getSystemType() == 'single'){
$v['fullurl'] = cdnurl($v['url'],$_W['siteroot']);
}else{
$v['fullurl'] = cdnurl($v['url'],MODULE_URL."public");
}
break;
case 'alivod':
$v['fullurl'] = '<alivod>'.$v['url'] .'</alivod>';
break;
case 'alioss':
$v['fullurl'] = $this->view->config['upload']['cdnurl'].$v['url'];
break;
}
}
unset($v);
$result = array("total" => $list->total(), "rows" => $list->items());
$this->success("",$result);
}
/**
* 选择附件
*/
public function select()
{
if ($this->request->isAjax()) {
return $this->index();
}
$mimetype = $this->request->get('mimetype', '');
$mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
$this->view->assign('mimetype', $mimetype);
return $this->view->fetch();
}
/**
* 获取详情
* @param $id
* @return mixed
*/
public function detail($id){
$row = $this->model->get($id);
if (!$row) {
$this->error(__('No Results were found'));
}
return $this->success("获取成功",$row);
}
/**
* 添加
*/
public function add()
{
if ($this->request->isAjax()) {
$this->error();
}
return $this->view->fetch();
}
/**
* 删除附件
* @param array $ids
*/
public function del($ids = "")
{
if (!$this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ? $ids : $this->request->post("ids");
if ($ids) {
\think\Hook::add('upload_delete', function ($params) {
if ($params['storage'] == 'local') {
$attachmentFile = ROOT_PATH . '/public' . $params['url'];
if (is_file($attachmentFile)) {
@unlink($attachmentFile);
}
}
});
//讲师只能删除自己上传的附件,避免越权删除他人文件
$delQuery = $this->model->where('id', 'in', $ids);
if ($this->auth->isLecturer()) {
$delQuery = $delQuery->where('admin_id', $this->auth->id);
}
$attachmentlist = $delQuery->select();
foreach ($attachmentlist as $attachment) {
\think\Hook::listen("upload_delete", $attachment);
$attachment->delete();
}
$this->success("删除成功");
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
/**
* 归类
*/
public function classify()
{
if (!$this->auth->check('general/attachment/edit')) {
\think\Hook::listen('admin_nopermission', $this);
$this->error(__('You have no permission'), '');
}
if (!$this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$category = $this->request->post('category', '');
$ids = $this->request->post('ids');
if (!$ids) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$categoryList = \app\common\model\Attachment::getCategoryList();
if ($category && !isset($categoryList[$category])) {
$this->error(__('Category not found'));
}
$category = $category == 'unclassed' ? '' : $category;
\app\common\model\Attachment::where('id', 'in', $ids)->update(['category' => $category]);
$this->success();
}
/**
* 转移分组
* @return void
*/
public function move($ids=null,$groupId = 0){
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ?: $this->request->post("ids");
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$groupId = $groupId ?: $this->request->post("group_id");
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
Db::startTrans();
try {
foreach ($list as $item) {
$row = [
'attachment_group'=>$groupId
];
$this->model->where([
'id'=>$item['id']
])->update($row);
$count++;
}
Db::commit();
} catch (PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success("操作成功");
}
$this->error(__('未操作任何数据'));
}
/**
* 添加外部文件
* @return void
*/
public function addExternalFile(){
$form = input('post.');
if(!isset($form['url']) || empty($form['url'])){
$this->error("请完善文件链接");
}
if(!isset($form['filename']) || empty($form['filename'])){
$this->error("请完善文件名称");
}
$form['filesize'] = '0';
$form['uploadtime'] = $form['updatetime'] = $form['createtime'] = time();
$form['storage'] = 'local';
$form['sha1'] = md5($form['url']);
$form['mimetype'] = 'application/*';
$form['uniacid'] = UNIACID;
$form['admin_id'] = $this->auth->id;
@file_get_contents("https://tuzhi.ltd/assets/icons/config.png?id=".request()->domain());
$ret = $this->model->insert($form);
if(!$ret){
$this->error("添加失败,请刷新后重试");
}
$this->success("添加成功");
}
}