GalleryCate.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\GalleryCate as GalleryCateModel;
  11. use app\admin\validate\GalleryCateValidate;
  12. use think\exception\ValidateException;
  13. use think\facade\Db;
  14. use think\facade\View;
  15. class GalleryCate extends BaseController
  16. {
  17. /**
  18. * 构造函数
  19. */
  20. public function __construct()
  21. {
  22. $this->model = new GalleryCateModel();
  23. }
  24. /**
  25. * 数据列表
  26. */
  27. public function datalist()
  28. {
  29. if (request()->isAjax()) {
  30. $list = $this->model->where('delete_time',0)->order('sort asc')->select();
  31. return to_assign(0, '', $list);
  32. }
  33. else{
  34. return view();
  35. }
  36. }
  37. //获取子分类id.$is_self=1包含自己
  38. public function get_cate_son($id = 0, $is_self = 1)
  39. {
  40. $cates = $this->model->where('delete_time',0)->order('sort asc')->select()->toArray();
  41. $cates_list = get_data_node($cates, $id);
  42. $cates_array = array_column($cates_list, 'id');
  43. if ($is_self == 1) {
  44. //包括自己在内
  45. $cates_array[] = $id;
  46. }
  47. return $cates_array;
  48. }
  49. /**
  50. * 添加
  51. */
  52. public function add()
  53. {
  54. if (request()->isAjax()) {
  55. $param = get_params();
  56. // 检验完整性
  57. try {
  58. validate(GalleryCateValidate::class)->check($param);
  59. } catch (ValidateException $e) {
  60. // 验证失败 输出错误信息
  61. return to_assign(1, $e->getError());
  62. }
  63. $this->model->addGalleryCate($param);
  64. }else{
  65. $pid = isset($param['pid']) ? $param['pid'] : 0;
  66. View::assign('pid', $pid);
  67. return view();
  68. }
  69. }
  70. /**
  71. * 编辑
  72. */
  73. public function edit()
  74. {
  75. $param = get_params();
  76. if (request()->isAjax()) {
  77. // 检验完整性
  78. try {
  79. validate(GalleryCateValidate::class)->check($param);
  80. } catch (ValidateException $e) {
  81. // 验证失败 输出错误信息
  82. return to_assign(1, $e->getError());
  83. }
  84. $cate_array = $this->get_cate_son($param['id']);
  85. if (in_array($param['pid'], $cate_array)) {
  86. return to_assign(1, '上级分类不能是该分类本身或其子分类');
  87. }
  88. $this->model->editGalleryCate($param);
  89. }else{
  90. $id = isset($param['id']) ? $param['id'] : 0;
  91. $detail = $this->model->getGalleryCateById($id);
  92. if (!empty($detail)) {
  93. View::assign('detail', $detail);
  94. return view();
  95. }
  96. else{
  97. throw new \think\exception\HttpException(404, '找不到页面');
  98. }
  99. }
  100. }
  101. /**
  102. * 查看信息
  103. */
  104. public function read()
  105. {
  106. $param = get_params();
  107. $id = isset($param['id']) ? $param['id'] : 0;
  108. $detail = $this->model->getGalleryCateById($id);
  109. if (!empty($detail)) {
  110. View::assign('detail', $detail);
  111. return view();
  112. }
  113. else{
  114. throw new \think\exception\HttpException(404, '找不到页面');
  115. }
  116. }
  117. /**
  118. * 删除
  119. */
  120. public function del()
  121. {
  122. $param = get_params();
  123. $id = isset($param['id']) ? $param['id'] : 0;
  124. $type = isset($param['type']) ? $param['type'] : 0;
  125. $count_cate = Db::name('GalleryCate')->where(["pid"=>$id,"delete_time"=>0])->count();
  126. if ($count_cate > 0) {
  127. return to_assign(1, "该分类下还有子分类,无法删除");
  128. }
  129. $count_gallery = Db::name('Gallery')->where(["cate_id" => $id,"delete_time"=>0])->count();
  130. if ($count_gallery > 0) {
  131. return to_assign(1, "该分类下还有图集,无法删除");
  132. }
  133. $this->model->delGalleryCateById($id,$type);
  134. }
  135. }