Topicdongtai.php 5.0 KB

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