Backend.php 3.3 KB

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