model = model('Attachment'); $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList()); $this->view->assign("categoryList", \app\common\model\Attachment::getCategoryList()); $this->assignconfig("categoryList", \app\common\model\Attachment::getCategoryList()); } /** * 查看 */ 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); 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 ->where($mimetypeQuery) ->where($where) ->order($sort, $order) ->paginate($limit); $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root()); foreach ($list as $k => &$v) { $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url']; } unset($v); $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 选择附件 */ public function select_copy() { 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(); } /** * 搜索分类 * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function searchNav() { $key = input('param.keyword'); $attachmentNavModel = new AttachmentNavModel(); $navList = $attachmentNavModel->searchNav($key); return json([ 'code' => 200, 'msg' => '获取成功', 'data' => [ 'navList' => $navList ] ]); } /** * 文件选择 * @return mixed|\think\response\Json */ public function select() { $multiple = input('param.multiple'); if (request()->isAjax()) { $attachmentNavModel = new AttachmentNavModel(); $type = input('param.type', ''); $key = input('param.keyword', ''); if ($type == 7 && !empty($key)) { $navList = $attachmentNavModel->searchNav($key); } else if ($type == 7 && empty($key)) { $navList = $attachmentNavModel->getNavList(); } else { $navList = $attachmentNavModel->getNavList(); } $attachmentModel = new AttachmentModel(); $page = input('param.page/d', 1); $rows = input('param.rows/d', 20); $nav = input('param.nav/a', ''); $nid = input('param.nid/d', ''); $category = input('param.category', ''); $mimetype = input('param.mimetype', ''); $startTime = input('param.start_time', ''); $endTime = input('param.end_time', ''); $order = input('param.order', ''); $result = $attachmentModel->getLists([ 'keyword' => $key, 'type' => $type, 'nav' => $nav, 'nid' => $nid, 'category' => $category, 'mimetype' => $mimetype, 'start_time' => $startTime, 'end_time' => $endTime, 'order' => $order, ], $rows, $page); $result['navList'] = $navList; return json($result); } $this->assign([ 'multiple' => $multiple ]); return $this->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')); } /** * 归类 */ 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(); } }