Articlecate.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2022/11/25
  6. * Time: 15:10
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\model\ArticleCate as cateModel;
  11. use app\common\model\Catalog as cataLogModel;
  12. use app\common\service\CmsService;
  13. use think\Db;
  14. class Articlecate extends Permissions
  15. {
  16. public function index()
  17. {
  18. if ($this->request->isAjax()) {
  19. $data = (new cateModel())->order('sort desc')->select();
  20. foreach ($data as $k => $v) {
  21. /**@var cateModel $v */
  22. $v['catalog'] = $v->getCatalogValues();
  23. $v['article_count'] = $v->article_count ?: '';
  24. try {
  25. $v['uri'] = $v->getUri();
  26. } catch (\Exception $e) {
  27. $v['uri'] = '';
  28. }
  29. $data[$k] = $v;
  30. }
  31. return $data;
  32. } else {
  33. return $this->fetch();
  34. }
  35. }
  36. public function publish()
  37. {
  38. $id = $this->request->param('id', 0, 'intval');
  39. $model = new cateModel();
  40. $post = $this->request->post();
  41. if ($this->request->isPost()) {
  42. $validate = new \think\Validate([
  43. ['title|分类名称', 'require'],
  44. ['pid', 'require', '请选择上级分类'],
  45. ]);
  46. if (!$validate->check($post)) {
  47. $this->error('提交失败:' . $validate->getError());
  48. }
  49. } else {
  50. //栏目
  51. $cataLogModel = new cataLogModel();
  52. $catalogs = $cataLogModel->where('type', cataLogModel::TYPE_ARTICLE_LIST)->select();
  53. $this->assign('catalogs', $catalogs);
  54. //分类
  55. $catelist = $model->order('sort desc')->select();
  56. $cates = $model->treelist($catelist);
  57. $this->assign('cates', $cates);
  58. }
  59. if ($id > 0) {
  60. //修改
  61. $cate = $model->where('id', $id)->find();
  62. if (empty($cate)) {
  63. $this->error('id不正确');
  64. }
  65. if ($this->request->isPost()) {
  66. if ($id == $post['pid']) {
  67. $this->error('上级节点不能选自己');
  68. }
  69. //保存中间表
  70. $res1 = $this->updateCatalog($post, $cate);
  71. //更新tree_path
  72. $post = $this->updatePath($post, $id);
  73. if (!$res1 && false == $cate->allowField(true)->save($post)) {
  74. $this->error('修改失败');
  75. } else {
  76. $this->success('修改成功', 'index');
  77. }
  78. } else {
  79. $this->assign('cate', $cate);
  80. return $this->fetch();
  81. }
  82. } else {
  83. //新增
  84. if ($this->request->isPost()) {
  85. $post['status'] = $model::STATUS_OPEN;
  86. if (false == $model->allowField(true)->save($post)) {
  87. $this->error('添加失败');
  88. } else {
  89. $cate = $model;
  90. //更新tree_path
  91. $post = $this->updatePath($post, $model->id);
  92. if (false == $cate->save(['tree_path' => $post['tree_path']])) {
  93. $this->error('更新tree_path失败');
  94. }
  95. //保存中间表
  96. $this->updateCatalog($post, $cate);
  97. $this->success('添加成功', 'index');
  98. }
  99. } else {
  100. $pid = $this->request->param('pid', null, 'intval');
  101. if (!empty($pid)) {
  102. $this->assign('pid', $pid);
  103. }
  104. return $this->fetch();
  105. }
  106. }
  107. }
  108. /**
  109. * 保存中间表
  110. * @param $post
  111. * @param $cate cateModel
  112. * @return bool [是否有修改]
  113. */
  114. private function updateCatalog($post, $cate)
  115. {
  116. if (isset($post['catalog_ids'])) {
  117. $catalog_ids = explode(',', $post['catalog_ids']);
  118. $old_catalog_ids = $cate->getCatalogValues('id');
  119. if ($old_catalog_ids == $post['catalog_ids']) {
  120. return false;
  121. }
  122. foreach ($cate->catalogs as $catalog) {
  123. if (!in_array($catalog->id, $catalog_ids)) {
  124. $cate->catalogs()->detach($catalog->id);// 删除中间表数据
  125. }
  126. }
  127. if (false == $cate->catalogs()->attach($catalog_ids)) {//插入数据
  128. $this->error('保存中间表失败');
  129. }
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * 更新tree_path
  136. * @param $post
  137. * @param $id
  138. * @return mixed
  139. */
  140. private function updatePath($post, $id)
  141. {
  142. if ($post['pid']) {
  143. $pdoc = (new cateModel())->find($post['pid']);
  144. if (!$pdoc) {
  145. $this->error('该上级节点不存在,请重新选择');
  146. }
  147. $post['tree_path'] = $pdoc->tree_path . '-' . $id;
  148. } else {
  149. $post['tree_path'] = $id;
  150. }
  151. return $post;
  152. }
  153. /**
  154. * 删除
  155. */
  156. public function delete()
  157. {
  158. if ($this->request->isAjax()) {
  159. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  160. if (Db::name('article_cate')->where('pid', $id)->count() == 0) {
  161. if (false == Db::name('article_cate')->where('id', $id)->delete()) {
  162. $this->error('删除失败');
  163. } else {
  164. $this->success('删除成功', 'index');
  165. }
  166. } else {
  167. $this->error('请先删除子节点');
  168. }
  169. }
  170. }
  171. /**
  172. * 审核
  173. */
  174. public function status()
  175. {
  176. if ($this->request->isPost()) {
  177. $post = $this->request->post();
  178. if (false == Db::name('article_cate')->where('id', $post['id'])->update(['status' => $post['status']])) {
  179. $this->error('设置失败');
  180. } else {
  181. $this->success('设置成功', 'index');
  182. }
  183. }
  184. }
  185. /**
  186. * 排序
  187. */
  188. public function sort()
  189. {
  190. if ($this->request->isPost() && $this->request->has('ids')) {
  191. $post = $this->request->post();
  192. $i = 0;
  193. foreach ($post['ids'] as $k => $id) {
  194. $sort = Db::name('article_cate')->where('id', $id)->value('sort');
  195. $newsort = $post['sorts'][$k]??$sort;
  196. if ($sort != $newsort) {
  197. if (false == Db::name('article_cate')->where('id', $id)->update(['sort' => $newsort])) {
  198. $this->error('更新失败');
  199. } else {
  200. $i++;
  201. }
  202. }
  203. }
  204. $this->success('成功更新' . $i . '个数据', 'index');
  205. } else {
  206. $this->error('无数据更新', 'index');
  207. }
  208. }
  209. /**
  210. * 更新所有节点tree_path
  211. * admin/Articlecate/updateTreePath
  212. */
  213. public function updateTreePath()
  214. {
  215. (new cateModel())->updateTreePath();
  216. return $this->success();
  217. }
  218. //预览
  219. public function perview()
  220. {
  221. $id = $this->request->param('id', 0, 'intval');
  222. if ($id) {
  223. $articleCate = (new cateModel())->where('id', $id)->find();
  224. return (new CmsService())->perviewArticleCate($articleCate);
  225. }
  226. $this->error('预览失败:id is null');
  227. }
  228. }