DiverseYouthNightSchool/application/manystore/controller/general/Attachment.php

175 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2024-11-04 15:00:20 +08:00
<?php
namespace app\manystore\controller\general;
use app\common\controller\ManystoreBase;
2024-12-18 18:47:13 +08:00
use app\common\model\ManystoreAttachment;
2024-11-04 15:00:20 +08:00
/**
* 附件管理
*
* @icon fa fa-circle-o
* @remark 主要用于管理上传到又拍云的数据或上传至本服务的上传数据
*/
class Attachment extends ManystoreBase
{
/**
* @var \app\common\model\ManystoreAttachment
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = model('ManystoreAttachment');
$this->view->assign("mimetypeList", \app\common\model\ManystoreAttachment::getMimetypeList());
2024-12-18 18:47:13 +08:00
$this->view->assign("categoryList", \app\common\model\Attachment::getCategoryList());
$this->assignconfig("categoryList", \app\common\model\Attachment::getCategoryList());
2024-11-04 15:00:20 +08:00
}
/**
* 查看
*/
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
$mimetypeQuery = [];
$filter = $this->request->request('filter');
$filterArr = (array)json_decode($filter, true);
2024-12-18 18:47:13 +08:00
if (isset($filterArr['category']) && $filterArr['category'] == 'unclassed') {
$filterArr['category'] = ',unclassed';
$this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['category' => '']))]);
}
2024-11-04 15:00:20 +08:00
if (isset($filterArr['mimetype']) && preg_match("/[]\,|\*]/", $filterArr['mimetype'])) {
$this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['mimetype' => '']))]);
$mimetypeQuery = function ($query) use ($filterArr) {
$mimetypeArr = explode(',', $filterArr['mimetype']);
foreach ($mimetypeArr as $index => $item) {
if (stripos($item, "/*") !== false) {
$query->whereOr('mimetype', 'like', str_replace("/*", "/", $item) . '%');
} else {
$query->whereOr('mimetype', 'like', '%' . $item . '%');
}
}
};
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
2024-12-19 17:30:50 +08:00
$list = $this->model->with(["user"])
2024-11-04 15:00:20 +08:00
->where($mimetypeQuery)
->whereRaw("`filename` NOT REGEXP '^[0-9A-Fa-f]{32}'")
2024-11-04 15:00:20 +08:00
->where($where)
->order($sort, $order)
->paginate($limit);
2024-12-19 17:30:50 +08:00
foreach ($list as $row) {
$row->getRelation('user')->visible(['nickname', 'realname', 'mobile', 'avatar']);
}
$rows = $list->items();
2024-11-04 15:00:20 +08:00
$cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
2024-12-19 17:30:50 +08:00
foreach ($rows as $k => &$v) {
2024-11-04 15:00:20 +08:00
$v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
}
unset($v);
2024-12-19 17:30:50 +08:00
$result = array("total" => $list->total(), "rows" => $rows);
2024-11-04 15:00:20 +08:00
return json($result);
}
return $this->view->fetch();
}
/**
* 选择附件
*/
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();
}
/**
* 添加
*/
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);
}
}
});
$attachmentlist = $this->model->where('id', 'in', $ids)->select();
foreach ($attachmentlist as $attachment) {
\think\Hook::listen("upload_delete", $attachment);
$attachment->delete();
}
$this->success();
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
2024-12-18 18:47:13 +08:00
/**
* 归类
*/
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'));
}
// if(!defined('SHOP_ID')){
// define('SHOP_ID', $this->auth->shop_id);
// }
$category = $category == 'unclassed' ? '' : $category;
ManystoreAttachment::where('id', 'in', $ids)->update(['category' => $category]);
$this->success();
}
2024-11-04 15:00:20 +08:00
}