Backend.php 6.3 KB

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