Backend.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. //是否采用模型验证
  45. if ($this->modelValidate)
  46. {
  47. $name = basename(str_replace('\\', '/', get_class($this->model)));
  48. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  49. $this->model->validate($validate);
  50. }
  51. $result = $this->model->save($params);
  52. if ($result !== false)
  53. {
  54. $this->code = 1;
  55. }
  56. else
  57. {
  58. $this->msg = $this->model->getError();
  59. }
  60. }
  61. catch (\think\exception\PDOException $e)
  62. {
  63. $this->msg = $e->getMessage();
  64. }
  65. }
  66. else
  67. {
  68. $this->msg = __('Parameter %s can not be empty', '');
  69. }
  70. return;
  71. }
  72. return $this->view->fetch();
  73. }
  74. /**
  75. * 编辑
  76. */
  77. public function edit($ids = NULL)
  78. {
  79. $row = $this->model->get($ids);
  80. if (!$row)
  81. $this->error(__('No Results were found'));
  82. if ($this->request->isPost())
  83. {
  84. $this->code = -1;
  85. $params = $this->request->post("row/a");
  86. if ($params)
  87. {
  88. foreach ($params as $k => &$v)
  89. {
  90. $v = is_array($v) ? implode(',', $v) : $v;
  91. }
  92. try
  93. {
  94. //是否采用模型验证
  95. if ($this->modelValidate)
  96. {
  97. $name = basename(str_replace('\\', '/', get_class($this->model)));
  98. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  99. $row->validate($validate);
  100. }
  101. $result = $row->save($params);
  102. if ($result !== false)
  103. {
  104. $this->code = 1;
  105. }
  106. else
  107. {
  108. $this->msg = $row->getError();
  109. }
  110. }
  111. catch (think\exception\PDOException $e)
  112. {
  113. $this->msg = $e->getMessage();
  114. }
  115. }
  116. else
  117. {
  118. $this->msg = __('Parameter %s can not be empty', '');
  119. }
  120. return;
  121. }
  122. $this->view->assign("row", $row);
  123. return $this->view->fetch();
  124. }
  125. /**
  126. * 删除
  127. */
  128. public function del($ids = "")
  129. {
  130. $this->code = -1;
  131. if ($ids)
  132. {
  133. $count = $this->model->destroy($ids);
  134. if ($count)
  135. {
  136. $this->code = 1;
  137. }
  138. }
  139. return;
  140. }
  141. /**
  142. * 批量更新
  143. */
  144. public function multi($ids = "")
  145. {
  146. $this->code = -1;
  147. $ids = $ids ? $ids : $this->request->param("ids");
  148. if ($ids)
  149. {
  150. if ($this->request->has('params'))
  151. {
  152. parse_str($this->request->post("params"), $values);
  153. $values = array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  154. if ($values)
  155. {
  156. $count = $this->model->where($this->model->getPk(), 'in', $ids)->update($values);
  157. if ($count)
  158. {
  159. $this->code = 1;
  160. }
  161. }
  162. else
  163. {
  164. $this->msg = __('You have no permission');
  165. }
  166. }
  167. else
  168. {
  169. $this->msg = __('Parameter %s can not be empty', '');
  170. }
  171. }
  172. return;
  173. }
  174. }