// +---------------------------------------------------------------------- // +---------------------------------------------------------------------- // | cms管理 // +---------------------------------------------------------------------- namespace app\admin\controller\cms; use addons\cms\library\Service; use app\admin\model\cms\Cms as CmsModel; use app\admin\model\cms\Page as PageModel; use app\common\controller\Adminbase; use think\Db; class Cms extends Adminbase { protected $searchFields = 'id,title'; protected $noNeedRight = ['check_title', 'panl', 'classlist']; protected function initialize() { parent::initialize(); $this->CmsModel = new CmsModel; $this->cmsConfig = get_addon_config("cms"); $this->assign("cmsConfig", $this->cmsConfig); } public function index() { $isAdministrator = $this->auth->isAdministrator(); $json = $priv_catids = []; if (0 !== (int) $this->cmsConfig['site_category_auth']) { //栏目权限 超级管理员例外 if ($isAdministrator !== true) { $role_id = $this->auth->roleid; $priv_result = Db::name('CategoryPriv')->where(['roleid' => $role_id, 'action' => 'init'])->select(); foreach ($priv_result as $_v) { $priv_catids[] = $_v['catid']; } } } $categorys = Db::name('Category')->order('listorder DESC, id DESC')->select(); foreach ($categorys as $rs) { //剔除无子栏目外部链接 if ($rs['type'] == 3 && $rs['child'] == 0) { continue; } if (0 !== (int) $this->cmsConfig['site_category_auth']) { //只显示有init权限的,超级管理员除外 if ($isAdministrator !== true && !in_array($rs['id'], $priv_catids)) { $arrchildid = explode(',', $rs['arrchildid']); $array_intersect = array_intersect($priv_catids, $arrchildid); if (empty($array_intersect)) { continue; } } } $data = [ 'id' => $rs['id'], 'parentid' => $rs['parentid'], 'catname' => $rs['catname'], 'type' => $rs['type'], ]; //终极栏目 if ($rs['child'] !== 0) { $data['isParent'] = true; } $data['target'] = 'right'; if ($rs['type'] == 2) { $data['url'] = url('classlist', ['catid' => $rs['id']]); } //单页 if ($rs['type'] == 1) { $data['target'] = 'right'; $data['url'] = url('add', ['catid' => $rs['id']]); } $json[] = $data; } $this->assign('json', json_encode($json)); return $this->fetch(); } //栏目信息列表 public function classlist() { $catid = $this->request->param('catid/d', 0); //当前栏目信息 $catInfo = getCategory($catid); if (empty($catInfo)) { $this->error('该栏目不存在!'); } //栏目所属模型 $modelid = $catInfo['modelid']; if ($this->request->isAjax()) { //检查模型是否被禁用 if (!getModel($modelid, 'status')) { $this->error('模型被禁用!'); } $modelCache = cache("Model"); $tableName = $modelCache[$modelid]['tablename']; $this->modelClass = Db::name($tableName); //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($page, $limit, $where) = $this->buildTableParames(); $conditions = [ ['catid', '=', $catid], ['status', 'in', [0, 1]], ]; $total = Db::name($tableName)->where($where)->where($conditions)->count(); $list = Db::name($tableName)->page($page, $limit)->where($where)->where($conditions)->order('listorder DESC, id DESC')->select(); $_list = []; foreach ($list as $k => $v) { $v['update_time'] = date('Y-m-d H:i:s', $v['update_time']); $v['url'] = buildContentUrl($v['catid'], $v['id'], $v['url']); $_list[] = $v; } $result = ["code" => 0, "count" => $total, "data" => $_list]; return json($result); } /*移动栏目 复制栏目*/ $tree = new \util\Tree(); $tree->icon = ['   │ ', '   ├─ ', '   └─ ']; $tree->nbsp = '   '; $categorys = []; $result = Db::name('category')->order('listorder DESC, id DESC')->select(); foreach ($result as $k => $v) { if ($v['type'] != 2) { $v['disabled'] = 'disabled'; } if ($modelid && $modelid != $v['modelid']) { $v['disabled'] = 'disabled'; } //$v['disabled'] = $v['child'] ? 'disabled' : ''; $v['selected'] = $v['id'] == $catid ? 'selected' : ''; $categorys[$k] = $v; } $str = ""; $tree->init($categorys); $string = $tree->getTree(0, $str, $catid); $this->assign('string', $string); $this->assign('catid', $catid); return $this->fetch(); } //移动文章 public function remove() { $this->check_priv('remove'); if ($this->request->isPost()) { $catid = $this->request->param('catid/d', 0); if (!$catid) { $this->error("请指定栏目!"); } //需要移动的信息ID集合 $ids = $this->request->param('ids/s'); //目标栏目 $tocatid = $this->request->param('tocatid/d', 0); if ($ids) { if ($tocatid == $catid) { $this->error('目标栏目和当前栏目是同一个栏目!'); } $modelid = getCategory($tocatid, 'modelid'); if (!$modelid) { $this->error('该模型不存在!'); } $ids = array_filter(explode('|', $ids), 'intval'); $tableName = Db::name('model')->where('id', $modelid)->where('status', 1)->value('tablename'); if (!$tableName) { $this->error('模型被冻结不可操作~'); } if (Db::name(ucwords($tableName))->where('id', 'in', $ids)->update(['catid' => $tocatid])) { Db::name('Category')->where('id', $catid)->setDec('items', count($ids)); Db::name('Category')->where('id', $tocatid)->setInc('items', count($ids)); $this->success('移动成功~'); } else { $this->error('移动失败~'); } } else { $this->error('请选择需要移动的信息!'); } } } //添加信息 public function add() { $this->check_priv('add'); if ($this->request->isPost()) { $data = $this->request->post(); $catid = intval($data['modelField']['catid']); if (empty($catid)) { $this->error("请指定栏目ID!"); } $category = getCategory($catid); //dump($data['modelField']);die; if (empty($category)) { $this->error('该栏目不存在!'); } if ($category['type'] == 2) { //模型类型 if((int)$catid === 17){ //关键词栏目 //进行判断标题是否存在 //不用考虑状态还有指令类型 $one = Db::name('cms_zl')->where([ 'gzxh' => $data['modelField']['gzxh'], 'title' => $data['modelField']['title'], ])->find(); if(!empty($one)){ $this->error("该规则下此标题已存在!"); } if((int)$data['modelField']['is_qj'] == 1){ //是全局 $t_data = [ '全局-非主非副','全局-主词类型','全局-副词类型' ]; if(!in_array($data['modelField']['title'],$t_data)){ $this->error("标题必须是'全局-非主非副','全局-主词类型','全局-副词类型'其中一个!"); } } } $data['modelFieldExt'] = $data['modelFieldExt'] ?? []; try { $this->CmsModel->addModelData($data['modelField'], $data['modelFieldExt']); } catch (\Exception $ex) { $this->error($ex->getMessage()); } } elseif ($category['type'] == 1) { $PageModel = new PageModel; if (!$PageModel->savePage($data['modelField'])) { $error = $PageModel->getError(); $this->error($error ? $error : '操作失败!'); } } $this->success('操作成功!'); } else { $catid = $this->request->param('catid/d', 0); $category = getCategory($catid); if (empty($category)) { $this->error('该栏目不存在!'); } if ($category['type'] == 2) { $modelid = $category['modelid']; $fieldList = Service::getFieldList($modelid); $this->assign([ 'catid' => $catid, 'fieldList' => $fieldList, ]); return $this->fetch(); } elseif ($category['type'] == 1) { $PageModel = new PageModel; $info = $PageModel->getPage($catid); $this->assign([ 'info' => $info, 'catid' => $catid, ]); return $this->fetch('singlepage'); } } } //编辑信息 public function edit() { $this->check_priv('edit'); if ($this->request->isPost()) { $data = $this->request->post(); $data['modelFieldExt'] = $data['modelFieldExt'] ?? []; //模型类型 if((int)$data['modelField']['catid'] === 17){ if((int)$data['modelField']['is_qj'] == 1){ //是全局 $t_data = [ '全局-非主非副','全局-主词类型','全局-副词类型' ]; if(!in_array($data['modelField']['title'],$t_data)){ $this->error("标题必须是'全局-非主非副','全局-主词类型','全局-副词类型'其中一个!"); } } } try { $this->CmsModel->editModelData($data['modelField'], $data['modelFieldExt']); } catch (\Exception $ex) { $this->error($ex->getMessage()); } $this->success('编辑成功!'); } else { $catid = $this->request->param('catid/d', 0); $id = $this->request->param('id/d', 0); $category = getCategory($catid); if (empty($category)) { $this->error('该栏目不存在!'); } if ($category['type'] == 2) { $modelid = $category['modelid']; $fieldList = Service::getFieldList($modelid, $id); $this->assign([ 'catid' => $catid, 'id' => $id, 'fieldList' => $fieldList, ]); return $this->fetch(); } else { return $this->fetch('singlepage'); } } } //删除 public function del() { $this->check_priv('delete'); $catid = $this->request->param('catid/d', 0); $ids = $this->request->param('id/a', null); if (empty($ids) || !$catid) { $this->error('参数错误!'); } if (!is_array($ids)) { $ids = [0 => $ids]; } $modelid = getCategory($catid, 'modelid'); try { foreach ($ids as $id) { $this->CmsModel->deleteModelData($modelid, $id, $this->cmsConfig['web_site_recycle']); } } catch (\Exception $ex) { $this->error($ex->getMessage()); } $this->success('删除成功!'); } //清空回收站 public function destroy() { $catid = $this->request->param('catid/d', 0); $ids = $this->request->param('id/a', null); if (empty($ids) || !$catid) { $this->error('参数错误!'); } if (!is_array($ids)) { $ids = [0 => $ids]; } $modelid = getCategory($catid, 'modelid'); try { foreach ($ids as $id) { $this->CmsModel->deleteModelData($modelid, $id); } } catch (\Exception $ex) { $this->error($ex->getMessage()); } $this->success('销毁成功!'); } //面板 public function panl() { if ($this->request->isPost()) { $date = $this->request->post('date'); list($xAxisData, $seriesData) = $this->getAdminPostData($date); $this->success('', '', ['xAxisData' => $xAxisData, 'seriesData' => $seriesData]); } else { $info['category'] = Db::name('Category')->count(); $info['model'] = Db::name('Model')->where(['module' => 'cms'])->count(); $info['tags'] = Db::name('Tags')->count(); $info['doc'] = Db::name('Category')->where('type', 2)->sum('items'); list($xAxisData, $seriesData) = $this->getAdminPostData(); $this->assign('xAxisData', $xAxisData); $this->assign('seriesData', $seriesData); $this->assign('info', $info); return $this->fetch(); } } protected function getAdminPostData($date = '') { if ($date) { list($start, $end) = explode(' - ', $date); $start_time = strtotime($start); $end_time = strtotime($end); } else { $start_time = \util\Date::unixtime('day', 0, 'begin'); $end_time = \util\Date::unixtime('day', 0, 'end'); } $diff_time = $end_time - $start_time; $format = '%Y-%m-%d'; if ($diff_time > 86400 * 30 * 2) { $format = '%Y-%m'; } else { if ($diff_time > 86400) { $format = '%Y-%m-%d'; } else { $format = '%H:00'; } } //获取所有表名 $models = Db::name('model')->where(['module' => 'cms', 'status' => 1])->select(); $list = $xAxisData = $seriesData = []; if (count($models) > 0) { $table1 = $models[0]['tablename']; unset($models[0]); $field = 'a.username,uid,FROM_UNIXTIME(b.create_time, "' . $format . '") as create_times,COUNT(*) AS num'; $dbObj = Db::name($table1)->alias('b')->field($field)->where('b.create_time', 'between time', [$start_time, $end_time])->join('admin a', 'a.id = b.uid'); foreach ($models as $k => $v) { $dbObj->union(function ($query) use ($field, $start_time, $end_time, $v) { $query->name($v['tablename'])->alias('b')->field($field)->where('b.create_time', 'between time', [$start_time, $end_time])->join('admin a', 'a.id = b.uid')->group('uid,create_times'); }); }; $res = $dbObj->group('uid,create_times')->select(); if ($diff_time > 84600 * 30 * 2) { $start_time = strtotime('last month', $start_time); while (($start_time = strtotime('next month', $start_time)) <= $end_time) { $column[] = date('Y-m', $start_time); } } else { if ($diff_time > 86400) { for ($time = $start_time; $time <= $end_time;) { $column[] = date("Y-m-d", $time); $time += 86400; } } else { for ($time = $start_time; $time <= $end_time;) { $column[] = date("H:00", $time); $time += 3600; } } } $xAxisData = array_fill_keys($column, 0); foreach ($res as $k => $v) { if (!isset($list[$v['username']])) { $list[$v['username']] = $xAxisData; } $list[$v['username']][$v['create_times']] = $v['num']; } foreach ($list as $index => $item) { $seriesData[] = [ 'name' => $index, 'type' => 'line', 'smooth' => true, 'areaStyle' => [], 'data' => array_values($item), ]; } } return [array_keys($xAxisData), $seriesData]; } /** * 排序 */ public function listorder() { $this->check_priv('listorder'); $catid = $this->request->param('catid/d', 0); $id = $this->request->param('id/d', 0); $listorder = $this->request->param('value/d', 0); $modelid = getCategory($catid, 'modelid'); $modelCache = cache("Model"); if (empty($modelCache[$modelid])) { return false; }; $tableName = $modelCache[$modelid]['tablename']; if (Db::name($tableName)->where('id', $id)->update(['listorder' => $listorder])) { $this->success("排序成功!"); } else { $this->error("排序失败!"); } } //回收站 public function recycle() { $catid = $this->request->param('catid/d', 0); //当前栏目信息 $catInfo = getCategory($catid); if (empty($catInfo)) { $this->error('该栏目不存在!'); } //栏目所属模型 $modelid = $catInfo['modelid']; if ($this->request->isAjax()) { $modelCache = cache("Model"); $tableName = $modelCache[$modelid]['tablename']; $this->modelClass = Db::name($tableName); list($page, $limit, $where) = $this->buildTableParames(); $conditions = [ ['catid', '=', $catid], ['status', '=', -1], ]; $total = Db::name($tableName)->where($where)->where($conditions)->count(); $_list = Db::name($tableName)->where($where)->page($page, $limit)->where($conditions)->order('listorder DESC, id DESC')->select(); $result = ["code" => 0, "count" => $total, "data" => $_list]; return json($result); } $this->assign('catid', $catid); return $this->fetch(); } //还原回收站 public function restore() { $catid = $this->request->param('catid/d', 0); //当前栏目信息 $catInfo = getCategory($catid); if (empty($catInfo)) { $this->error('该栏目不存在!'); } //栏目所属模型 $modelid = $catInfo['modelid']; $ids = $this->request->param('id/a'); $modelInfo = cache('Model'); $modelInfo = $modelInfo[$modelid]; if ($ids) { if (!is_array($ids)) { $ids = [0 => $ids]; } Db::name($modelInfo['tablename'])->where('id', 'in', $ids)->setField('status', 1); } $this->success('还原成功!'); } //状态 public function setstate() { $this->check_priv('status'); $catid = $this->request->param('catid/d', 0); $id = $this->request->param('id/d', 0); $status = $this->request->param('value/d'); $modelid = getCategory($catid, 'modelid'); $modelCache = cache("Model"); if (empty($modelCache[$modelid])) { return false; }; $tableName = ucwords($modelCache[$modelid]['tablename']); if (Db::name($tableName)->where('id', $id)->update(['status' => $status])) { //更新栏目缓存 cache('Category', null); getCategory($id, '', true); $data = Db::name($tableName)->where('id', $id)->find(); //标签 hook('content_edit_end', $data); $this->success('操作成功!'); } else { $this->error('操作失败!'); } } public function check_title() { $title = $this->request->param('data/s', ''); $catid = $this->request->param('catid/d', 0); $id = $this->request->param('id/d', 0); if (empty($title)) { $this->success('标题没有重复!'); return false; } $modelid = getCategory($catid, 'modelid'); $modelCache = cache("Model"); if (empty($modelCache[$modelid])) { $this->error('模型不存在!'); return false; }; $tableName = ucwords($modelCache[$modelid]['tablename']); $repeat = Db::name($tableName)->where('title', $title); empty($id) ?: $repeat->where('id', '<>', $id); if ($repeat->find()) { $this->error('标题有重复!'); } else { $this->success('标题没有重复!'); } } //批量更新 public function multi() { // 管理员禁止批量操作 $this->error(); } protected function check_priv($action) { if ($this->auth->isAdministrator() !== true) { if (0 !== (int) $this->cmsConfig['site_category_auth']) { $catid = $this->request->param('catid/d', 0); $action = getCategory($catid, 'type') == 1 ? 'init' : $action; $priv_datas = Db::name('CategoryPriv')->where(['catid' => $catid, 'is_admin' => 1, 'roleid' => $this->auth->roleid, 'action' => $action])->find(); if (empty($priv_datas)) { $this->error('您没有操作该项的权限!'); } } } } }