Question.php 3.2 KB

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