Catalog.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\model\Article as articleModel;
  11. use app\common\model\ArticleCate as articleCateModel;
  12. use app\common\model\Catalog as cataLogModel;
  13. use app\common\model\Templet as templetModel;
  14. use app\common\service\CmsService;
  15. use file\PathHelper;
  16. use paginate\Bootstrap;
  17. use think\Db;
  18. class Catalog extends Permissions
  19. {
  20. /**
  21. * index2 使用新的树形表格组件
  22. * @return array|mixed
  23. */
  24. public function index()
  25. {
  26. if ($this->request->isAjax()) {
  27. $model = new cataLogModel();
  28. $data = $model->order('pid,sort desc,id asc')->select();
  29. foreach ($data as $k => $v) {
  30. /**@var cataLogModel $v */
  31. $v['type_text'] = $v->type_text;
  32. $v['article_count'] = $v->article_count ?: '';
  33. $v['tpath'] = $v->tpath();
  34. $data[$k] = $v;
  35. }
  36. return array('code' => 0, 'count' => count($data), 'data' => $data, "tip" => "操作成功!");
  37. } else {
  38. return $this->fetch();
  39. }
  40. }
  41. public function publish()
  42. {
  43. $id = $this->request->param('id', 0, 'intval');
  44. $post = $this->request->post();
  45. $model = new cataLogModel();
  46. if ($this->request->isPost()) {
  47. $validate = new \think\Validate([
  48. ['title|栏目名称', 'require'],
  49. ['catalog_templet|栏目模板', 'requireIf:type,0|requireIf:type,1'],
  50. ['article_templet|文章模板', 'requireIf:type,1'],
  51. ['path|栏目路径', 'requireIf:type,0|requireIf:type,1'],
  52. ['article_rule|文章路径', 'requireIf:type,1'],
  53. ['articlelist_rule|列表页路径', 'requireIf:type,1'],
  54. ]);
  55. if (!$validate->check($post)) {
  56. $this->error('提交失败:' . $validate->getError());
  57. }
  58. $post['path'] = trim($post['path']);
  59. $post['path'] = empty($post['path']) ? "" : appendStartDS(trim($post['path']), '/');
  60. $post['catalog_templet'] = trim($post['catalog_templet']);
  61. $post['article_templet'] = trim($post['article_templet']);
  62. $post['article_rule'] = trim($post['article_rule']);
  63. $post['articlelist_rule'] = trim($post['articlelist_rule']);
  64. $post['articlelist_rows'] = empty($post['articlelist_rows']) ? 10 : $post['articlelist_rows'];
  65. } else {
  66. $pid = $this->request->param('pid', 0, 'intval');
  67. $this->assign('pid', $pid);
  68. $this->assign("templets", templetModel::getTemplets());
  69. $this->assign('cates', $model->treelistForCatalog());
  70. $this->assign('types', cataLogModel::TYPES);
  71. }
  72. if ($id > 0) {
  73. //修改
  74. $item = $model->where('id', $id)->find();
  75. if (empty($item)) {
  76. $this->error('id不存在');
  77. }
  78. if ($this->request->isPost()) {
  79. if ($id == $post['pid']) {
  80. $this->error('上级节点不能选自己');
  81. }
  82. //更新tree_path
  83. $post = $this->updatePath($post, $id);
  84. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  85. $this->error('修改失败');
  86. } else {
  87. $this->success('修改成功', 'index');
  88. }
  89. } else {
  90. $this->assign('item', $item);
  91. return $this->fetch();
  92. }
  93. } else {
  94. //新增
  95. if ($this->request->isPost()) {
  96. $post['status'] = $model::STATUS_OPEN;
  97. if (false == $model->allowField(true)->save($post)) {
  98. $this->error('添加失败');
  99. } else {
  100. $cate = $model;
  101. //更新tree_path
  102. $post = $this->updatePath($post, $model->id);
  103. if (false == $cate->save(['tree_path' => $post['tree_path']])) {
  104. $this->error('更新tree_path失败');
  105. }
  106. $this->success('添加成功', 'index');
  107. }
  108. } else {
  109. return $this->fetch();
  110. }
  111. }
  112. }
  113. /**
  114. * 更新tree_path
  115. * @param $post
  116. * @param $id
  117. * @return mixed
  118. */
  119. private function updatePath($post, $id)
  120. {
  121. if ($post['pid']) {
  122. $pdoc = (new cataLogModel())->find($post['pid']);
  123. if (!$pdoc) {
  124. $this->error('该上级节点不存在,请重新选择');
  125. }
  126. $post['tree_path'] = $pdoc->tree_path . '-' . $id;
  127. } else {
  128. $post['tree_path'] = $id;
  129. }
  130. return $post;
  131. }
  132. /**
  133. * 删除
  134. */
  135. public function delete()
  136. {
  137. if ($this->request->isAjax()) {
  138. $id = $this->request->param('id', 0, 'intval');
  139. $exits = cataLogModel::get(['pid' => $id]);
  140. if ($exits) {
  141. $this->error('请先删除子节点');
  142. }
  143. if (false == Db::name('catalog')->where('id', $id)->delete()) {
  144. $this->error('删除失败');
  145. } else {
  146. $this->success('删除成功', 'index');
  147. }
  148. }
  149. }
  150. /**
  151. * 审核
  152. */
  153. public function status()
  154. {
  155. if ($this->request->isPost()) {
  156. $post = $this->request->post();
  157. if (false == Db::name('catalog')->where('id', $post['id'])->update(['status' => $post['status']])) {
  158. $this->error('设置失败');
  159. } else {
  160. $this->success('设置成功', 'index');
  161. }
  162. }
  163. }
  164. /**
  165. * 排序
  166. */
  167. public function sort()
  168. {
  169. if ($this->request->isPost() && $this->request->has('ids')) {
  170. $post = $this->request->post();
  171. $i = 0;
  172. foreach ($post['ids'] as $k => $id) {
  173. $sort = Db::name('catalog')->where('id', $id)->value('sort');
  174. $newsort = $post['sorts'][$k]??$sort;
  175. if ($sort != $newsort) {
  176. if (false == Db::name('catalog')->where('id', $id)->update(['sort' => $newsort])) {
  177. $this->error('更新失败');
  178. } else {
  179. $i++;
  180. }
  181. }
  182. }
  183. if (empty($i)) {
  184. $this->error('没有更新排序', 'index');
  185. }
  186. $this->success('成功更新' . $i . '个排序', 'index');
  187. } else {
  188. $this->error('无数据更新', 'index');
  189. }
  190. }
  191. /**
  192. * 更新所有节点tree_path
  193. * admin/catalog/updateTreePath
  194. */
  195. public function updateTreePath()
  196. {
  197. (new cataLogModel())->updateTreePath();
  198. return $this->success();
  199. }
  200. //预览
  201. public function perview()
  202. {
  203. $id = $this->request->param('id', 0, 'intval');
  204. if ($id) {
  205. /** @var cataLogModel $catalog */
  206. $catalog = (new cataLogModel())->where('id', $id)->find();
  207. try {
  208. $cate = $catalog->getFirstArticleCate();
  209. } catch (\Exception $e) {
  210. $cate = null;
  211. }
  212. return (new CmsService())->perviewCatalog($catalog, $cate);
  213. }
  214. $this->error('预览失败:id is null');
  215. }
  216. /**
  217. * @param $where array
  218. * @param $catalog cataLogModel
  219. * @param $tid int
  220. * @param $cate articleCateModel
  221. */
  222. private function exportArticle($where, $catalog, $tid, $cate)
  223. {
  224. $replace = CmsService::getArticleReplace();
  225. $service = new CmsService();
  226. $articleModel = new articleModel();
  227. $catalogPath = $catalog->getRealPath();
  228. $total = $articleModel->where($where)->where($catalog->articlelist_where)->count();
  229. $listRows = $catalog->articlelist_rows??10;//分页
  230. $orderby = $catalog->articlelist_sort??'create_time desc';//排序
  231. if ($total) {
  232. $lastPage = (int)ceil($total / $listRows);
  233. //分页
  234. for ($page = 1; $page <= $lastPage; $page++) {
  235. $articleCatePath = articleCateModel::getRulePath($catalogPath, $catalog->articlelist_rule, $catalog->id, $tid, $page);
  236. $articleList = $articleModel->where($where)->where($catalog->articlelist_where)->page($page, $listRows)->order($orderby)->select();
  237. $prefix = isset($replace['_ROOT_']) ? $replace['_ROOT_'] : '_ROOT_';
  238. $paginate = new Bootstrap($articleList, $listRows, $page, $total, false, ["path" => $prefix . CmsService::getPaginatePath($catalog, $tid)]);
  239. $service->info(" ├─ 列表页:" . $articleCatePath);
  240. $content = $this->fetch($catalog->getCatalogTemplet()->getRealPath(), ['catalog' => $catalog, 'articleCate' => $cate, 'paginate' => $paginate], $replace);
  241. $service->createFile($articleCatePath, $content);
  242. foreach ($articleList as $article) {
  243. /**@var articleModel $article */
  244. $articlePath = $article->getRulePath($catalogPath, $catalog->article_rule);
  245. $content = $this->fetch($catalog->getArticleTemplet()->getRealPath(), ['catalog' => $catalog, 'article' => $article, 'articleCate' => $cate], $replace);
  246. $service->info(" ├─ 文章:" . $articlePath);
  247. $service->createFile($articlePath, $content);
  248. }
  249. }
  250. }
  251. $articleCatePath = articleCateModel::getRulePath($catalogPath, $catalog->articlelist_rule, $catalog->id, $tid);
  252. $content = file_exists($articleCatePath) ? $this->fetch($articleCatePath, [], $replace) : CmsService::errorHtml($articleCatePath);
  253. $dir = PathHelper::getDir($articleCatePath);
  254. $service->info(" ├─ 列表默认页:" . $dir . DS . "index.html");
  255. $service->createFile($dir . DS . "index.html", $content);
  256. }
  257. /**
  258. * 生成文章列表&文章页
  259. * @param $catalog cataLogModel
  260. * @param $cate
  261. */
  262. private function exportArticleList($catalog, $cate)
  263. {
  264. $where = [
  265. 'article_cate_id' => $cate->id,
  266. 'status' => articleModel::STATUS_OPEN,
  267. ];
  268. $this->exportArticle($where, $catalog, $cate->id, $cate);
  269. }
  270. /**
  271. * 生成栏目文章页
  272. * @param $catalog cataLogModel
  273. */
  274. private function exportCatalogArticle($catalog)
  275. {
  276. $where = [
  277. 'catalog_id' => $catalog->id,
  278. 'status' => articleModel::STATUS_OPEN,
  279. ];
  280. $this->exportArticle($where, $catalog, 0, null);
  281. }
  282. /**
  283. * 生成静态文件
  284. */
  285. public function exportHtml()
  286. {
  287. if (!$this->request->isAjax() && !$this->request->isCli()) {
  288. return;
  289. }
  290. if ($this->request->isCli()) {
  291. $ids = (new cataLogModel())->column('id');
  292. } else {
  293. $post = $this->request->param();
  294. if (!isset($post['ids']) || empty($post['ids'])) {
  295. $this->error('请选择栏目');
  296. }
  297. $ids = $post['ids'];
  298. }
  299. clear_temp_cache();//预览模式和生成模式切换时会缓存
  300. //全局对象
  301. $cataLogModel = new cataLogModel();
  302. $articleModel = new articleModel();
  303. $articleCateModel = new articleCateModel();
  304. $service = new CmsService();
  305. $this->assign([
  306. 'cataLogModel' => $cataLogModel,
  307. 'articleCateModel' => $articleCateModel,
  308. 'articleModel' => $articleModel,
  309. ]);
  310. $catalogs = $cataLogModel->where(['id' => ['in', $ids], 'status' => cataLogModel::STATUS_OPEN])->order('sort asc')->select();
  311. $replace = CmsService::getArticleReplace();
  312. try {
  313. /**@var cataLogModel $catalog */
  314. foreach ($catalogs as $catalog) {
  315. $service->info("栏目:" . $catalog->title);
  316. if ($catalog->type == cataLogModel::TYPE_DIRECTORY) {
  317. $service->info(" ├─ 类型:目录");
  318. continue;
  319. }
  320. $catalogPath = $catalog->getRealPath();
  321. if ($catalog->type == cataLogModel::TYPE_CATALOG) {
  322. $service->info(" ├─ 类型:栏目页:" . $catalogPath);
  323. $content = $this->fetch($catalog->getCatalogTemplet()->getRealPath(), ['catalog' => $catalog], $replace);
  324. $service->createFile($catalogPath, $content);
  325. } else {
  326. $articleCates = $catalog->articleCates;
  327. if (count($articleCates) > 0) {
  328. $service->info(" ├─ 类型:文章列表+分类");
  329. foreach ($articleCates as $cate) {
  330. $service->info(" ├─ 分类:" . $cate->title);
  331. $this->exportArticleList($catalog, $cate);
  332. }
  333. $service->info(" ├─ 栏目默认页:" . $catalogPath);
  334. $articleCatePath = articleCateModel::getRulePath($catalogPath, $catalog->articlelist_rule, $catalog->id, $articleCates[0]->id);
  335. $content = file_exists($articleCatePath) ? $this->fetch($articleCatePath, [], $replace) : CmsService::errorHtml($articleCatePath);
  336. $service->createFile($catalogPath, $content);
  337. } else {
  338. $service->info(" ├─ 类型:文章列表+栏目");
  339. if ($catalog->article_count > 0) {
  340. $this->exportCatalogArticle($catalog);
  341. } else {
  342. $service->info(" ├─ 栏目默认页:" . $catalogPath);
  343. $content = $this->fetch($catalog->getCatalogTemplet()->getRealPath(), ['catalog' => $catalog], $replace);
  344. $service->createFile($catalogPath, $content);
  345. }
  346. }
  347. }
  348. }
  349. if (!$this->request->has('delete')) {
  350. $service->copyUploads();
  351. }
  352. } catch (\Exception $e) {
  353. CmsService::errorlog($e);
  354. if ($this->request->isCli()) {
  355. return $service->info('执行失败 ' . $e->getMessage());
  356. } else {
  357. $this->error('执行失败 ' . $e->getMessage());
  358. }
  359. }
  360. if ($this->request->isCli()) {
  361. $service->info('执行成功');
  362. } else {
  363. $this->success('执行成功', 'index');
  364. }
  365. }
  366. }