all(); } if (!is_array($fieldList)) { return $fieldList; } foreach ($fieldList as $key => $field) { $fieldName = is_array($field) ? ($field['name'] ?? '') : ($field->name ?? ''); if ($fieldName !== 'project_id') { continue; } $projectOptions = $this->getProjectOptions($currentLang); if (is_array($field)) { $field['type'] = 'select'; $field['options'] = $projectOptions; $field['setting']['options'] = $projectOptions; $fieldList[$key] = $field; } else { $field->type = 'select'; $field->options = $projectOptions; $setting = $field->setting ?: []; $setting['options'] = $projectOptions; $field->setting = $setting; $fieldList[$key] = $field; } } return $fieldList; } protected function getProjectOptions($currentLang = null) { static $cache = []; $cacheKey = (string)($currentLang ?: ''); if (isset($cache[$cacheKey])) { return $cache[$cacheKey]; } $options = []; try { $projectModel = Models::where('status', 1) ->where(function ($query) { $query->where('tablename', 'cms_product') ->whereOr('name', '产品模型'); }) ->order('id', 'asc') ->find(); if (!$projectModel) { $projectModel = Models::where('tablename', 'project')->where('status', 1)->find(); } if (!$projectModel) { return $cache[$cacheKey] = $options; } $tableName = $this->physicalTableName($projectModel['tablename']); $extTable = $tableName . '_data'; $defaultLang = LangModel::getDefaultMark(); $lang = $currentLang ?: $defaultLang; $mainFields = array_map('strtolower', array_keys(Db::getFields($tableName))); $extFields = Db::hasTable($extTable) ? array_map('strtolower', array_keys(Db::getFields($extTable))) : []; $hasStatus = in_array('status', $mainFields, true); $hasMainTitle = in_array('title', $mainFields, true); $hasMainName = in_array('name', $mainFields, true); $hasLang = in_array('lang', $extFields, true); $hasTitle = in_array('title', $extFields, true); $hasName = in_array('name', $extFields, true); $query = Db::table($tableName)->order('id desc'); if ($hasStatus) { $query->where('status', 'in', [0, 1]); } $list = $query->select()->toArray(); foreach ($list as $item) { $label = 'ID:' . $item['id']; if ($hasMainTitle && !empty($item['title'])) { $label = $item['title']; } elseif ($hasMainName && !empty($item['name'])) { $label = $item['name']; } if ($extFields) { $extQuery = Db::table($extTable)->where('did', $item['id']); if ($hasLang) { $extQuery->where('lang', $lang); } $ext = $extQuery->find(); if (!$ext && $hasLang && $lang !== $defaultLang) { $ext = Db::table($extTable)->where(['did' => $item['id'], 'lang' => $defaultLang])->find(); } if ($ext) { if ($hasTitle && !empty($ext['title'])) { $label = $ext['title']; } elseif ($hasName && !empty($ext['name'])) { $label = $ext['name']; } } } $options[$item['id']] = $label . ' [#' . $item['id'] . ']'; } } catch (Throwable $e) { return $cache[$cacheKey] = $options; } return $cache[$cacheKey] = $options; } protected $noNeedRight = ['check_title', 'panl', 'classlist']; protected $CmsModel = null; protected $cmsConfig = []; protected function initialize() { parent::initialize(); $this->CmsModel = new CmsModel; $this->cmsConfig = get_addon_config('cms'); $this->assign('cmsConfig', $this->cmsConfig); if (strtolower($this->request->action()) === 'classlist') { $config = $this->view->config ?: []; $config['jsname'] = 'backend/cms/cms'; $this->view->config = $config; $this->assign('config', $config); } } public function index() { $isAdministrator = $this->auth->isAdministrator(); $categoryList = $priv_catids = []; if (0 !== (int) $this->cmsConfig['category_auth'] && $isAdministrator !== true) { $role_id = $this->auth->getChildrenGroupIds(true); $priv_result = CategoryPrivModel::whereIn('roleid', $role_id)->where(['action' => 'init'])->select()->toArray(); foreach ($priv_result as $_v) { $priv_catids[] = $_v['catid']; } } $currentLang = LangModel::getDefaultMark(); $categorys = CategoryModel::order('listorder DESC, id DESC')->select()->toArray(); foreach ($categorys as $rs) { $state = ['opened' => true, 'disabled' => false]; $icon = 'iconfont icon-list-check'; if (0 !== (int) $this->cmsConfig['category_auth'] && $isAdministrator !== true && !in_array($rs['id'], $priv_catids)) { $arrchildid = explode(',', $rs['arrchildid']); $array_intersect = array_intersect($priv_catids, $arrchildid); if (empty($array_intersect)) { continue; } } if ($rs['type'] == 3) { $state['disabled'] = true; $icon = 'iconfont icon-links-line'; } elseif ($rs['type'] == 1) { $icon = 'iconfont icon-file-line'; } $translate = CategoryModel::getTranslate($rs['id'], $currentLang); $translate = $translate ? $translate->toArray() : []; $data = [ 'id' => $rs['id'], 'parent' => $rs['parentid'] ?: '#', 'text' => $translate['catname'] ?? $rs['catname'], 'icon' => $icon, 'state' => $state, ]; if ($rs['type'] == 2) { $data['url'] = (string) url('classlist', ['catid' => $rs['id']]); } if ($rs['type'] == 1) { $data['url'] = (string) url('cms.page/edit', ['catid' => $rs['id']]); } $categoryList[] = $data; } $this->assignconfig('categoryList', $categoryList); return $this->fetch(); } public function classlist() { $catid = $this->request->param('catid/d', 0); $catInfo = CategoryModel::find($catid); if (empty($catInfo)) { $this->error('该栏目不存在!'); } $modelid = $catInfo['modelid']; if ($this->request->isAjax()) { $model = Models::find($modelid); if (!$model['status']) { $this->error('模型被禁用!'); } $this->modelClass = Db::table($this->physicalTableName($model['tablename'])); if ($this->request->request('keyField')) { return $this->selectpage(); } list($page, $limit, $where) = $this->buildTableParames(); $conditions = [['catid', '=', $catid], ['status', 'in', [0, 1]]]; $total = Db::table($this->physicalTableName($model['tablename']))->where($where)->where($conditions)->count(); $list = Db::table($this->physicalTableName($model['tablename']))->page($page, $limit)->where($where)->where($conditions)->order('listorder DESC, id DESC')->select(); $enabledLangs = LangModel::getEnabledList(); $currentLang = $this->request->param('lang/s', LangModel::getDefaultMark()); $extTable = $this->physicalTableName($model['tablename']) . '_data'; $extFields = array_map('strtolower', array_keys(Db::getFields($extTable))); $hasLangColumn = in_array('lang', $extFields, true); $hasTitleColumn = in_array('title', $extFields, true); $_list = []; foreach ($list as $v) { $v['update_time'] = date('Y-m-d H:i:s', $v['update_time']); $v['url'] = buildContentUrl($v['catid'], $v['id'], $v['url']); $statusMap = []; foreach ($enabledLangs as $langItem) { if ($hasLangColumn) { $statusMap[$langItem->mark] = Db::table($extTable)->where(['did' => $v['id'], 'lang' => $langItem->mark])->count() > 0 ? 1 : 0; } else { $statusMap[$langItem->mark] = Db::table($extTable)->where(['did' => $v['id']])->count() > 0 ? 1 : 0; } } $v['lang_status'] = $statusMap; if ($hasTitleColumn) { if ($hasLangColumn) { $langRow = Db::table($extTable)->where(['did' => $v['id'], 'lang' => $currentLang])->find(); if (!$langRow && $currentLang !== LangModel::getDefaultMark()) { $langRow = Db::table($extTable)->where(['did' => $v['id'], 'lang' => LangModel::getDefaultMark()])->find(); } } else { $langRow = Db::table($extTable)->where(['did' => $v['id']])->find(); } if ($langRow && isset($langRow['title']) && $langRow['title'] !== '') { $v['title'] = $langRow['title']; } } $_list[] = $v; } return json(['code' => 0, 'count' => $total, 'data' => $_list]); } $tree = new \util\Tree(); $tree->icon = ['   │ ', '   ├─ ', '   └─ ']; $tree->nbsp = '   '; $categorys = []; $result = CategoryModel::order('listorder DESC, id DESC')->select()->toArray(); foreach ($result as $k => $v) { if ($v['type'] != 2 || ($modelid && $modelid != $v['modelid'])) { $v['disabled'] = '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); $this->assignconfig('catid', $catid); $langOptions = []; foreach (LangModel::getEnabledList() as $item) { $langOptions[] = [ 'mark' => $item->mark, 'name' => $item->name, 'code' => $item->code, ]; } $this->assignconfig('enabledLangs', $langOptions); return $this->fetch(); } public function remove() { $this->check_priv('remove'); if ($this->request->isPost()) { $catid = $this->request->param('catid/d', 0); $ids = $this->request->param('ids/a'); $tocatid = $this->request->param('tocatid/d', 0); if (!$catid) $this->error('请指定栏目!'); if ($ids) { if ($tocatid == $catid) $this->error('目标栏目和当前栏目是同一个栏目!'); $catInfo = CategoryModel::find($catid); if (empty($catInfo)) $this->error('该栏目不存在!'); $model = Models::find($catInfo['modelid']); if (!$model['status']) $this->error('模型被禁用!'); Db::table($this->physicalTableName($model['tablename']))->where('id', 'in', $ids)->update(['catid' => $tocatid]); $row = CategoryModel::find($catid); if ($row['items'] >= count($ids)) $row->dec('items', count($ids))->save(); CategoryModel::where('id', $tocatid)->inc('items', count($ids))->save(); Db::name('cms_content')->where('content_id', 'in', $ids)->update(['catid' => $tocatid]); $this->success('移动成功~'); } $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!'); $data['modelFieldExt'] = $data['modelFieldExt'] ?? []; try { $lang = $data['modelFieldExt']['lang'] ?? LangModel::getDefaultMark(); foreach (['title', 'keywords', 'description', 'tags'] as $field) { if (!isset($data['modelField'][$field]) && isset($data['modelFieldExt'][$field])) { $data['modelField'][$field] = $data['modelFieldExt'][$field]; } elseif (!isset($data['modelFieldExt'][$field]) && isset($data['modelField'][$field])) { $data['modelFieldExt'][$field] = $data['modelField'][$field]; } } $data['modelField']['sysadd'] = 1; $this->CmsModel->addModelData($data['modelField'], $data['modelFieldExt'], $lang); } catch (\Exception $ex) { $this->error($ex->getMessage()); } $this->success('操作成功!'); } $catid = $this->request->param('catid/d', 0); $category = CategoryModel::find($catid); if (empty($category)) $this->error('该栏目不存在!'); $modelid = $category['modelid']; $currentLang = LangModel::getDefaultMark(); $fieldList = Service::getFieldList($modelid, null, $currentLang); $fieldList = $this->normalizeProjectFieldOptions($fieldList, $currentLang); $this->assign([ 'catid' => $catid, 'fieldList' => $fieldList, 'langs' => LangModel::getEnabledList(), 'currentLang' => $currentLang, 'defaultLang' => LangModel::getDefaultMark(), ]); $this->assignconfig('catid', $catid); return $this->fetch(); } public function edit() { $this->check_priv('edit'); if ($this->request->isPost()) { $data = $this->request->post(); $data['modelFieldExt'] = $data['modelFieldExt'] ?? []; try { $lang = $data['modelFieldExt']['lang'] ?? LangModel::getDefaultMark(); if ($lang !== LangModel::getDefaultMark()) { foreach (['title', 'keywords', 'description', 'tags'] as $field) { if (isset($data['modelFieldExt'][$field])) { $data['modelField'][$field] = $data['modelFieldExt'][$field]; } elseif (isset($data['modelField'][$field])) { $data['modelFieldExt'][$field] = $data['modelField'][$field]; } } } $this->CmsModel->editModelData($data['modelField'], $data['modelFieldExt'], $lang); } catch (\Exception $ex) { $this->error($ex->getMessage()); } $this->success('编辑成功!'); } $catid = $this->request->param('catid/d', 0); $id = $this->request->param('id/d', 0); $lang = $this->request->param('lang/s', LangModel::getDefaultMark()); $category = CategoryModel::find($catid); if (empty($category)) $this->error('该栏目不存在!'); $modelid = $category['modelid']; $fieldList = Service::getFieldList($modelid, $id, $lang); $fieldList = $this->normalizeProjectFieldOptions($fieldList, $lang); $tablename = $this->physicalTableName(Models::where('id', $modelid)->value('tablename')); $extTable = $tablename . '_data'; $extFields = array_map('strtolower', array_keys(Db::getFields($extTable))); $hasLangColumn = in_array('lang', $extFields, true); $langStatus = []; foreach (LangModel::getEnabledList() as $item) { if ($hasLangColumn) { $langStatus[$item->mark] = Db::table($extTable)->where(['did' => $id, 'lang' => $item->mark])->count() > 0 ? 1 : 0; } else { $langStatus[$item->mark] = Db::table($extTable)->where(['did' => $id])->count() > 0 ? 1 : 0; } } $this->assign([ 'catid' => $catid, 'id' => $id, 'fieldList' => $fieldList, 'langs' => LangModel::getEnabledList(), 'currentLang' => $lang, 'defaultLang' => LangModel::getDefaultMark(), 'langStatus' => $langStatus, ]); $this->assignconfig('id', $id); $this->assignconfig('catid', $catid); return $this->fetch(); } 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]; try { foreach ($ids as $id) { $this->CmsModel->deleteModelData($catid, $id, $this->cmsConfig['site_recycle']); } } catch (\Exception $ex) { $this->error($ex->getMessage()); } $this->success('删除成功!'); } public function panl() { if ($this->request->isPost()) { $date = $this->request->post('date'); [$xAxisData, $seriesData] = $this->getAdminPostData($date); $this->success('', '', ['xAxisData' => $xAxisData, 'seriesData' => $seriesData]); } $info['category'] = CategoryModel::count(); $info['model'] = Db::name('cms_model')->where('module', 'cms')->count(); $info['tags'] = TagsModel::count(); $info['doc'] = CategoryModel::where('type', 2)->sum('items'); [$xAxisData, $seriesData] = $this->getAdminPostData(); $this->assignconfig('xAxisData', $xAxisData); $this->assignconfig('seriesData', $seriesData); $this->assign('info', $info); return $this->fetch(); } protected function getAdminPostData($date = '') { if ($date) { [$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 = $diff_time > 86400 * 30 * 2 ? '%Y-%m' : ($diff_time > 86400 ? '%Y-%m-%d' : '%H:00'); $models = Models::where(['module' => 'cms', 'status' => 1])->select(); $list = $xAxisData = $seriesData = []; if (count($models) > 0) { $table1 = $models[0]['tablename']; $table1Name = $models[0]['name']; unset($models[0]); $field = "'{$table1Name}' as table_name, FROM_UNIXTIME(create_time, '{$format}') as create_times, COUNT(*) AS num"; $dbObj = Db::name($table1)->field($field)->where('create_time', 'between time', [$start_time, $end_time])->group('create_times'); foreach ($models as $v) { $tableName = $v['name']; $dbObj->unionAll(function ($query) use ($format, $start_time, $end_time, $v, $tableName) { $query->name($v['tablename'])->field("'{$tableName}' as table_name, FROM_UNIXTIME(create_time, '{$format}') as create_times, COUNT(*) AS num")->where('create_time', 'between time', [$start_time, $end_time])->group('create_times'); }); } $res = $dbObj->select(); $column = []; 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); } elseif ($diff_time > 86400) { for ($time = $start_time; $time <= $end_time; $time += 86400) $column[] = date('Y-m-d', $time); } else { for ($time = $start_time; $time <= $end_time; $time += 3600) $column[] = date('H:00', $time); } $xAxisData = array_fill_keys($column, 0); foreach ($res as $v) { if (!isset($list[$v['table_name']])) $list[$v['table_name']] = $xAxisData; $list[$v['table_name']][$v['create_times']] = (int) $v['num']; } foreach ($list as $tableName => $data) { $seriesData[] = ['name' => $tableName, 'type' => 'line', 'smooth' => true, 'areaStyle' => [], 'data' => array_values($data)]; } } return [array_keys($xAxisData), $seriesData]; } public function recyclebin() { $catid = $this->request->param('catid/d', 0); $catInfo = CategoryModel::find($catid); if (empty($catInfo)) $this->error('该栏目不存在!'); $modelid = $catInfo['modelid']; if ($this->request->isAjax()) { $model = Models::find($modelid); if (!$model['status']) $this->error('模型被禁用!'); $this->modelClass = Db::table($this->physicalTableName($model['tablename'])); list($page, $limit, $where) = $this->buildTableParames(); $conditions = [['catid', '=', $catid], ['status', '=', -1]]; $total = Db::table($this->physicalTableName($model['tablename']))->where($where)->where($conditions)->count(); $list = Db::table($this->physicalTableName($model['tablename']))->where($where)->page($page, $limit)->where($conditions)->order('listorder DESC, id DESC')->select(); $_list = []; foreach ($list as $v) { $v['update_time'] = date('Y-m-d H:i:s', $v['update_time']); $_list[] = $v; } return json(['code' => 0, 'count' => $total, 'data' => $_list]); } $this->assignconfig('catid', $catid); return $this->fetch(); } public function restore() { $catid = $this->request->param('catid/d', 0); $catInfo = CategoryModel::find($catid); if (empty($catInfo)) $this->error('该栏目不存在!'); $modelid = $catInfo['modelid']; $ids = $this->request->param('id/a'); $model = Models::find($modelid); if (!$model['status']) $this->error('模型被禁用!'); if ($ids) { if (!is_array($ids)) $ids = [0 => $ids]; Db::table($this->physicalTableName($model['tablename']))->where('id', 'in', $ids)->update(['status' => 1]); } $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]; try { foreach ($ids as $id) { $this->CmsModel->deleteModelData($catid, $id); } } catch (\Exception $ex) { $this->error($ex->getMessage()); } $this->success('销毁成功!'); } public function multi() { $this->check_priv('status'); $catid = $this->request->param('catid/d', 0); $id = $this->request->param('id/d', 0); parse_str($this->request->param('param/s'), $values); $values = $this->auth->isAdministrator() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields))); if (empty($values)) $this->error('你没有权限操作!'); $catInfo = getCategory($catid); if (empty($catInfo)) $this->error('该栏目不存在!'); $tableName = $this->physicalTableName(Models::where('id', $catInfo['modelid'])->value('tablename')); Db::table($tableName)->where('id', $id)->update($values); getCategory($id, '', true); cms_cache('Category', null); $data = Db::table($tableName)->where('id', $id)->find(); if ($data['sysadd'] == 0 && isset($values['status'])) { CmsContent::where('catid', $catid)->where('content_id', $id)->update(['status' => $values['status']]); } $this->success('操作成功!'); } 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; } $catInfo = getCategory($catid); if (empty($catInfo)) $this->error('该栏目不存在!'); $tableName = $this->physicalTableName(Models::where('id', $catInfo['modelid'])->value('tablename')); $repeat = Db::table($tableName)->where('title', $title); empty($id) ?: $repeat->where('id', '<>', $id); if ($repeat->find()) { $this->error('标题有重复!'); } $this->success('标题没有重复!'); } protected function check_priv($action) { if (!$this->auth->isAdministrator() && 0 !== (int) $this->cmsConfig['category_auth']) { $catid = $this->request->param('catid/d', 0); $catInfo = CategoryModel::find($catid); $action = $catInfo['type'] == 1 ? 'init' : $action; $role_id = $this->auth->getChildrenGroupIds(true); $priv_datas = CategoryPrivModel::whereIn('roleid', $role_id)->where(['catid' => $catid, 'is_admin' => 1, 'action' => $action])->find(); if (empty($priv_datas)) { $this->error('您没有操作该项的权限!'); } } } }