model = new AttachmentModel(); } public function index() { $multiple = input('param.multiple'); $select = input('param.select'); if (request()->isAjax()) { $page = input('param.page/d', 1); $rows = input('param.rows/d', 50); $key = input('param.keyword', ''); $type = input('param.type', 1); $nav = input('param.nav/d', ''); $order = input('param.order/d', 2); $navData = getNavArrData($nav); $result = $this->model->getLists([ 'keyword' => $key, 'type' => $type, 'nav' => $navData, 'is_del' => 0, 'order' => $order ], $rows, $page); if (!empty($result['list'])) { foreach ($result['list'] as &$v) { $v['checked'] = false; $v['showTools'] = false; } } $attachmentNavModel = new AttachmentNavModel(); $result['navList'] = $attachmentNavModel->getNavList(); // 追加一个全部图片 array_unshift($result['navList'], [ 'value' => 0, 'label' => '全部图片', 'children' => [] ]); $breadcrumb = [['id' => 0, 'name' => '全部图片']]; // 排序 $sort = 'id DESC'; switch ($order) { case 1 : $sort = 'id ASC'; break; case 2 : $sort = 'id DESC'; break; case 3 : $sort = 'letter ASC'; break; case 4 : $sort = 'letter DESC'; break; } if (!empty($navData)) { if (count($navData) == 1) { $condition = !empty($key) ? ['pid' => $navData[0], 'name' => ['LIKE', '%' . $key . '%']] : ['pid' => $navData[0]]; $dirList = $attachmentNavModel ->where($condition) ->field(['id' => 'value', 'name' => 'label']) ->order($sort) ->select(); $breadcrumb[] = $this->getNavData($navData[0], $attachmentNavModel); } else { $dirList = []; $breadcrumb[] = $this->getNavData($navData[0], $attachmentNavModel); $breadcrumb[] = $this->getNavData($navData[1], $attachmentNavModel); } } else { $condition = !empty($key) ? ['pid' => 0, 'name' => ['LIKE', '%' . $key . '%']] : ['pid' => 0]; $dirList = $attachmentNavModel ->where($condition) ->field(['id' => 'value', 'name' => 'label']) ->order($sort) ->select(); } $result['dirList'] = $dirList; $result['breadcrumb'] = $breadcrumb; return json($result); } $this->assign([ 'multiple' => $multiple, 'select' => $select, ]); return $this->fetch(); } /** * 归类 * @return \think\response\Json */ public function nav() { $data = input('post.'); if ($this->model->nav($data)) { return json([ 'code' => 200, 'msg' => '归类成功' ]); } else { return json([ 'code' => 400, 'msg' => '归类失败' ]); } } public function dell($ids) { $ids = $ids ? $ids : $this->request->post("ids"); $ids = explode(',', $ids); $res = $this->model->transaction(function () use ($ids) { foreach ($ids as $v) { $this->model->update([ 'id' => $v, 'is_del' => 1 ]); } return true; }); if (!empty($res)) { return json([ 'code' => 1, 'msg' => '删除成功' ]); } else { return json([ 'code' => 400, 'msg' => '删除失败' ]); } } // 回收站 public function recycle() { if (request()->isAjax()) { $page = input('param.page/d', 1); $rows = input('param.rows/d', 20); $key = input('param.keyword', ''); $type = input('param.type', ''); $category = input('param.category', ''); $mimetype = input('param.mimetype', ''); $startTime = input('param.start_time', ''); $endTime = input('param.end_time', ''); $result = $this->model->getLists([ 'is_del' => 1, 'keyword' => $key, 'type' => $type, 'category' => $category, 'mimetype' => $mimetype, 'start_time' => $startTime, 'end_time' => $endTime, ], $rows, $page); $attachmentNavModel = new AttachmentNavModel(); if (!empty($result['list'])) { foreach ($result['list'] as &$v) { if (empty($v['one_id']) && empty($v['two_id'])) { $v['navName'] = '暂无分类'; } else if (!empty($v['one_id']) && empty($v['two_id'])) { $v['navName'] = $this->getNavData($v['one_id'], $attachmentNavModel); } else { $v['navName'] = $this->getNavData($v['one_id'], $attachmentNavModel) . '/' . $this->getNavData($v['two_id'], $attachmentNavModel); } } } return json($result); } return $this->fetch(); } /** * 恢复 * @param null $ids * @return \think\response\Json|void */ public function restoreData() { $ids = $this->request->post("ids"); $ids = explode(',', $ids); $res = $this->model->transaction(function () use ($ids) { foreach ($ids as $v) { $this->model->update([ 'id' => $v, 'is_del' => 0 ]); } return true; }); if (!empty($res)) { return json([ 'code' => 1, 'msg' => '恢复成功' ]); } else { return json([ 'code' => 400, 'msg' => '恢复失败' ]); } } public function getNavData($id, $attachmentNavModel) { return $attachmentNavModel ->where(['id' => $id]) ->field(['id', 'name']) ->find(); } }