Backend.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\admin\library\traits;
  3. trait Backend
  4. {
  5. /**
  6. * 查看
  7. */
  8. public function index()
  9. {
  10. if ($this->request->isAjax())
  11. {
  12. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  13. $total = $this->model
  14. ->where($where)
  15. ->order($sort, $order)
  16. ->count();
  17. $list = $this->model
  18. ->where($where)
  19. ->order($sort, $order)
  20. ->limit($offset, $limit)
  21. ->select();
  22. $result = array("total" => $total, "rows" => $list);
  23. return json($result);
  24. }
  25. return $this->view->fetch();
  26. }
  27. /**
  28. * 添加
  29. */
  30. public function add()
  31. {
  32. if ($this->request->isPost())
  33. {
  34. $this->code = -1;
  35. $params = $this->request->post("row/a");
  36. if ($params)
  37. {
  38. $this->model->create($params);
  39. $this->code = 1;
  40. }
  41. return;
  42. }
  43. return $this->view->fetch();
  44. }
  45. /**
  46. * 编辑
  47. */
  48. public function edit($ids = NULL)
  49. {
  50. $row = $this->model->get(['id' => $ids]);
  51. if (!$row)
  52. $this->error(__('No Results were found'));
  53. if ($this->request->isPost())
  54. {
  55. $this->code = -1;
  56. $params = $this->request->post("row/a");
  57. if ($params)
  58. {
  59. $row->save($params);
  60. $this->code = 1;
  61. }
  62. return;
  63. }
  64. $this->view->assign("row", $row);
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 删除
  69. */
  70. public function del($ids = "")
  71. {
  72. $this->code = -1;
  73. if ($ids)
  74. {
  75. $count = $this->model->where('id', 'in', $ids)->delete();
  76. if ($count)
  77. {
  78. $this->code = 1;
  79. }
  80. }
  81. return;
  82. }
  83. /**
  84. * 批量更新
  85. */
  86. public function multi($ids = "")
  87. {
  88. $this->code = -1;
  89. $ids = $ids ? $ids : $this->request->param("ids");
  90. if ($ids)
  91. {
  92. if ($this->request->has('params'))
  93. {
  94. parse_str($this->request->post("params"), $values);
  95. $values = array_intersect_key($values, array_flip(array('status')));
  96. if ($values)
  97. {
  98. $count = $this->model->where('id', 'in', $ids)->update($values);
  99. if ($count)
  100. {
  101. $this->code = 1;
  102. }
  103. }
  104. }
  105. else
  106. {
  107. $this->code = 1;
  108. }
  109. }
  110. return;
  111. }
  112. }