Nav.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Nav as NavList;
  11. use app\admin\model\NavInfo;
  12. use app\admin\validate\NavCheck;
  13. use think\exception\ValidateException;
  14. use think\facade\Db;
  15. use think\facade\View;
  16. class Nav extends BaseController
  17. {
  18. public function index()
  19. {
  20. if (request()->isAjax()) {
  21. $param = get_params();
  22. $where = array();
  23. if (!empty($param['keywords'])) {
  24. $where[] = ['id|name|title|desc', 'like', '%' . $param['keywords'] . '%'];
  25. }
  26. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  27. $nav = NavList::where($where)
  28. ->order('create_time asc')
  29. ->paginate($rows, false, ['query' => $param]);
  30. return table_assign(0, '', $nav);
  31. } else {
  32. return view();
  33. }
  34. }
  35. //添加
  36. public function add()
  37. {
  38. $param = get_params();
  39. if (request()->isAjax()) {
  40. if (!empty($param['id']) && $param['id'] > 0) {
  41. try {
  42. validate(NavCheck::class)->scene('edit')->check($param);
  43. } catch (ValidateException $e) {
  44. // 验证失败 输出错误信息
  45. return to_assign(1, $e->getError());
  46. }
  47. NavList::where(['id' => $param['id']])
  48. ->strict(false)
  49. ->field(true)
  50. ->update($param);
  51. // 删除导航缓存
  52. clear_cache('homeNav');
  53. add_log('edit', $param['id'], $param);
  54. return to_assign();
  55. } else {
  56. try {
  57. validate(NavCheck::class)->scene('add')->check($param);
  58. } catch (ValidateException $e) {
  59. // 验证失败 输出错误信息
  60. return to_assign(1, $e->getError());
  61. }
  62. $nid = NavList::strict(false)->field(true)->insertGetId($param);
  63. // 删除导航缓存
  64. clear_cache('homeNav');
  65. add_log('add', $nid, $param);
  66. return to_assign();
  67. }
  68. } else {
  69. $id = isset($param['id']) ? $param['id'] : 0;
  70. if ($id > 0) {
  71. $nav = Db::name('Nav')->where(['id' => $id])->find();
  72. View::assign('nav', $nav);
  73. }
  74. View::assign('id', $id);
  75. return view();
  76. }
  77. }
  78. //删除
  79. public function delete()
  80. {
  81. $id = get_params('id');
  82. $count = Db::name('NavInfo')->where(['nav_id' => $id])->count();
  83. if ($count > 0) {
  84. return to_assign(1, '该组下还有导航,无法删除');
  85. }
  86. if (Db::name('Nav')->delete($id) !== false) {
  87. return to_assign(0, '删除成功');
  88. // 删除导航缓存
  89. clear_cache('homeNav');
  90. add_log('delete', $id, []);
  91. } else {
  92. return to_assign(1, '删除失败');
  93. }
  94. }
  95. //管理导航
  96. public function nav_info()
  97. {
  98. $param = get_params();
  99. if (request()->isAjax()) {
  100. $id = $param['id'];
  101. $navInfoList = Db::name('NavInfo')
  102. ->where(['nav_id' => $id])
  103. ->order('sort asc')
  104. ->select();
  105. return to_assign(0, '', $navInfoList);
  106. } else {
  107. return view('', [
  108. 'nav_id' => $param['id'],
  109. ]);
  110. }
  111. }
  112. //添加导航
  113. public function nav_info_add()
  114. {
  115. $param = get_params();
  116. if (request()->isAjax()) {
  117. if (!empty($param['id']) && $param['id'] > 0) {
  118. try {
  119. validate(NavCheck::class)->scene('editInfo')->check($param);
  120. } catch (ValidateException $e) {
  121. // 验证失败 输出错误信息
  122. return to_assign(1, $e->getError());
  123. }
  124. NavInfo::strict(false)->field(true)->update($param);
  125. // 删除导航缓存
  126. clear_cache('homeNav');
  127. add_log('edit', $param['id'], $param);
  128. return to_assign();
  129. } else {
  130. try {
  131. validate(NavCheck::class)->scene('addInfo')->check($param);
  132. } catch (ValidateException $e) {
  133. // 验证失败 输出错误信息
  134. return to_assign(1, $e->getError());
  135. }
  136. $nid = NavInfo::strict(false)->field(true)->insertGetId($param);
  137. // 删除导航缓存
  138. clear_cache('homeNav');
  139. add_log('add', $nid, $param);
  140. return to_assign();
  141. }
  142. } else {
  143. $id = isset($param['id']) ? $param['id'] : 0;
  144. $nid = isset($param['nid']) ? $param['nid'] : 0;
  145. $pid = isset($param['pid']) ? $param['pid'] : 0;
  146. if ($id > 0) {
  147. $nav = Db::name('NavInfo')->where(['id' => $id])->find();
  148. View::assign('nav', $nav);
  149. $nid = $nav['nav_id'];
  150. $pid = $nav['pid'];
  151. }
  152. View::assign('id', $id);
  153. View::assign('nav_id', $nid);
  154. View::assign('pid', $pid);
  155. return view();
  156. }
  157. }
  158. //删除
  159. public function nav_info_delete()
  160. {
  161. $id = get_params('id');
  162. if (Db::name('NavInfo')->delete($id) !== false) {
  163. //清除导航缓存
  164. clear_cache('homeNav');
  165. add_log('delete', $id, []);
  166. return to_assign(0, '删除成功');
  167. } else {
  168. return to_assign(1, '删除失败');
  169. }
  170. }
  171. }