Faq.php 3.7 KB

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