Cms.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use addons\cms\library\Service;
  4. use app\admin\model\cms\Category as CategoryModel;
  5. use app\admin\model\cms\CategoryPriv as CategoryPrivModel;
  6. use app\admin\model\cms\Cms as CmsModel;
  7. use app\admin\model\cms\CmsContent;
  8. use app\admin\model\cms\Models;
  9. use app\admin\model\cms\Tags as TagsModel;
  10. use app\admin\model\general\Lang as LangModel;
  11. use app\common\controller\Backend;
  12. use think\facade\Config;
  13. use think\facade\Db;
  14. use Throwable;
  15. class Cms extends Backend
  16. {
  17. protected $searchFields = 'id,title';
  18. protected function physicalTableName($tableName)
  19. {
  20. $prefix = Config::get('database.connections.mysql.prefix', '');
  21. return strpos($tableName, $prefix) === 0 ? $tableName : $prefix . $tableName;
  22. }
  23. protected function normalizeProjectFieldOptions($fieldList, $currentLang = null)
  24. {
  25. if ($fieldList instanceof \think\Model || $fieldList instanceof \think\model\Pivot) {
  26. $fieldList = [$fieldList];
  27. }
  28. if ($fieldList instanceof \think\model\Collection || $fieldList instanceof \think\Collection) {
  29. $fieldList = $fieldList->all();
  30. }
  31. if (!is_array($fieldList)) {
  32. return $fieldList;
  33. }
  34. foreach ($fieldList as $key => $field) {
  35. $fieldName = is_array($field) ? ($field['name'] ?? '') : ($field->name ?? '');
  36. if ($fieldName !== 'project_id') {
  37. continue;
  38. }
  39. $projectOptions = $this->getProjectOptions($currentLang);
  40. if (is_array($field)) {
  41. $field['type'] = 'select';
  42. $field['options'] = $projectOptions;
  43. $field['setting']['options'] = $projectOptions;
  44. $fieldList[$key] = $field;
  45. } else {
  46. $field->type = 'select';
  47. $field->options = $projectOptions;
  48. $setting = $field->setting ?: [];
  49. $setting['options'] = $projectOptions;
  50. $field->setting = $setting;
  51. $fieldList[$key] = $field;
  52. }
  53. }
  54. return $fieldList;
  55. }
  56. protected function getProjectOptions($currentLang = null)
  57. {
  58. static $cache = [];
  59. $cacheKey = (string)($currentLang ?: '');
  60. if (isset($cache[$cacheKey])) {
  61. return $cache[$cacheKey];
  62. }
  63. $options = [];
  64. try {
  65. $projectModel = Models::where('status', 1)
  66. ->where(function ($query) {
  67. $query->where('tablename', 'cms_product')
  68. ->whereOr('name', '产品模型');
  69. })
  70. ->order('id', 'asc')
  71. ->find();
  72. if (!$projectModel) {
  73. $projectModel = Models::where('tablename', 'project')->where('status', 1)->find();
  74. }
  75. if (!$projectModel) {
  76. return $cache[$cacheKey] = $options;
  77. }
  78. $tableName = $this->physicalTableName($projectModel['tablename']);
  79. $extTable = $tableName . '_data';
  80. $defaultLang = LangModel::getDefaultMark();
  81. $lang = $currentLang ?: $defaultLang;
  82. $mainFields = array_map('strtolower', array_keys(Db::getFields($tableName)));
  83. $extFields = Db::hasTable($extTable) ? array_map('strtolower', array_keys(Db::getFields($extTable))) : [];
  84. $hasStatus = in_array('status', $mainFields, true);
  85. $hasMainTitle = in_array('title', $mainFields, true);
  86. $hasMainName = in_array('name', $mainFields, true);
  87. $hasLang = in_array('lang', $extFields, true);
  88. $hasTitle = in_array('title', $extFields, true);
  89. $hasName = in_array('name', $extFields, true);
  90. $query = Db::table($tableName)->order('id desc');
  91. if ($hasStatus) {
  92. $query->where('status', 'in', [0, 1]);
  93. }
  94. $list = $query->select()->toArray();
  95. foreach ($list as $item) {
  96. $label = 'ID:' . $item['id'];
  97. if ($hasMainTitle && !empty($item['title'])) {
  98. $label = $item['title'];
  99. } elseif ($hasMainName && !empty($item['name'])) {
  100. $label = $item['name'];
  101. }
  102. if ($extFields) {
  103. $extQuery = Db::table($extTable)->where('did', $item['id']);
  104. if ($hasLang) {
  105. $extQuery->where('lang', $lang);
  106. }
  107. $ext = $extQuery->find();
  108. if (!$ext && $hasLang && $lang !== $defaultLang) {
  109. $ext = Db::table($extTable)->where(['did' => $item['id'], 'lang' => $defaultLang])->find();
  110. }
  111. if ($ext) {
  112. if ($hasTitle && !empty($ext['title'])) {
  113. $label = $ext['title'];
  114. } elseif ($hasName && !empty($ext['name'])) {
  115. $label = $ext['name'];
  116. }
  117. }
  118. }
  119. $options[$item['id']] = $label . ' [#' . $item['id'] . ']';
  120. }
  121. } catch (Throwable $e) {
  122. return $cache[$cacheKey] = $options;
  123. }
  124. return $cache[$cacheKey] = $options;
  125. }
  126. protected $noNeedRight = ['check_title', 'panl', 'classlist'];
  127. protected $CmsModel = null;
  128. protected $cmsConfig = [];
  129. protected function initialize()
  130. {
  131. parent::initialize();
  132. $this->CmsModel = new CmsModel;
  133. $this->cmsConfig = get_addon_config('cms');
  134. $this->assign('cmsConfig', $this->cmsConfig);
  135. if (strtolower($this->request->action()) === 'classlist') {
  136. $config = $this->view->config ?: [];
  137. $config['jsname'] = 'backend/cms/cms';
  138. $this->view->config = $config;
  139. $this->assign('config', $config);
  140. }
  141. }
  142. public function index()
  143. {
  144. $isAdministrator = $this->auth->isAdministrator();
  145. $categoryList = $priv_catids = [];
  146. if (0 !== (int) $this->cmsConfig['category_auth'] && $isAdministrator !== true) {
  147. $role_id = $this->auth->getChildrenGroupIds(true);
  148. $priv_result = CategoryPrivModel::whereIn('roleid', $role_id)->where(['action' => 'init'])->select()->toArray();
  149. foreach ($priv_result as $_v) {
  150. $priv_catids[] = $_v['catid'];
  151. }
  152. }
  153. $currentLang = LangModel::getDefaultMark();
  154. $categorys = CategoryModel::order('listorder DESC, id DESC')->select()->toArray();
  155. foreach ($categorys as $rs) {
  156. $state = ['opened' => true, 'disabled' => false];
  157. $icon = 'iconfont icon-list-check';
  158. if (0 !== (int) $this->cmsConfig['category_auth'] && $isAdministrator !== true && !in_array($rs['id'], $priv_catids)) {
  159. $arrchildid = explode(',', $rs['arrchildid']);
  160. $array_intersect = array_intersect($priv_catids, $arrchildid);
  161. if (empty($array_intersect)) {
  162. continue;
  163. }
  164. }
  165. if ($rs['type'] == 3) {
  166. $state['disabled'] = true;
  167. $icon = 'iconfont icon-links-line';
  168. } elseif ($rs['type'] == 1) {
  169. $icon = 'iconfont icon-file-line';
  170. }
  171. $translate = CategoryModel::getTranslate($rs['id'], $currentLang);
  172. $translate = $translate ? $translate->toArray() : [];
  173. $data = [
  174. 'id' => $rs['id'],
  175. 'parent' => $rs['parentid'] ?: '#',
  176. 'text' => $translate['catname'] ?? $rs['catname'],
  177. 'icon' => $icon,
  178. 'state' => $state,
  179. ];
  180. if ($rs['type'] == 2) {
  181. $data['url'] = (string) url('classlist', ['catid' => $rs['id']]);
  182. }
  183. if ($rs['type'] == 1) {
  184. $data['url'] = (string) url('cms.page/edit', ['catid' => $rs['id']]);
  185. }
  186. $categoryList[] = $data;
  187. }
  188. $this->assignconfig('categoryList', $categoryList);
  189. return $this->fetch();
  190. }
  191. public function classlist()
  192. {
  193. $catid = $this->request->param('catid/d', 0);
  194. $catInfo = CategoryModel::find($catid);
  195. if (empty($catInfo)) {
  196. $this->error('该栏目不存在!');
  197. }
  198. $modelid = $catInfo['modelid'];
  199. if ($this->request->isAjax()) {
  200. $model = Models::find($modelid);
  201. if (!$model['status']) {
  202. $this->error('模型被禁用!');
  203. }
  204. $this->modelClass = Db::table($this->physicalTableName($model['tablename']));
  205. if ($this->request->request('keyField')) {
  206. return $this->selectpage();
  207. }
  208. list($page, $limit, $where) = $this->buildTableParames();
  209. $conditions = [['catid', '=', $catid], ['status', 'in', [0, 1]]];
  210. $total = Db::table($this->physicalTableName($model['tablename']))->where($where)->where($conditions)->count();
  211. $list = Db::table($this->physicalTableName($model['tablename']))->page($page, $limit)->where($where)->where($conditions)->order('listorder DESC, id DESC')->select();
  212. $enabledLangs = LangModel::getEnabledList();
  213. $currentLang = $this->request->param('lang/s', LangModel::getDefaultMark());
  214. $extTable = $this->physicalTableName($model['tablename']) . '_data';
  215. $extFields = array_map('strtolower', array_keys(Db::getFields($extTable)));
  216. $hasLangColumn = in_array('lang', $extFields, true);
  217. $hasTitleColumn = in_array('title', $extFields, true);
  218. $_list = [];
  219. foreach ($list as $v) {
  220. $v['update_time'] = date('Y-m-d H:i:s', $v['update_time']);
  221. $v['url'] = buildContentUrl($v['catid'], $v['id'], $v['url']);
  222. $statusMap = [];
  223. foreach ($enabledLangs as $langItem) {
  224. if ($hasLangColumn) {
  225. $statusMap[$langItem->mark] = Db::table($extTable)->where(['did' => $v['id'], 'lang' => $langItem->mark])->count() > 0 ? 1 : 0;
  226. } else {
  227. $statusMap[$langItem->mark] = Db::table($extTable)->where(['did' => $v['id']])->count() > 0 ? 1 : 0;
  228. }
  229. }
  230. $v['lang_status'] = $statusMap;
  231. if ($hasTitleColumn) {
  232. if ($hasLangColumn) {
  233. $langRow = Db::table($extTable)->where(['did' => $v['id'], 'lang' => $currentLang])->find();
  234. if (!$langRow && $currentLang !== LangModel::getDefaultMark()) {
  235. $langRow = Db::table($extTable)->where(['did' => $v['id'], 'lang' => LangModel::getDefaultMark()])->find();
  236. }
  237. } else {
  238. $langRow = Db::table($extTable)->where(['did' => $v['id']])->find();
  239. }
  240. if ($langRow && isset($langRow['title']) && $langRow['title'] !== '') {
  241. $v['title'] = $langRow['title'];
  242. }
  243. }
  244. $_list[] = $v;
  245. }
  246. return json(['code' => 0, 'count' => $total, 'data' => $_list]);
  247. }
  248. $tree = new \util\Tree();
  249. $tree->icon = ['&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ '];
  250. $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
  251. $categorys = [];
  252. $result = CategoryModel::order('listorder DESC, id DESC')->select()->toArray();
  253. foreach ($result as $k => $v) {
  254. if ($v['type'] != 2 || ($modelid && $modelid != $v['modelid'])) {
  255. $v['disabled'] = 'disabled';
  256. }
  257. $v['selected'] = $v['id'] == $catid ? 'selected' : '';
  258. $categorys[$k] = $v;
  259. }
  260. $str = '<option value=@id @selected @disabled>@spacer @catname</option>';
  261. $tree->init($categorys);
  262. $string = $tree->getTree(0, $str, $catid);
  263. $this->assign('string', $string);
  264. $this->assign('catid', $catid);
  265. $this->assignconfig('catid', $catid);
  266. $langOptions = [];
  267. foreach (LangModel::getEnabledList() as $item) {
  268. $langOptions[] = [
  269. 'mark' => $item->mark,
  270. 'name' => $item->name,
  271. 'code' => $item->code,
  272. ];
  273. }
  274. $this->assignconfig('enabledLangs', $langOptions);
  275. return $this->fetch();
  276. }
  277. public function remove()
  278. {
  279. $this->check_priv('remove');
  280. if ($this->request->isPost()) {
  281. $catid = $this->request->param('catid/d', 0);
  282. $ids = $this->request->param('ids/a');
  283. $tocatid = $this->request->param('tocatid/d', 0);
  284. if (!$catid) $this->error('请指定栏目!');
  285. if ($ids) {
  286. if ($tocatid == $catid) $this->error('目标栏目和当前栏目是同一个栏目!');
  287. $catInfo = CategoryModel::find($catid);
  288. if (empty($catInfo)) $this->error('该栏目不存在!');
  289. $model = Models::find($catInfo['modelid']);
  290. if (!$model['status']) $this->error('模型被禁用!');
  291. Db::table($this->physicalTableName($model['tablename']))->where('id', 'in', $ids)->update(['catid' => $tocatid]);
  292. $row = CategoryModel::find($catid);
  293. if ($row['items'] >= count($ids)) $row->dec('items', count($ids))->save();
  294. CategoryModel::where('id', $tocatid)->inc('items', count($ids))->save();
  295. Db::name('cms_content')->where('content_id', 'in', $ids)->update(['catid' => $tocatid]);
  296. $this->success('移动成功~');
  297. }
  298. $this->error('请选择需要移动的信息!');
  299. }
  300. }
  301. public function add()
  302. {
  303. $this->check_priv('add');
  304. if ($this->request->isPost()) {
  305. $data = $this->request->post();
  306. $catid = intval($data['modelField']['catid']);
  307. if (empty($catid)) $this->error('请指定栏目ID!');
  308. $data['modelFieldExt'] = $data['modelFieldExt'] ?? [];
  309. try {
  310. $lang = $data['modelFieldExt']['lang'] ?? LangModel::getDefaultMark();
  311. foreach (['title', 'keywords', 'description', 'tags'] as $field) {
  312. if (!isset($data['modelField'][$field]) && isset($data['modelFieldExt'][$field])) {
  313. $data['modelField'][$field] = $data['modelFieldExt'][$field];
  314. } elseif (!isset($data['modelFieldExt'][$field]) && isset($data['modelField'][$field])) {
  315. $data['modelFieldExt'][$field] = $data['modelField'][$field];
  316. }
  317. }
  318. $data['modelField']['sysadd'] = 1;
  319. $this->CmsModel->addModelData($data['modelField'], $data['modelFieldExt'], $lang);
  320. } catch (\Exception $ex) {
  321. $this->error($ex->getMessage());
  322. }
  323. $this->success('操作成功!');
  324. }
  325. $catid = $this->request->param('catid/d', 0);
  326. $category = CategoryModel::find($catid);
  327. if (empty($category)) $this->error('该栏目不存在!');
  328. $modelid = $category['modelid'];
  329. $currentLang = LangModel::getDefaultMark();
  330. $fieldList = Service::getFieldList($modelid, null, $currentLang);
  331. $fieldList = $this->normalizeProjectFieldOptions($fieldList, $currentLang);
  332. $this->assign([
  333. 'catid' => $catid,
  334. 'fieldList' => $fieldList,
  335. 'langs' => LangModel::getEnabledList(),
  336. 'currentLang' => $currentLang,
  337. 'defaultLang' => LangModel::getDefaultMark(),
  338. ]);
  339. $this->assignconfig('catid', $catid);
  340. return $this->fetch();
  341. }
  342. public function edit()
  343. {
  344. $this->check_priv('edit');
  345. if ($this->request->isPost()) {
  346. $data = $this->request->post();
  347. $data['modelFieldExt'] = $data['modelFieldExt'] ?? [];
  348. try {
  349. $lang = $data['modelFieldExt']['lang'] ?? LangModel::getDefaultMark();
  350. if ($lang !== LangModel::getDefaultMark()) {
  351. foreach (['title', 'keywords', 'description', 'tags'] as $field) {
  352. if (isset($data['modelFieldExt'][$field])) {
  353. $data['modelField'][$field] = $data['modelFieldExt'][$field];
  354. } elseif (isset($data['modelField'][$field])) {
  355. $data['modelFieldExt'][$field] = $data['modelField'][$field];
  356. }
  357. }
  358. }
  359. $this->CmsModel->editModelData($data['modelField'], $data['modelFieldExt'], $lang);
  360. } catch (\Exception $ex) {
  361. $this->error($ex->getMessage());
  362. }
  363. $this->success('编辑成功!');
  364. }
  365. $catid = $this->request->param('catid/d', 0);
  366. $id = $this->request->param('id/d', 0);
  367. $lang = $this->request->param('lang/s', LangModel::getDefaultMark());
  368. $category = CategoryModel::find($catid);
  369. if (empty($category)) $this->error('该栏目不存在!');
  370. $modelid = $category['modelid'];
  371. $fieldList = Service::getFieldList($modelid, $id, $lang);
  372. $fieldList = $this->normalizeProjectFieldOptions($fieldList, $lang);
  373. $tablename = $this->physicalTableName(Models::where('id', $modelid)->value('tablename'));
  374. $extTable = $tablename . '_data';
  375. $extFields = array_map('strtolower', array_keys(Db::getFields($extTable)));
  376. $hasLangColumn = in_array('lang', $extFields, true);
  377. $langStatus = [];
  378. foreach (LangModel::getEnabledList() as $item) {
  379. if ($hasLangColumn) {
  380. $langStatus[$item->mark] = Db::table($extTable)->where(['did' => $id, 'lang' => $item->mark])->count() > 0 ? 1 : 0;
  381. } else {
  382. $langStatus[$item->mark] = Db::table($extTable)->where(['did' => $id])->count() > 0 ? 1 : 0;
  383. }
  384. }
  385. $this->assign([
  386. 'catid' => $catid,
  387. 'id' => $id,
  388. 'fieldList' => $fieldList,
  389. 'langs' => LangModel::getEnabledList(),
  390. 'currentLang' => $lang,
  391. 'defaultLang' => LangModel::getDefaultMark(),
  392. 'langStatus' => $langStatus,
  393. ]);
  394. $this->assignconfig('id', $id);
  395. $this->assignconfig('catid', $catid);
  396. return $this->fetch();
  397. }
  398. public function del()
  399. {
  400. $this->check_priv('delete');
  401. $catid = $this->request->param('catid/d', 0);
  402. $ids = $this->request->param('id/a', null);
  403. if (empty($ids) || !$catid) $this->error('参数错误!');
  404. if (!is_array($ids)) $ids = [0 => $ids];
  405. try {
  406. foreach ($ids as $id) {
  407. $this->CmsModel->deleteModelData($catid, $id, $this->cmsConfig['site_recycle']);
  408. }
  409. } catch (\Exception $ex) {
  410. $this->error($ex->getMessage());
  411. }
  412. $this->success('删除成功!');
  413. }
  414. public function panl()
  415. {
  416. if ($this->request->isPost()) {
  417. $date = $this->request->post('date');
  418. [$xAxisData, $seriesData] = $this->getAdminPostData($date);
  419. $this->success('', '', ['xAxisData' => $xAxisData, 'seriesData' => $seriesData]);
  420. }
  421. $info['category'] = CategoryModel::count();
  422. $info['model'] = Db::name('cms_model')->where('module', 'cms')->count();
  423. $info['tags'] = TagsModel::count();
  424. $info['doc'] = CategoryModel::where('type', 2)->sum('items');
  425. [$xAxisData, $seriesData] = $this->getAdminPostData();
  426. $this->assignconfig('xAxisData', $xAxisData);
  427. $this->assignconfig('seriesData', $seriesData);
  428. $this->assign('info', $info);
  429. return $this->fetch();
  430. }
  431. protected function getAdminPostData($date = '')
  432. {
  433. if ($date) {
  434. [$start, $end] = explode(' - ', $date);
  435. $start_time = strtotime($start);
  436. $end_time = strtotime($end);
  437. } else {
  438. $start_time = \util\Date::unixtime('day', 0, 'begin');
  439. $end_time = \util\Date::unixtime('day', 0, 'end');
  440. }
  441. $diff_time = $end_time - $start_time;
  442. $format = $diff_time > 86400 * 30 * 2 ? '%Y-%m' : ($diff_time > 86400 ? '%Y-%m-%d' : '%H:00');
  443. $models = Models::where(['module' => 'cms', 'status' => 1])->select();
  444. $list = $xAxisData = $seriesData = [];
  445. if (count($models) > 0) {
  446. $table1 = $models[0]['tablename'];
  447. $table1Name = $models[0]['name'];
  448. unset($models[0]);
  449. $field = "'{$table1Name}' as table_name, FROM_UNIXTIME(create_time, '{$format}') as create_times, COUNT(*) AS num";
  450. $dbObj = Db::name($table1)->field($field)->where('create_time', 'between time', [$start_time, $end_time])->group('create_times');
  451. foreach ($models as $v) {
  452. $tableName = $v['name'];
  453. $dbObj->unionAll(function ($query) use ($format, $start_time, $end_time, $v, $tableName) {
  454. $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');
  455. });
  456. }
  457. $res = $dbObj->select();
  458. $column = [];
  459. if ($diff_time > 84600 * 30 * 2) {
  460. $start_time = strtotime('last month', $start_time);
  461. while (($start_time = strtotime('next month', $start_time)) <= $end_time) $column[] = date('Y-m', $start_time);
  462. } elseif ($diff_time > 86400) {
  463. for ($time = $start_time; $time <= $end_time; $time += 86400) $column[] = date('Y-m-d', $time);
  464. } else {
  465. for ($time = $start_time; $time <= $end_time; $time += 3600) $column[] = date('H:00', $time);
  466. }
  467. $xAxisData = array_fill_keys($column, 0);
  468. foreach ($res as $v) {
  469. if (!isset($list[$v['table_name']])) $list[$v['table_name']] = $xAxisData;
  470. $list[$v['table_name']][$v['create_times']] = (int) $v['num'];
  471. }
  472. foreach ($list as $tableName => $data) {
  473. $seriesData[] = ['name' => $tableName, 'type' => 'line', 'smooth' => true, 'areaStyle' => [], 'data' => array_values($data)];
  474. }
  475. }
  476. return [array_keys($xAxisData), $seriesData];
  477. }
  478. public function recyclebin()
  479. {
  480. $catid = $this->request->param('catid/d', 0);
  481. $catInfo = CategoryModel::find($catid);
  482. if (empty($catInfo)) $this->error('该栏目不存在!');
  483. $modelid = $catInfo['modelid'];
  484. if ($this->request->isAjax()) {
  485. $model = Models::find($modelid);
  486. if (!$model['status']) $this->error('模型被禁用!');
  487. $this->modelClass = Db::table($this->physicalTableName($model['tablename']));
  488. list($page, $limit, $where) = $this->buildTableParames();
  489. $conditions = [['catid', '=', $catid], ['status', '=', -1]];
  490. $total = Db::table($this->physicalTableName($model['tablename']))->where($where)->where($conditions)->count();
  491. $list = Db::table($this->physicalTableName($model['tablename']))->where($where)->page($page, $limit)->where($conditions)->order('listorder DESC, id DESC')->select();
  492. $_list = [];
  493. foreach ($list as $v) {
  494. $v['update_time'] = date('Y-m-d H:i:s', $v['update_time']);
  495. $_list[] = $v;
  496. }
  497. return json(['code' => 0, 'count' => $total, 'data' => $_list]);
  498. }
  499. $this->assignconfig('catid', $catid);
  500. return $this->fetch();
  501. }
  502. public function restore()
  503. {
  504. $catid = $this->request->param('catid/d', 0);
  505. $catInfo = CategoryModel::find($catid);
  506. if (empty($catInfo)) $this->error('该栏目不存在!');
  507. $modelid = $catInfo['modelid'];
  508. $ids = $this->request->param('id/a');
  509. $model = Models::find($modelid);
  510. if (!$model['status']) $this->error('模型被禁用!');
  511. if ($ids) {
  512. if (!is_array($ids)) $ids = [0 => $ids];
  513. Db::table($this->physicalTableName($model['tablename']))->where('id', 'in', $ids)->update(['status' => 1]);
  514. }
  515. $this->success('还原成功!');
  516. }
  517. public function destroy()
  518. {
  519. $catid = $this->request->param('catid/d', 0);
  520. $ids = $this->request->param('id/a', null);
  521. if (empty($ids) || !$catid) $this->error('参数错误!');
  522. if (!is_array($ids)) $ids = [0 => $ids];
  523. try {
  524. foreach ($ids as $id) {
  525. $this->CmsModel->deleteModelData($catid, $id);
  526. }
  527. } catch (\Exception $ex) {
  528. $this->error($ex->getMessage());
  529. }
  530. $this->success('销毁成功!');
  531. }
  532. public function multi()
  533. {
  534. $this->check_priv('status');
  535. $catid = $this->request->param('catid/d', 0);
  536. $id = $this->request->param('id/d', 0);
  537. parse_str($this->request->param('param/s'), $values);
  538. $values = $this->auth->isAdministrator() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  539. if (empty($values)) $this->error('你没有权限操作!');
  540. $catInfo = getCategory($catid);
  541. if (empty($catInfo)) $this->error('该栏目不存在!');
  542. $tableName = $this->physicalTableName(Models::where('id', $catInfo['modelid'])->value('tablename'));
  543. Db::table($tableName)->where('id', $id)->update($values);
  544. getCategory($id, '', true);
  545. cms_cache('Category', null);
  546. $data = Db::table($tableName)->where('id', $id)->find();
  547. if ($data['sysadd'] == 0 && isset($values['status'])) {
  548. CmsContent::where('catid', $catid)->where('content_id', $id)->update(['status' => $values['status']]);
  549. }
  550. $this->success('操作成功!');
  551. }
  552. public function check_title()
  553. {
  554. $title = $this->request->param('data/s', '');
  555. $catid = $this->request->param('catid/d', 0);
  556. $id = $this->request->param('id/d', 0);
  557. if (empty($title)) {
  558. $this->success('标题没有重复!');
  559. return false;
  560. }
  561. $catInfo = getCategory($catid);
  562. if (empty($catInfo)) $this->error('该栏目不存在!');
  563. $tableName = $this->physicalTableName(Models::where('id', $catInfo['modelid'])->value('tablename'));
  564. $repeat = Db::table($tableName)->where('title', $title);
  565. empty($id) ?: $repeat->where('id', '<>', $id);
  566. if ($repeat->find()) {
  567. $this->error('标题有重复!');
  568. }
  569. $this->success('标题没有重复!');
  570. }
  571. protected function check_priv($action)
  572. {
  573. if (!$this->auth->isAdministrator() && 0 !== (int) $this->cmsConfig['category_auth']) {
  574. $catid = $this->request->param('catid/d', 0);
  575. $catInfo = CategoryModel::find($catid);
  576. $action = $catInfo['type'] == 1 ? 'init' : $action;
  577. $role_id = $this->auth->getChildrenGroupIds(true);
  578. $priv_datas = CategoryPrivModel::whereIn('roleid', $role_id)->where(['catid' => $catid, 'is_admin' => 1, 'action' => $action])->find();
  579. if (empty($priv_datas)) {
  580. $this->error('您没有操作该项的权限!');
  581. }
  582. }
  583. }
  584. }