Backend.php 5.6 KB

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