Faq.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\common\controller\AdminController;
  4. use app\common\model\SystemFaq;
  5. use think\App;
  6. /**
  7. * @ControllerAnnotation(title="system_faq")
  8. */
  9. class Faq extends AdminController
  10. {
  11. use \app\admin\traits\Curd;
  12. public function __construct (App $app)
  13. {
  14. parent::__construct($app);
  15. $this->model = new SystemFaq();
  16. }
  17. /**
  18. * @NodeAnotation(title="列表")
  19. */
  20. public function index ()
  21. {
  22. if ($this->request->isAjax()) {
  23. if (input('selectFields')) {
  24. return $this->selectList();
  25. }
  26. list($page, $limit, $where) = $this->buildTableParames();
  27. $count = $this->model
  28. ->where($where)
  29. ->count();
  30. $list = $this->model
  31. ->where($where)
  32. ->page($page, $limit)
  33. ->order($this->sort)
  34. ->select();
  35. foreach ($list as &$v) {
  36. if ($v->parent_id == 0) {
  37. $v->parent_id = '最上级';
  38. } else {
  39. $self = \think\facade\Db::name('system_faq')
  40. ->where(['id' => $v->parent_id])
  41. ->value('title');
  42. $v->parent_id = $self;
  43. }
  44. }
  45. $data = [
  46. 'code' => 0,
  47. 'msg' => '',
  48. 'count' => $count,
  49. 'data' => $list,
  50. ];
  51. return json($data);
  52. }
  53. return $this->fetch();
  54. }
  55. /**
  56. * @NodeAnotation(title="修改")
  57. */
  58. public function edit ($id)
  59. {
  60. $row = $this->model->find($id);
  61. empty($row) && $this->error('数据不存在');
  62. if ($this->request->isAjax()) {
  63. $post = $this->request->post();
  64. $rule = [];
  65. $this->validate($post, $rule);
  66. try {
  67. if (empty($post['url'])) {
  68. unset($post['url']);
  69. }
  70. if (!empty($post['content'])) {
  71. $post['content'] = htmlspecialchars_decode($post['content']);
  72. }
  73. $save = $row->save($post);
  74. } catch (\Exception $e) {
  75. $this->error('保存失败');
  76. }
  77. $save ? $this->success('保存成功') : $this->error('保存失败');
  78. }
  79. $this->assign('row', $row);
  80. return $this->fetch();
  81. }
  82. /**
  83. * @NodeAnotation(title="添加")
  84. */
  85. public function add ()
  86. {
  87. if ($this->request->isAjax()) {
  88. $post = $this->request->post();
  89. try {
  90. if ($post['parent_id'] == 1) {
  91. $db = \think\facade\Db::name('system_faq')
  92. ->where(['parent_id' => 1])
  93. ->count();
  94. if ($db >= 5) {
  95. $this->error('保存失败');
  96. }
  97. }
  98. if (!empty($post['content'])) {
  99. $post['content'] = htmlspecialchars_decode($post['content']);
  100. }
  101. $save = $this->model->save($post);
  102. } catch (\Exception $e) {
  103. $this->error('保存失败');
  104. }
  105. $save ? $this->success('保存成功') : $this->error('保存失败');
  106. }
  107. return $this->fetch();
  108. }
  109. /**
  110. * @NodeAnotation(title="删除")
  111. */
  112. public function delete ($id)
  113. {
  114. $row = $this->model->find($id);
  115. if ($row->id == 1) {
  116. $this->error('父类常见问题不能删除');
  117. }
  118. empty($row) && $this->error('数据不存在');
  119. try {
  120. $save = $row->delete();
  121. } catch (\Exception $e) {
  122. $this->error('删除失败');
  123. }
  124. if ($save) {
  125. $this->success('删除成功');
  126. } else {
  127. $this->error('删除失败');
  128. }
  129. }
  130. /**
  131. * @NodeAnotation(title="选择上级")
  132. */
  133. public function type ()
  134. {
  135. $list = \think\facade\Db::name('system_faq')
  136. ->field('id,title')
  137. ->where(['parent_id' => 0])
  138. ->select()
  139. ->toArray();
  140. array_push($list, ['id' => 0, 'title' => '最上级']);
  141. $data = [
  142. 'code' => 1,
  143. 'msg' => '',
  144. 'count' => count($list),
  145. 'data' => $list,
  146. ];
  147. return json($data);
  148. }
  149. }