controller.tpl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\<module>\controller;
  15. use app\base\BaseController;
  16. use app\<module>\model\<Bmodel> as <Bmodel>Model;
  17. use app\<module>\validate\<Bcontroller>Validate;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\View;
  21. class <Bcontroller> extends BaseController
  22. {
  23. /**
  24. * 构造函数
  25. */
  26. protected $model;
  27. public function __construct()
  28. {
  29. parent::__construct(); // 调用父类构造函数
  30. $this->model = new <Bmodel>Model();
  31. }
  32. /**
  33. * 数据列表
  34. */
  35. public function datalist()
  36. {
  37. if (request()->isAjax()) {
  38. $list = $this->model->where('delete_time',0)->order('sort asc')->select();
  39. return to_assign(0, '', $list);
  40. }
  41. else{
  42. return view();
  43. }
  44. }
  45. /**
  46. * 子分类
  47. * $id
  48. * $is_self=1包含自己
  49. */
  50. public function sonlist($id = 0, $is_self = 1)
  51. {
  52. $cates = $this->model->where('delete_time',0)->order('sort asc')->select()->toArray();
  53. $cates_list = get_data_node($cates, $id);
  54. $cates_array = array_column($cates_list, 'id');
  55. if ($is_self == 1) {
  56. //包括自己在内
  57. $cates_array[] = $id;
  58. }
  59. return $cates_array;
  60. }
  61. /**
  62. * 添加/编辑
  63. */
  64. public function add()
  65. {
  66. $param = get_params();
  67. if (request()->isAjax()) {
  68. if (!empty($param['id']) && $param['id'] > 0) {
  69. try {
  70. validate(<Bcontroller>Validate::class)->scene('edit')->check($param);
  71. } catch (ValidateException $e) {
  72. // 验证失败 输出错误信息
  73. return to_assign(1, $e->getError());
  74. }
  75. $cate_array = $this->sonlist($param['id']);
  76. if (in_array($param['pid'], $cate_array)) {
  77. return to_assign(1, '上级分类不能是该分类本身或其子分类');
  78. }
  79. $this->model->edit($param);
  80. } else {
  81. try {
  82. validate(<Bcontroller>Validate::class)->scene('add')->check($param);
  83. } catch (ValidateException $e) {
  84. // 验证失败 输出错误信息
  85. return to_assign(1, $e->getError());
  86. }
  87. $this->model->add($param);
  88. }
  89. }else{
  90. $id = isset($param['id']) ? $param['id'] : 0;
  91. $pid = isset($param['pid']) ? $param['pid'] : 0;
  92. if ($id>0) {
  93. $detail = $this->model->getById($id);
  94. View::assign('detail', $detail);
  95. }
  96. View::assign('pid', $pid);
  97. return view();
  98. }
  99. }
  100. /**
  101. * 查看
  102. */
  103. public function view($id)
  104. {
  105. $detail = $this->model->getById($id);
  106. if (!empty($detail)) {
  107. View::assign('detail', $detail);
  108. return view();
  109. }
  110. else{
  111. return view(EEEOR_REPORTING,['code'=>404,'warning'=>'找不到页面']);
  112. }
  113. }
  114. /**
  115. * 删除
  116. */
  117. public function del()
  118. {
  119. if (request()->isDelete()) {
  120. $param = get_params();
  121. $id = isset($param['id']) ? $param['id'] : 0;
  122. $type = isset($param['type']) ? $param['type'] : 0;
  123. $count_cate = $this->model->where(["pid"=>$id,"delete_time"=>0])->count();
  124. if ($count_cate > 0) {
  125. return to_assign(1, "该分类下还有子分类,无法删除");
  126. }
  127. $count_list = 0;
  128. if ($count_list > 0) {
  129. return to_assign(1, "该分类下还有内容,无法删除");
  130. }
  131. $this->model->delById($id,$type);
  132. } else {
  133. return to_assign(1, "错误的请求");
  134. }
  135. }
  136. /**
  137. * 设置
  138. */
  139. public function set()
  140. {
  141. if (request()->isAjax()) {
  142. $param = get_params();
  143. if($param['status'] == 0){
  144. $count_cate = $this->model->where(["pid"=>$param['id'],"delete_time"=>0])->count();
  145. if ($count_cate > 0) {
  146. return to_assign(1, "该分类下还有子分类,无法禁用");
  147. }
  148. $count_list = 0;
  149. if ($count_list > 0) {
  150. return to_assign(1, "该分类下还有内容,无法禁用");
  151. }
  152. $this->model->strict(false)->field('id,status')->update($param);
  153. add_log('disable', $param['id'], $param);
  154. }
  155. else if($param['status'] == 1){
  156. $res = $this->model->strict(false)->field('id,status')->update($param);
  157. add_log('recovery', $param['id'], $param);
  158. }
  159. return to_assign();
  160. } else {
  161. return to_assign(1, "错误的请求");
  162. }
  163. }
  164. }