Topicdongtai.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. 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'])
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row->getRelation('user')->visible(['username','nickname']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 删除
  64. */
  65. public function del($ids = "")
  66. {
  67. if (!$this->request->isPost()) {
  68. $this->error(__("Invalid parameters"));
  69. }
  70. $ids = $ids ? $ids : $this->request->post("ids");
  71. if ($ids) {
  72. $pk = $this->model->getPk();
  73. $adminIds = $this->getDataLimitAdminIds();
  74. if (is_array($adminIds)) {
  75. $this->model->where($this->dataLimitField, 'in', $adminIds);
  76. }
  77. $list = $this->model->where($pk, 'in', $ids)->select();
  78. $count = 0;
  79. Db::startTrans();
  80. try {
  81. foreach ($list as $k => $v) {
  82. $count ++;
  83. $this->delete_dongtai($v['id']);
  84. }
  85. Db::commit();
  86. } catch (PDOException $e) {
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. } catch (Exception $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. }
  93. if ($count) {
  94. $this->success();
  95. } else {
  96. $this->error(__('No rows were deleted'));
  97. }
  98. }
  99. $this->error(__('Parameter %s can not be empty', 'ids'));
  100. }
  101. /**
  102. * 动态删除
  103. */
  104. private function delete_dongtai($id){
  105. $where['id'] = $id;
  106. $dongtai = Db::name('topic_dongtai')->field('id')->where($where)->find();
  107. if (empty($dongtai)) {
  108. return true;
  109. }
  110. $delRes = Db::name('topic_dongtai')->where('id',$id)->delete();
  111. //点赞,
  112. Db::name('topic_dongtai_good')->where('dt_id',$id)->delete();
  113. }
  114. }