News.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\admin\controller\weixin;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 图文消息管理管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class News extends Backend
  14. {
  15. /**
  16. * News模型对象
  17. * @var \app\admin\model\weixin\News
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\weixin\News;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags']);
  33. if ($this->request->isAjax()) {
  34. //如果发送的来源是Selectpage,则转发到Selectpage
  35. if ($this->request->request('keyField')) {
  36. return $this->selectpage();
  37. }
  38. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  39. $total = $this->model
  40. ->where($where)
  41. ->order($sort, $order)
  42. ->count();
  43. $list = $this->model
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->limit($offset, $limit)
  47. ->select();
  48. $list = collection($list)->toArray();
  49. $result = array("total" => $total, "rows" => $list);
  50. return json($result);
  51. }
  52. return $this->view->fetch();
  53. }
  54. /**
  55. * 添加
  56. */
  57. public function add()
  58. {
  59. if ($this->request->isPost()) {
  60. $params = $this->request->post("list/a");
  61. if ($params) {
  62. $data = [];
  63. foreach ($params as $val) {
  64. $val = json_decode($val, true);
  65. if (isset($val['id']) && !$val['id']) {
  66. unset($val['id']);
  67. }
  68. $data[] = $val;
  69. }
  70. $result = false;
  71. Db::startTrans();
  72. try {
  73. $result = $this->model->allowField(true)->saveAll($data);
  74. Db::commit();
  75. } catch (ValidateException $e) {
  76. Db::rollback();
  77. $this->error($e->getMessage());
  78. } catch (PDOException $e) {
  79. Db::rollback();
  80. $this->error($e->getMessage());
  81. } catch (Exception $e) {
  82. Db::rollback();
  83. $this->error($e->getMessage());
  84. }
  85. if ($result !== false) {
  86. $this->success();
  87. } else {
  88. $this->error(__('No rows were updated'));
  89. }
  90. }
  91. $this->error(__('Parameter %s can not be empty', ''));
  92. }
  93. return $this->view->fetch();
  94. }
  95. /**
  96. * 编辑
  97. */
  98. public function edit($ids = null)
  99. {
  100. if ($this->request->isPost()) {
  101. self::add();
  102. }
  103. $row = $this->model->get($ids);
  104. if (!$row) {
  105. $this->error(__('No Results were found'));
  106. }
  107. $adminIds = $this->getDataLimitAdminIds();
  108. if (is_array($adminIds)) {
  109. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  110. $this->error(__('You have no permission'));
  111. }
  112. }
  113. $this->view->assign("row", $row);
  114. return $this->view->fetch();
  115. }
  116. /**
  117. * 删除
  118. */
  119. public function del($ids = "")
  120. {
  121. if ($ids) {
  122. $pk = $this->model->getPk();
  123. $adminIds = $this->getDataLimitAdminIds();
  124. if (is_array($adminIds)) {
  125. $this->model->where($this->dataLimitField, 'in', $adminIds);
  126. }
  127. $list = $this->model->where($pk, 'in', $ids)->whereOr('parent_id', 'in', $ids)->select();
  128. $count = 0;
  129. Db::startTrans();
  130. try {
  131. foreach ($list as $k => $v) {
  132. $count += $v->delete();
  133. }
  134. Db::commit();
  135. } catch (PDOException $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. } catch (Exception $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if ($count) {
  143. $this->success();
  144. } else {
  145. $this->error(__('No rows were deleted'));
  146. }
  147. }
  148. $this->error(__('Parameter %s can not be empty', 'ids'));
  149. }
  150. }