Backend.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. }
  42. try
  43. {
  44. $result = $this->model->create($params);
  45. if ($result !== false)
  46. {
  47. $this->code = 1;
  48. }
  49. else
  50. {
  51. $this->msg = $this->model->getError();
  52. }
  53. }
  54. catch (think\Exception $e)
  55. {
  56. $this->msg = $e->getMessage();
  57. }
  58. }
  59. else
  60. {
  61. $this->msg = __('Parameter %s can not be empty', '');
  62. }
  63. return;
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 编辑
  69. */
  70. public function edit($ids = NULL)
  71. {
  72. $row = $this->model->get($ids);
  73. if (!$row)
  74. $this->error(__('No Results were found'));
  75. if ($this->request->isPost())
  76. {
  77. $this->code = -1;
  78. $params = $this->request->post("row/a");
  79. if ($params)
  80. {
  81. foreach ($params as $k => &$v)
  82. {
  83. $v = is_array($v) ? implode(',', $v) : $v;
  84. }
  85. try
  86. {
  87. $result = $row->save($params);
  88. if ($result !== false)
  89. {
  90. $this->code = 1;
  91. }
  92. else
  93. {
  94. $this->msg = $row->getError();
  95. }
  96. }
  97. catch (think\Exception $e)
  98. {
  99. $this->msg = $e->getMessage();
  100. }
  101. }
  102. else
  103. {
  104. $this->msg = __('Parameter %s can not be empty', '');
  105. }
  106. return;
  107. }
  108. $this->view->assign("row", $row);
  109. return $this->view->fetch();
  110. }
  111. /**
  112. * 删除
  113. */
  114. public function del($ids = "")
  115. {
  116. $this->code = -1;
  117. if ($ids)
  118. {
  119. $count = $this->model->destroy($ids);
  120. if ($count)
  121. {
  122. $this->code = 1;
  123. }
  124. }
  125. return;
  126. }
  127. /**
  128. * 批量更新
  129. */
  130. public function multi($ids = "")
  131. {
  132. $this->code = -1;
  133. $ids = $ids ? $ids : $this->request->param("ids");
  134. if ($ids)
  135. {
  136. if ($this->request->has('params'))
  137. {
  138. parse_str($this->request->post("params"), $values);
  139. $values = array_intersect_key($values, array_flip(array('status')));
  140. if ($values)
  141. {
  142. $count = $this->model->where($this->model->getPk(), 'in', $ids)->update($values);
  143. if ($count)
  144. {
  145. $this->code = 1;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. $this->msg = __('Parameter %s can not be empty', '');
  152. }
  153. }
  154. return;
  155. }
  156. }