Backend.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\library\traits;
  3. trait Backend
  4. {
  5. /**
  6. * 查看
  7. */
  8. public function index()
  9. {
  10. //设置过滤方法
  11. $this->request->filter(['strip_tags']);
  12. if ($this->request->isAjax())
  13. {
  14. //如果发送的来源是Selectpage,则转发到Selectpage
  15. if ($this->request->request('pkey_name'))
  16. {
  17. return $this->selectpage();
  18. }
  19. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  20. $total = $this->model
  21. ->where($where)
  22. ->order($sort, $order)
  23. ->count();
  24. $list = $this->model
  25. ->where($where)
  26. ->order($sort, $order)
  27. ->limit($offset, $limit)
  28. ->select();
  29. $result = array("total" => $total, "rows" => $list);
  30. return json($result);
  31. }
  32. return $this->view->fetch();
  33. }
  34. /**
  35. * 添加
  36. */
  37. public function add()
  38. {
  39. if ($this->request->isPost())
  40. {
  41. $params = $this->request->post("row/a");
  42. if ($params)
  43. {
  44. foreach ($params as $k => &$v)
  45. {
  46. $v = is_array($v) ? implode(',', $v) : $v;
  47. }
  48. try
  49. {
  50. //是否采用模型验证
  51. if ($this->modelValidate)
  52. {
  53. $name = basename(str_replace('\\', '/', get_class($this->model)));
  54. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  55. $this->model->validate($validate);
  56. }
  57. $result = $this->model->save($params);
  58. if ($result !== false)
  59. {
  60. $this->success();
  61. }
  62. else
  63. {
  64. $this->error($this->model->getError());
  65. }
  66. }
  67. catch (\think\exception\PDOException $e)
  68. {
  69. $this->error($e->getMessage());
  70. }
  71. }
  72. $this->error(__('Parameter %s can not be empty', ''));
  73. }
  74. return $this->view->fetch();
  75. }
  76. /**
  77. * 编辑
  78. */
  79. public function edit($ids = NULL)
  80. {
  81. $row = $this->model->get($ids);
  82. if (!$row)
  83. $this->error(__('No Results were found'));
  84. if ($this->request->isPost())
  85. {
  86. $params = $this->request->post("row/a");
  87. if ($params)
  88. {
  89. foreach ($params as $k => &$v)
  90. {
  91. $v = is_array($v) ? implode(',', $v) : $v;
  92. }
  93. try
  94. {
  95. //是否采用模型验证
  96. if ($this->modelValidate)
  97. {
  98. $name = basename(str_replace('\\', '/', get_class($this->model)));
  99. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  100. $row->validate($validate);
  101. }
  102. $result = $row->save($params);
  103. if ($result !== false)
  104. {
  105. $this->success();
  106. }
  107. else
  108. {
  109. $this->error($row->getError());
  110. }
  111. }
  112. catch (think\exception\PDOException $e)
  113. {
  114. $this->error($e->getMessage());
  115. }
  116. }
  117. $this->error(__('Parameter %s can not be empty', ''));
  118. }
  119. $this->view->assign("row", $row);
  120. return $this->view->fetch();
  121. }
  122. /**
  123. * 删除
  124. */
  125. public function del($ids = "")
  126. {
  127. if ($ids)
  128. {
  129. $count = $this->model->destroy($ids);
  130. if ($count)
  131. {
  132. $this->success();
  133. }
  134. }
  135. $this->error(__('Parameter %s can not be empty', 'ids'));
  136. }
  137. /**
  138. * 批量更新
  139. */
  140. public function multi($ids = "")
  141. {
  142. $ids = $ids ? $ids : $this->request->param("ids");
  143. if ($ids)
  144. {
  145. if ($this->request->has('params'))
  146. {
  147. parse_str($this->request->post("params"), $values);
  148. $values = array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  149. if ($values)
  150. {
  151. $count = $this->model->where($this->model->getPk(), 'in', $ids)->update($values);
  152. if ($count)
  153. {
  154. $this->success();
  155. }
  156. }
  157. else
  158. {
  159. $this->error(__('You have no permission'));
  160. }
  161. }
  162. }
  163. $this->error(__('Parameter %s can not be empty', 'ids'));
  164. }
  165. }