Backend.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. foreach ($params as $k => &$v)
  40. {
  41. $v = is_array($v) ? implode(',', $v) : $v;
  42. $v = substr($k, -4) == 'time' && !is_numeric($v) ? strtotime($v) : $v;
  43. }
  44. $this->model->create($params);
  45. AdminLog::record(__('Add'), $this->model->getLastInsID());
  46. $this->code = 1;
  47. }
  48. return;
  49. }
  50. return $this->view->fetch();
  51. }
  52. /**
  53. * 编辑
  54. */
  55. public function edit($ids = NULL)
  56. {
  57. $row = $this->model->get(['id' => $ids]);
  58. if (!$row)
  59. $this->error(__('No Results were found'));
  60. if ($this->request->isPost())
  61. {
  62. $this->code = -1;
  63. $params = $this->request->post("row/a");
  64. if ($params)
  65. {
  66. foreach ($params as $k => &$v)
  67. {
  68. $v = is_array($v) ? implode(',', $v) : $v;
  69. $v = substr($k, -4) == 'time' && !is_numeric($v) ? strtotime($v) : $v;
  70. }
  71. $row->save($params);
  72. AdminLog::record(__('Edit'), $ids);
  73. $this->code = 1;
  74. }
  75. return;
  76. }
  77. $this->view->assign("row", $row);
  78. return $this->view->fetch();
  79. }
  80. /**
  81. * 删除
  82. */
  83. public function del($ids = "")
  84. {
  85. $this->code = -1;
  86. if ($ids)
  87. {
  88. $count = $this->model->where('id', 'in', $ids)->delete();
  89. if ($count)
  90. {
  91. AdminLog::record(__('Del'), $ids);
  92. $this->code = 1;
  93. }
  94. }
  95. return;
  96. }
  97. /**
  98. * 批量更新
  99. */
  100. public function multi($ids = "")
  101. {
  102. $this->code = -1;
  103. $ids = $ids ? $ids : $this->request->param("ids");
  104. if ($ids)
  105. {
  106. if ($this->request->has('params'))
  107. {
  108. parse_str($this->request->post("params"), $values);
  109. $values = array_intersect_key($values, array_flip(array('status')));
  110. if ($values)
  111. {
  112. $count = $this->model->where('id', 'in', $ids)->update($values);
  113. if ($count)
  114. {
  115. AdminLog::record(__('Multi'), $ids);
  116. $this->code = 1;
  117. }
  118. }
  119. }
  120. else
  121. {
  122. $this->code = 1;
  123. }
  124. }
  125. return;
  126. }
  127. }