Topicdongtai.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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("auditstatusList", $this->model->getAuditstatusList());
  24. }
  25. /**
  26. * 查看
  27. */
  28. public function index()
  29. {
  30. //当前是否为关联查询
  31. $this->relationSearch = true;
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags', 'trim']);
  34. if ($this->request->isAjax()) {
  35. //如果发送的来源是Selectpage,则转发到Selectpage
  36. if ($this->request->request('keyField')) {
  37. return $this->selectpage();
  38. }
  39. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  40. $list = $this->model
  41. ->with(['user'])
  42. ->where($where)
  43. ->order('topicdongtai.id desc')
  44. ->order($sort, $order)
  45. //->select(false);echo $list;exit;
  46. ->paginate($limit);
  47. foreach ($list as $row) {
  48. $row->getRelation('user')->visible(['username','nickname']);
  49. }
  50. $result = array("total" => $list->total(), "rows" => $list->items());
  51. return json($result);
  52. }
  53. return $this->view->fetch();
  54. }
  55. /**
  56. * 删除
  57. */
  58. public function del($ids = "")
  59. {
  60. if (!$this->request->isPost()) {
  61. $this->error(__("Invalid parameters"));
  62. }
  63. $ids = $ids ? $ids : $this->request->post("ids");
  64. if ($ids) {
  65. $pk = $this->model->getPk();
  66. $adminIds = $this->getDataLimitAdminIds();
  67. if (is_array($adminIds)) {
  68. $this->model->where($this->dataLimitField, 'in', $adminIds);
  69. }
  70. $list = $this->model->where($pk, 'in', $ids)->select();
  71. $count = 0;
  72. Db::startTrans();
  73. try {
  74. foreach ($list as $k => $v) {
  75. $count ++;
  76. $this->delete_dongtai($v['id']);
  77. }
  78. Db::commit();
  79. } catch (PDOException $e) {
  80. Db::rollback();
  81. $this->error($e->getMessage());
  82. } catch (Exception $e) {
  83. Db::rollback();
  84. $this->error($e->getMessage());
  85. }
  86. if ($count) {
  87. $this->success();
  88. } else {
  89. $this->error(__('No rows were deleted'));
  90. }
  91. }
  92. $this->error(__('Parameter %s can not be empty', 'ids'));
  93. }
  94. /**
  95. * 动态删除
  96. */
  97. private function delete_dongtai($id){
  98. $where['id'] = $id;
  99. $dongtai = Db::name('topic_dongtai')->field('id')->where($where)->find();
  100. if (empty($dongtai)) {
  101. return true;
  102. }
  103. $delRes = Db::name('topic_dongtai')->where('id',$id)->delete();
  104. //删除对应的评论,
  105. Db::name('topic_dongtai_answer')->where('dt_id',$id)->delete();
  106. //点赞,
  107. Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
  108. //评论点赞
  109. Db::name('topic_answer_good')->where('dt_id',$id)->delete();
  110. }
  111. }