Backend.php 3.2 KB

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