123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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']) {
-
- 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);
-
- 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['selected'] = $v['id'] == $catid ? 'selected' : '';
- $categorys[$k] = $v;
- }
- $str = "<option value=@id @selected @disabled>@spacer @catname</option>";
- $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("请指定栏目!");
- }
-
- $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);
-
-
- 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('您没有操作该项的权限!');
- }
- }
- }
- }
-
- }
|