Topicdongtai.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 用户动态
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Topicdongtai extends Backend
  11. {
  12. protected $noNeedRight = ['delete_dongtai'];
  13. /**
  14. * Topicdongtai模型对象
  15. * @var \app\admin\model\Topicdongtai
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Topicdongtai;
  22. $this->view->assign("typeList", $this->model->getTypeList());
  23. $this->view->assign("isPublicList", $this->model->getIsPublicList());
  24. }
  25. public function import()
  26. {
  27. parent::import();
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //当前是否为关联查询
  40. $this->relationSearch = true;
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $list = $this->model
  50. ->with(['user','topichub'])
  51. ->where($where)
  52. // ->order($sort, $order)
  53. ->order('toptime desc,id desc')
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->getRelation('user')->visible(['username']);
  57. $row->getRelation('topichub')->visible(['name']);
  58. }
  59. $result = array("total" => $list->total(), "rows" => $list->items());
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 置顶
  66. */
  67. public function toptime(){
  68. if ($this->request->isAjax()) {
  69. $id = input('id',0);
  70. $rs = Db::name('topic_dongtai')->where('id',$id)->update(['toptime'=>time()]);
  71. $this->success('完成');
  72. }
  73. }
  74. /**
  75. * 取消置顶
  76. */
  77. public function untoptime(){
  78. if ($this->request->isAjax()) {
  79. $id = input('id',0);
  80. $rs = Db::name('topic_dongtai')->where('id',$id)->update(['toptime'=>0]);
  81. $this->success('完成');
  82. }
  83. }
  84. /**
  85. * 删除
  86. */
  87. public function del($ids = "")
  88. {
  89. if (!$this->request->isPost()) {
  90. $this->error(__("Invalid parameters"));
  91. }
  92. $ids = $ids ? $ids : $this->request->post("ids");
  93. if ($ids) {
  94. $pk = $this->model->getPk();
  95. $adminIds = $this->getDataLimitAdminIds();
  96. if (is_array($adminIds)) {
  97. $this->model->where($this->dataLimitField, 'in', $adminIds);
  98. }
  99. $list = $this->model->where($pk, 'in', $ids)->select();
  100. $count = 0;
  101. Db::startTrans();
  102. try {
  103. foreach ($list as $k => $v) {
  104. $count ++;
  105. $this->delete_dongtai($v['id']);
  106. }
  107. Db::commit();
  108. } catch (PDOException $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. } catch (Exception $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. }
  115. if ($count) {
  116. $this->success();
  117. } else {
  118. $this->error(__('No rows were deleted'));
  119. }
  120. }
  121. $this->error(__('Parameter %s can not be empty', 'ids'));
  122. }
  123. /**
  124. * 动态删除
  125. */
  126. private function delete_dongtai($id){
  127. $where['id'] = $id;
  128. $dongtai = Db::name('topic_dongtai')->field('id,topic_ids')->where($where)->find();
  129. if (empty($dongtai)) {
  130. return true;
  131. }
  132. $delRes = Db::name('topic_dongtai')->where('id',$id)->delete();
  133. //话题少一个贴
  134. if (!empty($dongtai['topic_ids'])) {
  135. $res = Db::name('topic_hub')->where('id','IN',$dongtai['topic_ids'])->setDec('t_number');
  136. }
  137. //删除对应的评论,
  138. Db::name('topic_dongtai_answer')->where('dt_id',$id)->delete();
  139. //点赞,
  140. Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
  141. //评论点赞
  142. Db::name('topic_answer_good')->where('dt_id',$id)->delete();
  143. }
  144. }