Topicdongtai.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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'])
  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. }
  60. $result = array("total" => $list->total(), "rows" => $list->items());
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 置顶
  67. */
  68. public function toptime(){
  69. if ($this->request->isAjax()) {
  70. $id = input('id',0);
  71. $rs = Db::name('topic_dongtai')->where('id',$id)->update(['toptime'=>time()]);
  72. $this->success('完成');
  73. }
  74. }
  75. /**
  76. * 取消置顶
  77. */
  78. public function untoptime(){
  79. if ($this->request->isAjax()) {
  80. $id = input('id',0);
  81. $rs = Db::name('topic_dongtai')->where('id',$id)->update(['toptime'=>0]);
  82. $this->success('完成');
  83. }
  84. }
  85. /**
  86. * 删除
  87. */
  88. public function del($ids = "")
  89. {
  90. if (!$this->request->isPost()) {
  91. $this->error(__("Invalid parameters"));
  92. }
  93. $ids = $ids ? $ids : $this->request->post("ids");
  94. if ($ids) {
  95. $pk = $this->model->getPk();
  96. $adminIds = $this->getDataLimitAdminIds();
  97. if (is_array($adminIds)) {
  98. $this->model->where($this->dataLimitField, 'in', $adminIds);
  99. }
  100. $list = $this->model->where($pk, 'in', $ids)->select();
  101. $count = 0;
  102. Db::startTrans();
  103. try {
  104. foreach ($list as $k => $v) {
  105. $count ++;
  106. $this->delete_dongtai($v['id']);
  107. }
  108. Db::commit();
  109. } catch (PDOException $e) {
  110. Db::rollback();
  111. $this->error($e->getMessage());
  112. } catch (Exception $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. }
  116. if ($count) {
  117. $this->success();
  118. } else {
  119. $this->error(__('No rows were deleted'));
  120. }
  121. }
  122. $this->error(__('Parameter %s can not be empty', 'ids'));
  123. }
  124. /**
  125. * 动态删除
  126. */
  127. private function delete_dongtai($id){
  128. $where['id'] = $id;
  129. $dongtai = Db::name('topic_dongtai')->field('id,topic_ids')->where($where)->find();
  130. if (empty($dongtai)) {
  131. return true;
  132. }
  133. $delRes = Db::name('topic_dongtai')->where('id',$id)->delete();
  134. //话题少一个贴
  135. if (!empty($dongtai['topic_ids'])) {
  136. $res = Db::name('topic_hub')->where('id','IN',$dongtai['topic_ids'])->setDec('t_number');
  137. }
  138. //删除对应的评论,
  139. Db::name('topic_dongtai_answer')->where('dt_id',$id)->delete();
  140. //点赞,
  141. Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
  142. //评论点赞
  143. Db::name('topic_answer_good')->where('dt_id',$id)->delete();
  144. }
  145. }