Faq.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\admin\controller\shopro\data;
  3. use app\admin\controller\shopro\Common;
  4. use think\Db;
  5. /**
  6. * 常见问题
  7. */
  8. class Faq extends Common
  9. {
  10. /**
  11. * Faq模型对象
  12. * @var \app\admin\model\shopro\data\Faq
  13. */
  14. protected $model = null;
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $this->model = new \app\admin\model\shopro\data\Faq;
  19. }
  20. /**
  21. * 查看
  22. *
  23. * @return string|Json
  24. * @throws \think\Exception
  25. * @throws DbException
  26. */
  27. public function index()
  28. {
  29. if (!$this->request->isAjax()) {
  30. return $this->view->fetch();
  31. }
  32. $list = $this->model->sheepFilter()->paginate($this->request->request('list_rows', 10));
  33. $this->success('', null, $list);
  34. }
  35. /**
  36. * 添加
  37. */
  38. public function add()
  39. {
  40. if (!$this->request->isAjax()) {
  41. return $this->view->fetch();
  42. }
  43. $params = $this->request->only(['title', 'content', 'status']);
  44. $this->svalidate($params, '.add');
  45. $this->model->save($params);
  46. $this->success('保存成功', null, $this->model);
  47. }
  48. /**
  49. * 详情
  50. *
  51. * @param $id
  52. * @return \think\Response
  53. */
  54. public function detail($id)
  55. {
  56. $detail = $this->model->where('id', $id)->find();
  57. if (!$detail) {
  58. $this->error(__('No Results were found'));
  59. }
  60. $this->success('获取成功', null, $detail);
  61. }
  62. /**
  63. * 编辑(支持批量)
  64. */
  65. public function edit($id = null)
  66. {
  67. if (!$this->request->isAjax()) {
  68. return $this->view->fetch('add');
  69. }
  70. $params = $this->request->only(['title', 'content', 'status']);
  71. $list = $this->model->where('id', 'in', $id)->select();
  72. $result = Db::transaction(function () use ($list, $params) {
  73. $count = 0;
  74. foreach ($list as $item) {
  75. $params['id'] = $item->id;
  76. $this->svalidate($params);
  77. $count += $item->save($params);
  78. }
  79. return $count;
  80. });
  81. if ($result) {
  82. $this->success('更新成功', null, $result);
  83. } else {
  84. $this->error('更新失败,未改变任何记录');
  85. }
  86. }
  87. /**
  88. * 删除(支持批量)
  89. *
  90. * @param $id
  91. * @return \think\Response
  92. */
  93. public function delete($id)
  94. {
  95. if (empty($id)) {
  96. $this->error(__('Parameter %s can not be empty', 'id'));
  97. }
  98. $list = $this->model->where('id', 'in', $id)->select();
  99. $result = Db::transaction(function () use ($list) {
  100. $count = 0;
  101. foreach ($list as $item) {
  102. $count += $item->delete();
  103. }
  104. return $count;
  105. });
  106. if ($result) {
  107. $this->success('删除成功', null, $result);
  108. } else {
  109. $this->error(__('No rows were deleted'));
  110. }
  111. }
  112. }